HTTPRequestClient.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Copyright (c) 2023 King Wai Mark
  2. #include "HTTPRequestClient.h"
  3. int UHTTPRequestClient::RequestTimeoutDuration = 60;
  4. FString UHTTPRequestClient::MethodToString(EMethod Method)
  5. {
  6. switch (Method)
  7. {
  8. case EMethod::GET:
  9. return "GET";
  10. case EMethod::PUT:
  11. return "PUT";
  12. case EMethod::POST:
  13. return "POST";
  14. case EMethod::PATCH:
  15. return "PATCH";
  16. case EMethod::DEL:
  17. return "DELETE";
  18. case EMethod::COPY:
  19. return "COPY";
  20. case EMethod::HEAD:
  21. return "HEAD";
  22. case EMethod::OPTIONS:
  23. return "OPTIONS";
  24. case EMethod::LINK:
  25. return "LINK";
  26. case EMethod::UNLINK:
  27. return "UNLINK";
  28. case EMethod::LOCK:
  29. return "LOCK";
  30. case EMethod::UNLOCK:
  31. return "UNLOCK";
  32. case EMethod::PROPFIND:
  33. return "PROPFIND";
  34. case EMethod::VIEW:
  35. return "VIEW";
  36. }
  37. return "";
  38. }
  39. void UHTTPRequestClient::MakeAHttpRequest(const EMethod Method, const FString URL, const TMap<FString, FString> Params, const TMap<FString, FString> Headers, const FString Body, const FResponse &OnComplete)
  40. {
  41. FHttpModule& HttpModule = FHttpModule::Get();
  42. // 1. Create Request Object
  43. TSharedRef<IHttpRequest, ESPMode::ThreadSafe> HttpRequest = HttpModule.CreateRequest();
  44. // 2. Set the request method. E.g. GET, POST, PUSH, etc.
  45. const FString RequestMethod = UHTTPRequestClient::MethodToString(Method);
  46. if(RequestMethod.Len() <= 0)
  47. {
  48. UE_LOG(LogTemp, Error, TEXT("Request method not set."));
  49. return;
  50. }
  51. HttpRequest->SetVerb(RequestMethod);
  52. // 3. Set the url.
  53. if(URL.Len() <= 0)
  54. {
  55. UE_LOG(LogTemp, Error, TEXT("The URL for the request cannot be empty."));
  56. return;
  57. };
  58. FString FullUrl = URL.TrimStartAndEnd();
  59. // 3.a Add the query parameters using the Param map.
  60. int ParamIndex = 0;
  61. for (auto Param : Params)
  62. {
  63. if(ParamIndex == 0) FullUrl = FullUrl + "?";
  64. else FullUrl = FullUrl + "&";
  65. FullUrl = FullUrl + Param.Key + "=" + Param.Value;
  66. ParamIndex = ParamIndex + 1;
  67. }
  68. HttpRequest->SetURL(FullUrl);
  69. // 4. Set the request headers
  70. for (auto Header : Headers)
  71. {
  72. HttpRequest->AppendToHeader(Header.Key, Header.Value);
  73. }
  74. // 5. Add body to the request.
  75. HttpRequest->SetContentAsString(Body);
  76. // 6. Attach the callback to notify us that the process is completed.
  77. HttpRequest->OnProcessRequestComplete().BindLambda(
  78. // Here, we "capture" the 'this' pointer (the "&"), so our lambda can call this
  79. // class's methods in the callback.
  80. [&, OnComplete](
  81. FHttpRequestPtr Req,
  82. FHttpResponsePtr Res,
  83. bool ConnectedSuccessfully) mutable {
  84. OnComplete.ExecuteIfBound(Res->GetResponseCode(), Res->GetContentAsString());
  85. });
  86. // 7. Set Settings
  87. HttpRequest->SetTimeout(UHTTPRequestClient::RequestTimeoutDuration);
  88. // 8. Submit the request for processing
  89. HttpRequest->ProcessRequest();
  90. }
  91. FJSONObject UHTTPRequestClient::ResponseStringToJSON(const FString ResponseString)
  92. {
  93. FJSONObject JSONObject = FJSONObject();
  94. const TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(ResponseString);
  95. TSharedPtr<FJsonObject> JsonParsed;
  96. if (FJsonSerializer::Deserialize(JsonReader, JsonParsed)) {
  97. JSONObject.JsonParsed = JsonParsed;
  98. }
  99. return JSONObject;
  100. }
  101. FJSONObject UHTTPRequestClient::GetObjectFromJSONObject(const FJSONObject JSON, const FString Key)
  102. {
  103. FJSONObject JsonObject = FJSONObject();
  104. if(JSON.JsonParsed != nullptr)
  105. {
  106. JsonObject.JsonParsed = JSON.JsonParsed->GetObjectField(Key);
  107. }
  108. return JsonObject;
  109. }
  110. TArray<FJSONObject> UHTTPRequestClient::GetObjectArrayFromJSONObject(const FJSONObject JSON, const FString Key)
  111. {
  112. TArray<FJSONObject> ObjectArray = TArray<FJSONObject>();
  113. if(JSON.JsonParsed != nullptr)
  114. {
  115. for (auto ArrayField : JSON.JsonParsed->GetArrayField(Key))
  116. {
  117. FJSONObject TempJsonObject = FJSONObject();
  118. TempJsonObject.JsonParsed = ArrayField->AsObject();
  119. ObjectArray.Add(TempJsonObject);
  120. }
  121. }
  122. return ObjectArray;
  123. }
  124. TArray<FString> UHTTPRequestClient::GetStringArrayFromJSONObject(const FJSONObject JSON, const FString Key)
  125. {
  126. TArray<FString> StringArray = TArray<FString>();
  127. if(JSON.JsonParsed != nullptr)
  128. {
  129. JSON.JsonParsed->TryGetStringArrayField(Key, StringArray);
  130. }
  131. return StringArray;
  132. }
  133. TArray<float> UHTTPRequestClient::GetNumberArrayFromJSONObject(const FJSONObject JSON, const FString Key)
  134. {
  135. TArray<float> NumberArray = TArray<float>();
  136. if(JSON.JsonParsed != nullptr)
  137. {
  138. for (auto ArrayField : JSON.JsonParsed->GetArrayField(Key))
  139. {
  140. float TempNumber = ArrayField->AsNumber();
  141. NumberArray.Add(TempNumber);
  142. }
  143. }
  144. return NumberArray;
  145. }
  146. TArray<bool> UHTTPRequestClient::GetBooleanArrayFromJSONObject(const FJSONObject JSON, const FString Key)
  147. {
  148. TArray<bool> BooleanArray = TArray<bool>();
  149. if(JSON.JsonParsed != nullptr)
  150. {
  151. for (auto ArrayField : JSON.JsonParsed->GetArrayField(Key))
  152. {
  153. bool TempBool = ArrayField->AsBool();
  154. BooleanArray.Add(TempBool);
  155. }
  156. }
  157. return BooleanArray;
  158. }
  159. FString UHTTPRequestClient::GetStringFromJSONObject(const FJSONObject JSON, const FString Key)
  160. {
  161. FString StringData = "";
  162. if(JSON.JsonParsed != nullptr)
  163. {
  164. JSON.JsonParsed->TryGetStringField(Key, StringData);
  165. }
  166. return StringData;
  167. }
  168. float UHTTPRequestClient::GetNumberFromJSONObject(const FJSONObject JSON, const FString Key)
  169. {
  170. double NumberData = 0;
  171. if(JSON.JsonParsed != nullptr)
  172. {
  173. JSON.JsonParsed->TryGetNumberField(Key, NumberData);
  174. }
  175. return NumberData;
  176. }
  177. bool UHTTPRequestClient::GetBooleanFromJSONObject(const FJSONObject JSON, const FString Key)
  178. {
  179. bool BooleanData = false;
  180. if(JSON.JsonParsed != nullptr)
  181. {
  182. JSON.JsonParsed->TryGetBoolField(Key, BooleanData);
  183. }
  184. return BooleanData;
  185. }
  186. void UHTTPRequestClient::SetTimeoutDuration(const int TimeoutDuration)
  187. {
  188. if(RequestTimeoutDuration < 3)
  189. {
  190. UHTTPRequestClient::RequestTimeoutDuration = 3;
  191. } else
  192. {
  193. UHTTPRequestClient::RequestTimeoutDuration = TimeoutDuration;
  194. }
  195. }
  196. int UHTTPRequestClient::GetTimeoutDuration()
  197. {
  198. return UHTTPRequestClient::RequestTimeoutDuration;
  199. }
  200. FString UHTTPRequestClient::ReplaceWithASCII(const FString& Str)
  201. {
  202. FString EncodedURL = Str;
  203. EncodedURL.ReplaceInline(TEXT(" "), TEXT("%20"), ESearchCase::CaseSensitive);
  204. EncodedURL.ReplaceInline(TEXT("!"), TEXT("%21"), ESearchCase::CaseSensitive);
  205. EncodedURL.ReplaceInline(TEXT("\""), TEXT("%22"), ESearchCase::CaseSensitive);
  206. EncodedURL.ReplaceInline(TEXT("#"), TEXT("%23"), ESearchCase::CaseSensitive);
  207. EncodedURL.ReplaceInline(TEXT("$"), TEXT("%24"), ESearchCase::CaseSensitive);
  208. EncodedURL.ReplaceInline(TEXT("&"), TEXT("%26"), ESearchCase::CaseSensitive);
  209. EncodedURL.ReplaceInline(TEXT("'"), TEXT("%27"), ESearchCase::CaseSensitive);
  210. EncodedURL.ReplaceInline(TEXT("("), TEXT("%28"), ESearchCase::CaseSensitive);
  211. EncodedURL.ReplaceInline(TEXT(")"), TEXT("%29"), ESearchCase::CaseSensitive);
  212. EncodedURL.ReplaceInline(TEXT("*"), TEXT("%2A"), ESearchCase::CaseSensitive);
  213. EncodedURL.ReplaceInline(TEXT("+"), TEXT("%2B"), ESearchCase::CaseSensitive);
  214. EncodedURL.ReplaceInline(TEXT(","), TEXT("%2C"), ESearchCase::CaseSensitive);
  215. EncodedURL.ReplaceInline(TEXT("-"), TEXT("%2D"), ESearchCase::CaseSensitive);
  216. EncodedURL.ReplaceInline(TEXT("."), TEXT("%2E"), ESearchCase::CaseSensitive);
  217. EncodedURL.ReplaceInline(TEXT("/"), TEXT("%2F"), ESearchCase::CaseSensitive);
  218. EncodedURL.ReplaceInline(TEXT(":"), TEXT("%3A"), ESearchCase::CaseSensitive);
  219. EncodedURL.ReplaceInline(TEXT(";"), TEXT("%3B"), ESearchCase::CaseSensitive);
  220. EncodedURL.ReplaceInline(TEXT("<"), TEXT("%3C"), ESearchCase::CaseSensitive);
  221. EncodedURL.ReplaceInline(TEXT("="), TEXT("%3D"), ESearchCase::CaseSensitive);
  222. EncodedURL.ReplaceInline(TEXT(">"), TEXT("%3E"), ESearchCase::CaseSensitive);
  223. EncodedURL.ReplaceInline(TEXT("?"), TEXT("%3F"), ESearchCase::CaseSensitive);
  224. EncodedURL.ReplaceInline(TEXT("@"), TEXT("%40"), ESearchCase::CaseSensitive);
  225. EncodedURL.ReplaceInline(TEXT("["), TEXT("%5B"), ESearchCase::CaseSensitive);
  226. EncodedURL.ReplaceInline(TEXT("\\"), TEXT("%5C"), ESearchCase::CaseSensitive);
  227. EncodedURL.ReplaceInline(TEXT("]"), TEXT("%5D"), ESearchCase::CaseSensitive);
  228. EncodedURL.ReplaceInline(TEXT("^"), TEXT("%5E"), ESearchCase::CaseSensitive);
  229. EncodedURL.ReplaceInline(TEXT("_"), TEXT("%5F"), ESearchCase::CaseSensitive);
  230. EncodedURL.ReplaceInline(TEXT("`"), TEXT("%60"), ESearchCase::CaseSensitive);
  231. EncodedURL.ReplaceInline(TEXT("{"), TEXT("%7B"), ESearchCase::CaseSensitive);
  232. EncodedURL.ReplaceInline(TEXT("|"), TEXT("%7C"), ESearchCase::CaseSensitive);
  233. EncodedURL.ReplaceInline(TEXT("}"), TEXT("%7D"), ESearchCase::CaseSensitive);
  234. EncodedURL.ReplaceInline(TEXT("~"), TEXT("%7E"), ESearchCase::CaseSensitive);
  235. return EncodedURL;
  236. }