MetaSoul

Loading...
Searching...
No Matches
ESRequestJSON.h
Go to the documentation of this file.
1#pragma once
2
3#include "ESIOTypes.h"
4#include "Http.h"
5#include "Delegates/Delegate.h"
6#include "Runtime/Core/Public/Containers/Map.h"
7#include "LatentActions.h"
8#include "Engine/LatentActionManager.h"
9#include "ESJsonObject.h"
10#include "ESRequestJSON.generated.h"
11
12 class UESJsonObject;
16 template <class T> class EMOSHAPESUBSYSTEM_API FEmoShapeJsonLatentAction : public FPendingLatentAction
17{
18 public:
19 virtual void Call(const T &Value)
20 {
21 Result = Value;
22 Called = true;
23 }
24
25 void operator()(const T &Value)
26 {
27 Call(Value);
28 }
29
30 void Cancel();
31
32 FEmoShapeJsonLatentAction(FWeakObjectPtr RequestObj, T& ResultParam, const FLatentActionInfo& LatentInfo) :
33 Called(false),
34 Request(RequestObj),
35 ExecutionFunction(LatentInfo.ExecutionFunction),
36 OutputLink(LatentInfo.Linkage),
37 CallbackTarget(LatentInfo.CallbackTarget),
38 Result(ResultParam)
39 {
40 }
41
42 virtual void UpdateOperation(FLatentResponse& Response) override
43 {
44 Response.FinishAndTriggerIf(Called, ExecutionFunction, OutputLink, CallbackTarget);
45 }
46
47 virtual void NotifyObjectDestroyed()
48 {
49 Cancel();
50 }
51
52 virtual void NotifyActionAborted()
53 {
54 Cancel();
55 }
56
57 private:
58 bool Called;
59 FWeakObjectPtr Request;
60
61 public:
62 const FName ExecutionFunction;
63 const int32 OutputLink;
64 const FWeakObjectPtr CallbackTarget;
65 T &Result;
66};
67
68
70 DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnRequestComplete, class UESRequestJSON*, Request);
71 DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnRequestFail, class UESRequestJSON*, Request);
72
73 DECLARE_MULTICAST_DELEGATE_OneParam(FOnStaticRequestComplete, class UESRequestJSON*);
74 DECLARE_MULTICAST_DELEGATE_OneParam(FOnStaticRequestFail, class UESRequestJSON*);
75
76UCLASS(BlueprintType, Blueprintable)
77class EMOSHAPESUBSYSTEM_API UESRequestJSON : public UObject
78{
79 GENERATED_UCLASS_BODY()
80
81 public:
83 // Construction
84
86 UFUNCTION(BlueprintCallable, meta = (DisplayName = "Construct Json Request (Empty)", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "EmoShapeJson|Request")
87 static UESRequestJSON* ConstructRequest(UObject* WorldContextObject);
88
90 UFUNCTION(BlueprintCallable, meta = (DisplayName = "Construct Json Request", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "EmoShapeJson|Request")
91 static UESRequestJSON* ConstructRequestExt(UObject* WorldContextObject, EEmoShapeIORequestVerb Verb, EEmoShapeIORequestContentType ContentType);
92
94 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Request")
95 void SetVerb(EEmoShapeIORequestVerb Verb);
96
98 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Request")
99 void SetCustomVerb(FString Verb);
100
103 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Request")
104 void SetContentType(EEmoShapeIORequestContentType ContentType);
105
107 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Request")
108 void SetBinaryContentType(const FString &ContentType);
109
111 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Request")
112 void SetBinaryRequestContent(const TArray<uint8> &Content);
113
115 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Request")
116 void SetHeader(const FString& HeaderName, const FString& HeaderValue);
117
118
120 // Destruction and reset
121
123 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Utility")
124 void ResetData();
125
127 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Request")
128 void ResetRequestData();
129
131 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Response")
132 void ResetResponseData();
133
135 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Response")
136 void Cancel();
137
138
140 // JSON data accessors
141
143 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Request")
144 UESJsonObject* GetRequestObject();
145
147 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Request")
148 void SetRequestObject(UESJsonObject* JsonObject);
149
151 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Response")
152 UESJsonObject* GetResponseObject();
153
155 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Response")
156 void SetResponseObject(UESJsonObject* JsonObject);
157
158
160 // Request/response data access
161
163 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Request")
164 FString GetURL();
165
167 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Request")
168 EEmoShapeIORequestStatus GetStatus();
169
171 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Response")
172 int32 GetResponseCode();
173
175 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Response")
176 FString GetResponseHeader(const FString HeaderName);
177
179 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Response")
180 TArray<FString> GetAllResponseHeaders();
181
183 // URL processing
184
186 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Request")
187 virtual void ProcessURL(const FString& Url = TEXT("http://alyamkin.com"));
188
190 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Request", meta = (Latent, LatentInfo = "LatentInfo", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"))
191 virtual void ApplyURL(const FString& Url, UESJsonObject *&Result, UObject* WorldContextObject, struct FLatentActionInfo LatentInfo);
192
194 void ProcessRequest();
195
196
198 // Request callbacks
199
200private:
202 void OnProcessRequestComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
203
204 void OnProcessRequestCompleteBinaryResult(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
205
206public:
208 UPROPERTY(BlueprintAssignable, Category = "EmoShapeJson|Event")
209 FOnRequestComplete OnRequestComplete;
210
212 UPROPERTY(BlueprintAssignable, Category = "EmoShapeJson|Event")
213 FOnRequestFail OnRequestFail;
214
216 FOnStaticRequestComplete OnStaticRequestComplete;
217
219 FOnStaticRequestFail OnStaticRequestFail;
220
221
223 // Tags
224
225public:
227 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Utility")
228 void AddTag(FName Tag);
229
235 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Utility")
236 int32 RemoveTag(FName Tag);
237
239 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Utility")
240 bool HasTag(FName Tag) const;
241
242protected:
244 TArray<FName> Tags;
245
246
248 // Data
249
250public:
252 UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "EmoShapeJson|Response")
253 FString ResponseContent;
254
256 UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "EmoShapeJson|Response")
257 bool bIsValidJsonResponse;
258
260 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "EmoShapeJson|Response")
261 bool bShouldHaveBinaryResponse;
262
263 TFunction<void(TArray<uint8>&)> OnProcessURLCompleteCallback;
264
265 TArray<uint8> ResultBinaryData;
266
267protected:
269 FEmoShapeJsonLatentAction<UESJsonObject*>* ContinueAction;
270
272 UPROPERTY()
273 UESJsonObject* RequestJsonObj;
274
275 UPROPERTY()
276 TArray<uint8> RequestBytes;
277
278 UPROPERTY()
279 FString BinaryContentType;
280
282 UPROPERTY()
283 UESJsonObject* ResponseJsonObj;
284
286 EEmoShapeIORequestVerb RequestVerb;
287
289 EEmoShapeIORequestContentType RequestContentType;
290
292 TMap<FString, FString> RequestHeaders;
293
295 TMap<FString, FString> ResponseHeaders;
296
298 int32 ResponseCode;
299
301 FString CustomVerb;
302
304 FHttpRequestRef HttpRequest = FHttpModule::Get().CreateRequest();
305
306};
ESIOTypes.h
EEmoShapeIORequestVerb
EEmoShapeIORequestVerb
Definition ESIOTypes.h:11
EEmoShapeIORequestStatus
EEmoShapeIORequestStatus
Definition ESIOTypes.h:33
EEmoShapeIORequestContentType
EEmoShapeIORequestContentType
Definition ESIOTypes.h:23
ESJsonObject.h
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnRequestComplete, class UESRequestJSON *, Request)
DECLARE_MULTICAST_DELEGATE_OneParam
DECLARE_MULTICAST_DELEGATE_OneParam(FOnStaticRequestComplete, class UESRequestJSON *)
FEmoShapeJsonLatentAction
Definition ESRequestJSON.h:17
FEmoShapeJsonLatentAction::UpdateOperation
virtual void UpdateOperation(FLatentResponse &Response) override
Definition ESRequestJSON.h:42
FEmoShapeJsonLatentAction::ExecutionFunction
const FName ExecutionFunction
Definition ESRequestJSON.h:62
FEmoShapeJsonLatentAction::FEmoShapeJsonLatentAction
FEmoShapeJsonLatentAction(FWeakObjectPtr RequestObj, T &ResultParam, const FLatentActionInfo &LatentInfo)
Definition ESRequestJSON.h:32
FEmoShapeJsonLatentAction::CallbackTarget
const FWeakObjectPtr CallbackTarget
Definition ESRequestJSON.h:64
FEmoShapeJsonLatentAction::NotifyActionAborted
virtual void NotifyActionAborted()
Definition ESRequestJSON.h:52
FEmoShapeJsonLatentAction::NotifyObjectDestroyed
virtual void NotifyObjectDestroyed()
Definition ESRequestJSON.h:47
FEmoShapeJsonLatentAction::Result
T & Result
Definition ESRequestJSON.h:65
FEmoShapeJsonLatentAction::OutputLink
const int32 OutputLink
Definition ESRequestJSON.h:63
FEmoShapeJsonLatentAction::Call
virtual void Call(const T &Value)
Definition ESRequestJSON.h:19
FEmoShapeJsonLatentAction::operator()
void operator()(const T &Value)
Definition ESRequestJSON.h:25
UESJsonObject
Definition ESJsonObject.h:10
UESRequestJSON
Definition ESRequestJSON.h:78
UESRequestJSON::UFUNCTION
UFUNCTION(BlueprintCallable, meta=(DisplayName="Construct Json Request (Empty)", HidePin="WorldContextObject", DefaultToSelf="WorldContextObject"), Category="EmoShapeJson|Request") static UESRequestJSON *ConstructRequest(UObject *WorldContextObject)