Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
Finish v1.1-r14
Browse files Browse the repository at this point in the history
  • Loading branch information
ufna committed Aug 3, 2016
2 parents eae2dc3 + cad3ec6 commit 17b1ca9
Show file tree
Hide file tree
Showing 12 changed files with 462 additions and 143 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ Key features:
* Blueprintable FJsonValue wrapper - **full Json features made for blueprints!**
* Both bindable events and **latent functions** are provided to control the asynchronous requests

Check the [Wiki](https://github.com/ufna/VaRest/wiki) tab for plugin usage examples and installation notes.
Check the [Wiki](https://hiazma.atlassian.net/wiki/display/VAR) for plugin usage examples and installation notes.

Current version: **1.1 R 13** (UE 4.11)
Current version: **1.1 R 14** (UE 4.11-4.12)

![SCREENSHOT](SCREENSHOT.jpg)

Expand Down
6 changes: 3 additions & 3 deletions Source/VaRestEditorPlugin/Classes/VaRest_BreakJson.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ struct FVaRest_NamedType
{
GENERATED_USTRUCT_BODY();

UPROPERTY(EditAnywhere)
UPROPERTY(EditAnywhere, Category = NamedType)
FString Name;

UPROPERTY(EditAnywhere)
UPROPERTY(EditAnywhere, Category = NamedType)
EVaRest_JsonType Type;

UPROPERTY(EditAnywhere)
UPROPERTY(EditAnywhere, Category = NamedType)
bool bIsArray;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class VARESTPLUGIN_API UVaRestJsonObject : public UObject
UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
FString EncodeJson() const;

/** Serialize Json to string (signel string without line breaks) */
/** Serialize Json to string (single string without line breaks) */
UFUNCTION(BlueprintCallable, Category = "VaRest|Json")
FString EncodeJsonToSingleString() const;

Expand Down
91 changes: 91 additions & 0 deletions Source/VaRestPlugin/Classes/VaRestLibrary.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright 2016 Vladimir Alyamkin. All Rights Reserved.

#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"

#include "VaRestTypes.h"
#include "VaRestLibrary.generated.h"

class UVaRestRequestJSON;
class UVaRestJsonObject;

DECLARE_DYNAMIC_DELEGATE_OneParam(FVaRestCallDelegate, UVaRestRequestJSON*, Request);

USTRUCT()
struct FVaRestCallResponse
{
GENERATED_USTRUCT_BODY()

UPROPERTY()
UVaRestRequestJSON* Request;

UPROPERTY()
UObject* WorldContextObject;

UPROPERTY()
FVaRestCallDelegate Callback;

FDelegateHandle CompleteDelegateHandle;
FDelegateHandle FailDelegateHandle;

FVaRestCallResponse()
: Request(nullptr)
, WorldContextObject(nullptr)
{
}

};

/**
* Usefull tools for REST communications
*/
UCLASS()
class UVaRestLibrary : public UBlueprintFunctionLibrary
{
GENERATED_BODY()


//////////////////////////////////////////////////////////////////////////
// Helpers

public:
/** Applies percent-encoding to text */
UFUNCTION(BlueprintCallable, Category = "VaRest|Utility")
static FString PercentEncode(const FString& Source);

/**
* Encodes a FString into a Base64 string
*
* @param Source The string data to convert
* @return A string that encodes the binary data in a way that can be safely transmitted via various Internet protocols
*/
UFUNCTION(BlueprintCallable, Category = "VaRest|Utility", meta = (DisplayName = "Base64 Encode"))
static FString Base64Encode(const FString& Source);

/**
* Decodes a Base64 string into a FString
*
* @param Source The stringified data to convert
* @param Dest The out buffer that will be filled with the decoded data
* @return True if the buffer was decoded, false if it failed to decode
*/
UFUNCTION(BlueprintCallable, Category = "VaRest|Utility", meta = (DisplayName = "Base64 Decode"))
static bool Base64Decode(const FString& Source, FString& Dest);


//////////////////////////////////////////////////////////////////////////
// Easy URL processing

public:
/** Easy way to process http requests */
UFUNCTION(BlueprintCallable, Category = "VaRest|Utility", meta = (WorldContext = "WorldContextObject"))
static void CallURL(UObject* WorldContextObject, const FString& URL, ERequestVerb Verb, ERequestContentType ContentType, UVaRestJsonObject* VaRestJson, const FVaRestCallDelegate& Callback);

/** Called when URL is processed (one for both success/unsuccess events)*/
static void OnCallComplete(UVaRestRequestJSON* Request);

private:
static TMap<UVaRestRequestJSON*, FVaRestCallResponse> RequestMap;

};
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

#pragma once

#include "Delegate.h"
#include "Http.h"
#include "Map.h"
#include "Json.h"

#include "VaRestTypes.h"
#include "VaRestRequestJSON.generated.h"

/**
Expand Down Expand Up @@ -57,41 +63,26 @@ template <class T> class FVaRestLatentAction : public FPendingLatentAction
const int32 OutputLink;
const FWeakObjectPtr CallbackTarget;
T &Result;

};

/** Verb (GET, PUT, POST) used by the request */
UENUM(BlueprintType)
namespace ERequestVerb
template <class T> void FVaRestLatentAction<T>::Cancel()
{
enum Type
UObject *Obj = Request.Get();
if (Obj != nullptr)
{
GET,
POST,
PUT,
DEL UMETA(DisplayName="DELETE"),
/** Set CUSTOM verb by SetCustomVerb() function */
CUSTOM
};
((UVaRestRequestJSON*)Obj)->Cancel();
}
}

/** Content type (json, urlencoded, etc.) used by the request */
UENUM(BlueprintType)
namespace ERequestContentType
{
enum Type
{
x_www_form_urlencoded_url UMETA(DisplayName = "x-www-form-urlencoded (URL)"),
x_www_form_urlencoded_body UMETA(DisplayName = "x-www-form-urlencoded (Request Body)"),
json,
binary
};
}

/** Generate a delegates for callback events */
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnRequestComplete, class UVaRestRequestJSON*, Request);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnRequestFail, class UVaRestRequestJSON*, Request);

DECLARE_MULTICAST_DELEGATE_OneParam(FOnStaticRequestComplete, class UVaRestRequestJSON*);
DECLARE_MULTICAST_DELEGATE_OneParam(FOnStaticRequestFail, class UVaRestRequestJSON*);


/**
* General helper class http requests via blueprints
*/
Expand All @@ -110,11 +101,11 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject

/** Creates new request with defined verb and content type */
UFUNCTION(BlueprintPure, meta = (DisplayName = "Construct Json Request", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "VaRest|Request")
static UVaRestRequestJSON* ConstructRequestExt(UObject* WorldContextObject, ERequestVerb::Type Verb, ERequestContentType::Type ContentType);
static UVaRestRequestJSON* ConstructRequestExt(UObject* WorldContextObject, ERequestVerb Verb, ERequestContentType ContentType);

/** Set verb to the request */
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
void SetVerb(ERequestVerb::Type Verb);
void SetVerb(ERequestVerb Verb);

/** Set custom verb to the request */
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
Expand All @@ -123,7 +114,7 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject
/** Set content type to the request. If you're using the x-www-form-urlencoded,
* params/constaints should be defined as key=ValueString pairs from Json data */
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
void SetContentType(ERequestContentType::Type ContentType);
void SetContentType(ERequestContentType ContentType);

/** Set content type of the request for binary post data */
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
Expand All @@ -137,10 +128,6 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
void SetHeader(const FString& HeaderName, const FString& HeaderValue);

/** Applies percent-encoding to text */
UFUNCTION(BlueprintCallable, Category = "VaRest|Utility")
static FString PercentEncode(const FString& Text);


//////////////////////////////////////////////////////////////////////////
// Destruction and reset
Expand Down Expand Up @@ -183,9 +170,17 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject


///////////////////////////////////////////////////////////////////////////
// Response data access
// Request/response data access

/** Get url of http request */
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
FString GetURL();

/** Get the responce code of the last query */
/** Get status of http request */
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
ERequestStatus GetStatus();

/** Get the response code of the last query */
UFUNCTION(BlueprintCallable, Category = "VaRest|Response")
int32 GetResponseCode();

Expand All @@ -210,7 +205,7 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject
virtual void ApplyURL(const FString& Url, UVaRestJsonObject *&Result, UObject* WorldContextObject, struct FLatentActionInfo LatentInfo);

/** Apply current internal setup to request and process it */
void ProcessRequest(TSharedRef<IHttpRequest> HttpRequest);
void ProcessRequest();


//////////////////////////////////////////////////////////////////////////
Expand All @@ -228,6 +223,37 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject
/** Event occured when the request wasn't successfull */
UPROPERTY(BlueprintAssignable, Category = "VaRest|Event")
FOnRequestFail OnRequestFail;

/** Event occured when the request has been completed */
FOnStaticRequestComplete OnStaticRequestComplete;

/** Event occured when the request wasn't successfull */
FOnStaticRequestFail OnStaticRequestFail;


//////////////////////////////////////////////////////////////////////////
// Tags

public:
/** Add tag to this request */
UFUNCTION(BlueprintCallable, Category = "VaRest|Utility")
void AddTag(FName Tag);

/**
* Remove tag from this request
*
* @return Number of removed elements
*/
UFUNCTION(BlueprintCallable, Category = "VaRest|Utility")
int32 RemoveTag(FName Tag);

/** See if this request contains the supplied tag */
UFUNCTION(BlueprintCallable, Category = "VaRest|Utility")
bool HasTag(FName Tag) const;

protected:
/** Array of tags that can be used for grouping and categorizing */
TArray<FName> Tags;


//////////////////////////////////////////////////////////////////////////
Expand All @@ -244,7 +270,7 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject

protected:
/** Latent action helper */
FVaRestLatentAction <UVaRestJsonObject*> *ContinueAction;
FVaRestLatentAction<UVaRestJsonObject*>* ContinueAction;

/** Internal request data stored as JSON */
UPROPERTY()
Expand All @@ -261,10 +287,10 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject
UVaRestJsonObject* ResponseJsonObj;

/** Verb for making request (GET,POST,etc) */
ERequestVerb::Type RequestVerb;
ERequestVerb RequestVerb;

/** Content type to be applied for request */
ERequestContentType::Type RequestContentType;
ERequestContentType RequestContentType;

/** Mapping of header section to values. Used to generate final header string for request */
TMap<FString, FString> RequestHeaders;
Expand All @@ -278,4 +304,7 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject
/** Custom verb that will be used with RequestContentType == CUSTOM */
FString CustomVerb;

/** Request we're currently processing */
TSharedRef<IHttpRequest> HttpRequest = FHttpModule::Get().CreateRequest();

};
41 changes: 41 additions & 0 deletions Source/VaRestPlugin/Classes/VaRestTypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2016 Vladimir Alyamkin. All Rights Reserved.

#pragma once

/** Verb (GET, PUT, POST) used by the request */
UENUM(BlueprintType)
enum class ERequestVerb : uint8
{
GET,
POST,
PUT,
DEL UMETA(DisplayName = "DELETE"),
/** Set CUSTOM verb by SetCustomVerb() function */
CUSTOM
};

/** Content type (json, urlencoded, etc.) used by the request */
UENUM(BlueprintType)
enum class ERequestContentType : uint8
{
x_www_form_urlencoded_url UMETA(DisplayName = "x-www-form-urlencoded (URL)"),
x_www_form_urlencoded_body UMETA(DisplayName = "x-www-form-urlencoded (Request Body)"),
json,
binary
};

/** Enumerates the current state of an Http request */
UENUM(BlueprintType)
enum class ERequestStatus : uint8
{
/** Has not been started via ProcessRequest() */
NotStarted,
/** Currently being ticked and processed */
Processing,
/** Finished but failed */
Failed,
/** Failed because it was unable to connect (safe to retry) */
Failed_ConnectionError,
/** Finished and was successful */
Succeeded
};
Loading

0 comments on commit 17b1ca9

Please sign in to comment.