Skip to content
This repository was archived by the owner on Nov 1, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Source/VaRest/Private/VaRestSimpleRequestAsync.cpp
Original file line number Diff line number Diff line change
@@ -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<UVaRestSimpleRequestAsync>();

object->URL = URL;
object->Verb = Verb;
object->ContentType = ContentType;
object->RequestMessage = RequestMessage;

return object;
}

void UVaRestSimpleRequestAsync::Activate()
{
// Create request
UVaRestSubsystem* VaRestSubsystem = GEngine->GetEngineSubsystem<UVaRestSubsystem>();
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());
}
51 changes: 51 additions & 0 deletions Source/VaRest/Public/VaRestSimpleRequestAsync.h
Original file line number Diff line number Diff line change
@@ -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);
};