A Demo Project for the UnrealEngineSDK
Loading...
Searching...
No Matches
VaRestRequestJSON.h
Go to the documentation of this file.
1// Copyright 2014-2019 Vladimir Alyamkin. All Rights Reserved.
2
3#pragma once
4
5#include "Engine/LatentActionManager.h"
6#include "Http.h"
7#include "HttpModule.h"
8#include "Interfaces/IHttpRequest.h"
9#include "LatentActions.h"
10
11#include "VaRestTypes.h"
12
13#include "VaRestRequestJSON.generated.h"
14
17class UVaRestSettings;
18
22template <class T>
23class FVaRestLatentAction : public FPendingLatentAction
24{
25public:
26 virtual void Call(const T& Value)
27 {
28 Result = Value;
29 Called = true;
30 }
31
32 void operator()(const T& Value)
33 {
34 Call(Value);
35 }
36
37 void Cancel();
38
39 FVaRestLatentAction(FWeakObjectPtr RequestObj, T& ResultParam, const FLatentActionInfo& LatentInfo)
40 : Called(false)
41 , Request(RequestObj)
43 , OutputLink(LatentInfo.Linkage)
44 , CallbackTarget(LatentInfo.CallbackTarget)
45 , Result(ResultParam)
46 {
47 }
48
49 virtual void UpdateOperation(FLatentResponse& Response) override
50 {
51 Response.FinishAndTriggerIf(Called, ExecutionFunction, OutputLink, CallbackTarget);
52 }
53
54 virtual void NotifyObjectDestroyed()
55 {
56 Cancel();
57 }
58
59 virtual void NotifyActionAborted()
60 {
61 Cancel();
62 }
63
64private:
65 bool Called;
66 FWeakObjectPtr Request;
67
68public:
69 const FName ExecutionFunction;
70 const int32 OutputLink;
71 const FWeakObjectPtr CallbackTarget;
73};
74
78
81
85UCLASS(BlueprintType, Blueprintable)
86class VAREST_API UVaRestRequestJSON : public UObject
87{
88 GENERATED_UCLASS_BODY()
89
90public:
92 // Construction
93
95 UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
96 void SetVerb(EVaRestRequestVerb Verb);
97
99 UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
100 void SetCustomVerb(FString Verb);
101
104 UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
105 void SetContentType(EVaRestRequestContentType ContentType);
106
108 UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
109 void SetBinaryContentType(const FString& ContentType);
110
112 UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
113 void SetBinaryRequestContent(const TArray<uint8>& Content);
114
116 UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
117 void SetStringRequestContent(const FString& Content);
118
120 UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
121 void SetHeader(const FString& HeaderName, const FString& HeaderValue);
122
124 // Destruction and reset
125
127 UFUNCTION(BlueprintCallable, Category = "VaRest|Utility")
128 void ResetData();
129
131 UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
132 void ResetRequestData();
133
135 UFUNCTION(BlueprintCallable, Category = "VaRest|Response")
136 void ResetResponseData();
137
139 UFUNCTION(BlueprintCallable, Category = "VaRest|Response")
140 void Cancel();
141
143 // JSON data accessors
144
146 UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
147 UVaRestJsonObject* GetRequestObject() const;
148
150 UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
151 void SetRequestObject(UVaRestJsonObject* JsonObject);
152
154 UFUNCTION(BlueprintCallable, Category = "VaRest|Response")
155 UVaRestJsonObject* GetResponseObject() const;
156
158 UFUNCTION(BlueprintCallable, Category = "VaRest|Response")
159 void SetResponseObject(UVaRestJsonObject* JsonObject);
160
162 UFUNCTION(BlueprintCallable, Category = "VaRest|Response")
163 UVaRestJsonValue* GetResponseValue() const;
164
166 // Request/response data access
167
169 UFUNCTION(BlueprintPure, Category = "VaRest|Request")
170 FString GetURL() const;
171
173 UFUNCTION(BlueprintPure, Category = "VaRest|Request")
174 EVaRestRequestVerb GetVerb() const;
175
177 UFUNCTION(BlueprintPure, Category = "VaRest|Request")
178 EVaRestRequestStatus GetStatus() const;
179
181 UFUNCTION(BlueprintPure, Category = "VaRest|Response")
182 int32 GetResponseCode() const;
183
185 UFUNCTION(BlueprintPure, Category = "VaRest|Response")
186 FString GetResponseHeader(const FString& HeaderName);
187
189 UFUNCTION(BlueprintPure, Category = "VaRest|Response")
190 TArray<FString> GetAllResponseHeaders() const;
191
193 // URL processing
194
195public:
197 UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
198 void SetURL(const FString& Url = TEXT("http://alyamkin.com"));
199
201 UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
202 virtual void ProcessURL(const FString& Url = TEXT("http://alyamkin.com"));
203
205 UFUNCTION(BlueprintCallable, Category = "VaRest|Request", meta = (Latent, LatentInfo = "LatentInfo", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"))
206 virtual void ApplyURL(const FString& Url, UVaRestJsonObject*& Result, UObject* WorldContextObject, struct FLatentActionInfo LatentInfo);
207
209 UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
210 virtual void ExecuteProcessRequest();
211
212protected:
214 void ProcessRequest();
215
217 // Request callbacks
218
219private:
221 void OnProcessRequestComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
222
223public:
225 UPROPERTY(BlueprintAssignable, Category = "VaRest|Event")
226 FOnRequestComplete OnRequestComplete;
227
229 UPROPERTY(BlueprintAssignable, Category = "VaRest|Event")
230 FOnRequestFail OnRequestFail;
231
233 FOnStaticRequestComplete OnStaticRequestComplete;
234
236 FOnStaticRequestFail OnStaticRequestFail;
237
239 // Tags
240
241public:
243 UFUNCTION(BlueprintCallable, Category = "VaRest|Utility")
244 void AddTag(FName Tag);
245
251 UFUNCTION(BlueprintCallable, Category = "VaRest|Utility")
252 int32 RemoveTag(FName Tag);
253
255 UFUNCTION(BlueprintCallable, Category = "VaRest|Utility")
256 bool HasTag(FName Tag) const;
257
258protected:
260 TArray<FName> Tags;
261
263 // Data
264
265public:
270 UFUNCTION(BlueprintCallable, Category = "VaRest|Response")
271 FString GetResponseContentAsString(bool bCacheResponseContent = true);
272
273public:
275 UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "VaRest|Response")
276 int32 ResponseSize;
277
279 UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "VaRest|Response")
280 FString ResponseContent;
281
283 UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "VaRest|Response")
284 bool bIsValidJsonResponse;
285
286protected:
288 static FString DeprecatedResponseString;
289
290protected:
292 FVaRestLatentAction<UVaRestJsonObject*>* ContinueAction;
293
295 UPROPERTY()
296 UVaRestJsonObject* RequestJsonObj;
297
298 TArray<uint8> RequestBytes;
299 FString BinaryContentType;
300
303 FString StringRequestContent;
304
306 UPROPERTY()
307 UVaRestJsonObject* ResponseJsonObj;
308
310 UPROPERTY()
311 UVaRestJsonValue* ResponseJsonValue;
312
314 EVaRestRequestVerb RequestVerb;
315
317 EVaRestRequestContentType RequestContentType;
320 TMap<FString, FString> RequestHeaders;
323 TMap<FString, FString> ResponseHeaders;
326 int32 ResponseCode;
327
329 FString CustomVerb;
330
332 TSharedRef<IHttpRequest, ESPMode::ThreadSafe> HttpRequest = FHttpModule::Get().CreateRequest();
333
334public:
336 TSharedRef<IHttpRequest, ESPMode::ThreadSafe> GetHttpRequest() const { return HttpRequest; };
337};
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnRequestComplete, class UVaRestRequestJSON *, Request)
DECLARE_MULTICAST_DELEGATE_OneParam(FOnStaticRequestComplete, class UVaRestRequestJSON *)
EVaRestRequestContentType
UENUM(BlueprintType)
Definition VaRestTypes.h:31
EVaRestRequestVerb
UENUM(BlueprintType)
Definition VaRestTypes.h:15
EVaRestRequestStatus
UENUM(BlueprintType)
Definition VaRestTypes.h:46
const FName ExecutionFunction
FVaRestLatentAction(FWeakObjectPtr RequestObj, T &ResultParam, const FLatentActionInfo &LatentInfo)
virtual void NotifyObjectDestroyed()
const FWeakObjectPtr CallbackTarget
virtual void NotifyActionAborted()
void operator()(const T &Value)
virtual void Call(const T &Value)
virtual void UpdateOperation(FLatentResponse &Response) override
UCLASS(BlueprintType, Blueprintable)
UCLASS(BlueprintType, Blueprintable)
UCLASS(BlueprintType, Blueprintable)
UVaRestJsonObject * ResponseJsonObj
UPROPERTY()
TSharedRef< IHttpRequest, ESPMode::ThreadSafe > GetHttpRequest() const
UVaRestJsonObject * RequestJsonObj
UPROPERTY()
TArray< uint8 > RequestBytes
FOnRequestComplete OnRequestComplete
UPROPERTY(BlueprintAssignable, Category = "VaRest|Event")
TMap< FString, FString > ResponseHeaders
UVaRestJsonValue * ResponseJsonValue
UPROPERTY()
int32 ResponseSize
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "VaRest|Response")
bool bIsValidJsonResponse
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "VaRest|Response")
EVaRestRequestContentType RequestContentType
TMap< FString, FString > RequestHeaders
FString ResponseContent
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "VaRest|Response")
FOnRequestFail OnRequestFail
UPROPERTY(BlueprintAssignable, Category = "VaRest|Event")
FOnStaticRequestFail OnStaticRequestFail
EVaRestRequestVerb RequestVerb
FVaRestLatentAction< UVaRestJsonObject * > * ContinueAction
static FString DeprecatedResponseString
FOnStaticRequestComplete OnStaticRequestComplete
UCLASS(config = Engine, defaultconfig)