A Demo Project for the UnrealEngineSDK
Loading...
Searching...
No Matches
VaRestSubsystem.cpp
Go to the documentation of this file.
1// Copyright 2014-2020 Vladimir Alyamkin. All Rights Reserved.
2
3#include "VaRestSubsystem.h"
4
5#include "VaRestDefines.h"
6#include "VaRestJsonObject.h"
7#include "VaRestJsonValue.h"
8
9#include "Misc/FileHelper.h"
10#include "Misc/Paths.h"
11#include "Serialization/JsonReader.h"
12#include "Serialization/JsonSerializer.h"
13#include "Subsystems/SubsystemBlueprintLibrary.h"
14
19
21{
22 Super::Initialize(Collection);
23
24 UE_LOG(LogVaRest, Log, TEXT("%s: VaRest subsystem initialized"), *VA_FUNC_LINE);
25}
26
28{
29 // Do nothing for now
30 Super::Deinitialize();
31}
32
33void UVaRestSubsystem::CallURL(const FString& URL, EVaRestRequestVerb Verb, EVaRestRequestContentType ContentType, UVaRestJsonObject* VaRestJson, const FVaRestCallDelegate& Callback)
34{
35 // Check we have valid data json
36 if (VaRestJson == nullptr)
37 {
38 VaRestJson = ConstructVaRestJsonObject();
39 }
40
42
43 Request->SetVerb(Verb);
44 Request->SetContentType(ContentType);
45 Request->SetRequestObject(VaRestJson);
46
47 FVaRestCallResponse Response;
48 Response.Request = Request;
49 Response.Callback = Callback;
50
52 Response.FailDelegateHandle = Request->OnStaticRequestFail.AddUObject(this, &UVaRestSubsystem::OnCallComplete);
53
54 RequestMap.Add(Request, Response);
55
56 Request->ResetResponseData();
57 Request->ProcessURL(URL);
58}
59
61{
62 if (!RequestMap.Contains(Request))
63 {
64 return;
65 }
66
67 const auto Response = RequestMap.Find(Request);
68 Request->OnStaticRequestComplete.Remove(Response->CompleteDelegateHandle);
69 Request->OnStaticRequestFail.Remove(Response->FailDelegateHandle);
70
71 Response->Callback.ExecuteIfBound(Request);
72 Response->Request = nullptr;
73 RequestMap.Remove(Request);
74}
75
77{
78 return NewObject<UVaRestRequestJSON>(this);
79}
80
90
92{
93 return NewObject<UVaRestJsonObject>(this);
94}
95
97{
98 auto SelfSystem = CastChecked<UVaRestSubsystem>(USubsystemBlueprintLibrary::GetEngineSubsystem(UVaRestSubsystem::StaticClass()), ECastCheckedType::NullChecked);
99 return SelfSystem->ConstructVaRestJsonObject();
100}
101
103{
104 TSharedPtr<FJsonValue> NewVal = MakeShareable(new FJsonValueNumber(Number));
105
106 UVaRestJsonValue* NewValue = NewObject<UVaRestJsonValue>(this);
107 NewValue->SetRootValue(NewVal);
108
109 return NewValue;
110}
111
113{
114 TSharedPtr<FJsonValue> NewVal = MakeShareable(new FJsonValueString(StringValue));
115
116 UVaRestJsonValue* NewValue = NewObject<UVaRestJsonValue>(this);
117 NewValue->SetRootValue(NewVal);
118
119 return NewValue;
120}
121
123{
124 TSharedPtr<FJsonValue> NewVal = MakeShareable(new FJsonValueBoolean(InValue));
125
126 UVaRestJsonValue* NewValue = NewObject<UVaRestJsonValue>(this);
127 NewValue->SetRootValue(NewVal);
128
129 return NewValue;
130}
131
133{
134 // Prepare data array to create new value
135 TArray<TSharedPtr<FJsonValue>> ValueArray;
136 for (auto InVal : InArray)
137 {
138 ValueArray.Add(InVal->GetRootValue());
139 }
140
141 TSharedPtr<FJsonValue> NewVal = MakeShareable(new FJsonValueArray(ValueArray));
142
143 UVaRestJsonValue* NewValue = NewObject<UVaRestJsonValue>(this);
144 NewValue->SetRootValue(NewVal);
145
146 return NewValue;
147}
148
150{
151 TSharedPtr<FJsonValue> NewVal = MakeShareable(new FJsonValueObject(JsonObject->GetRootObject()));
152
153 UVaRestJsonValue* NewValue = NewObject<UVaRestJsonValue>(this);
154 NewValue->SetRootValue(NewVal);
155
156 return NewValue;
157}
158
160{
161 TSharedPtr<FJsonValue> NewVal = InValue;
162
163 UVaRestJsonValue* NewValue = NewObject<UVaRestJsonValue>(this);
164 NewValue->SetRootValue(NewVal);
165
166 return NewValue;
167}
168
170{
171 const TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(*JsonString);
172 TSharedPtr<FJsonValue> OutJsonValue;
173 if (FJsonSerializer::Deserialize(Reader, OutJsonValue))
174 {
175 return ConstructJsonValue(OutJsonValue);
176 }
177
178 return nullptr;
179}
180
182{
183 const TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(*JsonString);
184 TSharedPtr<FJsonObject> OutJsonObj;
185 if (FJsonSerializer::Deserialize(Reader, OutJsonObj))
186 {
187 auto NewJsonObj = NewObject<UVaRestJsonObject>(this);
188 NewJsonObj->SetRootObject(OutJsonObj);
189 return NewJsonObj;
190 }
191
192 return nullptr;
193}
194
196{
197 auto* Json = ConstructVaRestJsonObject();
198
199 FString JSONString;
200 if (FFileHelper::LoadFileToString(JSONString, *(bIsRelativeToContentDir ? FPaths::ProjectContentDir() / Path : Path)))
201 {
202 if (Json->DecodeJson(JSONString))
203 {
204 return Json;
205 }
206 else
207 {
208 UE_LOG(LogVaRest, Error, TEXT("%s: Can't decode json from file %s"), *VA_FUNC_LINE, *Path);
209 }
210 }
211 else
212 {
213 UE_LOG(LogVaRest, Error, TEXT("%s: Can't open file %s"), *VA_FUNC_LINE, *Path);
214 }
215
216 return nullptr;
217}
#define VA_FUNC_LINE
EVaRestRequestContentType
UENUM(BlueprintType)
Definition VaRestTypes.h:31
EVaRestRequestVerb
UENUM(BlueprintType)
Definition VaRestTypes.h:15
UCLASS(BlueprintType, Blueprintable)
TArray< T > GetTypeArrayField(const FString &FieldName) const
TSharedRef< FJsonObject > & GetRootObject()
UCLASS(BlueprintType, Blueprintable)
void SetRootValue(TSharedPtr< FJsonValue > &JsonValue)
UCLASS(BlueprintType, Blueprintable)
virtual void ProcessURL(const FString &Url=TEXT("http://alyamkin.com"))
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
void SetRequestObject(UVaRestJsonObject *JsonObject)
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
void SetContentType(EVaRestRequestContentType ContentType)
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
FOnStaticRequestFail OnStaticRequestFail
void SetVerb(EVaRestRequestVerb Verb)
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
void ResetResponseData()
UFUNCTION(BlueprintCallable, Category = "VaRest|Response")
FOnStaticRequestComplete OnStaticRequestComplete
static UVaRestJsonObject * StaticConstructVaRestJsonObject()
UFUNCTION()
UVaRestJsonValue * ConstructJsonValueArray(const TArray< UVaRestJsonValue * > &InArray)
UFUNCTION(BlueprintPure, meta = (DisplayName = "Construct Json Array Value"), Category = "VaRest|Subs...
UVaRestJsonValue * ConstructJsonValue(const TSharedPtr< FJsonValue > &InValue)
virtual void Initialize(FSubsystemCollectionBase &Collection) override
void CallURL(const FString &URL, EVaRestRequestVerb Verb, EVaRestRequestContentType ContentType, UVaRestJsonObject *VaRestJson, const FVaRestCallDelegate &Callback)
UFUNCTION(BlueprintCallable, Category = "VaRest|Utility")
UVaRestJsonValue * DecodeJsonValue(const FString &JsonString)
UFUNCTION(BlueprintCallable, Category = "VaRest|Subsystem")
UVaRestJsonValue * ConstructJsonValueString(const FString &StringValue)
UFUNCTION(BlueprintPure, meta = (DisplayName = "Construct Json String Value"), Category = "VaRest|Sub...
UVaRestJsonObject * LoadJsonFromFile(const FString &Path, const bool bIsRelativeToContentDir=true)
UFUNCTION(BlueprintCallable, Category = "VaRest|Utility")
UVaRestRequestJSON * ConstructVaRestRequest()
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Construct Json Request (Empty)"),...
UVaRestJsonValue * ConstructJsonValueBool(bool InValue)
UFUNCTION(BlueprintPure, meta = (DisplayName = "Construct Json Bool Value"), Category = "VaRest|Subsy...
UVaRestRequestJSON * ConstructVaRestRequestExt(EVaRestRequestVerb Verb, EVaRestRequestContentType ContentType)
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Construct Json Request"), Category = "VaRest|Subs...
UVaRestJsonObject * ConstructVaRestJsonObject()
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Construct Json Object"), Category = "VaRest|Subsy...
UVaRestJsonValue * ConstructJsonValueNumber(float Number)
UFUNCTION(BlueprintPure, meta = (DisplayName = "Construct Json Number Value"), Category = "VaRest|Sub...
UVaRestJsonObject * DecodeJsonObject(const FString &JsonString)
UFUNCTION(BlueprintCallable, Category = "VaRest|Subsystem")
UVaRestJsonValue * ConstructJsonValueObject(UVaRestJsonObject *JsonObject)
UFUNCTION(BlueprintPure, meta = (DisplayName = "Construct Json Object Value"), Category = "VaRest|Sub...
void OnCallComplete(UVaRestRequestJSON *Request)
virtual void Deinitialize() override
TMap< UVaRestRequestJSON *, FVaRestCallResponse > RequestMap
UPROPERTY()
FVaRestCallDelegate Callback
UPROPERTY()
FDelegateHandle CompleteDelegateHandle
UVaRestRequestJSON * Request
UPROPERTY()
FDelegateHandle FailDelegateHandle