11#include "Engine/Engine.h"
12#include "Engine/EngineTypes.h"
13#include "Engine/LatentActionManager.h"
14#include "Engine/World.h"
15#include "Interfaces/IHttpResponse.h"
17#include "Runtime/Launch/Resources/Version.h"
31UVaRestRequestJSON::UVaRestRequestJSON(
const class FObjectInitializer& PCIP)
33 , BinaryContentType(TEXT(
"application/octet-stream"))
35 ContinueAction =
nullptr;
153 if (JsonObject ==
nullptr)
155 UE_LOG(LogVaRest,
Error, TEXT(
"%s: Provided JsonObject is nullptr"), *
VA_FUNC_LINE);
170 if (JsonObject ==
nullptr)
172 UE_LOG(LogVaRest,
Error, TEXT(
"%s: Provided JsonObject is nullptr"), *
VA_FUNC_LINE);
213 if (Header !=
nullptr)
223 TArray<FString> Result;
224 for (TMap<FString, FString>::TConstIterator It(
ResponseHeaders); It; ++It)
226 Result.Add(It.Key() + TEXT(
": ") + It.Value());
237 FString TrimmedUrl = Url;
239 TrimmedUrl.TrimStartInline();
240 TrimmedUrl.TrimEndInline();
254 FString TrimmedUrl = Url;
256 TrimmedUrl.TrimStartInline();
257 TrimmedUrl.TrimEndInline();
262 if (UWorld* World = GEngine->GetWorldFromContextObjectChecked(WorldContextObject))
264 FLatentActionManager& LatentActionManager = World->GetLatentActionManager();
270 LatentActionManager.RemoveActionsForObject(LatentInfo.CallbackTarget);
283 UE_LOG(LogVaRest,
Error, TEXT(
"Request execution attempt with empty URL"));
324 HttpRequest->SetHeader(TEXT(
"Content-Type"), TEXT(
"application/x-www-form-urlencoded"));
326 FString UrlParams =
"";
332 FString Key = RequestIt.Key();
333 FString Value = RequestIt.Value().Get()->AsString();
335 if (!Key.IsEmpty() && !Value.IsEmpty())
337 UrlParams += ParamIdx == 0 ?
"?" :
"&";
360 UE_LOG(LogVaRest, Log, TEXT(
"%s: Request (urlencoded): %s %s (check bExtendedLog for additional data)"), *
VA_FUNC_LINE, *
HttpRequest->GetVerb(), *
HttpRequest->GetURL());
367 HttpRequest->SetHeader(TEXT(
"Content-Type"), TEXT(
"application/x-www-form-urlencoded"));
369 FString UrlParams =
"";
382 FString Key = RequestIt.Key();
383 FString Value = RequestIt.Value().Get()->AsString();
385 if (!Key.IsEmpty() && !Value.IsEmpty())
387 UrlParams += ParamIdx == 0 ?
"" :
"&";
405 UE_LOG(LogVaRest, Log, TEXT(
"%s: Request (url body): %s %s (check bExtendedLog for additional data)"), *
VA_FUNC_LINE, *
HttpRequest->GetVerb(), *
HttpRequest->GetURL());
421 HttpRequest->SetHeader(TEXT(
"Content-Type"), TEXT(
"application/json"));
424 FString OutputString;
425 TSharedRef<TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&OutputString);
433 UE_LOG(LogVaRest, Log, TEXT(
"Request (json): %s %s %sJSON(%s%s%s)JSON"), *
HttpRequest->GetVerb(), *
HttpRequest->GetURL(), LINE_TERMINATOR, LINE_TERMINATOR, *OutputString, LINE_TERMINATOR);
437 UE_LOG(LogVaRest, Log, TEXT(
"Request (json): %s %s (check bExtendedLog for additional data)"), *
HttpRequest->GetVerb(), *
HttpRequest->GetURL());
453 for (TMap<FString, FString>::TConstIterator It(
RequestHeaders); It; ++It)
474 if (Response.IsValid())
480 if (!bWasSuccessful || !Response.IsValid())
482 UE_LOG(LogVaRest,
Error, TEXT(
"Request failed (%d): %s"),
ResponseCode, *Request->GetURL());
493 UE_LOG(LogVaRest, Log, TEXT(
"Response (%d): %sJSON(%s%s%s)JSON"),
ResponseCode, LINE_TERMINATOR, LINE_TERMINATOR, *Response->GetContentAsString(), LINE_TERMINATOR);
497 TArray<FString> Headers = Response->GetAllHeaders();
498 for (FString Header : Headers)
502 if (Header.Split(TEXT(
": "), &Key, &Value))
511 const TArray<uint8>& Bytes = Response->GetContent();
519 UE_LOG(LogVaRest,
Warning, TEXT(
"JSON could not be decoded!"));
525 const TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(*Response->GetContentAsString());
526 TSharedPtr<FJsonValue> OutJsonValue;
527 if (FJsonSerializer::Deserialize(Reader, OutJsonValue))
568 if (Tag != NAME_None)
576 return Tags.Remove(Tag);
581 return (Tag != NAME_None) &&
Tags.Contains(Tag);
602 return TEXT(
"Invalid response");
606 if (!bCacheResponseContent)
608 UE_LOG(LogVaRest,
Warning, TEXT(
"%s: Use of uncashed getter could be slow"), *
VA_FUNC_LINE);
EVaRestRequestContentType
UENUM(BlueprintType)
@ x_www_form_urlencoded_body
@ x_www_form_urlencoded_url
EVaRestRequestVerb
UENUM(BlueprintType)
EVaRestRequestStatus
UENUM(BlueprintType)
virtual void Call(const T &Value)
UCLASS(BlueprintType, Blueprintable)
TSharedRef< FJsonObject > & GetRootObject()
void SetRootObject(const TSharedPtr< FJsonObject > &JsonObject)
FString EncodeJson() const
UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
void Reset()
UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
int32 DeserializeFromUTF8Bytes(const ANSICHAR *Bytes, int32 Size)
UCLASS(BlueprintType, Blueprintable)
void Reset()
UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
void SetRootValue(TSharedPtr< FJsonValue > &JsonValue)
TSharedPtr< FJsonValue > & GetRootValue()
EVaJson GetType() const
UFUNCTION(BlueprintPure, Category = "VaRest|Json")
static UVaRestSettings * GetVaRestSettings()
UFUNCTION(BlueprintPure, Category = "VaRest|Common")
static FString PercentEncode(const FString &Source)
UFUNCTION(BlueprintCallable, Category = "VaRest|Utility")
UCLASS(BlueprintType, Blueprintable)
void SetCustomVerb(FString Verb)
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
virtual void ProcessURL(const FString &Url=TEXT("http://alyamkin.com"))
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
FString StringRequestContent
UVaRestJsonObject * ResponseJsonObj
UPROPERTY()
EVaRestRequestVerb GetVerb() const
UFUNCTION(BlueprintPure, Category = "VaRest|Request")
void SetRequestObject(UVaRestJsonObject *JsonObject)
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
virtual void ApplyURL(const FString &Url, UVaRestJsonObject *&Result, UObject *WorldContextObject, struct FLatentActionInfo LatentInfo)
UFUNCTION(BlueprintCallable, Category = "VaRest|Request", meta = (Latent, LatentInfo = "LatentInfo",...
void ResetRequestData()
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
void Cancel()
UFUNCTION(BlueprintCallable, Category = "VaRest|Response")
void SetBinaryContentType(const FString &ContentType)
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
FString GetResponseHeader(const FString &HeaderName)
UFUNCTION(BlueprintPure, Category = "VaRest|Response")
UVaRestJsonObject * RequestJsonObj
UPROPERTY()
void ResetData()
UFUNCTION(BlueprintCallable, Category = "VaRest|Utility")
TArray< uint8 > RequestBytes
FOnRequestComplete OnRequestComplete
UPROPERTY(BlueprintAssignable, Category = "VaRest|Event")
int32 RemoveTag(FName Tag)
UFUNCTION(BlueprintCallable, Category = "VaRest|Utility")
UVaRestJsonObject * GetResponseObject() const
UFUNCTION(BlueprintCallable, Category = "VaRest|Response")
TMap< FString, FString > ResponseHeaders
UVaRestJsonValue * ResponseJsonValue
UPROPERTY()
UVaRestJsonObject * GetRequestObject() const
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
int32 ResponseSize
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "VaRest|Response")
EVaRestRequestStatus GetStatus() const
UFUNCTION(BlueprintPure, Category = "VaRest|Request")
void SetHeader(const FString &HeaderName, const FString &HeaderValue)
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
FString BinaryContentType
void SetResponseObject(UVaRestJsonObject *JsonObject)
UFUNCTION(BlueprintCallable, Category = "VaRest|Response")
int32 GetResponseCode() const
UFUNCTION(BlueprintPure, Category = "VaRest|Response")
bool bIsValidJsonResponse
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "VaRest|Response")
void OnProcessRequestComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
TArray< FString > GetAllResponseHeaders() const
UFUNCTION(BlueprintPure, Category = "VaRest|Response")
void SetContentType(EVaRestRequestContentType ContentType)
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
EVaRestRequestContentType RequestContentType
void SetURL(const FString &Url=TEXT("http://alyamkin.com"))
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
FString GetResponseContentAsString(bool bCacheResponseContent=true)
UFUNCTION(BlueprintCallable, Category = "VaRest|Response")
TMap< FString, FString > RequestHeaders
void AddTag(FName Tag)
UFUNCTION(BlueprintCallable, Category = "VaRest|Utility")
FString ResponseContent
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "VaRest|Response")
FOnRequestFail OnRequestFail
UPROPERTY(BlueprintAssignable, Category = "VaRest|Event")
bool HasTag(FName Tag) const
UFUNCTION(BlueprintCallable, Category = "VaRest|Utility")
FOnStaticRequestFail OnStaticRequestFail
TSharedRef< IHttpRequest, ESPMode::ThreadSafe > HttpRequest
EVaRestRequestVerb RequestVerb
FString GetURL() const
UFUNCTION(BlueprintPure, Category = "VaRest|Request")
void SetVerb(EVaRestRequestVerb Verb)
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
virtual void ExecuteProcessRequest()
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
FVaRestLatentAction< UVaRestJsonObject * > * ContinueAction
void SetBinaryRequestContent(const TArray< uint8 > &Content)
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
static FString DeprecatedResponseString
void ResetResponseData()
UFUNCTION(BlueprintCallable, Category = "VaRest|Response")
UVaRestJsonValue * GetResponseValue() const
UFUNCTION(BlueprintCallable, Category = "VaRest|Response")
FOnStaticRequestComplete OnStaticRequestComplete
void SetStringRequestContent(const FString &Content)
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
bool bUseChunkedParser
UPROPERTY(Config, EditAnywhere, Category = "VaRest")