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

Commit

Permalink
Add response headers access
Browse files Browse the repository at this point in the history
  • Loading branch information
ufna committed Sep 20, 2015
1 parent 697e3a1 commit 1344c45
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 24 deletions.
55 changes: 34 additions & 21 deletions Source/VaRestPlugin/Classes/Json/VaRestRequestJSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,78 +45,88 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject
// Construction

/** Creates new request (totally empty) */
UFUNCTION(BlueprintPure, meta = (DisplayName = "Construct Json Request (Empty)", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "VaRest")
UFUNCTION(BlueprintPure, meta = (DisplayName = "Construct Json Request (Empty)", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "VaRest|Request")
static UVaRestRequestJSON* ConstructRequest(UObject* WorldContextObject);

/** Creates new request with defined verb and content type */
UFUNCTION(BlueprintPure, meta = (DisplayName = "Construct Json Request", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "VaRest")
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);

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

/** 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")
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
void SetContentType(ERequestContentType::Type ContentType);

/** Sets optional header info */
UFUNCTION(BlueprintCallable, Category = "VaRest")
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
void SetHeader(const FString& HeaderName, const FString& HeaderValue);

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


//////////////////////////////////////////////////////////////////////////
// Destruction and reset

/** Reset all internal saved data */
UFUNCTION(BlueprintCallable, Category = "VaRest")
UFUNCTION(BlueprintCallable, Category = "VaRest|Utility")
void ResetData();

/** Reset saved request data */
UFUNCTION(BlueprintCallable, Category = "VaRest")
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
void ResetRequestData();

/** Reset saved response data */
UFUNCTION(BlueprintCallable, Category = "VaRest")
UFUNCTION(BlueprintCallable, Category = "VaRest|Response")
void ResetResponseData();


//////////////////////////////////////////////////////////////////////////
// JSON data accessors

/** Get the Request Json object */
UFUNCTION(BlueprintCallable, Category = "VaRest")
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
UVaRestJsonObject* GetRequestObject();

/** Set the Request Json object */
UFUNCTION(BlueprintCallable, Category = "VaRest")
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
void SetRequestObject(UVaRestJsonObject* JsonObject);

/** Get the Response Json object */
UFUNCTION(BlueprintCallable, Category = "VaRest")
UFUNCTION(BlueprintCallable, Category = "VaRest|Response")
UVaRestJsonObject* GetResponseObject();

/** Set the Response Json object */
UFUNCTION(BlueprintCallable, Category = "VaRest")
UFUNCTION(BlueprintCallable, Category = "VaRest|Response")
void SetResponseObject(UVaRestJsonObject* JsonObject);


///////////////////////////////////////////////////////////////////////////
// Response Code accessor
// Response data access

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

/** Get value of desired response header */
UFUNCTION(BlueprintCallable, Category = "VaRest|Response")
FString GetResponseHeader(const FString HeaderName);

/** Get list of all response headers */
UFUNCTION(BlueprintCallable, Category = "VaRest|Response")
TArray<FString> GetAllResponseHeaders();


//////////////////////////////////////////////////////////////////////////
// URL processing

/** Open URL with current setup */
UFUNCTION(BlueprintCallable, Category = "VaRest")
UFUNCTION(BlueprintCallable, Category = "VaRest|Request")
virtual void ProcessURL(const FString& Url = TEXT("http://alyamkin.com"));

/** Apply current internal setup to request and process it */
Expand All @@ -132,11 +142,11 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject

public:
/** Event occured when the request has been completed */
UPROPERTY(BlueprintAssignable, Category = "VaRest")
UPROPERTY(BlueprintAssignable, Category = "VaRest|Event")
FOnRequestComplete OnRequestComplete;

/** Event occured when the request wasn't successfull */
UPROPERTY(BlueprintAssignable, Category = "VaRest")
UPROPERTY(BlueprintAssignable, Category = "VaRest|Event")
FOnRequestFail OnRequestFail;


Expand All @@ -145,14 +155,14 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject

public:
/** Request response stored as a string */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "VaRest")
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "VaRest|Response")
FString ResponseContent;

/** Is the response valid JSON? */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "VaRest")
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "VaRest|Response")
bool bIsValidJsonResponse;

private:
protected:
/** Internal request data stored as JSON */
UPROPERTY()
UVaRestJsonObject* RequestJsonObj;
Expand All @@ -170,6 +180,9 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject
/** Mapping of header section to values. Used to generate final header string for request */
TMap<FString, FString> RequestHeaders;

/** Cached key/value header pairs. Parsed once request completes */
TMap<FString, FString> ResponseHeaders;

/** Http Response code */
int32 ResponseCode;
};
44 changes: 41 additions & 3 deletions Source/VaRestPlugin/Private/Json/VaRestRequestJSON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ void UVaRestRequestJSON::ResetData()
{
ResetRequestData();
ResetResponseData();

ResponseCode = -1;
}

void UVaRestRequestJSON::ResetRequestData()
Expand All @@ -108,6 +106,9 @@ void UVaRestRequestJSON::ResetResponseData()
ResponseJsonObj = NewObject<UVaRestJsonObject>();
}

ResponseHeaders.Empty();
ResponseCode = -1;

bIsValidJsonResponse = false;
}

Expand Down Expand Up @@ -135,14 +136,39 @@ void UVaRestRequestJSON::SetResponseObject(UVaRestJsonObject* JsonObject)
ResponseJsonObj = JsonObject;
}


///////////////////////////////////////////////////////////////////////////
// Response Code accessor
// Response data access

int32 UVaRestRequestJSON::GetResponseCode()
{
return ResponseCode;
}

FString UVaRestRequestJSON::GetResponseHeader(const FString HeaderName)
{
FString Result;

FString* Header = ResponseHeaders.Find(HeaderName);
if (Header != NULL)
{
Result = *Header;
}

return Result;
}

TArray<FString> UVaRestRequestJSON::GetAllResponseHeaders()
{
TArray<FString> Result;
for (TMap<FString, FString>::TConstIterator It(ResponseHeaders); It; ++It)
{
Result.Add(It.Key() + TEXT(": ") + It.Value());
}
return Result;
}


//////////////////////////////////////////////////////////////////////////
// URL processing

Expand Down Expand Up @@ -271,6 +297,18 @@ void UVaRestRequestJSON::OnProcessRequestComplete(FHttpRequestPtr Request, FHttp
// Log response state
UE_LOG(LogVaRest, Log, TEXT("Response (%d): %s"), Response->GetResponseCode(), *Response->GetContentAsString());

// Process response headers
TArray<FString> Headers = Response->GetAllHeaders();
for (FString Header : Headers)
{
FString Key;
FString Value;
if (Header.Split(TEXT(": "), &Key, &Value))
{
ResponseHeaders.Add(Key, Value);
}
}

// Try to deserialize data to JSON
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(ResponseContent);
FJsonSerializer::Deserialize(JsonReader, ResponseJsonObj->GetRootObject());
Expand Down

0 comments on commit 1344c45

Please sign in to comment.