ChatPlexSDK-BS 1.0.1-v6.2.0
C++ ChatPlex BeatSaber SDK
Loading...
Searching...
No Matches
WebResponse.hpp
1#pragma once
2
3#include "../Utils/MonoPtr.hpp"
4#include "../Utils/Json.hpp"
5
6#include <UnityEngine/Networking/UnityWebRequest.hpp>
7#include <System/Net/HttpStatusCode.hpp>
8#include <System/String.hpp>
9
10#include <memory>
11#include <optional>
12
13namespace CP_SDK::Network {
14
15 namespace _u
16 {
17 using namespace System::Net;
18 using namespace UnityEngine::Networking;
19 }
20 namespace _v
21 {
22 using namespace CP_SDK::Utils;
23 }
24
27 {
28 CP_SDK_NO_COPYMOVE_CTORS(WebResponse);
29 CP_SDK_PRIV_TAG();
30
31 private:
33 _u::HttpStatusCode m_StatusCode;
35 std::u16string m_ReasonPhrase;
37 bool m_IsSuccessStatusCode;
39 bool m_ShouldRetry;
43 std::optional<std::u16string> m_BodyString;
44
45 public:
47 _u::HttpStatusCode StatusCode();
49 std::u16string_view ReasonPhrase();
57 std::u16string_view BodyString();
58
59 public:
60 using Ptr = std::shared_ptr<WebResponse>;
61
64 WebResponse(_u::UnityWebRequest* p_Request);
65
66 public:
70 bool TryAsJObject(std::shared_ptr<_v::Json::U16Document>& p_Object)
71 {
72 p_Object = nullptr;
73
74 auto l_New = std::make_shared<_v::Json::U16Document>();
75 try
76 {
77 if (!_v::Json::TryFromU16String(BodyString(), *l_New.get()))
78 return false;
79
80 p_Object = l_New;
81 }
82 catch (const std::exception& l_Exception)
83 {
84 p_Object = nullptr;
85 return false;
86 }
87
88 return p_Object != nullptr;
89 }
90
95 template<typename t_Type>
96 bool TryGetObject(std::shared_ptr<t_Type>& p_Object)
97 {
98 p_Object = nullptr;
99 try
100 {
101 _v::Json::U16Document l_Document;
102 _v::Json::TryFromU16String(BodyString(), l_Document);
103
104 p_Object = std::make_shared<t_Type>();
105 p_Object->Unserialize(l_Document);
106 }
107 catch (const std::exception& l_Exception)
108 {
109 p_Object = nullptr;
110 return false;
111 }
112
113 return p_Object != nullptr;
114 }
115
116 };
117
118}
std::u16string_view ReasonPhrase()
Reason phrase.
bool IsSuccessStatusCode()
Is success.
bool ShouldRetry()
Should retry.
WebResponse(_u::UnityWebRequest *p_Request)
bool TryGetObject(std::shared_ptr< t_Type > &p_Object)
Get Object from serialized JSON.
std::u16string_view BodyString()
Response bytes.
_v::MonoPtr<::Array< uint8_t > > & BodyBytes()
Response bytes.
bool TryAsJObject(std::shared_ptr< _v::Json::U16Document > &p_Object)
Get JObject from serialized JSON.
_u::HttpStatusCode StatusCode()
Result code.
Network and HTTP utilities.
Various platform utils like Delegate/Action/Function/Event system.
Definition Delegate.hpp:8