HTTPRequestClient.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright (c) 2023 King Wai Mark
  2. #pragma once
  3. #include "HttpModule.h"
  4. #include "Interfaces/IHttpRequest.h"
  5. #include "Interfaces/IHttpResponse.h"
  6. #include "Serialization/JsonReader.h"
  7. #include "Serialization/JsonSerializer.h"
  8. #include "CoreMinimal.h"
  9. #include "Kismet/BlueprintFunctionLibrary.h"
  10. #include "HTTPRequestClient.generated.h"
  11. /**
  12. * The types of request methods available.
  13. *
  14. * @author King Wai Mark
  15. */
  16. UENUM(BlueprintType)
  17. enum class EMethod : uint8
  18. {
  19. GET,
  20. POST,
  21. PUT,
  22. PATCH,
  23. DEL UMETA(DisplayName = "DELETE"),
  24. COPY,
  25. HEAD,
  26. OPTIONS,
  27. LINK,
  28. UNLINK,
  29. LOCK,
  30. UNLOCK,
  31. PROPFIND,
  32. VIEW
  33. };
  34. /**
  35. * @author King Wai Mark
  36. *
  37. * JSON Object that is readable for blueprints.
  38. *
  39. */
  40. USTRUCT(BlueprintType)
  41. struct FJSONObject
  42. {
  43. GENERATED_BODY()
  44. TSharedPtr<FJsonObject> JsonParsed = nullptr;
  45. };
  46. DECLARE_DYNAMIC_DELEGATE_TwoParams(FResponse, uint8, Status, FString, ResponseString);
  47. /**
  48. * @author King Wai Mark
  49. */
  50. UCLASS()
  51. class HTTPREQUESTFORBLUEPRINTS_API UHTTPRequestClient : public UBlueprintFunctionLibrary
  52. {
  53. GENERATED_BODY()
  54. public:
  55. /**
  56. * Set the timeout duration in seconds for the request. Should stop the request.
  57. *
  58. * Cannot be less than 3 seconds.
  59. */
  60. static int RequestTimeoutDuration;
  61. /**
  62. * Converts the EMethod emum value to a String.
  63. * @author King Wai Mark
  64. */
  65. static FString MethodToString(EMethod Method);
  66. /**
  67. * The start of a HTTP Request.
  68. *
  69. * @param Method The request Verb, GET, PUSH, POST, DELETE, etc.
  70. * @param URL
  71. * @param Params Query params, ?param1=...&param2=...
  72. * @param Headers Request Headers
  73. * @param Body Content for the request
  74. * @param OnComplete Callback
  75. *
  76. * @author King Wai Mark
  77. */
  78. UFUNCTION(BlueprintCallable, meta = (DisplayName = "Make a HTTP Request", Keywords = "Make a HTTP Request"), Category = "HTTP Requests For Blueprint Plugin")
  79. static void MakeAHttpRequest(const EMethod Method, const FString URL, const TMap<FString, FString> Params, const TMap<FString, FString> Headers, const FString Body, const FResponse &OnComplete);
  80. /**
  81. * Converts the Response String from the Make a HTTP Request callback into a JSON Object that can be used in a blueprint.
  82. *
  83. * @param ResponseString The response string returned from Make a HTTP Request
  84. *
  85. * @author King Wai Mark
  86. */
  87. UFUNCTION(BlueprintCallable, meta = (DisplayName = "To JSON", Keywords = "Response String to JSON"), Category = "HTTP Requests For Blueprint Plugin")
  88. static UPARAM(DisplayName = "JSON") FJSONObject ResponseStringToJSON(const FString ResponseString);
  89. /**
  90. * Attempts to get a Object value from the JSON object.
  91. *
  92. * @param JSON JSON Object from the HTTP Requests For Blueprint Plugin
  93. * @param Key Name of the string from JSON
  94. *
  95. * @author King Wai Mark
  96. */
  97. UFUNCTION(BlueprintCallable, meta = (DisplayName = "Get Object", Keywords = "Get Object from JSON"), Category = "HTTP Requests For Blueprint Plugin")
  98. static UPARAM(DisplayName = "JSON") FJSONObject GetObjectFromJSONObject(const FJSONObject JSON, const FString Key);
  99. /**
  100. * Attempts to get a Array of JSON objects from the JSON object.
  101. *
  102. * @param JSON JSON Object from the HTTP Requests For Blueprint Plugin
  103. * @param Key Name of the string from JSON
  104. *
  105. * @author King Wai Mark
  106. */
  107. UFUNCTION(BlueprintCallable, meta = (DisplayName = "Get Object Array", Keywords = "Get Object Array from JSON"), Category = "HTTP Requests For Blueprint Plugin")
  108. static UPARAM(DisplayName = "Array of JSON") TArray<FJSONObject> GetObjectArrayFromJSONObject(const FJSONObject JSON, const FString Key);
  109. /**
  110. * Attempts to get the String Array from the JSON object.
  111. *
  112. * @param JSON JSON Object from the HTTP Requests For Blueprint Plugin
  113. * @param Key Name of the string from JSON
  114. *
  115. * @author King Wai Mark
  116. */
  117. UFUNCTION(BlueprintCallable, meta = (DisplayName = "Get String Array", Keywords = "Get String Array from JSON"), Category = "HTTP Requests For Blueprint Plugin")
  118. static UPARAM(DisplayName = "Array of Strings") TArray<FString> GetStringArrayFromJSONObject(const FJSONObject JSON, const FString Key);
  119. /**
  120. * Attempts to get the Number Array from the JSON object.
  121. *
  122. * @param JSON JSON Object from the HTTP Requests For Blueprint Plugin
  123. * @param Key Name of the string from JSON
  124. *
  125. * @author King Wai Mark
  126. */
  127. UFUNCTION(BlueprintCallable, meta = (DisplayName = "Get Number Array", Keywords = "Get Number Array from JSON"), Category = "HTTP Requests For Blueprint Plugin")
  128. static UPARAM(DisplayName = "Array of Numbers") TArray<float> GetNumberArrayFromJSONObject(const FJSONObject JSON, const FString Key);
  129. /**
  130. * Attempts to get the Boolean Array from the JSON object.
  131. *
  132. * @param JSON JSON Object from the HTTP Requests For Blueprint Plugin
  133. * @param Key Name of the string from JSON
  134. *
  135. * @author King Wai Mark
  136. */
  137. UFUNCTION(BlueprintCallable, meta = (DisplayName = "Get Boolean Array", Keywords = "Get Boolean Array from JSON"), Category = "HTTP Requests For Blueprint Plugin")
  138. static UPARAM(DisplayName = "Array of Boolean") TArray<bool> GetBooleanArrayFromJSONObject(const FJSONObject JSON, const FString Key);
  139. /**
  140. * Attempts to get a string value from the JSON object.
  141. *
  142. * @param JSON JSON Object from the HTTP Requests For Blueprint Plugin
  143. * @param Key Name of the string from JSON
  144. *
  145. * @author King Wai Mark
  146. */
  147. UFUNCTION(BlueprintCallable, meta = (DisplayName = "Get String", Keywords = "Get String from JSON"), Category = "HTTP Requests For Blueprint Plugin")
  148. static UPARAM(DisplayName = "String") FString GetStringFromJSONObject(const FJSONObject JSON, const FString Key);
  149. /**
  150. * Attempts to get a number value from the JSON object.
  151. *
  152. * @param JSON JSON Object from the HTTP Requests For Blueprint Plugin
  153. * @param Key Name of the string from JSON
  154. *
  155. * @author King Wai Mark
  156. */
  157. UFUNCTION(BlueprintCallable, meta = (DisplayName = "Get Number", Keywords = "Get Number from JSON"), Category = "HTTP Requests For Blueprint Plugin")
  158. static UPARAM(DisplayName = "Number") float GetNumberFromJSONObject(const FJSONObject JSON, const FString Key);
  159. /**
  160. * Attempts to get a boolean value from the JSON object.
  161. *
  162. * @param JSON JSON Object from the HTTP Requests For Blueprint Plugin
  163. * @param Key Name of the string from JSON
  164. *
  165. * @author King Wai Mark
  166. */
  167. UFUNCTION(BlueprintCallable, meta = (DisplayName = "Get Boolean", Keywords = "Get Boolean from JSON"), Category = "HTTP Requests For Blueprint Plugin")
  168. static UPARAM(DisplayName = "Boolean") bool GetBooleanFromJSONObject(const FJSONObject JSON, const FString Key);
  169. /**
  170. * Sets the timeout duration of the request.
  171. *
  172. * Default is 60 seconds.
  173. *
  174. * @param TimeoutDuration Duration in seconds. Cannot be less than 3.
  175. *
  176. * @author King Wai Mark
  177. */
  178. UFUNCTION(BlueprintCallable, meta = (DisplayName = "Set Timeout Duration", Keywords = "Set Timeout Duration"), Category = "HTTP Requests For Blueprint Plugin")
  179. static void SetTimeoutDuration(const int TimeoutDuration);
  180. /**
  181. * Gets the current timeout duration.
  182. *
  183. *
  184. * @author King Wai Mark
  185. */
  186. UFUNCTION(BlueprintCallable, meta = (DisplayName = "Get Timeout Duration", Keywords = "Get Timeout Duration"), Category = "HTTP Requests For Blueprint Plugin")
  187. static int GetTimeoutDuration();
  188. /**
  189. * Converts a string to base 64 url.
  190. *
  191. *
  192. * @author King Wai Mark
  193. */
  194. UFUNCTION(BlueprintCallable, meta = (DisplayName = "Replace With ASCII", Keywords = "Replace With ASCII"), Category = "HTTP Requests For Blueprint Plugin")
  195. static FString ReplaceWithASCII(const FString& Str);
  196. };