2020-08-18 23:19:17 +02:00
|
|
|
|
// ***************************************************************************
|
|
|
|
|
//
|
|
|
|
|
// Delphi MVC Framework
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) 2010-2020 Daniele Teti and the DMVCFramework Team
|
|
|
|
|
//
|
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
|
//
|
|
|
|
|
// Collaborators on this file:
|
|
|
|
|
// Jo<4A>o Ant<6E>nio Duarte (https://github.com/joaoduarte19)
|
|
|
|
|
//
|
|
|
|
|
// ***************************************************************************
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
//
|
|
|
|
|
// *************************************************************************** }
|
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
|
unit MVCFramework.RESTClient;
|
|
|
|
|
|
2020-09-09 02:00:08 +02:00
|
|
|
|
{$I dmvcframework.inc}
|
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
|
interface
|
|
|
|
|
|
|
|
|
|
uses
|
2020-08-04 00:48:35 +02:00
|
|
|
|
System.Classes,
|
2020-09-08 03:21:28 +02:00
|
|
|
|
System.SysUtils,
|
|
|
|
|
System.Net.HttpClient,
|
|
|
|
|
System.Net.Mime,
|
|
|
|
|
System.Net.URLClient,
|
|
|
|
|
System.Generics.Collections,
|
|
|
|
|
System.Rtti,
|
|
|
|
|
System.TypInfo,
|
|
|
|
|
MVCFramework.Commons,
|
|
|
|
|
MVCFramework.RESTClient.Indy,
|
2020-08-09 02:18:36 +02:00
|
|
|
|
MVCFramework.RESTClient.Intf,
|
2020-09-09 02:00:08 +02:00
|
|
|
|
MVCFramework.RESTClient.Commons,
|
2020-08-04 00:48:35 +02:00
|
|
|
|
MVCFramework.Serializer.Intf,
|
2020-08-09 02:18:36 +02:00
|
|
|
|
MVCFramework.Serializer.Commons,
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Data.DB;
|
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
|
type
|
2020-08-18 23:19:17 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Alias for the Indy-based TRESTClient. The implementation of TRESTClient has been discontinued, it remains for
|
|
|
|
|
/// compatibility only.
|
|
|
|
|
/// </summary>
|
|
|
|
|
TRESTClient = MVCFramework.RESTClient.Indy.TRESTClient deprecated
|
|
|
|
|
'Moved to the MVCFramework.RESTClient.Indy unit. It is highly recommended to migrate to the TMVCRESTClient implementation.';
|
|
|
|
|
|
|
|
|
|
IRESTResponse = MVCFramework.RESTClient.Indy.IRESTResponse deprecated
|
|
|
|
|
'Moved to the MVCFramework.RESTClient.Indy unit. It is highly recommended to migrate to the TMVCRESTClient implementation.';
|
|
|
|
|
|
2020-08-26 01:07:21 +02:00
|
|
|
|
TCookie = System.Net.HttpClient.TCookie;
|
|
|
|
|
TCookies = System.Net.HttpClient.TCookies;
|
2020-09-24 21:29:49 +02:00
|
|
|
|
TURLRequest = System.Net.URLClient.TURLRequest;
|
|
|
|
|
TCertificate = System.Net.URLClient.TCertificate;
|
2021-01-15 19:31:33 +01:00
|
|
|
|
TNameValuePair = System.Net.URLClient.TNameValuePair;
|
|
|
|
|
TNameValueArray = System.Net.URLClient.TNameValueArray;
|
2020-12-17 00:05:39 +01:00
|
|
|
|
IHTTPRequest = System.Net.HttpClient.IHTTPRequest;
|
|
|
|
|
IHTTPResponse = System.Net.HttpClient.IHTTPResponse;
|
2020-09-24 21:29:49 +02:00
|
|
|
|
|
2020-09-09 02:00:08 +02:00
|
|
|
|
{$IF defined(TOKYOORBETTER)}
|
2020-08-26 22:13:18 +02:00
|
|
|
|
THTTPSecureProtocol = System.Net.HttpClient.THTTPSecureProtocol;
|
|
|
|
|
THTTPSecureProtocols = System.Net.HttpClient.THTTPSecureProtocols;
|
2020-09-09 02:00:08 +02:00
|
|
|
|
{$ENDIF}
|
2020-08-26 01:07:21 +02:00
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
TMVCRESTClient = class(TInterfacedObject, IMVCRESTClient)
|
2017-04-24 00:19:53 +02:00
|
|
|
|
private
|
2021-04-11 22:37:14 +02:00
|
|
|
|
fLock: TObject;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
fHTTPClient: THTTPClient;
|
|
|
|
|
fBaseURL: string;
|
|
|
|
|
fResource: string;
|
|
|
|
|
fProxySettings: TProxySettings;
|
2020-09-09 02:00:08 +02:00
|
|
|
|
fParameters: TList<TMVCRESTParam>;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
fRawBody: TStringStream;
|
|
|
|
|
fBodyFormData: TMultipartFormData;
|
2020-08-04 00:48:35 +02:00
|
|
|
|
fSerializer: IMVCSerializer;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
fRttiContext: TRttiContext;
|
2020-09-22 01:32:21 +02:00
|
|
|
|
fNextRequestIsAsync: Boolean;
|
|
|
|
|
fAsyncCompletionHandler: TProc<IMVCRESTResponse>;
|
|
|
|
|
fAsyncCompletionHandlerWithError: TProc<Exception>;
|
|
|
|
|
fAsyncSynchronized: Boolean;
|
2020-09-24 21:29:49 +02:00
|
|
|
|
fValidateServerCertificate: TValidateServerCertificateProc;
|
2020-12-17 00:05:39 +01:00
|
|
|
|
fBeforeRequestProc: TBeforeRequestProc;
|
|
|
|
|
fRequestCompletedProc: TRequestCompletedProc;
|
|
|
|
|
fResponseCompletedProc: TResponseCompletedProc;
|
2020-09-24 21:29:49 +02:00
|
|
|
|
procedure DoValidateServerCertificate(const aSender: TObject; const aRequest: TURLRequest;
|
|
|
|
|
const aCertificate: TCertificate; var aAccepted: Boolean);
|
2020-12-17 00:05:39 +01:00
|
|
|
|
procedure DoBeforeRequest(aRequest: IHTTPRequest);
|
|
|
|
|
procedure DoRequestCompleted(aResponse: IHTTPResponse; var aHandled: Boolean);
|
|
|
|
|
procedure DoResponseCompleted(aMVCRESTResponse: IMVCRESTResponse);
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function GetBodyFormData: TMultipartFormData;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
function ObjectIsList(aObject: TObject): Boolean;
|
|
|
|
|
function SerializeObject(aObject: TObject): string;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
procedure SetContentType(const aContentType: string);
|
2020-09-22 01:32:21 +02:00
|
|
|
|
procedure SetParameter(const aParamType: TMVCRESTParamType; const aName, aValue: string);
|
2020-09-09 02:00:08 +02:00
|
|
|
|
procedure ClearParameters(const aParamType: TMVCRESTParamType);
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function GetFullURL: string;
|
2020-09-29 16:56:25 +02:00
|
|
|
|
function HTTPMethodName(const aHTTPMethod: TMVCHTTPMethodType): string;
|
2020-09-10 02:40:14 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Convert path parameters of type ($xxx) to {xxx}
|
|
|
|
|
/// </summary>
|
|
|
|
|
procedure DoConvertMVCPathParamsToRESTParams(var aURL: string);
|
2020-09-08 03:21:28 +02:00
|
|
|
|
procedure DoApplyPathParams(var aURL: string);
|
|
|
|
|
procedure DoApplyQueryParams(var aURL: string);
|
2020-09-09 02:00:08 +02:00
|
|
|
|
procedure DoApplyCookies(const aURL: string);
|
2020-09-18 01:06:24 +02:00
|
|
|
|
procedure DoApplyHeaders;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
procedure DoEncodeURL(var aURL: string);
|
2020-09-11 19:55:26 +02:00
|
|
|
|
procedure DoPrepareBodyRequest(var aBodyStream: TStream);
|
2020-09-08 03:21:28 +02:00
|
|
|
|
|
2020-09-22 01:32:21 +02:00
|
|
|
|
procedure ExecuteAsyncRequest(const aMethod: TMVCHTTPMethodType);
|
|
|
|
|
function InternalExecuteRequest(const aMethod: TMVCHTTPMethodType): IMVCRESTResponse;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function ExecuteRequest(const aMethod: TMVCHTTPMethodType): IMVCRESTResponse;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
public
|
2020-08-04 00:48:35 +02:00
|
|
|
|
constructor Create;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
destructor Destroy; override;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
class function New: IMVCRESTClient;
|
|
|
|
|
|
|
|
|
|
{ IMVCRESTClient }
|
2020-08-14 00:43:25 +02:00
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function BaseURL(const aBaseURL: string): IMVCRESTClient; overload;
|
2020-08-26 01:07:21 +02:00
|
|
|
|
function BaseURL(const aHost: string; const aPort: Integer): IMVCRESTClient; overload;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function BaseURL: string; overload;
|
2020-08-21 02:45:51 +02:00
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function ProxyServer(const aProxyServer: string): IMVCRESTClient; overload;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function ProxyServer: string; overload;
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function ProxyPort(const aProxyPort: Integer): IMVCRESTClient; overload;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function ProxyPort: Integer; overload;
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function ProxyUsername(const aProxyUsername: string): IMVCRESTClient; overload;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function ProxyUsername: string; overload;
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function ProxyPassword(const aProxyPassword: string): IMVCRESTClient; overload;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function ProxyPassword: string; overload;
|
2020-08-18 23:19:17 +02:00
|
|
|
|
|
2020-09-09 02:00:08 +02:00
|
|
|
|
{$IF defined(TOKYOORBETTER)}
|
2020-08-26 22:13:18 +02:00
|
|
|
|
function SecureProtocols(const aSecureProtocols: THTTPSecureProtocols): IMVCRESTClient; overload;
|
|
|
|
|
function SecureProtocols: THTTPSecureProtocols; overload;
|
2020-09-09 02:00:08 +02:00
|
|
|
|
{$ENDIF}
|
2020-09-24 21:29:49 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Add a custom SSL certificate validation. By default all certificates are accepted.
|
|
|
|
|
/// </summary>
|
|
|
|
|
function SetValidateServerCertificateProc(aValidateCertificateProc: TValidateServerCertificateProc): IMVCRESTClient;
|
|
|
|
|
|
2020-09-24 20:03:11 +02:00
|
|
|
|
/// <summary>
|
2020-12-17 00:05:39 +01:00
|
|
|
|
/// Executes before send the request
|
|
|
|
|
/// </summary>
|
|
|
|
|
function SetBeforeRequestProc(aBeforeRequestProc: TBeforeRequestProc): IMVCRESTClient;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Executes after send the request
|
|
|
|
|
/// </summary>
|
|
|
|
|
function SetRequestCompletedProc(aRequestCompletedProc: TRequestCompletedProc): IMVCRESTClient;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Executes after the response is processed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
function SetResponseCompletedProc(aResponseCompletedProc: TResponseCompletedProc): IMVCRESTClient;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Clears all parameters (headers, body, path params and query params). This method is executed after each
|
|
|
|
|
/// request is completed.
|
2020-08-21 16:22:23 +02:00
|
|
|
|
/// </summary>
|
2020-09-24 20:03:11 +02:00
|
|
|
|
/// <remarks>
|
2020-12-17 00:05:39 +01:00
|
|
|
|
/// Cookies and authorization set in SetBasicAuthorization or SetBearerAuthorization is not removed
|
2020-09-24 20:03:11 +02:00
|
|
|
|
/// </remarks>
|
2020-08-20 20:08:41 +02:00
|
|
|
|
function ClearAllParams: IMVCRESTClient;
|
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Connection timeout in milliseconds to be used for the requests.
|
|
|
|
|
/// </summary>
|
|
|
|
|
function ConnectTimeout(const aConnectTimeout: Integer): IMVCRESTClient; overload;
|
|
|
|
|
function ConnectTimeout: Integer; overload;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Response reading timeout in milliseconds to be used for the requests.
|
|
|
|
|
/// </summary>
|
|
|
|
|
function ReadTimeout(const aReadTimeout: Integer): IMVCRESTClient; overload;
|
|
|
|
|
function ReadTimeout: Integer; overload;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Add basic authorization header. Authorization = Basic <Username:Password> (encoded in Base64)
|
|
|
|
|
/// </summary>
|
2020-09-24 20:03:11 +02:00
|
|
|
|
function SetBasicAuthorization(const aUsername, aPassword: string): IMVCRESTClient;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Add bearer authorization header. Authorization = Bearer <Token>
|
|
|
|
|
/// </summary>
|
2020-09-24 20:03:11 +02:00
|
|
|
|
function SetBearerAuthorization(const aAccessToken: string): IMVCRESTClient;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the stored authorization. Includes Basic or Bearer prefix
|
|
|
|
|
/// </summary>
|
|
|
|
|
function Authorization: string;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes the authorization header defined in <see cref="MVCFramework.RESTClient.Intf|IMVCRESTClient.SetBasicAuthorization(string,string)">
|
|
|
|
|
/// SetBasicAuthorization</see> or <see cref="MVCFramework.RESTClient.Intf|IMVCRESTClient.SetBearerAuthorization(string)">
|
|
|
|
|
/// SetBearerAuthorization</see>
|
|
|
|
|
/// </summary>
|
|
|
|
|
function ClearAuthorization: IMVCRESTClient;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Add a header.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="aName">
|
|
|
|
|
/// Header name
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="aValue">
|
|
|
|
|
/// Header value
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="aDoNotEncode">
|
|
|
|
|
/// Indicates whether the value of this header should be used as is (True), or encoded by the component (False)
|
|
|
|
|
/// </param>
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function AddHeader(const aName, aValue: string): IMVCRESTClient; overload;
|
2020-08-26 14:05:12 +02:00
|
|
|
|
function HeaderValue(const aName: string): string;
|
2021-01-15 19:31:33 +01:00
|
|
|
|
function Headers: TNameValueArray;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Clears all headers.
|
|
|
|
|
/// </summary>
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function ClearHeaders: IMVCRESTClient;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function AllowCookies(const aAllowCookies: Boolean): IMVCRESTClient; overload;
|
|
|
|
|
function AllowCookies: Boolean; overload;
|
|
|
|
|
|
2020-09-24 20:03:11 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Add DMVC session cookie
|
|
|
|
|
/// </summary>
|
|
|
|
|
function SessionId(const aSessionId: string): IMVCRESTClient; overload;
|
|
|
|
|
function SessionId: string; overload;
|
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Add a cookie header.
|
|
|
|
|
/// </summary>
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function AddCookie(const aName, aValue: string): IMVCRESTClient;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
/// <summary>
|
2020-09-08 03:21:28 +02:00
|
|
|
|
/// Clear all cookie headers.
|
2020-08-21 16:22:23 +02:00
|
|
|
|
/// </summary>
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function ClearCookies: IMVCRESTClient;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Add a URL segment parameter. The parameters of your url path may be enclosed in braces or in
|
|
|
|
|
/// parentheses starting with a money sign. <c>/api/{param1}/($param2)</c>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="aName">
|
|
|
|
|
/// Parameter name
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="aValue">
|
|
|
|
|
/// Parameter value
|
|
|
|
|
/// </param>
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function AddPathParam(const aName, aValue: string): IMVCRESTClient; overload;
|
|
|
|
|
function AddPathParam(const aName: string; aValue: Integer): IMVCRESTClient; overload;
|
|
|
|
|
function AddPathParam(const aName: string; aValue: Int64): IMVCRESTClient; overload;
|
|
|
|
|
function AddPathParam(const aName: string; aValue: TGUID): IMVCRESTClient; overload;
|
|
|
|
|
function AddPathParam(const aName: string; aValue: TDateTime): IMVCRESTClient; overload;
|
|
|
|
|
function AddPathParam(const aName: string; aValue: TDate): IMVCRESTClient; overload;
|
|
|
|
|
function AddPathParam(const aName: string; aValue: TTime): IMVCRESTClient; overload;
|
|
|
|
|
function AddPathParam(const aName: string; aValue: Double): IMVCRESTClient; overload;
|
|
|
|
|
function ClearPathParams: IMVCRESTClient;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Add a QueryString parameter. <c>/api/person?para1=value&param2=value</c>
|
|
|
|
|
/// </summary>
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function AddQueryStringParam(const aName, aValue: string): IMVCRESTClient; overload;
|
|
|
|
|
function AddQueryStringParam(const aName: string; aValue: Integer): IMVCRESTClient; overload;
|
|
|
|
|
function AddQueryStringParam(const aName: string; aValue: Int64): IMVCRESTClient; overload;
|
|
|
|
|
function AddQueryStringParam(const aName: string; aValue: TGUID): IMVCRESTClient; overload;
|
|
|
|
|
function AddQueryStringParam(const aName: string; aValue: TDateTime): IMVCRESTClient; overload;
|
|
|
|
|
function AddQueryStringParam(const aName: string; aValue: TDate): IMVCRESTClient; overload;
|
|
|
|
|
function AddQueryStringParam(const aName: string; aValue: TTime): IMVCRESTClient; overload;
|
|
|
|
|
function AddQueryStringParam(const aName: string; aValue: Double): IMVCRESTClient; overload;
|
|
|
|
|
function ClearQueryParams: IMVCRESTClient;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function Accept(const aAccept: string): IMVCRESTClient; overload;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function Accept: string; overload;
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function AcceptCharset(const aAcceptCharset: string): IMVCRESTClient; overload;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function AcceptCharset: string; overload;
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function AcceptEncoding(const aAcceptEncoding: string): IMVCRESTClient; overload;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function AcceptEncoding: string; overload;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
function HandleRedirects(const aHandleRedirects: Boolean): IMVCRESTClient; overload;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function HandleRedirects: Boolean; overload;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function MaxRedirects(const aMaxRedirects: Integer): IMVCRESTClient; overload;
|
|
|
|
|
function MaxRedirects: Integer; overload;
|
2020-09-18 01:06:24 +02:00
|
|
|
|
function UserAgent(const aUserAgent: string): IMVCRESTClient; overload;
|
|
|
|
|
function UserAgent: string; overload;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function Resource(const aResource: string): IMVCRESTClient; overload;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function Resource: string; overload;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Add a body to the requisition.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="aBody">
|
|
|
|
|
/// Body in string format.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="aContentType">
|
|
|
|
|
/// Body content type.
|
|
|
|
|
/// </param>
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function AddBody(const aBody: string; const aContentType: string = ''): IMVCRESTClient; overload;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Add a body to the requisition
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="aBodyStream">
|
|
|
|
|
/// Body in Stream format
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="aContentType">
|
|
|
|
|
/// Body content type
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="aOwnsStream">
|
|
|
|
|
/// If OwnsStream is true, Stream will be destroyed by IMVCRESTClient.
|
|
|
|
|
/// </param>
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function AddBody(aBodyStream: TStream; const aOwnsStream: Boolean = True;
|
|
|
|
|
const aContentType: string = ''): IMVCRESTClient; overload;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Add a body to the requisition
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="aBodyObject">
|
|
|
|
|
/// Body in Object format. The object will be serialized to a JSON string.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="aOwnsObject">
|
|
|
|
|
/// If OwnsObject is true, BodyObject will be destroyed by IMVCRESTClient.
|
|
|
|
|
/// </param>
|
|
|
|
|
function AddBody(aBodyObject: TObject; const aOwnsObject: Boolean = True): IMVCRESTClient; overload;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds a file as the request body. Several files can be added in the same request. In this case the request
|
|
|
|
|
/// will be of the multipart/form-data type
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="aName">
|
|
|
|
|
/// Field name
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="aFileName">
|
|
|
|
|
/// File path
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="aContentType">
|
|
|
|
|
/// File content type
|
|
|
|
|
/// </param>
|
2020-08-28 23:21:27 +02:00
|
|
|
|
function AddFile(const aName, aFileName: string; const aContentType: string = ''): IMVCRESTClient; overload;
|
|
|
|
|
function AddFile(const aFileName: string; const aContentType: string = ''): IMVCRESTClient; overload;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
|
|
|
|
|
function AddBodyFieldFormData(const aName, aValue: string): IMVCRESTClient; overload;
|
2020-09-23 01:26:13 +02:00
|
|
|
|
{$IF defined(RIOORBETTER)}
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function AddBodyFieldFormData(const aName: string; aStreamValue: TStream;
|
|
|
|
|
const aContentType: string = ''): IMVCRESTClient; overload;
|
2020-09-23 01:26:13 +02:00
|
|
|
|
{$ENDIF}
|
2020-09-08 03:21:28 +02:00
|
|
|
|
/// <summary>
|
2020-09-09 02:00:08 +02:00
|
|
|
|
/// Add a field to the x-www-form-urlencoded body. You must set ContentType to application/x-www-form-urlencoded
|
2020-09-08 03:21:28 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
function AddBodyFieldURLEncoded(const aName, aValue: string): IMVCRESTClient;
|
|
|
|
|
|
|
|
|
|
function ClearBody: IMVCRESTClient;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Executes the next request asynchronously.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="aCompletionHandler">
|
|
|
|
|
/// An anonymous method that will be run after the execution completed.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="aSynchronized">
|
|
|
|
|
/// Specifies if aCompletioHandler will be run in the main thread's (True) or execution thread's (False) context.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="aCompletionHandlerWithError">
|
|
|
|
|
/// An anonymous method that will be run if an exception is raised during execution.
|
|
|
|
|
/// </param>
|
2020-08-21 02:45:51 +02:00
|
|
|
|
function Async(aCompletionHandler: TProc<IMVCRESTResponse>; aCompletionHandlerWithError: TProc<Exception> = nil;
|
2020-08-26 01:07:21 +02:00
|
|
|
|
const aSynchronized: Boolean = False): IMVCRESTClient;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Execute a Get request.
|
|
|
|
|
/// </summary>
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function Get(const aResource: string): IMVCRESTResponse; overload;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function Get: IMVCRESTResponse; overload;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
|
2020-12-16 22:42:25 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Execute a Head request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
function Head(const aResource: string): IMVCRESTResponse; overload;
|
|
|
|
|
function Head: IMVCRESTResponse; overload;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Execute a Options request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
function Options(const aResource: string): IMVCRESTResponse; overload;
|
|
|
|
|
function Options: IMVCRESTResponse; overload;
|
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Execute a Post request.
|
|
|
|
|
/// </summary>
|
2020-09-08 03:21:28 +02:00
|
|
|
|
/// <param name="aResource">
|
|
|
|
|
/// Resource path
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="aBody">
|
|
|
|
|
/// Object to be serialized. It can be a simple object or a list of objects (TObjectList <T>)
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="aOwnsBody">
|
|
|
|
|
/// If OwnsBody is true, Body will be destroyed by IMVCRESTClient. <br />
|
|
|
|
|
/// </param>
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function Post(const aResource: string; aBody: TObject; const aOwnsBody: Boolean = True): IMVCRESTResponse; overload;
|
2020-09-10 02:40:14 +02:00
|
|
|
|
function Post(const aResource: string; const aBody: string = '';
|
2020-08-27 19:32:09 +02:00
|
|
|
|
const aContentType: string = TMVCMediaType.APPLICATION_JSON): IMVCRESTResponse; overload;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function Post: IMVCRESTResponse; overload;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Execute a Patch request.
|
|
|
|
|
/// </summary>
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function Patch(const aResource: string; aBody: TObject;
|
|
|
|
|
const aOwnsBody: Boolean = True): IMVCRESTResponse; overload;
|
2020-09-10 02:40:14 +02:00
|
|
|
|
function Patch(const aResource: string; const aBody: string = '';
|
2020-08-27 19:32:09 +02:00
|
|
|
|
const aContentType: string = TMVCMediaType.APPLICATION_JSON): IMVCRESTResponse; overload;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function Patch: IMVCRESTResponse; overload;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Execute a Put request.
|
|
|
|
|
/// </summary>
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function Put(const aResource: string; aBody: TObject; const aOwnsBody: Boolean = True): IMVCRESTResponse; overload;
|
2020-09-10 02:40:14 +02:00
|
|
|
|
function Put(const aResource: string; const aBody: string = '';
|
2020-08-27 19:32:09 +02:00
|
|
|
|
const aContentType: string = TMVCMediaType.APPLICATION_JSON): IMVCRESTResponse; overload;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function Put: IMVCRESTResponse; overload;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Execute a Delete request.
|
|
|
|
|
/// </summary>
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function Delete(const aResource: string): IMVCRESTResponse; overload;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function Delete: IMVCRESTResponse; overload;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
|
2020-09-29 16:56:25 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Executes any type of HTTP request
|
|
|
|
|
/// </summary>
|
|
|
|
|
function Execute(const aMethod: TMVCHTTPMethodType; const aResource: string): IMVCRESTResponse; overload;
|
|
|
|
|
function Execute(const aMethod: TMVCHTTPMethodType): IMVCRESTResponse; overload;
|
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Serialize the current dataset record and execute a POST request.
|
|
|
|
|
/// </summary>
|
2020-08-09 02:18:36 +02:00
|
|
|
|
function DataSetInsert(const aResource: string; aDataSet: TDataSet; const aIgnoredFields: TMVCIgnoredList = [];
|
2020-08-18 23:19:17 +02:00
|
|
|
|
const aNameCase: TMVCNameCase = ncAsIs): IMVCRESTResponse;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Serialize the current dataset record and execute a PUT request.
|
|
|
|
|
/// </summary>
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function DataSetUpdate(const aResource, aKeyValue: string; aDataSet: TDataSet;
|
|
|
|
|
const aIgnoredFields: TMVCIgnoredList = []; const aNameCase: TMVCNameCase = ncAsIs): IMVCRESTResponse;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Delete the current dataset record by executing a delete request.
|
|
|
|
|
/// </summary>
|
2020-08-26 14:05:12 +02:00
|
|
|
|
function DataSetDelete(const aResource, aKeyValue: string): IMVCRESTResponse;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
|
2021-04-11 22:37:14 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Access the RESTClient serializer
|
|
|
|
|
/// </summary>
|
|
|
|
|
function Serializer: IMVCSerializer; overload;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Add a serializer to the RESTClient
|
|
|
|
|
/// </summary>
|
|
|
|
|
function Serializer(const aSerializer: IMVCSerializer): IMVCRESTClient; overload;
|
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Register a custom serializer to the RESTClient serializer.
|
|
|
|
|
/// </summary>
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function RegisterTypeSerializer(const aTypeInfo: PTypeInfo; aInstance: IMVCTypeSerializer): IMVCRESTClient;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-10 02:40:14 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides access to the REST request response.
|
|
|
|
|
/// </summary>
|
|
|
|
|
TMVCRESTResponse = class(TInterfacedObject, IMVCRESTResponse)
|
|
|
|
|
private
|
|
|
|
|
fSuccess: Boolean;
|
|
|
|
|
fStatusCode: Integer;
|
|
|
|
|
fStatusText: string;
|
|
|
|
|
fHeaders: TStrings;
|
|
|
|
|
fCookies: TCookies;
|
|
|
|
|
fServer: string;
|
|
|
|
|
fContentType: string;
|
|
|
|
|
fContentEncoding: string;
|
|
|
|
|
fContentLength: Integer;
|
|
|
|
|
fContent: string;
|
2020-09-24 20:03:11 +02:00
|
|
|
|
fContentRawBytes: TBytes;
|
2020-09-10 02:40:14 +02:00
|
|
|
|
|
|
|
|
|
procedure FillResponse(aHTTPResponse: IHTTPResponse);
|
|
|
|
|
public
|
|
|
|
|
constructor Create(aHTTPResponse: IHTTPResponse);
|
|
|
|
|
destructor Destroy; override;
|
|
|
|
|
|
|
|
|
|
{ IMVCRESTResponse }
|
|
|
|
|
function Success: Boolean;
|
|
|
|
|
function StatusCode: Integer;
|
|
|
|
|
function StatusText: string;
|
|
|
|
|
function Headers: TStrings;
|
|
|
|
|
function HeaderValue(const aName: string): string;
|
|
|
|
|
function Cookies: TCookies;
|
|
|
|
|
function CookieByName(const aName: string): TCookie;
|
|
|
|
|
function Server: string;
|
|
|
|
|
function ContentType: string;
|
|
|
|
|
function ContentEncoding: string;
|
|
|
|
|
function ContentLength: Integer;
|
|
|
|
|
function Content: string;
|
2020-09-24 20:03:11 +02:00
|
|
|
|
function ContentRawBytes: TBytes;
|
2020-09-10 02:40:14 +02:00
|
|
|
|
procedure SaveContentToStream(aStream: TStream);
|
|
|
|
|
procedure SaveContentToFile(const aFileName: string);
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-04 00:48:35 +02:00
|
|
|
|
implementation
|
2015-12-18 20:59:40 +01:00
|
|
|
|
|
2020-08-04 00:48:35 +02:00
|
|
|
|
uses
|
2020-09-08 03:21:28 +02:00
|
|
|
|
System.NetConsts,
|
2020-08-09 02:18:36 +02:00
|
|
|
|
System.NetEncoding,
|
2020-09-10 02:40:14 +02:00
|
|
|
|
MVCFramework.Serializer.JsonDataObjects,
|
|
|
|
|
System.RegularExpressions;
|
2016-04-24 19:08:21 +02:00
|
|
|
|
|
2020-09-23 01:26:13 +02:00
|
|
|
|
{$IF not defined(RIOORBETTER)}
|
2020-12-17 00:05:39 +01:00
|
|
|
|
|
2020-09-23 01:26:13 +02:00
|
|
|
|
type
|
|
|
|
|
TCookieManagerHelper = class helper for TCookieManager
|
2020-09-24 20:03:11 +02:00
|
|
|
|
private
|
2020-09-23 01:26:13 +02:00
|
|
|
|
function CookieList: TCookies;
|
|
|
|
|
end;
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
{ TMVCRESTClient }
|
2016-06-23 12:11:01 +02:00
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.Accept: string;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2021-01-15 19:31:33 +01:00
|
|
|
|
Result := HeaderValue(sAccept);
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.Accept(const aAccept: string): IMVCRESTClient;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2021-01-15 19:31:33 +01:00
|
|
|
|
Result := AddHeader(sAccept, aAccept);
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.AcceptCharset: string;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2021-01-15 19:31:33 +01:00
|
|
|
|
Result := HeaderValue(sAcceptCharset);
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.AcceptCharset(const aAcceptCharset: string): IMVCRESTClient;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2021-01-15 19:31:33 +01:00
|
|
|
|
Result := AddHeader(sAcceptCharset, aAcceptCharset);
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.AcceptEncoding(const aAcceptEncoding: string): IMVCRESTClient;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2021-01-15 19:31:33 +01:00
|
|
|
|
Result := AddHeader(sAcceptEncoding, aAcceptEncoding);
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.AcceptEncoding: string;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2021-01-15 19:31:33 +01:00
|
|
|
|
Result := HeaderValue(sAcceptEncoding);
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.AddBody(const aBody: string; const aContentType: string): IMVCRESTClient;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
var
|
2020-09-08 03:21:28 +02:00
|
|
|
|
lContentCharset: string;
|
|
|
|
|
lEncoding: TEncoding;
|
|
|
|
|
lBytes: TArray<Byte>;
|
2020-09-11 19:55:26 +02:00
|
|
|
|
lContentType: string;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Result := Self;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
|
2020-09-11 19:55:26 +02:00
|
|
|
|
SplitContentMediaTypeAndCharset(aContentType, lContentType, lContentCharset);
|
2020-08-20 20:08:41 +02:00
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
if lContentCharset.IsEmpty then
|
|
|
|
|
begin
|
|
|
|
|
lContentCharset := TMVCCharSet.UTF_8;
|
|
|
|
|
end;
|
|
|
|
|
lEncoding := TEncoding.GetEncoding(lContentCharset);
|
2020-09-10 02:40:14 +02:00
|
|
|
|
try
|
|
|
|
|
fRawBody.Clear;
|
2020-09-11 19:55:26 +02:00
|
|
|
|
|
|
|
|
|
lBytes := lEncoding.GetBytes(aBody);
|
2020-09-10 02:40:14 +02:00
|
|
|
|
fRawBody.WriteData(lBytes, Length(lBytes));
|
2020-09-11 19:55:26 +02:00
|
|
|
|
SetContentType(BuildContentType(lContentType, lContentCharset));
|
2020-09-10 02:40:14 +02:00
|
|
|
|
finally
|
|
|
|
|
FreeAndNil(lEncoding);
|
|
|
|
|
end;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.AddBody(aBodyStream: TStream; const aOwnsStream: Boolean;
|
|
|
|
|
const aContentType: string): IMVCRESTClient;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Result := Self;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
|
2020-09-10 02:40:14 +02:00
|
|
|
|
if aBodyStream = nil then
|
|
|
|
|
raise EMVCRESTClientException.Create('You need a valid body!');
|
|
|
|
|
|
|
|
|
|
if aBodyStream is TStringStream then
|
|
|
|
|
raise EMVCRESTClientException.Create('aBodyStream must be of type TStringStream!');
|
2020-09-08 03:21:28 +02:00
|
|
|
|
|
|
|
|
|
SetContentType(aContentType);
|
2020-08-09 02:18:36 +02:00
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
fRawBody.Clear;
|
|
|
|
|
fRawBody.CopyFrom(aBodyStream, 0);
|
|
|
|
|
|
|
|
|
|
if aOwnsStream then
|
|
|
|
|
aBodyStream.Free;
|
2015-06-03 22:47:07 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function TMVCRESTClient.AddBody(aBodyObject: TObject; const aOwnsObject: Boolean): IMVCRESTClient;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-09-10 02:40:14 +02:00
|
|
|
|
if aBodyObject = nil then
|
|
|
|
|
raise EMVCRESTClientException.Create('You need a valid body!');
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := AddBody(SerializeObject(aBodyObject), TMVCMediaType.APPLICATION_JSON);
|
2020-08-09 02:18:36 +02:00
|
|
|
|
|
|
|
|
|
if aOwnsObject then
|
2020-08-21 16:22:23 +02:00
|
|
|
|
aBodyObject.Free;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.AddBodyFieldFormData(const aName, aValue: string): IMVCRESTClient;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Result := Self;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
GetBodyFormData.AddField(aName, aValue);
|
|
|
|
|
SetContentType(TMVCMediaType.MULTIPART_FORM_DATA);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-23 01:26:13 +02:00
|
|
|
|
{$IF defined(RIOORBETTER)}
|
2020-12-17 00:05:39 +01:00
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.AddBodyFieldFormData(const aName: string; aStreamValue: TStream;
|
|
|
|
|
const aContentType: string): IMVCRESTClient;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2020-08-09 02:18:36 +02:00
|
|
|
|
Result := Self;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
GetBodyFormData.AddStream(aName, aStreamValue, aContentType);
|
|
|
|
|
SetContentType(TMVCMediaType.MULTIPART_FORM_DATA);
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
2020-09-23 01:26:13 +02:00
|
|
|
|
{$ENDIF}
|
2015-12-18 20:59:40 +01:00
|
|
|
|
|
2020-12-17 00:05:39 +01:00
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.AddBodyFieldURLEncoded(const aName, aValue: string): IMVCRESTClient;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := Self;
|
2020-09-09 02:00:08 +02:00
|
|
|
|
fParameters.Add(TMVCRESTParam.Create(TMVCRESTParamType.FormURLEncoded, aName, aValue));
|
2020-09-08 03:21:28 +02:00
|
|
|
|
SetContentType(TMVCMediaType.APPLICATION_FORM_URLENCODED);
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.AddCookie(const aName, aValue: string): IMVCRESTClient;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-09-09 02:00:08 +02:00
|
|
|
|
Result := Self;
|
2020-09-22 01:32:21 +02:00
|
|
|
|
SetParameter(TMVCRESTParamType.Cookie, aName, aValue);
|
2020-09-08 03:21:28 +02:00
|
|
|
|
end;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.AddFile(const aFileName, aContentType: string): IMVCRESTClient;
|
|
|
|
|
begin
|
2020-09-09 02:00:08 +02:00
|
|
|
|
Result := AddFile(TMVCRESTClientConsts.DEFAULT_FILE_NAME, aFileName, aContentType);
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.AddFile(const aName, aFileName, aContentType: string): IMVCRESTClient;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := Self;
|
2020-09-23 01:26:13 +02:00
|
|
|
|
GetBodyFormData.AddFile(aName, aFileName {$IF defined(RIOORBETTER)}, aContentType{$ENDIF});
|
2020-09-08 03:21:28 +02:00
|
|
|
|
SetContentType(TMVCMediaType.MULTIPART_FORM_DATA);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.AddHeader(const aName, aValue: string): IMVCRESTClient;
|
2014-04-10 13:56:23 +02:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := Self;
|
2020-09-22 01:32:21 +02:00
|
|
|
|
|
|
|
|
|
SetParameter(TMVCRESTParamType.Header, aName, aValue);
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.AddPathParam(const aName: string; aValue: Double): IMVCRESTClient;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2020-08-09 02:18:36 +02:00
|
|
|
|
Result := AddPathParam(aName, aValue.ToString);
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.AddPathParam(const aName, aValue: string): IMVCRESTClient;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Result := Self;
|
2020-09-22 01:32:21 +02:00
|
|
|
|
SetParameter(TMVCRESTParamType.Path, aName, aValue);
|
2014-04-10 13:56:23 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.AddPathParam(const aName: string; aValue: TDate): IMVCRESTClient;
|
2014-04-10 13:56:23 +02:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := AddPathParam(aName, DateToISODate(aValue));
|
2014-04-10 13:56:23 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.AddPathParam(const aName: string; aValue: TTime): IMVCRESTClient;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2020-08-09 02:18:36 +02:00
|
|
|
|
Result := AddPathParam(aName, TimeToISOTime(aValue));
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.AddPathParam(const aName: string; aValue: TDateTime): IMVCRESTClient;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-08-09 02:18:36 +02:00
|
|
|
|
Result := AddPathParam(aName, DateTimeToISOTimeStamp(aValue));
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.AddPathParam(const aName: string; aValue: Int64): IMVCRESTClient;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := AddPathParam(aName, aValue.ToString);
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.AddPathParam(const aName: string; aValue: TGUID): IMVCRESTClient;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := AddPathParam(aName, aValue.ToString);
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.AddPathParam(const aName: string; aValue: Integer): IMVCRESTClient;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := AddPathParam(aName, aValue.ToString);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.AddQueryStringParam(const aName: string; aValue: Double): IMVCRESTClient;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2021-02-16 23:09:46 +01:00
|
|
|
|
Result := AddQueryStringParam(aName, aValue.ToString);
|
2020-08-21 16:22:23 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.AddQueryStringParam(const aName: string; aValue: TTime): IMVCRESTClient;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := AddQueryStringParam(aName, TimeToISOTime(aValue));
|
2020-08-21 16:22:23 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.AddQueryStringParam(const aName: string; aValue: TDate): IMVCRESTClient;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := AddQueryStringParam(aName, DateToISODate(aValue));
|
2020-08-21 02:45:51 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.AddQueryStringParam(const aName: string; aValue: TDateTime): IMVCRESTClient;
|
2020-08-21 02:45:51 +02:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := AddQueryStringParam(aName, DateTimeToISOTimeStamp(aValue));
|
2020-08-20 20:08:41 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.AddQueryStringParam(const aName: string; aValue: TGUID): IMVCRESTClient;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := AddQueryStringParam(aName, aValue.ToString);
|
2020-08-21 16:22:23 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.AddQueryStringParam(const aName: string; aValue: Int64): IMVCRESTClient;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := AddQueryStringParam(aName, aValue.ToString);
|
2020-08-21 16:22:23 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.AddQueryStringParam(const aName, aValue: string): IMVCRESTClient;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := Self;
|
2020-09-22 01:32:21 +02:00
|
|
|
|
SetParameter(TMVCRESTParamType.Query, aName, aValue);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.AddQueryStringParam(const aName: string; aValue: Integer): IMVCRESTClient;
|
2018-10-23 16:18:34 +02:00
|
|
|
|
begin
|
2020-08-09 02:18:36 +02:00
|
|
|
|
Result := AddQueryStringParam(aName, aValue.ToString);
|
2018-10-23 16:18:34 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.AllowCookies: Boolean;
|
2015-12-22 12:17:13 +01:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := fHTTPClient.AllowCookies;
|
2015-12-22 12:17:13 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.AllowCookies(const aAllowCookies: Boolean): IMVCRESTClient;
|
2015-12-22 12:17:13 +01:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := Self;
|
|
|
|
|
fHTTPClient.AllowCookies := aAllowCookies;
|
2015-12-22 12:17:13 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.Async(aCompletionHandler: TProc<IMVCRESTResponse>;
|
|
|
|
|
aCompletionHandlerWithError: TProc<Exception>; const aSynchronized: Boolean): IMVCRESTClient;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
begin
|
2020-09-22 01:32:21 +02:00
|
|
|
|
Result := Self;
|
|
|
|
|
fNextRequestIsAsync := True;
|
|
|
|
|
fAsyncCompletionHandler := aCompletionHandler;
|
|
|
|
|
fAsyncCompletionHandlerWithError := aCompletionHandlerWithError;
|
|
|
|
|
fAsyncSynchronized := aSynchronized;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
end;
|
2020-08-26 14:05:12 +02:00
|
|
|
|
|
2020-09-24 20:03:11 +02:00
|
|
|
|
function TMVCRESTClient.Authorization: string;
|
|
|
|
|
begin
|
2021-01-22 15:37:57 +01:00
|
|
|
|
Result := HeaderValue(TMVCRESTClientConsts.AUTHORIZATION_HEADER);
|
2020-09-24 20:03:11 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.BaseURL(const aBaseURL: string): IMVCRESTClient;
|
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
2020-08-20 23:35:28 +02:00
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
fBaseURL := aBaseURL;
|
2021-02-05 20:03:33 +01:00
|
|
|
|
if not (fBaseURL.IsEmpty or fBaseURL.Contains('://')) then
|
2020-09-08 03:21:28 +02:00
|
|
|
|
fBaseURL := 'http://' + fBaseURL;
|
2020-09-10 02:40:14 +02:00
|
|
|
|
|
|
|
|
|
fBaseURL := fBaseURL;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
end;
|
2020-08-21 02:45:51 +02:00
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.BaseURL(const aHost: string; const aPort: Integer): IMVCRESTClient;
|
|
|
|
|
begin
|
|
|
|
|
Result := BaseURL(aHost + ':' + aPort.ToString);
|
|
|
|
|
end;
|
2020-08-26 14:05:12 +02:00
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.BaseURL: string;
|
|
|
|
|
begin
|
|
|
|
|
Result := fBaseURL;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.ClearAllParams: IMVCRESTClient;
|
2021-01-15 19:31:33 +01:00
|
|
|
|
var
|
|
|
|
|
lAuthorization: string;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
2021-01-15 19:31:33 +01:00
|
|
|
|
|
2021-01-22 15:37:57 +01:00
|
|
|
|
lAuthorization := HeaderValue(TMVCRESTClientConsts.AUTHORIZATION_HEADER);
|
2020-09-09 02:00:08 +02:00
|
|
|
|
fParameters.Clear;
|
2020-09-11 19:55:26 +02:00
|
|
|
|
ClearBody;
|
2020-09-24 20:03:11 +02:00
|
|
|
|
|
2021-01-15 19:31:33 +01:00
|
|
|
|
AddHeader(sAccept, TMVCRESTClientConsts.DEFAULT_ACCEPT);
|
|
|
|
|
AddHeader(sAcceptEncoding, TMVCRESTClientConsts.DEFAULT_ACCEPT_ENCODING);
|
|
|
|
|
AddHeader(sUserAgent, TMVCRESTClientConsts.DEFAULT_USER_AGENT);
|
2021-01-22 15:37:57 +01:00
|
|
|
|
AddHeader(TMVCRESTClientConsts.AUTHORIZATION_HEADER, lAuthorization);
|
2021-01-15 19:31:33 +01:00
|
|
|
|
|
2020-09-24 20:03:11 +02:00
|
|
|
|
fNextRequestIsAsync := False;
|
|
|
|
|
fAsyncCompletionHandler := nil;
|
|
|
|
|
fAsyncCompletionHandlerWithError := nil;
|
|
|
|
|
fAsyncSynchronized := False;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTClient.ClearAuthorization: IMVCRESTClient;
|
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
2021-01-22 15:37:57 +01:00
|
|
|
|
AddHeader(TMVCRESTClientConsts.AUTHORIZATION_HEADER, '');
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.ClearBody: IMVCRESTClient;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
fRawBody.Clear;
|
|
|
|
|
if Assigned(fBodyFormData) then
|
|
|
|
|
FreeAndNil(fBodyFormData);
|
|
|
|
|
|
2020-09-11 19:55:26 +02:00
|
|
|
|
Result := Self;
|
2020-09-09 02:00:08 +02:00
|
|
|
|
ClearParameters(TMVCRESTParamType.FormURLEncoded);
|
2021-01-15 19:31:33 +01:00
|
|
|
|
AddHeader(sContentType, '');
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.ClearCookies: IMVCRESTClient;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
2020-09-09 02:00:08 +02:00
|
|
|
|
Result := Self;
|
|
|
|
|
ClearParameters(TMVCRESTParamType.Cookie);
|
2020-09-23 01:26:13 +02:00
|
|
|
|
{$IF defined(RIOORBETTER)}
|
2020-09-08 03:21:28 +02:00
|
|
|
|
fHTTPClient.CookieManager.Clear;
|
2020-09-23 01:26:13 +02:00
|
|
|
|
{$ELSE}
|
|
|
|
|
if fHTTPClient.CookieManager.CookieList <> nil then
|
|
|
|
|
fHTTPClient.CookieManager.CookieList.Clear;
|
|
|
|
|
{$ENDIF}
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.ClearHeaders: IMVCRESTClient;
|
2014-03-24 17:37:08 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := Self;
|
2020-09-09 02:00:08 +02:00
|
|
|
|
ClearParameters(TMVCRESTParamType.Header);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMVCRESTClient.ClearParameters(const aParamType: TMVCRESTParamType);
|
|
|
|
|
var
|
|
|
|
|
I: Integer;
|
|
|
|
|
begin
|
|
|
|
|
for I := Pred(fParameters.Count) downto 0 do
|
|
|
|
|
begin
|
|
|
|
|
if fParameters[I].&Type = aParamType then
|
|
|
|
|
fParameters.Delete(I);
|
|
|
|
|
end;
|
2014-03-24 17:37:08 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.ClearPathParams: IMVCRESTClient;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := Self;
|
2020-09-09 02:00:08 +02:00
|
|
|
|
ClearParameters(TMVCRESTParamType.Path);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.ClearQueryParams: IMVCRESTClient;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
2020-09-09 02:00:08 +02:00
|
|
|
|
ClearParameters(TMVCRESTParamType.Query);
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.ConnectTimeout: Integer;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := fHTTPClient.ConnectionTimeout;
|
2020-08-20 23:35:28 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function TMVCRESTClient.ConnectTimeout(const aConnectTimeout: Integer): IMVCRESTClient;
|
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
fHTTPClient.ConnectionTimeout := aConnectTimeout;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
constructor TMVCRESTClient.Create;
|
2015-12-22 12:17:13 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
inherited Create;
|
2015-12-22 12:17:13 +01:00
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
fHTTPClient := THTTPClient.Create;
|
2020-09-24 21:29:49 +02:00
|
|
|
|
fHTTPClient.OnValidateServerCertificate := DoValidateServerCertificate;
|
2020-09-18 01:06:24 +02:00
|
|
|
|
fHTTPClient.HandleRedirects := True;
|
2020-09-23 01:26:13 +02:00
|
|
|
|
fHTTPClient.MaxRedirects := TMVCRESTClientConsts.DEFAULT_MAX_REDIRECTS;
|
|
|
|
|
{$IF defined(TOKYOORBETTER)}
|
2020-09-18 01:06:24 +02:00
|
|
|
|
fHTTPClient.SecureProtocols := CHTTPDefSecureProtocols;
|
2020-09-23 01:26:13 +02:00
|
|
|
|
{$ENDIF}
|
2020-09-24 21:29:49 +02:00
|
|
|
|
fValidateServerCertificate := nil;
|
2020-12-17 00:05:39 +01:00
|
|
|
|
fBeforeRequestProc := nil;
|
|
|
|
|
fRequestCompletedProc := nil;
|
|
|
|
|
fResponseCompletedProc := nil;
|
2020-09-09 02:00:08 +02:00
|
|
|
|
fParameters := TList<TMVCRESTParam>.Create;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
fRawBody := TStringStream.Create;
|
|
|
|
|
fBodyFormData := nil;
|
2021-04-11 22:37:14 +02:00
|
|
|
|
fSerializer := nil;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
fRttiContext := TRttiContext.Create;
|
2020-09-09 02:00:08 +02:00
|
|
|
|
fProxySettings := TProxySettings.Create('', 0);
|
2021-04-11 22:37:14 +02:00
|
|
|
|
fLock := TObject.Create;
|
2020-09-09 02:00:08 +02:00
|
|
|
|
fBaseURL := '';
|
|
|
|
|
fResource := '';
|
|
|
|
|
|
2020-09-24 20:03:11 +02:00
|
|
|
|
ClearAllParams;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-26 14:05:12 +02:00
|
|
|
|
function TMVCRESTClient.DataSetDelete(const aResource, aKeyValue: string): IMVCRESTResponse;
|
2020-09-10 02:40:14 +02:00
|
|
|
|
var
|
|
|
|
|
lResource: string;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
2020-10-05 23:53:48 +02:00
|
|
|
|
lResource := aResource;
|
|
|
|
|
if not aKeyValue.IsEmpty then
|
|
|
|
|
begin
|
|
|
|
|
lResource := aResource + '/' + aKeyValue;
|
|
|
|
|
end;
|
2020-09-10 02:40:14 +02:00
|
|
|
|
Result := Delete(lResource);
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.DataSetInsert(const aResource: string; aDataSet: TDataSet;
|
|
|
|
|
const aIgnoredFields: TMVCIgnoredList; const aNameCase: TMVCNameCase): IMVCRESTResponse;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
2021-04-11 22:37:14 +02:00
|
|
|
|
Result := Post(aResource, Serializer.SerializeDataSetRecord(aDataSet, aIgnoredFields, aNameCase));
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-26 14:05:12 +02:00
|
|
|
|
function TMVCRESTClient.DataSetUpdate(const aResource, aKeyValue: string; aDataSet: TDataSet;
|
|
|
|
|
const aIgnoredFields: TMVCIgnoredList; const aNameCase: TMVCNameCase): IMVCRESTResponse;
|
2020-09-10 02:40:14 +02:00
|
|
|
|
var
|
|
|
|
|
lResource: string;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
2020-10-05 23:53:48 +02:00
|
|
|
|
lResource := aResource;
|
|
|
|
|
if not aKeyValue.IsEmpty then
|
|
|
|
|
begin
|
|
|
|
|
lResource := aResource + '/' + aKeyValue;
|
|
|
|
|
end;
|
2020-08-26 14:05:12 +02:00
|
|
|
|
|
2021-04-11 22:37:14 +02:00
|
|
|
|
Result := Put(lResource, Serializer.SerializeDataSetRecord(aDataSet, aIgnoredFields, aNameCase));
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.Delete(const aResource: string): IMVCRESTResponse;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
2020-09-10 02:40:14 +02:00
|
|
|
|
Resource(aResource);
|
|
|
|
|
Result := Delete;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.Delete: IMVCRESTResponse;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
2020-09-10 02:40:14 +02:00
|
|
|
|
Result := ExecuteRequest(TMVCHTTPMethodType.httpDELETE);
|
2014-04-01 02:12:34 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
destructor TMVCRESTClient.Destroy;
|
2013-11-11 12:23:49 +01:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
FreeAndNil(fHTTPClient);
|
2020-09-09 02:00:08 +02:00
|
|
|
|
FreeAndNil(fParameters);
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
if Assigned(fBodyFormData) then
|
|
|
|
|
FreeAndNil(fBodyFormData);
|
2020-09-09 02:00:08 +02:00
|
|
|
|
FreeAndNil(fRawBody);
|
2021-04-11 22:37:14 +02:00
|
|
|
|
FreeAndNil(fLock);
|
2020-08-04 00:48:35 +02:00
|
|
|
|
fSerializer := nil;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
fRttiContext.Free;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
inherited;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-09 02:00:08 +02:00
|
|
|
|
procedure TMVCRESTClient.DoApplyCookies(const aURL: string);
|
|
|
|
|
var
|
|
|
|
|
lParam: TMVCRESTParam;
|
|
|
|
|
lName: string;
|
|
|
|
|
lValue: string;
|
|
|
|
|
begin
|
|
|
|
|
for lParam in fParameters do
|
|
|
|
|
begin
|
|
|
|
|
if lParam.&Type = TMVCRESTParamType.Cookie then
|
|
|
|
|
begin
|
2020-09-10 02:40:14 +02:00
|
|
|
|
lName := TMVCRESTClientHelper.URIEncode(lParam.Name);
|
|
|
|
|
lValue := TMVCRESTClientHelper.URIEncode(lParam.Value);
|
2020-09-09 02:00:08 +02:00
|
|
|
|
fHTTPClient.CookieManager.AddServerCookie(lName + '=' + lValue, aURL);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-09-23 01:26:13 +02:00
|
|
|
|
{$IF not defined(SYDNEYORBETTER)}
|
2020-12-17 00:05:39 +01:00
|
|
|
|
|
2020-09-23 01:26:13 +02:00
|
|
|
|
type
|
|
|
|
|
THackURLClient = class(TURLClient);
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
2020-12-17 00:05:39 +01:00
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
procedure TMVCRESTClient.DoApplyHeaders;
|
2020-08-20 23:35:28 +02:00
|
|
|
|
var
|
2020-09-09 02:00:08 +02:00
|
|
|
|
lParam: TMVCRESTParam;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
begin
|
2020-09-23 01:26:13 +02:00
|
|
|
|
{$IF defined(SYDNEYORBETTER)}
|
2020-09-11 19:55:26 +02:00
|
|
|
|
fHTTPClient.CustHeaders.Clear;
|
2020-09-23 01:26:13 +02:00
|
|
|
|
{$ELSE}
|
|
|
|
|
SetLength(THackURLClient(fHTTPClient).FCustomHeaders, 0);
|
|
|
|
|
{$ENDIF}
|
2020-09-09 02:00:08 +02:00
|
|
|
|
for lParam in fParameters do
|
2020-08-20 20:08:41 +02:00
|
|
|
|
begin
|
2020-09-09 02:00:08 +02:00
|
|
|
|
if lParam.&Type = TMVCRESTParamType.Header then
|
|
|
|
|
begin
|
|
|
|
|
fHTTPClient.CustomHeaders[lParam.Name] := lParam.Value;
|
|
|
|
|
end;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
end;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
procedure TMVCRESTClient.DoApplyPathParams(var aURL: string);
|
|
|
|
|
var
|
2020-09-09 02:00:08 +02:00
|
|
|
|
lParam: TMVCRESTParam;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
lReplace: string;
|
|
|
|
|
lEncodedParam: string;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
begin
|
2020-09-09 02:00:08 +02:00
|
|
|
|
for lParam in fParameters do
|
2020-09-08 03:21:28 +02:00
|
|
|
|
begin
|
2020-09-09 02:00:08 +02:00
|
|
|
|
if lParam.&Type = TMVCRESTParamType.Path then
|
|
|
|
|
begin
|
|
|
|
|
lReplace := '{' + lParam.Name + '}';
|
2020-09-11 19:55:26 +02:00
|
|
|
|
lEncodedParam := TNetEncoding.URL.Encode(lParam.Value, TMVCRESTClientConsts.PATH_UNSAFE_CHARS,
|
|
|
|
|
[TURLEncoding.TEncodeOption.EncodePercent]);
|
2020-09-09 02:00:08 +02:00
|
|
|
|
aURL := aURL.Replace(lReplace, lEncodedParam, [rfReplaceAll, rfIgnoreCase]);
|
|
|
|
|
end;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
end;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
procedure TMVCRESTClient.DoApplyQueryParams(var aURL: string);
|
|
|
|
|
var
|
2020-09-09 02:00:08 +02:00
|
|
|
|
lParam: TMVCRESTParam;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
lName: string;
|
|
|
|
|
lValue: string;
|
|
|
|
|
lConcat: string;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
2020-09-09 02:00:08 +02:00
|
|
|
|
for lParam in fParameters do
|
2020-09-08 03:21:28 +02:00
|
|
|
|
begin
|
2020-09-09 02:00:08 +02:00
|
|
|
|
if lParam.&Type = TMVCRESTParamType.Query then
|
|
|
|
|
begin
|
2020-09-11 19:55:26 +02:00
|
|
|
|
lName := TMVCRESTClientHelper.URIEncode(lParam.Name);
|
2020-09-09 02:00:08 +02:00
|
|
|
|
lValue := TNetEncoding.URL.EncodeForm(lParam.Value);
|
2020-08-09 02:18:36 +02:00
|
|
|
|
|
2020-09-09 02:00:08 +02:00
|
|
|
|
if aURL.Contains('?') then
|
|
|
|
|
lConcat := '&'
|
|
|
|
|
else
|
|
|
|
|
lConcat := '?';
|
2020-08-20 20:08:41 +02:00
|
|
|
|
|
2020-09-09 02:00:08 +02:00
|
|
|
|
aURL := aURL + lConcat + lName + '=' + lValue;
|
|
|
|
|
end;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
end;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-12-17 00:05:39 +01:00
|
|
|
|
procedure TMVCRESTClient.DoBeforeRequest(aRequest: IHTTPRequest);
|
|
|
|
|
begin
|
|
|
|
|
if Assigned(fBeforeRequestProc) then
|
|
|
|
|
fBeforeRequestProc(aRequest);
|
|
|
|
|
end;
|
|
|
|
|
|
2020-09-10 02:40:14 +02:00
|
|
|
|
procedure TMVCRESTClient.DoConvertMVCPathParamsToRESTParams(var aURL: string);
|
|
|
|
|
begin
|
|
|
|
|
aURL := TRegEx.Replace(aURL, '(\([($])([\w_]+)([)])', '{\2}', [TRegExOption.roIgnoreCase]);
|
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
procedure TMVCRESTClient.DoEncodeURL(var aURL: string);
|
2020-08-26 14:05:12 +02:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
// It is necessary to encode the dots because the HTTPClient removes dotted URL segments.
|
|
|
|
|
// See https://tools.ietf.org/html/rfc3986#section-5.2.4
|
|
|
|
|
aURL := aURL.Replace('\', '/', [rfReplaceAll]);
|
|
|
|
|
aURL := aURL.Replace('../', '%2E%2E/', [rfReplaceAll]);
|
|
|
|
|
aURL := aURL.Replace('./', '%2E/', [rfReplaceAll]);
|
2020-08-26 14:05:12 +02:00
|
|
|
|
|
2020-09-23 01:26:13 +02:00
|
|
|
|
{$IF defined(RIOORBETTER)}
|
2020-09-08 03:21:28 +02:00
|
|
|
|
aURL := TURI.Create(aURL).Encode;
|
2020-09-23 01:26:13 +02:00
|
|
|
|
{$ELSE}
|
|
|
|
|
aURL := TURI.Create(aURL).ToString;
|
|
|
|
|
{$ENDIF}
|
2020-08-14 00:43:25 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-11 19:55:26 +02:00
|
|
|
|
procedure TMVCRESTClient.DoPrepareBodyRequest(var aBodyStream: TStream);
|
|
|
|
|
var
|
2021-01-15 19:31:33 +01:00
|
|
|
|
lCurrentContentType: string;
|
2020-09-11 19:55:26 +02:00
|
|
|
|
lContentType: string;
|
|
|
|
|
lContentCharset: string;
|
|
|
|
|
lParam: TMVCRESTParam;
|
|
|
|
|
lName: string;
|
|
|
|
|
lValue: string;
|
|
|
|
|
lBody: string;
|
|
|
|
|
begin
|
2021-01-15 19:31:33 +01:00
|
|
|
|
lCurrentContentType := HeaderValue(sContentType);
|
|
|
|
|
SplitContentMediaTypeAndCharset(lCurrentContentType, lContentType, lContentCharset);
|
2020-09-11 19:55:26 +02:00
|
|
|
|
|
|
|
|
|
if SameText(lContentType, TMVCMediaType.MULTIPART_FORM_DATA) then
|
|
|
|
|
begin
|
|
|
|
|
aBodyStream := GetBodyFormData.Stream;
|
|
|
|
|
SetContentType(GetBodyFormData.MimeTypeHeader);
|
|
|
|
|
end
|
|
|
|
|
else if SameText(lContentType, TMVCMediaType.APPLICATION_FORM_URLENCODED) then
|
|
|
|
|
begin
|
|
|
|
|
lBody := '';
|
|
|
|
|
for lParam in fParameters do
|
|
|
|
|
begin
|
|
|
|
|
if lParam.&Type = TMVCRESTParamType.FormURLEncoded then
|
|
|
|
|
begin
|
|
|
|
|
lName := TMVCRESTClientHelper.URIEncode(lParam.Name);
|
|
|
|
|
lValue := TNetEncoding.URL.EncodeForm(lParam.Value);
|
|
|
|
|
if not lBody.IsEmpty then
|
|
|
|
|
lBody := lBody + '&';
|
|
|
|
|
lBody := lBody + lName + '=' + lValue;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
2021-01-15 19:31:33 +01:00
|
|
|
|
AddBody(lBody, lCurrentContentType);
|
2020-09-11 19:55:26 +02:00
|
|
|
|
aBodyStream := fRawBody;
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
aBodyStream := fRawBody;
|
|
|
|
|
end;
|
|
|
|
|
aBodyStream.Position := 0;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-12-17 00:05:39 +01:00
|
|
|
|
procedure TMVCRESTClient.DoRequestCompleted(aResponse: IHTTPResponse; var aHandled: Boolean);
|
|
|
|
|
begin
|
|
|
|
|
if Assigned(fRequestCompletedProc) then
|
|
|
|
|
fRequestCompletedProc(aResponse, aHandled);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMVCRESTClient.DoResponseCompleted(aMVCRESTResponse: IMVCRESTResponse);
|
|
|
|
|
begin
|
|
|
|
|
if Assigned(fResponseCompletedProc) then
|
|
|
|
|
fResponseCompletedProc(aMVCRESTResponse);
|
|
|
|
|
end;
|
|
|
|
|
|
2020-09-29 16:56:25 +02:00
|
|
|
|
function TMVCRESTClient.Execute(const aMethod: TMVCHTTPMethodType): IMVCRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
Result := ExecuteRequest(aMethod);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTClient.Execute(const aMethod: TMVCHTTPMethodType; const aResource: string): IMVCRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
Resource(aResource);
|
|
|
|
|
Result := Execute(aMethod);
|
|
|
|
|
end;
|
|
|
|
|
|
2020-09-22 01:32:21 +02:00
|
|
|
|
procedure TMVCRESTClient.ExecuteAsyncRequest(const aMethod: TMVCHTTPMethodType);
|
2020-09-08 03:21:28 +02:00
|
|
|
|
var
|
2020-09-22 01:32:21 +02:00
|
|
|
|
lThread: TThread;
|
|
|
|
|
lRESTClient: IMVCRESTClient;
|
|
|
|
|
begin
|
|
|
|
|
// This is necessary for the thread to be able to access the RESTClient fields
|
|
|
|
|
lRESTClient := Self;
|
|
|
|
|
lThread := TThread.CreateAnonymousThread(
|
|
|
|
|
procedure
|
|
|
|
|
var
|
|
|
|
|
lResponse: IMVCRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
try
|
|
|
|
|
lResponse := TMVCRESTClient(lRESTClient).InternalExecuteRequest(aMethod);
|
|
|
|
|
TMonitor.Enter(TObject(lResponse));
|
|
|
|
|
try
|
|
|
|
|
if Assigned(fAsyncCompletionHandler) then
|
|
|
|
|
begin
|
|
|
|
|
if fAsyncSynchronized then
|
|
|
|
|
begin
|
|
|
|
|
TThread.Synchronize(nil,
|
|
|
|
|
procedure
|
|
|
|
|
begin
|
|
|
|
|
fAsyncCompletionHandler(lResponse);
|
|
|
|
|
end
|
|
|
|
|
);
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
fAsyncCompletionHandler(lResponse);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
TMonitor.Exit(TObject(lResponse));
|
|
|
|
|
end;
|
|
|
|
|
except
|
|
|
|
|
on E: Exception do
|
|
|
|
|
begin
|
|
|
|
|
if Assigned(fAsyncCompletionHandlerWithError) then
|
|
|
|
|
begin
|
|
|
|
|
if fAsyncSynchronized then
|
|
|
|
|
begin
|
|
|
|
|
TThread.Synchronize(nil,
|
|
|
|
|
procedure
|
|
|
|
|
begin
|
|
|
|
|
fAsyncCompletionHandlerWithError(E);
|
|
|
|
|
end
|
|
|
|
|
);
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
fAsyncCompletionHandlerWithError(E);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
2020-09-11 19:55:26 +02:00
|
|
|
|
end;
|
2020-09-22 01:32:21 +02:00
|
|
|
|
ClearAllParams;
|
|
|
|
|
end
|
|
|
|
|
);
|
|
|
|
|
lThread.Start;
|
|
|
|
|
end;
|
2020-09-10 02:40:14 +02:00
|
|
|
|
|
2020-09-22 01:32:21 +02:00
|
|
|
|
function TMVCRESTClient.ExecuteRequest(const aMethod: TMVCHTTPMethodType): IMVCRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
if fNextRequestIsAsync then
|
|
|
|
|
begin
|
|
|
|
|
Result := nil;
|
|
|
|
|
ExecuteAsyncRequest(aMethod);
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
Result := InternalExecuteRequest(aMethod);
|
|
|
|
|
ClearAllParams;
|
|
|
|
|
end;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.Get: IMVCRESTResponse;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
2020-09-10 02:40:14 +02:00
|
|
|
|
Result := ExecuteRequest(TMVCHTTPMethodType.httpGET);
|
2020-08-27 19:32:09 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.GetBodyFormData: TMultipartFormData;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
if not Assigned(fBodyFormData) then
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
fBodyFormData := TMultipartFormData.Create;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := fBodyFormData;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.GetFullURL: string;
|
|
|
|
|
var
|
|
|
|
|
lResource: string;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := fBaseURL;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
lResource := fResource;
|
|
|
|
|
if not lResource.IsEmpty then
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
2021-02-05 20:03:33 +01:00
|
|
|
|
if not (Result.IsEmpty or Result.EndsWith('/')) and
|
2020-09-08 03:21:28 +02:00
|
|
|
|
not (lResource.StartsWith('/') or lResource.StartsWith('?') or lResource.StartsWith('#')) then
|
|
|
|
|
begin
|
|
|
|
|
Result := Result + '/';
|
|
|
|
|
end;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := Result + lResource;
|
|
|
|
|
end;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.Get(const aResource: string): IMVCRESTResponse;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-09-10 02:40:14 +02:00
|
|
|
|
Resource(aResource);
|
|
|
|
|
Result := ExecuteRequest(TMVCHTTPMethodType.httpGET);
|
2020-08-26 14:05:12 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.HandleRedirects(const aHandleRedirects: Boolean): IMVCRESTClient;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := Self;
|
|
|
|
|
fHTTPClient.HandleRedirects := aHandleRedirects;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.HandleRedirects: Boolean;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := fHTTPClient.HandleRedirects;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-12-16 22:42:25 +01:00
|
|
|
|
function TMVCRESTClient.Head: IMVCRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
Result := ExecuteRequest(TMVCHTTPMethodType.httpHEAD);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTClient.Head(const aResource: string): IMVCRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
Resource(aResource);
|
|
|
|
|
Result := Head;
|
|
|
|
|
end;
|
|
|
|
|
|
2021-01-15 19:31:33 +01:00
|
|
|
|
function TMVCRESTClient.Headers: TNameValueArray;
|
|
|
|
|
var
|
|
|
|
|
lHeaders: TList<TNameValuePair>;
|
|
|
|
|
lParam: TMVCRESTParam;
|
|
|
|
|
begin
|
|
|
|
|
lHeaders := TList<TNameValuePair>.Create;
|
|
|
|
|
try
|
|
|
|
|
for lParam in fParameters do
|
|
|
|
|
begin
|
|
|
|
|
if lParam.&Type = TMVCRESTParamType.Header then
|
|
|
|
|
begin
|
|
|
|
|
lHeaders.Add(TNameValuePair.Create(lParam.Name, lParam.Value));
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
Result := lHeaders.ToArray;
|
|
|
|
|
finally
|
|
|
|
|
FreeAndNil(lHeaders);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.HeaderValue(const aName: string): string;
|
|
|
|
|
var
|
2020-09-09 02:00:08 +02:00
|
|
|
|
lParam: TMVCRESTParam;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := '';
|
2020-09-09 02:00:08 +02:00
|
|
|
|
|
|
|
|
|
for lParam in fParameters do
|
2020-09-08 03:21:28 +02:00
|
|
|
|
begin
|
2020-09-09 02:00:08 +02:00
|
|
|
|
if lParam.&Type = TMVCRESTParamType.Header then
|
|
|
|
|
begin
|
|
|
|
|
if SameText(lParam.Name, aName) then
|
|
|
|
|
Exit(lParam.Value);
|
|
|
|
|
end;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
end;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-29 16:56:25 +02:00
|
|
|
|
function TMVCRESTClient.HTTPMethodName(const aHTTPMethod: TMVCHTTPMethodType): string;
|
|
|
|
|
begin
|
|
|
|
|
case aHTTPMethod of
|
|
|
|
|
httpGET:
|
|
|
|
|
Result := 'GET';
|
|
|
|
|
httpPOST:
|
|
|
|
|
Result := 'POST';
|
|
|
|
|
httpPUT:
|
|
|
|
|
Result := 'PUT';
|
|
|
|
|
httpDELETE:
|
|
|
|
|
Result := 'DELETE';
|
|
|
|
|
httpHEAD:
|
|
|
|
|
Result := 'HEAD';
|
|
|
|
|
httpOPTIONS:
|
|
|
|
|
Result := 'OPTIONS';
|
|
|
|
|
httpPATCH:
|
|
|
|
|
Result := 'PATCH';
|
|
|
|
|
httpTRACE:
|
|
|
|
|
Result := 'TRACE';
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-09-22 01:32:21 +02:00
|
|
|
|
function TMVCRESTClient.InternalExecuteRequest(const aMethod: TMVCHTTPMethodType): IMVCRESTResponse;
|
|
|
|
|
var
|
|
|
|
|
lURL: string;
|
|
|
|
|
lResponse: IHTTPResponse;
|
|
|
|
|
lBodyStream: TStream;
|
2020-09-29 16:56:25 +02:00
|
|
|
|
lURI: TURI;
|
|
|
|
|
lRequest: IHTTPRequest;
|
2020-12-17 00:05:39 +01:00
|
|
|
|
lHandled: Boolean;
|
2020-09-22 01:32:21 +02:00
|
|
|
|
begin
|
|
|
|
|
fHTTPClient.ProxySettings := fProxySettings;
|
|
|
|
|
|
|
|
|
|
lURL := GetFullURL;
|
|
|
|
|
DoConvertMVCPathParamsToRESTParams(lURL);
|
|
|
|
|
DoApplyPathParams(lURL);
|
|
|
|
|
DoApplyQueryParams(lURL);
|
|
|
|
|
DoEncodeURL(lURL);
|
|
|
|
|
DoApplyCookies(lURL);
|
2020-09-29 16:56:25 +02:00
|
|
|
|
lURI := TURI.Create(lURL);
|
2020-09-22 01:32:21 +02:00
|
|
|
|
|
|
|
|
|
lBodyStream := nil;
|
|
|
|
|
DoPrepareBodyRequest(lBodyStream);
|
|
|
|
|
DoApplyHeaders;
|
|
|
|
|
|
2020-09-29 16:56:25 +02:00
|
|
|
|
lRequest := fHTTPClient.GetRequest(HTTPMethodName(aMethod), lURI);
|
|
|
|
|
lRequest.SourceStream := lBodyStream;
|
2020-12-17 00:05:39 +01:00
|
|
|
|
|
|
|
|
|
DoBeforeRequest(lRequest);
|
|
|
|
|
|
2021-03-23 13:23:56 +01:00
|
|
|
|
try
|
|
|
|
|
lResponse := fHTTPClient.Execute(lRequest, nil, []);
|
|
|
|
|
except
|
|
|
|
|
on E: Exception do
|
|
|
|
|
begin
|
|
|
|
|
raise EMVCRESTClientException.Create(E.Message);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
2020-09-22 01:32:21 +02:00
|
|
|
|
|
2020-12-17 00:05:39 +01:00
|
|
|
|
lHandled := False;
|
|
|
|
|
DoRequestCompleted(lResponse, lHandled);
|
|
|
|
|
|
|
|
|
|
if not lHandled then
|
|
|
|
|
begin
|
|
|
|
|
Result := TMVCRESTResponse.Create(lResponse);
|
|
|
|
|
DoResponseCompleted(Result);
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
Result := nil;
|
|
|
|
|
end;
|
2020-09-22 01:32:21 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.MaxRedirects(const aMaxRedirects: Integer): IMVCRESTClient;
|
2015-04-01 17:01:23 +02:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := Self;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
fHTTPClient.MaxRedirects := aMaxRedirects;
|
2015-04-01 17:01:23 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.MaxRedirects: Integer;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := fHTTPClient.MaxRedirects;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
class function TMVCRESTClient.New: IMVCRESTClient;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := TMVCRESTClient.Create;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.ObjectIsList(aObject: TObject): Boolean;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := fRttiContext.GetType(aObject.ClassType).GetMethod('GetEnumerator') <> nil;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-12-16 22:42:25 +01:00
|
|
|
|
function TMVCRESTClient.Options: IMVCRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
Result := ExecuteRequest(TMVCHTTPMethodType.httpOPTIONS);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTClient.Options(const aResource: string): IMVCRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
Resource(aResource);
|
|
|
|
|
Result := Options;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-09-24 21:29:49 +02:00
|
|
|
|
procedure TMVCRESTClient.DoValidateServerCertificate(const aSender: TObject; const aRequest: TURLRequest;
|
2020-12-17 00:05:39 +01:00
|
|
|
|
const aCertificate: TCertificate; var aAccepted: Boolean);
|
2020-09-24 21:29:49 +02:00
|
|
|
|
begin
|
|
|
|
|
if Assigned(fValidateServerCertificate) then
|
|
|
|
|
fValidateServerCertificate(aSender, aRequest, aCertificate, aAccepted)
|
|
|
|
|
else
|
|
|
|
|
aAccepted := True;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-09-10 02:40:14 +02:00
|
|
|
|
function TMVCRESTClient.Patch(const aResource, aBody: string; const aContentType: string): IMVCRESTResponse;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
2020-09-10 02:40:14 +02:00
|
|
|
|
Resource(aResource);
|
|
|
|
|
if not aBody.isEmpty then
|
|
|
|
|
begin
|
|
|
|
|
ClearBody;
|
|
|
|
|
AddBody(aBody, aContentType);
|
|
|
|
|
end;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
|
2020-09-10 02:40:14 +02:00
|
|
|
|
Result := Patch;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.Patch: IMVCRESTResponse;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
2020-09-10 02:40:14 +02:00
|
|
|
|
Result := ExecuteRequest(TMVCHTTPMethodType.httpPATCH);
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.Patch(const aResource: string; aBody: TObject; const aOwnsBody: Boolean): IMVCRESTResponse;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
2020-09-10 02:40:14 +02:00
|
|
|
|
if aBody = nil then
|
|
|
|
|
raise EMVCRESTClientException.Create('You need a valid body!');
|
|
|
|
|
|
|
|
|
|
Result := Patch(aResource, SerializeObject(aBody));
|
2020-08-09 02:18:36 +02:00
|
|
|
|
|
2020-09-10 02:40:14 +02:00
|
|
|
|
if aOwnsBody then
|
|
|
|
|
aBody.Free;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-10 02:40:14 +02:00
|
|
|
|
function TMVCRESTClient.Post(const aResource, aBody, aContentType: string): IMVCRESTResponse;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
begin
|
2020-09-10 02:40:14 +02:00
|
|
|
|
Resource(aResource);
|
|
|
|
|
if not aBody.IsEmpty then
|
|
|
|
|
begin
|
|
|
|
|
ClearBody;
|
|
|
|
|
AddBody(aBody, aContentType);
|
|
|
|
|
end;
|
|
|
|
|
Result := Post;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.Post(const aResource: string; aBody: TObject; const aOwnsBody: Boolean): IMVCRESTResponse;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
2020-09-10 02:40:14 +02:00
|
|
|
|
if aBody = nil then
|
|
|
|
|
raise EMVCRESTClientException.Create('You need a valid body!');
|
|
|
|
|
|
|
|
|
|
Result := Post(aResource, SerializeObject(aBody));
|
2020-08-20 20:08:41 +02:00
|
|
|
|
|
2020-09-10 02:40:14 +02:00
|
|
|
|
if aOwnsBody then
|
|
|
|
|
aBody.Free;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.Post: IMVCRESTResponse;
|
2018-10-24 23:07:31 +02:00
|
|
|
|
begin
|
2020-09-10 02:40:14 +02:00
|
|
|
|
Result := ExecuteRequest(TMVCHTTPMethodType.httpPOST);
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.ProxyPassword: string;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := fProxySettings.Password;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.ProxyPassword(const aProxyPassword: string): IMVCRESTClient;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
fProxySettings.Password := aProxyPassword;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.ProxyPort: Integer;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := fProxySettings.Port;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.ProxyPort(const aProxyPort: Integer): IMVCRESTClient;
|
2020-08-26 22:13:18 +02:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := Self;
|
|
|
|
|
fProxySettings.Port := aProxyPort;
|
2020-08-26 22:13:18 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.ProxyServer(const aProxyServer: string): IMVCRESTClient;
|
2020-08-26 22:13:18 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
fProxySettings.Host := aProxyServer;
|
2020-08-26 22:13:18 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.ProxyServer: string;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := fProxySettings.Host;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.ProxyUsername(const aProxyUsername: string): IMVCRESTClient;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := Self;
|
2020-09-08 03:21:28 +02:00
|
|
|
|
fProxySettings.UserName := aProxyUsername;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.ProxyUsername: string;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := fProxySettings.UserName;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.Put: IMVCRESTResponse;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-09-10 02:40:14 +02:00
|
|
|
|
Result := ExecuteRequest(TMVCHTTPMethodType.httpPUT);
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-10 02:40:14 +02:00
|
|
|
|
function TMVCRESTClient.Put(const aResource, aBody, aContentType: string): IMVCRESTResponse;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2020-09-10 02:40:14 +02:00
|
|
|
|
Resource(aResource);
|
|
|
|
|
if not aBody.IsEmpty then
|
|
|
|
|
begin
|
|
|
|
|
ClearBody;
|
|
|
|
|
AddBody(aBody, aContentType);
|
|
|
|
|
end;
|
|
|
|
|
Result := Put;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.Put(const aResource: string; aBody: TObject; const aOwnsBody: Boolean): IMVCRESTResponse;
|
2020-08-26 01:07:21 +02:00
|
|
|
|
begin
|
2020-09-10 02:40:14 +02:00
|
|
|
|
if aBody = nil then
|
|
|
|
|
raise EMVCRESTClientException.Create('You need a valid body!');
|
2020-08-26 01:07:21 +02:00
|
|
|
|
|
2020-09-10 02:40:14 +02:00
|
|
|
|
Result := Put(aResource, SerializeObject(aBody));
|
|
|
|
|
|
|
|
|
|
if aOwnsBody then
|
|
|
|
|
aBody.Free;
|
2020-08-21 02:45:51 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.ReadTimeout: Integer;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := fHTTPClient.ResponseTimeout;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.ReadTimeout(const aReadTimeout: Integer): IMVCRESTClient;
|
2015-04-01 17:01:23 +02:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := Self;
|
|
|
|
|
fHTTPClient.ResponseTimeout := aReadTimeout;
|
2015-04-01 17:01:23 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.RegisterTypeSerializer(const aTypeInfo: PTypeInfo;
|
2020-09-22 01:32:21 +02:00
|
|
|
|
aInstance: IMVCTypeSerializer): IMVCRESTClient;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := Self;
|
2021-04-11 22:37:14 +02:00
|
|
|
|
Serializer.RegisterTypeSerializer(aTypeInfo, aInstance);
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.Resource(const aResource: string): IMVCRESTClient;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := Self;
|
|
|
|
|
fResource := aResource;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.Resource: string;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := fResource;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-09 02:00:08 +02:00
|
|
|
|
{$IF defined(TOKYOORBETTER)}
|
2020-09-10 02:40:14 +02:00
|
|
|
|
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.SecureProtocols: THTTPSecureProtocols;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
Result := fHTTPClient.SecureProtocols;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-09 02:00:08 +02:00
|
|
|
|
function TMVCRESTClient.SecureProtocols(const aSecureProtocols: THTTPSecureProtocols): IMVCRESTClient;
|
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
|
|
|
|
fHTTPClient.SecureProtocols := aSecureProtocols;
|
|
|
|
|
end;
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
2020-09-10 02:40:14 +02:00
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.SerializeObject(aObject: TObject): string;
|
2016-03-03 17:07:34 +01:00
|
|
|
|
begin
|
2020-09-08 03:21:28 +02:00
|
|
|
|
if ObjectIsList(aObject) then
|
2021-04-11 22:37:14 +02:00
|
|
|
|
Result := Serializer.SerializeCollection(aObject)
|
2020-09-08 03:21:28 +02:00
|
|
|
|
else
|
2021-04-11 22:37:14 +02:00
|
|
|
|
Result := Serializer.SerializeObject(aObject);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTClient.Serializer(const aSerializer: IMVCSerializer): IMVCRESTClient;
|
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
|
|
|
|
TMonitor.Enter(fLock);
|
|
|
|
|
try
|
|
|
|
|
fSerializer := nil;
|
|
|
|
|
fSerializer := aSerializer;
|
|
|
|
|
finally
|
|
|
|
|
TMonitor.Exit(fLock);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTClient.Serializer: IMVCSerializer;
|
|
|
|
|
begin
|
|
|
|
|
if not Assigned(fSerializer) then
|
|
|
|
|
begin
|
|
|
|
|
TMonitor.Enter(fLock);
|
|
|
|
|
try
|
|
|
|
|
if not Assigned(fSerializer) then
|
|
|
|
|
begin
|
|
|
|
|
fSerializer := TMVCJsonDataObjectsSerializer.Create;
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
TMonitor.Exit(fLock);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
Result := fSerializer;
|
2016-03-03 17:07:34 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-24 20:03:11 +02:00
|
|
|
|
function TMVCRESTClient.SessionId: string;
|
|
|
|
|
var
|
|
|
|
|
lCookie: TCookie;
|
|
|
|
|
lParam: TMVCRESTParam;
|
|
|
|
|
begin
|
|
|
|
|
Result := '';
|
|
|
|
|
|
|
|
|
|
for lParam in fParameters do
|
|
|
|
|
begin
|
|
|
|
|
if lParam.&Type = TMVCRESTParamType.Cookie then
|
|
|
|
|
begin
|
|
|
|
|
if SameText(lParam.Name, TMVCConstants.SESSION_TOKEN_NAME) then
|
|
|
|
|
begin
|
|
|
|
|
Result := lParam.Value;
|
|
|
|
|
Break;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
if Result.IsEmpty then
|
|
|
|
|
begin
|
|
|
|
|
for lCookie in fHTTPClient.CookieManager.Cookies do
|
|
|
|
|
begin
|
|
|
|
|
if SameText(lCookie.Name, TMVCConstants.SESSION_TOKEN_NAME) then
|
|
|
|
|
begin
|
|
|
|
|
Result := lCookie.Value;
|
|
|
|
|
Break;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
Result := lCookie.Value;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
if Result.Contains('invalid') then
|
|
|
|
|
Result := '';
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTClient.SessionId(const aSessionId: string): IMVCRESTClient;
|
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
|
|
|
|
|
|
|
|
|
AddCookie(TMVCConstants.SESSION_TOKEN_NAME, aSessionId);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTClient.SetBasicAuthorization(const aUsername, aPassword: string): IMVCRESTClient;
|
2020-09-11 19:55:26 +02:00
|
|
|
|
var
|
|
|
|
|
lBase64: TNetEncoding;
|
2018-03-10 23:50:44 +01:00
|
|
|
|
begin
|
2020-09-24 20:03:11 +02:00
|
|
|
|
Result := Self;
|
2020-09-11 19:55:26 +02:00
|
|
|
|
// Do not use TNetEncoding.Base64 here, because it may break long line
|
|
|
|
|
lBase64 := TBase64Encoding.Create(0, '');
|
|
|
|
|
try
|
2021-01-22 15:37:57 +01:00
|
|
|
|
AddHeader(TMVCRESTClientConsts.AUTHORIZATION_HEADER,
|
|
|
|
|
TMVCRESTClientConsts.BASIC_AUTH_PREFIX + lBase64.Encode(aUsername + ':' + aPassword));
|
2020-09-11 19:55:26 +02:00
|
|
|
|
finally
|
|
|
|
|
FreeAndNil(lBase64);
|
|
|
|
|
end;
|
2018-03-10 23:50:44 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-24 20:03:11 +02:00
|
|
|
|
function TMVCRESTClient.SetBearerAuthorization(const aAccessToken: string): IMVCRESTClient;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
begin
|
2021-01-22 15:37:57 +01:00
|
|
|
|
Result := AddHeader(TMVCRESTClientConsts.AUTHORIZATION_HEADER,
|
|
|
|
|
TMVCRESTClientConsts.BEARER_AUTH_PREFIX + aAccessToken);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-12-17 00:05:39 +01:00
|
|
|
|
function TMVCRESTClient.SetBeforeRequestProc(aBeforeRequestProc: TBeforeRequestProc): IMVCRESTClient;
|
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
|
|
|
|
fBeforeRequestProc := aBeforeRequestProc;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
procedure TMVCRESTClient.SetContentType(const aContentType: string);
|
2013-11-05 14:57:50 +01:00
|
|
|
|
begin
|
2021-01-15 19:31:33 +01:00
|
|
|
|
AddHeader(sContentType, aContentType);
|
2013-11-05 14:57:50 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-22 01:32:21 +02:00
|
|
|
|
procedure TMVCRESTClient.SetParameter(const aParamType: TMVCRESTParamType; const aName, aValue: string);
|
|
|
|
|
var
|
|
|
|
|
I: Integer;
|
|
|
|
|
begin
|
|
|
|
|
for I := Pred(fParameters.Count) downto 0 do
|
|
|
|
|
begin
|
|
|
|
|
if (fParameters[I].&Type = aParamType) and SameText(fParameters[I].Name, aName) then
|
|
|
|
|
begin
|
|
|
|
|
fParameters.Delete(I);
|
|
|
|
|
Break;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
fParameters.Add(TMVCRESTParam.Create(aParamType, aName, aValue));
|
|
|
|
|
end;
|
|
|
|
|
|
2020-12-17 00:05:39 +01:00
|
|
|
|
function TMVCRESTClient.SetRequestCompletedProc(aRequestCompletedProc: TRequestCompletedProc): IMVCRESTClient;
|
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
|
|
|
|
fRequestCompletedProc := aRequestCompletedProc;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTClient.SetResponseCompletedProc(aResponseCompletedProc: TResponseCompletedProc): IMVCRESTClient;
|
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
|
|
|
|
fResponseCompletedProc := aResponseCompletedProc;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-09-24 21:29:49 +02:00
|
|
|
|
function TMVCRESTClient.SetValidateServerCertificateProc(
|
|
|
|
|
aValidateCertificateProc: TValidateServerCertificateProc): IMVCRESTClient;
|
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
|
|
|
|
fValidateServerCertificate := aValidateCertificateProc;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.UserAgent(const aUserAgent: string): IMVCRESTClient;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2021-01-15 19:31:33 +01:00
|
|
|
|
Result := AddHeader(sUserAgent, aUserAgent);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-08 03:21:28 +02:00
|
|
|
|
function TMVCRESTClient.UserAgent: string;
|
2020-08-26 01:07:21 +02:00
|
|
|
|
begin
|
2021-01-15 19:31:33 +01:00
|
|
|
|
Result := HeaderValue(sUserAgent);
|
2020-08-26 01:07:21 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-10 02:40:14 +02:00
|
|
|
|
{ TMVCRESTResponse }
|
|
|
|
|
|
|
|
|
|
function TMVCRESTResponse.Content: string;
|
|
|
|
|
begin
|
|
|
|
|
Result := fContent;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTResponse.ContentEncoding: string;
|
|
|
|
|
begin
|
|
|
|
|
Result := fContentEncoding;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTResponse.ContentLength: Integer;
|
|
|
|
|
begin
|
|
|
|
|
Result := fContentLength;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTResponse.ContentType: string;
|
|
|
|
|
begin
|
|
|
|
|
Result := fContentType;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTResponse.CookieByName(const aName: string): TCookie;
|
|
|
|
|
var
|
|
|
|
|
lCookie: TCookie;
|
|
|
|
|
begin
|
|
|
|
|
Result := Default (TCookie);
|
|
|
|
|
for lCookie in fCookies do
|
|
|
|
|
begin
|
|
|
|
|
if SameText(lCookie.Name, aName) then
|
|
|
|
|
Exit(lCookie);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTResponse.Cookies: TCookies;
|
|
|
|
|
begin
|
|
|
|
|
Result := fCookies;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
constructor TMVCRESTResponse.Create(aHTTPResponse: IHTTPResponse);
|
|
|
|
|
begin
|
|
|
|
|
fHeaders := TStringList.Create;
|
2020-09-24 20:03:11 +02:00
|
|
|
|
SetLength(fContentRawBytes, 0);
|
2020-09-10 02:40:14 +02:00
|
|
|
|
fCookies := TCookies.Create;
|
|
|
|
|
|
|
|
|
|
FillResponse(aHTTPResponse);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
destructor TMVCRESTResponse.Destroy;
|
|
|
|
|
begin
|
2020-09-24 20:03:11 +02:00
|
|
|
|
SetLength(fContentRawBytes, 0);
|
2020-09-10 02:40:14 +02:00
|
|
|
|
FreeAndNil(fHeaders);
|
|
|
|
|
FreeAndNil(fCookies);
|
|
|
|
|
inherited Destroy;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMVCRESTResponse.FillResponse(aHTTPResponse: IHTTPResponse);
|
|
|
|
|
var
|
|
|
|
|
lHeader: TNetHeader;
|
|
|
|
|
begin
|
|
|
|
|
fSuccess := (aHTTPResponse.StatusCode >= 200) and (aHTTPResponse.StatusCode < 300);
|
|
|
|
|
fStatusCode := aHTTPResponse.StatusCode;
|
|
|
|
|
fStatusText := aHTTPResponse.StatusText;
|
|
|
|
|
|
|
|
|
|
for lHeader in aHTTPResponse.Headers do
|
|
|
|
|
begin
|
|
|
|
|
fHeaders.Values[lHeader.Name] := lHeader.Value;
|
|
|
|
|
end;
|
|
|
|
|
fCookies.AddRange(aHTTPResponse.Cookies.ToArray);
|
|
|
|
|
fServer := aHTTPResponse.HeaderValue[TMVCRESTClientConsts.SERVER_HEADER];
|
2020-09-24 20:03:11 +02:00
|
|
|
|
fContentRawBytes := TMVCRESTClientHelper.GetResponseContentAsRawBytes(aHTTPResponse.ContentStream,
|
2020-09-10 02:40:14 +02:00
|
|
|
|
aHTTPResponse.ContentEncoding);
|
2020-12-17 00:05:39 +01:00
|
|
|
|
fContent := TMVCRESTClientHelper.GetResponseContentAsString(fContentRawBytes,
|
|
|
|
|
aHTTPResponse.HeaderValue[sContentType]);
|
2020-09-10 02:40:14 +02:00
|
|
|
|
fContentType := aHTTPResponse.HeaderValue[sContentType];
|
|
|
|
|
fContentEncoding := aHTTPResponse.ContentEncoding;
|
|
|
|
|
fContentLength := aHTTPResponse.ContentLength;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTResponse.Headers: TStrings;
|
|
|
|
|
begin
|
|
|
|
|
Result := fHeaders;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTResponse.HeaderValue(const aName: string): string;
|
|
|
|
|
begin
|
|
|
|
|
Result := fHeaders.Values[aName];
|
|
|
|
|
end;
|
|
|
|
|
|
2020-09-24 20:03:11 +02:00
|
|
|
|
function TMVCRESTResponse.ContentRawBytes: TBytes;
|
2020-09-10 02:40:14 +02:00
|
|
|
|
begin
|
2020-09-24 20:03:11 +02:00
|
|
|
|
Result := fContentRawBytes;
|
2020-09-10 02:40:14 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMVCRESTResponse.SaveContentToFile(const aFileName: string);
|
|
|
|
|
var
|
|
|
|
|
lStream: TMemoryStream;
|
|
|
|
|
begin
|
|
|
|
|
lStream := TMemoryStream.Create;
|
|
|
|
|
try
|
2020-09-24 20:03:11 +02:00
|
|
|
|
lStream.Write(fContentRawBytes, Length(fContentRawBytes));
|
2020-09-10 02:40:14 +02:00
|
|
|
|
lStream.Position := 0;
|
|
|
|
|
lStream.SaveToFile(aFileName);
|
|
|
|
|
finally
|
|
|
|
|
FreeAndNil(lStream);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMVCRESTResponse.SaveContentToStream(aStream: TStream);
|
|
|
|
|
begin
|
|
|
|
|
if aStream = nil then
|
|
|
|
|
raise EMVCRESTClientException.Create('Stream not assigned!');
|
|
|
|
|
|
2020-09-24 20:03:11 +02:00
|
|
|
|
aStream.Write(fContentRawBytes, Length(fContentRawBytes));
|
2020-09-10 02:40:14 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTResponse.Server: string;
|
|
|
|
|
begin
|
|
|
|
|
Result := fServer;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTResponse.StatusCode: Integer;
|
|
|
|
|
begin
|
|
|
|
|
Result := fStatusCode;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTResponse.StatusText: string;
|
|
|
|
|
begin
|
|
|
|
|
Result := fStatusText;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTResponse.Success: Boolean;
|
|
|
|
|
begin
|
|
|
|
|
Result := fSuccess;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-09-24 20:03:11 +02:00
|
|
|
|
{$IF not defined(RIOORBETTER)}
|
|
|
|
|
|
2020-09-23 01:26:13 +02:00
|
|
|
|
{ TCookieManagerHelper }
|
|
|
|
|
|
|
|
|
|
function TCookieManagerHelper.CookieList: TCookies;
|
|
|
|
|
var
|
|
|
|
|
lRttiContext: TRttiContext;
|
|
|
|
|
lField: TRttiField;
|
|
|
|
|
begin
|
|
|
|
|
lRttiContext := TRttiContext.Create;
|
|
|
|
|
try
|
2020-09-24 20:03:11 +02:00
|
|
|
|
lField := lRttiContext.GetType(Self.ClassType).GetField('FCookies');
|
2020-09-23 01:26:13 +02:00
|
|
|
|
Result := nil;
|
|
|
|
|
if Assigned(lField) then
|
|
|
|
|
Result := lField.GetValue(Self).AsObject as TCookies;
|
|
|
|
|
finally
|
|
|
|
|
lRttiContext.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end.
|