diff --git a/Source/VaRestPlugin/Classes/Json/VaRestRequestJSON.h b/Source/VaRestPlugin/Classes/Json/VaRestRequestJSON.h index bc2f9917..bd9010ee 100644 --- a/Source/VaRestPlugin/Classes/Json/VaRestRequestJSON.h +++ b/Source/VaRestPlugin/Classes/Json/VaRestRequestJSON.h @@ -45,28 +45,28 @@ 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); @@ -74,15 +74,15 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject // 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(); @@ -90,33 +90,43 @@ class VARESTPLUGIN_API UVaRestRequestJSON : public UObject // 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 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 */ @@ -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; @@ -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; @@ -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 RequestHeaders; + /** Cached key/value header pairs. Parsed once request completes */ + TMap ResponseHeaders; + /** Http Response code */ int32 ResponseCode; }; diff --git a/Source/VaRestPlugin/Private/Json/VaRestRequestJSON.cpp b/Source/VaRestPlugin/Private/Json/VaRestRequestJSON.cpp index b3f03285..90129839 100644 --- a/Source/VaRestPlugin/Private/Json/VaRestRequestJSON.cpp +++ b/Source/VaRestPlugin/Private/Json/VaRestRequestJSON.cpp @@ -81,8 +81,6 @@ void UVaRestRequestJSON::ResetData() { ResetRequestData(); ResetResponseData(); - - ResponseCode = -1; } void UVaRestRequestJSON::ResetRequestData() @@ -108,6 +106,9 @@ void UVaRestRequestJSON::ResetResponseData() ResponseJsonObj = NewObject(); } + ResponseHeaders.Empty(); + ResponseCode = -1; + bIsValidJsonResponse = false; } @@ -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 UVaRestRequestJSON::GetAllResponseHeaders() +{ + TArray Result; + for (TMap::TConstIterator It(ResponseHeaders); It; ++It) + { + Result.Add(It.Key() + TEXT(": ") + It.Value()); + } + return Result; +} + + ////////////////////////////////////////////////////////////////////////// // URL processing @@ -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 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> JsonReader = TJsonReaderFactory::Create(ResponseContent); FJsonSerializer::Deserialize(JsonReader, ResponseJsonObj->GetRootObject());