diff --git a/Source/VaRest/Private/VaRestSimpleRequestAsync.cpp b/Source/VaRest/Private/VaRestSimpleRequestAsync.cpp new file mode 100644 index 0000000..6f2a6cf --- /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(const FString URL, EVaRestRequestVerb Verb, EVaRestRequestContentType ContentType, const 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/VaRestSimpleRequestAsync.h b/Source/VaRest/Public/VaRestSimpleRequestAsync.h new file mode 100644 index 0000000..39125ba --- /dev/null +++ b/Source/VaRest/Public/VaRestSimpleRequestAsync.h @@ -0,0 +1,51 @@ +// Alan Verbilo 2021-2023 for VaRest plugin. + +#pragma once + +#include "CoreMinimal.h" +#include "Kismet/BlueprintAsyncActionBase.h" +#include "VaRestRequestJSON.h" +#include "VaRestSubsystem.h" +#include "VaRestTypes.h" +#include "VaRestSimpleRequestAsync.generated.h" + +/** +* 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 +*/ + +DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FResponseDelegate, int32, ResponseCode, FString, ResposeMessage); + +UCLASS(MinimalAPI) +class UVaRestSimpleRequestAsync : public UBlueprintAsyncActionBase +{ + GENERATED_BODY() + + FString URL; + EVaRestRequestVerb Verb; + EVaRestRequestContentType ContentType; + FString RequestMessage; + + UPROPERTY(BlueprintAssignable) + FResponseDelegate OnSuccess; + + UPROPERTY(BlueprintAssignable) + FResponseDelegate OnFail; + + UFUNCTION() + void OnSuccessResend(class UVaRestRequestJSON* RequestJSON); + + UFUNCTION() + void OnFailureResend(class UVaRestRequestJSON* RequestJSON); + + virtual void Activate() override; + +public: + 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