/** * OpenAPI Petstore * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. * * OpenAPI spec version: 1.0.0 * * * NOTE: This class is auto generated by OpenAPI Generator * https://github.com/OpenAPITools/openapi-generator * Do not edit the class manually. */ #include "OpenAPIStoreApi.h" #include "OpenAPIStoreApiOperations.h" #include "OpenAPIModule.h" #include "HttpModule.h" #include "Serialization/JsonSerializer.h" namespace OpenAPI { OpenAPIStoreApi::OpenAPIStoreApi() : Url(TEXT("http://petstore.swagger.io/v2")) { } OpenAPIStoreApi::~OpenAPIStoreApi() {} void OpenAPIStoreApi::SetURL(const FString& InUrl) { Url = InUrl; } void OpenAPIStoreApi::AddHeaderParam(const FString& Key, const FString& Value) { AdditionalHeaderParams.Add(Key, Value); } void OpenAPIStoreApi::ClearHeaderParams() { AdditionalHeaderParams.Reset(); } bool OpenAPIStoreApi::IsValid() const { if (Url.IsEmpty()) { UE_LOG(LogOpenAPI, Error, TEXT("OpenAPIStoreApi: Endpoint Url is not set, request cannot be performed")); return false; } return true; } void OpenAPIStoreApi::HandleResponse(FHttpResponsePtr HttpResponse, bool bSucceeded, Response& InOutResponse) const { InOutResponse.SetHttpResponse(HttpResponse); InOutResponse.SetSuccessful(bSucceeded); if (bSucceeded && HttpResponse.IsValid()) { InOutResponse.SetHttpResponseCode((EHttpResponseCodes::Type)HttpResponse->GetResponseCode()); FString ContentType = HttpResponse->GetContentType(); FString Content; if (ContentType.IsEmpty()) { return; // Nothing to parse } else if (ContentType.StartsWith(TEXT("application/json")) || ContentType.StartsWith("text/json")) { Content = HttpResponse->GetContentAsString(); TSharedPtr JsonValue; auto Reader = TJsonReaderFactory<>::Create(Content); if (FJsonSerializer::Deserialize(Reader, JsonValue) && JsonValue.IsValid()) { if (InOutResponse.FromJson(JsonValue)) return; // Successfully parsed } } else if(ContentType.StartsWith(TEXT("text/plain"))) { Content = HttpResponse->GetContentAsString(); InOutResponse.SetResponseString(Content); return; // Successfully parsed } // Report the parse error but do not mark the request as unsuccessful. Data could be partial or malformed, but the request succeeded. UE_LOG(LogOpenAPI, Error, TEXT("Failed to deserialize Http response content (type:%s):\n%s"), *ContentType , *Content); return; } // By default, assume we failed to establish connection InOutResponse.SetHttpResponseCode(EHttpResponseCodes::RequestTimeout); } bool OpenAPIStoreApi::DeleteOrder(const DeleteOrderRequest& Request, const FDeleteOrderDelegate& Delegate /*= FDeleteOrderDelegate()*/) const { if (!IsValid()) return false; FHttpRequestRef HttpRequest = FHttpModule::Get().CreateRequest(); HttpRequest->SetURL(*(Url + Request.ComputePath())); for(const auto& It : AdditionalHeaderParams) { HttpRequest->SetHeader(It.Key, It.Value); } Request.SetupHttpRequest(HttpRequest); HttpRequest->OnProcessRequestComplete().BindRaw(this, &OpenAPIStoreApi::OnDeleteOrderResponse, Delegate, Request.GetAutoRetryCount()); return HttpRequest->ProcessRequest(); } void OpenAPIStoreApi::OnDeleteOrderResponse(FHttpRequestPtr HttpRequest, FHttpResponsePtr HttpResponse, bool bSucceeded, FDeleteOrderDelegate Delegate, int AutoRetryCount) const { DeleteOrderResponse Response; Response.SetHttpRequest(HttpRequest); HandleResponse(HttpResponse, bSucceeded, Response); if(!Response.IsSuccessful() && AutoRetryCount > 0) { HttpRequest->OnProcessRequestComplete().BindRaw(this, &OpenAPIStoreApi::OnDeleteOrderResponse, Delegate, AutoRetryCount - 1); Response.AsyncRetry(); } else { Delegate.ExecuteIfBound(Response); } } bool OpenAPIStoreApi::GetInventory(const GetInventoryRequest& Request, const FGetInventoryDelegate& Delegate /*= FGetInventoryDelegate()*/) const { if (!IsValid()) return false; FHttpRequestRef HttpRequest = FHttpModule::Get().CreateRequest(); HttpRequest->SetURL(*(Url + Request.ComputePath())); for(const auto& It : AdditionalHeaderParams) { HttpRequest->SetHeader(It.Key, It.Value); } Request.SetupHttpRequest(HttpRequest); HttpRequest->OnProcessRequestComplete().BindRaw(this, &OpenAPIStoreApi::OnGetInventoryResponse, Delegate, Request.GetAutoRetryCount()); return HttpRequest->ProcessRequest(); } void OpenAPIStoreApi::OnGetInventoryResponse(FHttpRequestPtr HttpRequest, FHttpResponsePtr HttpResponse, bool bSucceeded, FGetInventoryDelegate Delegate, int AutoRetryCount) const { GetInventoryResponse Response; Response.SetHttpRequest(HttpRequest); HandleResponse(HttpResponse, bSucceeded, Response); if(!Response.IsSuccessful() && AutoRetryCount > 0) { HttpRequest->OnProcessRequestComplete().BindRaw(this, &OpenAPIStoreApi::OnGetInventoryResponse, Delegate, AutoRetryCount - 1); Response.AsyncRetry(); } else { Delegate.ExecuteIfBound(Response); } } bool OpenAPIStoreApi::GetOrderById(const GetOrderByIdRequest& Request, const FGetOrderByIdDelegate& Delegate /*= FGetOrderByIdDelegate()*/) const { if (!IsValid()) return false; FHttpRequestRef HttpRequest = FHttpModule::Get().CreateRequest(); HttpRequest->SetURL(*(Url + Request.ComputePath())); for(const auto& It : AdditionalHeaderParams) { HttpRequest->SetHeader(It.Key, It.Value); } Request.SetupHttpRequest(HttpRequest); HttpRequest->OnProcessRequestComplete().BindRaw(this, &OpenAPIStoreApi::OnGetOrderByIdResponse, Delegate, Request.GetAutoRetryCount()); return HttpRequest->ProcessRequest(); } void OpenAPIStoreApi::OnGetOrderByIdResponse(FHttpRequestPtr HttpRequest, FHttpResponsePtr HttpResponse, bool bSucceeded, FGetOrderByIdDelegate Delegate, int AutoRetryCount) const { GetOrderByIdResponse Response; Response.SetHttpRequest(HttpRequest); HandleResponse(HttpResponse, bSucceeded, Response); if(!Response.IsSuccessful() && AutoRetryCount > 0) { HttpRequest->OnProcessRequestComplete().BindRaw(this, &OpenAPIStoreApi::OnGetOrderByIdResponse, Delegate, AutoRetryCount - 1); Response.AsyncRetry(); } else { Delegate.ExecuteIfBound(Response); } } bool OpenAPIStoreApi::PlaceOrder(const PlaceOrderRequest& Request, const FPlaceOrderDelegate& Delegate /*= FPlaceOrderDelegate()*/) const { if (!IsValid()) return false; FHttpRequestRef HttpRequest = FHttpModule::Get().CreateRequest(); HttpRequest->SetURL(*(Url + Request.ComputePath())); for(const auto& It : AdditionalHeaderParams) { HttpRequest->SetHeader(It.Key, It.Value); } Request.SetupHttpRequest(HttpRequest); HttpRequest->OnProcessRequestComplete().BindRaw(this, &OpenAPIStoreApi::OnPlaceOrderResponse, Delegate, Request.GetAutoRetryCount()); return HttpRequest->ProcessRequest(); } void OpenAPIStoreApi::OnPlaceOrderResponse(FHttpRequestPtr HttpRequest, FHttpResponsePtr HttpResponse, bool bSucceeded, FPlaceOrderDelegate Delegate, int AutoRetryCount) const { PlaceOrderResponse Response; Response.SetHttpRequest(HttpRequest); HandleResponse(HttpResponse, bSucceeded, Response); if(!Response.IsSuccessful() && AutoRetryCount > 0) { HttpRequest->OnProcessRequestComplete().BindRaw(this, &OpenAPIStoreApi::OnPlaceOrderResponse, Delegate, AutoRetryCount - 1); Response.AsyncRetry(); } else { Delegate.ExecuteIfBound(Response); } } }