From 467f8ff9d0127381c270f9d3472b8a71d9582c83 Mon Sep 17 00:00:00 2001 From: AlfieAllen Date: Fri, 19 Nov 2021 17:01:41 +0200 Subject: [PATCH 1/5] Add new async request function, which encapsulate callback delegates and allow users to use third party json serializers. --- Source/VaRest/Private/VaRestRequestAsync.cpp | 40 +++++++++++++ Source/VaRest/Public/VaRestRequestAsync.h | 61 ++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 Source/VaRest/Private/VaRestRequestAsync.cpp create mode 100644 Source/VaRest/Public/VaRestRequestAsync.h diff --git a/Source/VaRest/Private/VaRestRequestAsync.cpp b/Source/VaRest/Private/VaRestRequestAsync.cpp new file mode 100644 index 00000000..8a1cba88 --- /dev/null +++ b/Source/VaRest/Private/VaRestRequestAsync.cpp @@ -0,0 +1,40 @@ +// Alan Verbilo 2021 for VaRest plugin. + +#include "VaRestRequestAsync.h" + +UVaRestRequestAsync* UVaRestRequestAsync::VaRestRequestAsync(FString URL, EVaRestRequestVerb Verb, EVaRestRequestContentType ContentType, FString RequestMessage) +{ + UVaRestRequestAsync* object = NewObject(); + + object->URL = URL; + object->Verb = Verb; + object->ContentType = ContentType; + object->RequestMessage = RequestMessage; + + return object; +} + +void UVaRestRequestAsync::Activate() +{ + VaRestSubsystem = GEngine->GetEngineSubsystem(); + JSONRequest = VaRestSubsystem->ConstructVaRestRequestExt(Verb, ContentType); + JsonObject = JSONRequest->GetRequestObject(); + JsonObject->DecodeJson(RequestMessage); + + JSONRequest->OnRequestComplete.AddDynamic(this, &UVaRestRequestAsync::OnSuccessResend); + JSONRequest->OnRequestFail.AddDynamic(this, &UVaRestRequestAsync::OnFailureResend); + + JSONRequest->ProcessURL(URL); +} + +void UVaRestRequestAsync::OnSuccessResend(class UVaRestRequestJSON* RequestJSON) +{ + OnSuccess.Broadcast(RequestJSON->GetResponseContentAsString()); + RemoveFromRoot(); +} + +void UVaRestRequestAsync::OnFailureResend(class UVaRestRequestJSON* RequestJSON) +{ + OnFail.Broadcast(RequestJSON->GetResponseContentAsString()); + RemoveFromRoot(); +} diff --git a/Source/VaRest/Public/VaRestRequestAsync.h b/Source/VaRest/Public/VaRestRequestAsync.h new file mode 100644 index 00000000..13f74c6a --- /dev/null +++ b/Source/VaRest/Public/VaRestRequestAsync.h @@ -0,0 +1,61 @@ +// Alan Verbilo 2021 for VaRest plugin. + +#pragma once + +#include "CoreMinimal.h" +#include "Kismet/BlueprintAsyncActionBase.h" +#include "VaRestRequestJSON.h" +#include "VaRestSubsystem.h" +#include "VaRestTypes.h" +#include "VaRestRequestAsync.generated.h" + +DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FResponseDelegate, FString, ResposeMessage); + +/** +* This is async function that encapsulate callback delegates and make your code more clear. +* +* Also this function works with String format messages instead of VaRest internal json serializer, +* to give user ability to use third party json serializer. +* +* Alan Verbilo recommend to use next free json serializer: +* https://www.unrealengine.com/marketplace/en-US/product/dbjson +*/ + +UCLASS() +class VAREST_API UVaRestRequestAsync : public UBlueprintAsyncActionBase +{ + GENERATED_BODY() + + UPROPERTY() + UVaRestSubsystem* VaRestSubsystem; + + UPROPERTY() + UVaRestRequestJSON* JSONRequest; + + UPROPERTY() + UVaRestJsonObject* JsonObject; + + + FString URL; + EVaRestRequestVerb Verb; + EVaRestRequestContentType ContentType; + FString RequestMessage; + +public: + UPROPERTY(BlueprintAssignable) + FResponseDelegate OnSuccess; + + UPROPERTY(BlueprintAssignable) + FResponseDelegate OnFail; + + UFUNCTION(BlueprintCallable, meta = (BlueprintUnternalUseOnly = "true", DisplayName = "VaRest Simple Request Async")) + static UVaRestRequestAsync* VaRestRequestAsync(FString URL, EVaRestRequestVerb Verb, EVaRestRequestContentType ContentType, FString RequestMessage); + + virtual void Activate() override; + + UFUNCTION() + void OnSuccessResend(class UVaRestRequestJSON* RequestJSON); + + UFUNCTION() + void OnFailureResend(class UVaRestRequestJSON* RequestJSON); +}; From ff948874bfcb576a1fa6b6d5a863f3b91966de1e Mon Sep 17 00:00:00 2001 From: AlfieAllen Date: Sun, 5 Dec 2021 00:16:10 +0200 Subject: [PATCH 2/5] Refactor async connection method, add more comments --- Source/VaRest/Private/VaRestRequestAsync.cpp | 21 ++++++++------ Source/VaRest/Public/VaRestRequestAsync.h | 29 +++++++------------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/Source/VaRest/Private/VaRestRequestAsync.cpp b/Source/VaRest/Private/VaRestRequestAsync.cpp index 8a1cba88..c790ebef 100644 --- a/Source/VaRest/Private/VaRestRequestAsync.cpp +++ b/Source/VaRest/Private/VaRestRequestAsync.cpp @@ -16,15 +16,18 @@ UVaRestRequestAsync* UVaRestRequestAsync::VaRestRequestAsync(FString URL, EVaRes void UVaRestRequestAsync::Activate() { - VaRestSubsystem = GEngine->GetEngineSubsystem(); - JSONRequest = VaRestSubsystem->ConstructVaRestRequestExt(Verb, ContentType); - JsonObject = JSONRequest->GetRequestObject(); + // Create request + UVaRestSubsystem* VaRestSubsystem = GEngine->GetEngineSubsystem(); + UVaRestRequestJSON* RequestJSON = VaRestSubsystem->ConstructVaRestRequestExt(Verb, ContentType); + UVaRestJsonObject* JsonObject = RequestJSON->GetRequestObject(); JsonObject->DecodeJson(RequestMessage); - - JSONRequest->OnRequestComplete.AddDynamic(this, &UVaRestRequestAsync::OnSuccessResend); - JSONRequest->OnRequestFail.AddDynamic(this, &UVaRestRequestAsync::OnFailureResend); - - JSONRequest->ProcessURL(URL); + + // Bind to delegates + RequestJSON->OnRequestComplete.AddDynamic(this, &UVaRestRequestAsync::OnSuccessResend); + RequestJSON->OnRequestFail.AddDynamic(this, &UVaRestRequestAsync::OnFailureResend); + + // Start request + RequestJSON->ProcessURL(URL); } void UVaRestRequestAsync::OnSuccessResend(class UVaRestRequestJSON* RequestJSON) @@ -37,4 +40,4 @@ void UVaRestRequestAsync::OnFailureResend(class UVaRestRequestJSON* RequestJSON) { OnFail.Broadcast(RequestJSON->GetResponseContentAsString()); RemoveFromRoot(); -} +} \ No newline at end of file diff --git a/Source/VaRest/Public/VaRestRequestAsync.h b/Source/VaRest/Public/VaRestRequestAsync.h index 13f74c6a..df2ef537 100644 --- a/Source/VaRest/Public/VaRestRequestAsync.h +++ b/Source/VaRest/Public/VaRestRequestAsync.h @@ -9,8 +9,6 @@ #include "VaRestTypes.h" #include "VaRestRequestAsync.generated.h" -DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FResponseDelegate, FString, ResposeMessage); - /** * This is async function that encapsulate callback delegates and make your code more clear. * @@ -21,41 +19,34 @@ DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FResponseDelegate, FString, ResposeM * https://www.unrealengine.com/marketplace/en-US/product/dbjson */ + +DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FResponseDelegate, FString, ResposeMessage); + UCLASS() class VAREST_API UVaRestRequestAsync : public UBlueprintAsyncActionBase { GENERATED_BODY() - - UPROPERTY() - UVaRestSubsystem* VaRestSubsystem; - UPROPERTY() - UVaRestRequestJSON* JSONRequest; - - UPROPERTY() - UVaRestJsonObject* JsonObject; - - FString URL; EVaRestRequestVerb Verb; EVaRestRequestContentType ContentType; FString RequestMessage; -public: UPROPERTY(BlueprintAssignable) FResponseDelegate OnSuccess; UPROPERTY(BlueprintAssignable) FResponseDelegate OnFail; - UFUNCTION(BlueprintCallable, meta = (BlueprintUnternalUseOnly = "true", DisplayName = "VaRest Simple Request Async")) - static UVaRestRequestAsync* VaRestRequestAsync(FString URL, EVaRestRequestVerb Verb, EVaRestRequestContentType ContentType, FString RequestMessage); - - virtual void Activate() override; - UFUNCTION() void OnSuccessResend(class UVaRestRequestJSON* RequestJSON); UFUNCTION() void OnFailureResend(class UVaRestRequestJSON* RequestJSON); -}; + + virtual void Activate() override; + +public: + UFUNCTION(BlueprintCallable, meta = (BlueprintUnternalUseOnly = "true", DisplayName = "VaRest Simple Request Async")) + static UVaRestRequestAsync* VaRestRequestAsync(FString URL, EVaRestRequestVerb Verb, EVaRestRequestContentType ContentType, FString RequestMessage); +}; \ No newline at end of file From 78730279e759314f83d31256ab4d2c7e2b187933 Mon Sep 17 00:00:00 2001 From: AlfieAllen <31819230+AlfieAllen@users.noreply.github.com> Date: Sun, 23 Jul 2023 12:39:02 +0300 Subject: [PATCH 3/5] update function code, apply Clang-Format --- Source/VaRest/Private/VaRestRequestAsync.cpp | 43 ---------------- .../Private/VaRestSimpleRequestAsync.cpp | 49 +++++++++++++++++++ ...uestAsync.h => VaRestSimpleRequestAsync.h} | 19 ++++--- 3 files changed, 58 insertions(+), 53 deletions(-) delete mode 100644 Source/VaRest/Private/VaRestRequestAsync.cpp create mode 100644 Source/VaRest/Private/VaRestSimpleRequestAsync.cpp rename Source/VaRest/Public/{VaRestRequestAsync.h => VaRestSimpleRequestAsync.h} (71%) diff --git a/Source/VaRest/Private/VaRestRequestAsync.cpp b/Source/VaRest/Private/VaRestRequestAsync.cpp deleted file mode 100644 index c790ebef..00000000 --- a/Source/VaRest/Private/VaRestRequestAsync.cpp +++ /dev/null @@ -1,43 +0,0 @@ -// Alan Verbilo 2021 for VaRest plugin. - -#include "VaRestRequestAsync.h" - -UVaRestRequestAsync* UVaRestRequestAsync::VaRestRequestAsync(FString URL, EVaRestRequestVerb Verb, EVaRestRequestContentType ContentType, FString RequestMessage) -{ - UVaRestRequestAsync* object = NewObject(); - - object->URL = URL; - object->Verb = Verb; - object->ContentType = ContentType; - object->RequestMessage = RequestMessage; - - return object; -} - -void UVaRestRequestAsync::Activate() -{ - // Create request - UVaRestSubsystem* VaRestSubsystem = GEngine->GetEngineSubsystem(); - UVaRestRequestJSON* RequestJSON = VaRestSubsystem->ConstructVaRestRequestExt(Verb, ContentType); - UVaRestJsonObject* JsonObject = RequestJSON->GetRequestObject(); - JsonObject->DecodeJson(RequestMessage); - - // Bind to delegates - RequestJSON->OnRequestComplete.AddDynamic(this, &UVaRestRequestAsync::OnSuccessResend); - RequestJSON->OnRequestFail.AddDynamic(this, &UVaRestRequestAsync::OnFailureResend); - - // Start request - RequestJSON->ProcessURL(URL); -} - -void UVaRestRequestAsync::OnSuccessResend(class UVaRestRequestJSON* RequestJSON) -{ - OnSuccess.Broadcast(RequestJSON->GetResponseContentAsString()); - RemoveFromRoot(); -} - -void UVaRestRequestAsync::OnFailureResend(class UVaRestRequestJSON* RequestJSON) -{ - OnFail.Broadcast(RequestJSON->GetResponseContentAsString()); - RemoveFromRoot(); -} \ No newline at end of file diff --git a/Source/VaRest/Private/VaRestSimpleRequestAsync.cpp b/Source/VaRest/Private/VaRestSimpleRequestAsync.cpp new file mode 100644 index 00000000..0c77aa1a --- /dev/null +++ b/Source/VaRest/Private/VaRestSimpleRequestAsync.cpp @@ -0,0 +1,49 @@ +// Alan Verbilo 2021-2023 for VaRest plugin. + +#include "VaRestSimpleRequestAsync.h" + +UVaRestSimpleRequestAsync* UVaRestSimpleRequestAsync::VaRestSimpleRequestAsync(FString URL, EVaRestRequestVerb Verb, EVaRestRequestContentType ContentType, FString RequestMessage) +{ + UVaRestSimpleRequestAsync* object = NewObject(); + + object->URL = URL; + object->Verb = Verb; + object->ContentType = ContentType; + object->RequestMessage = RequestMessage; + + return object; +} + +void UVaRestSimpleRequestAsync::Activate() +{ + // Create request + UVaRestSubsystem* VaRestSubsystem = GEngine->GetEngineSubsystem(); + UVaRestRequestJSON* RequestJSON = VaRestSubsystem->ConstructVaRestRequestExt(Verb, ContentType); + UVaRestJsonObject* JsonObject = RequestJSON->GetRequestObject(); + JsonObject->DecodeJson(RequestMessage); + + // Bind to delegates + RequestJSON->OnRequestComplete.AddDynamic(this, &UVaRestSimpleRequestAsync::OnSuccessResend); + RequestJSON->OnRequestFail.AddDynamic(this, &UVaRestSimpleRequestAsync::OnFailureResend); + + // Start request + RequestJSON->ProcessURL(URL); +} + +void UVaRestSimpleRequestAsync::OnSuccessResend(class UVaRestRequestJSON* RequestJSON) +{ + // Unbind delegates + RequestJSON->OnRequestComplete.RemoveDynamic(this, &UVaRestSimpleRequestAsync::OnSuccessResend); + RequestJSON->OnRequestFail.RemoveDynamic(this, &UVaRestSimpleRequestAsync::OnFailureResend); + + OnSuccess.Broadcast(RequestJSON->GetResponseCode(), RequestJSON->GetResponseContentAsString()); +} + +void UVaRestSimpleRequestAsync::OnFailureResend(class UVaRestRequestJSON* RequestJSON) +{ + // Unbind delegates + RequestJSON->OnRequestComplete.RemoveDynamic(this, &UVaRestSimpleRequestAsync::OnSuccessResend); + RequestJSON->OnRequestFail.RemoveDynamic(this, &UVaRestSimpleRequestAsync::OnFailureResend); + + OnFail.Broadcast(RequestJSON->GetResponseCode(), RequestJSON->GetResponseContentAsString()); +} \ No newline at end of file diff --git a/Source/VaRest/Public/VaRestRequestAsync.h b/Source/VaRest/Public/VaRestSimpleRequestAsync.h similarity index 71% rename from Source/VaRest/Public/VaRestRequestAsync.h rename to Source/VaRest/Public/VaRestSimpleRequestAsync.h index df2ef537..d917451f 100644 --- a/Source/VaRest/Public/VaRestRequestAsync.h +++ b/Source/VaRest/Public/VaRestSimpleRequestAsync.h @@ -1,4 +1,4 @@ -// Alan Verbilo 2021 for VaRest plugin. +// Alan Verbilo 2021-2023 for VaRest plugin. #pragma once @@ -7,7 +7,7 @@ #include "VaRestRequestJSON.h" #include "VaRestSubsystem.h" #include "VaRestTypes.h" -#include "VaRestRequestAsync.generated.h" +#include "VaRestSimpleRequestAsync.generated.h" /** * This is async function that encapsulate callback delegates and make your code more clear. @@ -19,19 +19,18 @@ * https://www.unrealengine.com/marketplace/en-US/product/dbjson */ +DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FResponseDelegate, int32, ResponseCode, FString, ResposeMessage); -DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FResponseDelegate, FString, ResposeMessage); - -UCLASS() -class VAREST_API UVaRestRequestAsync : public UBlueprintAsyncActionBase +UCLASS(MinimalAPI) +class UVaRestSimpleRequestAsync : public UBlueprintAsyncActionBase { GENERATED_BODY() - + FString URL; EVaRestRequestVerb Verb; EVaRestRequestContentType ContentType; FString RequestMessage; - + UPROPERTY(BlueprintAssignable) FResponseDelegate OnSuccess; @@ -45,8 +44,8 @@ class VAREST_API UVaRestRequestAsync : public UBlueprintAsyncActionBase void OnFailureResend(class UVaRestRequestJSON* RequestJSON); virtual void Activate() override; - + public: UFUNCTION(BlueprintCallable, meta = (BlueprintUnternalUseOnly = "true", DisplayName = "VaRest Simple Request Async")) - static UVaRestRequestAsync* VaRestRequestAsync(FString URL, EVaRestRequestVerb Verb, EVaRestRequestContentType ContentType, FString RequestMessage); + static UVaRestSimpleRequestAsync* VaRestSimpleRequestAsync(FString URL, EVaRestRequestVerb Verb, EVaRestRequestContentType ContentType, FString RequestMessage); }; \ No newline at end of file From dc64aa371058a78b599c185828ac11a1425b58cb Mon Sep 17 00:00:00 2001 From: AlfieAllen <31819230+AlfieAllen@users.noreply.github.com> Date: Sun, 23 Jul 2023 12:43:07 +0300 Subject: [PATCH 4/5] change FStrings to const FStrings --- Source/VaRest/Private/VaRestSimpleRequestAsync.cpp | 2 +- Source/VaRest/Public/VaRestSimpleRequestAsync.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/VaRest/Private/VaRestSimpleRequestAsync.cpp b/Source/VaRest/Private/VaRestSimpleRequestAsync.cpp index 0c77aa1a..6f2a6cf4 100644 --- a/Source/VaRest/Private/VaRestSimpleRequestAsync.cpp +++ b/Source/VaRest/Private/VaRestSimpleRequestAsync.cpp @@ -2,7 +2,7 @@ #include "VaRestSimpleRequestAsync.h" -UVaRestSimpleRequestAsync* UVaRestSimpleRequestAsync::VaRestSimpleRequestAsync(FString URL, EVaRestRequestVerb Verb, EVaRestRequestContentType ContentType, FString RequestMessage) +UVaRestSimpleRequestAsync* UVaRestSimpleRequestAsync::VaRestSimpleRequestAsync(const FString URL, EVaRestRequestVerb Verb, EVaRestRequestContentType ContentType, const FString RequestMessage) { UVaRestSimpleRequestAsync* object = NewObject(); diff --git a/Source/VaRest/Public/VaRestSimpleRequestAsync.h b/Source/VaRest/Public/VaRestSimpleRequestAsync.h index d917451f..788567ba 100644 --- a/Source/VaRest/Public/VaRestSimpleRequestAsync.h +++ b/Source/VaRest/Public/VaRestSimpleRequestAsync.h @@ -47,5 +47,5 @@ class UVaRestSimpleRequestAsync : public UBlueprintAsyncActionBase public: UFUNCTION(BlueprintCallable, meta = (BlueprintUnternalUseOnly = "true", DisplayName = "VaRest Simple Request Async")) - static UVaRestSimpleRequestAsync* VaRestSimpleRequestAsync(FString URL, EVaRestRequestVerb Verb, EVaRestRequestContentType ContentType, FString RequestMessage); + static UVaRestSimpleRequestAsync* VaRestSimpleRequestAsync(const FString URL, EVaRestRequestVerb Verb, EVaRestRequestContentType ContentType, const FString RequestMessage); }; \ No newline at end of file From 4ec3d28f295d2cf358c103be6c14ffaffe087a69 Mon Sep 17 00:00:00 2001 From: AlfieAllen <31819230+AlfieAllen@users.noreply.github.com> Date: Sun, 23 Jul 2023 12:48:03 +0300 Subject: [PATCH 5/5] fix typo in BlueprintInternalUseOnly word --- Source/VaRest/Public/VaRestSimpleRequestAsync.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/VaRest/Public/VaRestSimpleRequestAsync.h b/Source/VaRest/Public/VaRestSimpleRequestAsync.h index 788567ba..39125ba5 100644 --- a/Source/VaRest/Public/VaRestSimpleRequestAsync.h +++ b/Source/VaRest/Public/VaRestSimpleRequestAsync.h @@ -46,6 +46,6 @@ class UVaRestSimpleRequestAsync : public UBlueprintAsyncActionBase virtual void Activate() override; public: - UFUNCTION(BlueprintCallable, meta = (BlueprintUnternalUseOnly = "true", DisplayName = "VaRest Simple Request Async")) + UFUNCTION(BlueprintCallable, meta = (BlueprintInternalUseOnly = "true", DisplayName = "VaRest Simple Request Async")) static UVaRestSimpleRequestAsync* VaRestSimpleRequestAsync(const FString URL, EVaRestRequestVerb Verb, EVaRestRequestContentType ContentType, const FString RequestMessage); }; \ No newline at end of file