Quick.HttpClient.pas 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. { ***************************************************************************
  2. Copyright (c) 2016-2018 Kike Pérez
  3. Unit : Quick.HttpClient
  4. Description : Json Http Client
  5. Author : Kike Pérez
  6. Version : 1.1
  7. Created : 22/05/2018
  8. Modified : 27/05/2018
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.HttpClient;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. Classes,
  26. SysUtils,
  27. {$IFDEF DELPHIXE8_UP}
  28. System.Net.HttpClient,
  29. System.Net.URLClient,
  30. System.NetConsts,
  31. System.JSON;
  32. {$ELSE}
  33. IdHTTP,
  34. IdException,
  35. {$IFDEF FPC}
  36. fpjson;
  37. {$ELSE}
  38. Data.DBXJSON;
  39. {$ENDIF}
  40. {$ENDIF}
  41. type
  42. IHttpRequestResponse = interface
  43. ['{64DC58F7-B551-4619-85E9-D13E781529CD}']
  44. function StatusCode : Integer;
  45. function StatusText : string;
  46. function Response : TJSONObject;
  47. end;
  48. THttpRequestResponse = class(TInterfacedObject,IHttpRequestResponse)
  49. private
  50. fStatusCode : Integer;
  51. fStatusText : string;
  52. fResponse : TJSONObject;
  53. public
  54. {$IFDEF DELPHIXE8_UP}
  55. constructor Create(aResponse : IHTTPResponse; const aContent : string);
  56. {$ELSE}
  57. constructor Create(aResponse : TIdHTTPResponse; const aContent : string);
  58. {$ENDIF}
  59. destructor Destroy; override;
  60. function StatusCode : Integer;
  61. function StatusText : string;
  62. function Response : TJSONObject;
  63. end;
  64. TJsonHttpClient = class
  65. private
  66. {$IFDEF DELPHIXE8_UP}
  67. fHTTPClient : System.Net.HttpClient.THTTPClient;
  68. {$ELSE}
  69. fHTTPClient : TIdHTTP;
  70. {$ENDIF}
  71. fUserAgent : string;
  72. fContentType : string;
  73. fResponseTimeout : Integer;
  74. fConnectionTimeout : Integer;
  75. fHandleRedirects : Boolean;
  76. procedure SetContentType(const aValue: string);
  77. procedure SetUserAgent(const aValue: string);
  78. procedure SetResponseTimeout(const aValue: Integer);
  79. procedure SetConnectionTimeout(const aValue: Integer);
  80. procedure SetHandleRedirects(const aValue: Boolean);
  81. public
  82. constructor Create;
  83. destructor Destroy; override;
  84. property UserAgent : string read fUserAgent write SetUserAgent;
  85. property ContentType : string read fContentType write SetContentType;
  86. property ResponseTimeout : Integer read fResponseTimeout write SetResponseTimeout;
  87. property ConnectionTimeout : Integer read fConnectionTimeout write SetConnectionTimeout;
  88. property HandleRedirects : Boolean read fHandleRedirects write SetHandleRedirects;
  89. function Get(const aURL : string) : IHttpRequestResponse;
  90. function Post(const aURL, aInContent : string) : IHttpRequestResponse; overload;
  91. function Post(const aURL : string; aJsonContent : TJsonObject) : IHttpRequestResponse; overload;
  92. end;
  93. implementation
  94. const
  95. DEF_USER_AGENT = 'XLHttpClient';
  96. constructor TJsonHttpClient.Create;
  97. begin
  98. {$IFDEF DELPHIXE8_UP}
  99. fHTTPClient := THTTPClient.Create;
  100. fHTTPClient.ContentType := 'application/json';
  101. fHTTPClient.UserAgent := DEF_USER_AGENT;
  102. {$ELSE}
  103. fHTTPClient := TIdHTTP.Create(nil);
  104. fHTTPClient.Request.ContentType := 'application/json';
  105. fHTTPClient.Request.UserAgent := DEF_USER_AGENT;
  106. {$ENDIF}
  107. end;
  108. destructor TJsonHttpClient.Destroy;
  109. begin
  110. fHTTPClient.Free;
  111. inherited;
  112. end;
  113. function TJsonHttpClient.Get(const aURL : string) : IHttpRequestResponse;
  114. var
  115. {$IFDEF DELPHIXE8_UP}
  116. resp : IHTTPResponse;
  117. {$ELSE}
  118. resp : TIdHTTPResponse;
  119. {$ENDIF}
  120. bodycontent : TStringStream;
  121. responsecontent : TStringStream;
  122. begin
  123. bodycontent := TStringStream.Create;
  124. try
  125. responsecontent := TStringStream.Create;
  126. try
  127. {$IFDEF DELPHIXE8_UP}
  128. resp := fHTTPClient.Get(aURL,responsecontent,nil);
  129. {$ELSE}
  130. {$IFDEF FPC}
  131. fHTTPClient.Get(aURL,responsecontent);
  132. {$ELSE}
  133. fHTTPClient.Get(aURL,responsecontent,nil);
  134. {$ENDIF}
  135. resp := fHTTPClient.Response;
  136. {$ENDIF}
  137. Result := THttpRequestResponse.Create(resp,responsecontent.DataString);
  138. finally
  139. responsecontent.Free;
  140. end;
  141. finally
  142. bodycontent.Free;
  143. end;
  144. end;
  145. function TJsonHttpClient.Post(const aURL, aInContent : string) : IHttpRequestResponse;
  146. var
  147. {$IFDEF DELPHIXE8_UP}
  148. resp : IHTTPResponse;
  149. {$ELSE}
  150. resp : TIdHTTPResponse;
  151. {$ENDIF}
  152. responsecontent : TStringStream;
  153. postcontent : TStringStream;
  154. begin
  155. postcontent := TStringStream.Create(Utf8Encode(aInContent));
  156. try
  157. //postcontent.WriteString(aInContent);
  158. responsecontent := TStringStream.Create;
  159. try
  160. {$IFDEF DELPHIXE8_UP}
  161. resp := fHTTPClient.Post(aURL,postcontent,responsecontent);
  162. {$ELSE}
  163. {$IFDEF FPC}
  164. try
  165. fHTTPClient.Post(aURL,postcontent,responsecontent);
  166. fHTTPClient.Disconnect(False);
  167. except
  168. on E : Exception do
  169. begin
  170. if e.ClassType <> EIdConnClosedGracefully then raise e;
  171. end;
  172. end;
  173. {$ELSE}
  174. fHTTPClient.Post(aURL,postcontent,responsecontent);
  175. {$ENDIF}
  176. resp := fHTTPClient.Response;
  177. {$ENDIF}
  178. Result := THttpRequestResponse.Create(resp,responsecontent.DataString);
  179. finally
  180. responsecontent.Free;
  181. end;
  182. finally
  183. postcontent.Free;
  184. end;
  185. end;
  186. function TJsonHttpClient.Post(const aURL : string; aJsonContent : TJsonObject) : IHttpRequestResponse;
  187. begin
  188. {$IFDEF DELPHIXE8_UP}
  189. Result := Self.Post(aURL,aJsonContent.ToJSON);
  190. {$ELSE}
  191. {$IFDEF FPC}
  192. Result := Self.Post(aURL,aJsonContent.AsJson);
  193. {$ELSE}
  194. Result := Self.Post(aURL,aJsonContent.ToString);
  195. {$ENDIF}
  196. {$ENDIF}
  197. end;
  198. procedure TJsonHttpClient.SetConnectionTimeout(const aValue: Integer);
  199. begin
  200. fConnectionTimeout := aValue;
  201. {$IFDEF DELPHIXE8_UP}
  202. fHTTPClient.ConnectionTimeout := aValue;
  203. {$ELSE}
  204. fHTTPClient.ConnectTimeout := aValue;
  205. {$ENDIF}
  206. end;
  207. procedure TJsonHttpClient.SetContentType(const aValue: string);
  208. begin
  209. fContentType := aValue;
  210. {$IFDEF DELPHIXE8_UP}
  211. fHTTPClient.ContentType := aValue;
  212. {$ELSE}
  213. fHTTPClient.Request.ContentType := aValue;
  214. {$ENDIF}
  215. end;
  216. procedure TJsonHttpClient.SetHandleRedirects(const aValue: Boolean);
  217. begin
  218. fHandleRedirects := aValue;
  219. {$IFDEF DELPHIXE8_UP}
  220. fHTTPClient.HandleRedirects := aValue;
  221. {$ELSE}
  222. fHTTPClient.HandleRedirects := aValue;
  223. {$ENDIF}
  224. end;
  225. procedure TJsonHttpClient.SetResponseTimeout(const aValue: Integer);
  226. begin
  227. fResponseTimeout := aValue;
  228. {$IFDEF DELPHIXE8_UP}
  229. fHTTPClient.ResponseTimeout := aValue;
  230. {$ELSE}
  231. fHTTPClient.ReadTimeout := aValue;
  232. {$ENDIF}
  233. end;
  234. procedure TJsonHttpClient.SetUserAgent(const aValue: string);
  235. begin
  236. fUserAgent := aValue;
  237. {$IFDEF DELPHIXE8_UP}
  238. fHTTPClient.UserAgent := aValue;
  239. {$ELSE}
  240. fHTTPClient.Request.UserAgent := aValue;
  241. {$ENDIF}
  242. end;
  243. { THttpRequestResponse }
  244. {$IFDEF DELPHIXE8_UP}
  245. constructor THttpRequestResponse.Create(aResponse: IHTTPResponse; const aContent : string);
  246. begin
  247. fStatusCode := aResponse.StatusCode;
  248. fStatusText := aResponse.StatusText;
  249. if aContent <> '' then fResponse := TJSONObject.ParseJSONValue(aContent) as TJSONObject;
  250. //if response is not json, get as json result
  251. if fResponse = nil then
  252. begin
  253. fResponse := TJSONObject.Create;
  254. fResponse.AddPair('Result',aContent);
  255. end;
  256. end;
  257. {$ELSE}
  258. constructor THttpRequestResponse.Create(aResponse : TIdHTTPResponse; const aContent : string);
  259. begin
  260. fStatusCode := aResponse.ResponseCode;
  261. fStatusText := aResponse.ResponseText;
  262. if (aContent.Contains('{')) and (aContent.Contains('}')) then fResponse := GetJSON(aContent) as TJsonObject;
  263. //if response is not json, get as json result
  264. if fResponse = nil then
  265. begin
  266. fResponse := TJSONObject.Create;
  267. fResponse.Add('Result',aContent);
  268. end;
  269. end;
  270. {$ENDIF}
  271. destructor THttpRequestResponse.Destroy;
  272. begin
  273. if Assigned(fResponse) then fResponse.Free;
  274. inherited;
  275. end;
  276. function THttpRequestResponse.Response: TJSONObject;
  277. begin
  278. Result := fResponse;
  279. end;
  280. function THttpRequestResponse.StatusCode: Integer;
  281. begin
  282. Result := fStatusCode;
  283. end;
  284. function THttpRequestResponse.StatusText: string;
  285. begin
  286. Result := fStatusText;
  287. end;
  288. end.