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-08-18 23:19:17 +02:00
|
|
|
|
{$I dmvcframework.inc}
|
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
|
interface
|
|
|
|
|
|
|
|
|
|
uses
|
2015-12-18 20:59:40 +01:00
|
|
|
|
System.SysUtils,
|
2020-08-04 00:48:35 +02:00
|
|
|
|
System.Classes,
|
|
|
|
|
REST.Client,
|
|
|
|
|
REST.Types,
|
2020-08-09 02:18:36 +02:00
|
|
|
|
MVCFramework.RESTClient.Intf,
|
2020-08-04 00:48:35 +02:00
|
|
|
|
MVCFramework.Serializer.Intf,
|
2020-08-09 02:18:36 +02:00
|
|
|
|
MVCFramework.Serializer.Commons,
|
2020-08-18 23:19:17 +02:00
|
|
|
|
MVCFramework.RESTClient.Indy,
|
2020-08-09 02:18:36 +02:00
|
|
|
|
Data.DB,
|
2020-08-18 23:19:17 +02:00
|
|
|
|
System.Rtti,
|
2020-08-21 02:45:51 +02:00
|
|
|
|
System.TypInfo,
|
|
|
|
|
System.Net.HttpClient;
|
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-20 20:08:41 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides access to delphi RESTClient library types without the need to use the REST.Types unit.
|
|
|
|
|
/// </summary>
|
|
|
|
|
TRESTContentType = REST.Types.TRESTContentType;
|
2020-08-26 01:07:21 +02:00
|
|
|
|
TCookie = System.Net.HttpClient.TCookie;
|
|
|
|
|
TCookies = System.Net.HttpClient.TCookies;
|
2020-08-26 22:13:18 +02:00
|
|
|
|
THTTPSecureProtocol = System.Net.HttpClient.THTTPSecureProtocol;
|
|
|
|
|
THTTPSecureProtocols = System.Net.HttpClient.THTTPSecureProtocols;
|
2020-08-26 01:07:21 +02:00
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Encapsulates the methods of the delphi native RESTClient library.
|
|
|
|
|
/// </summary>
|
|
|
|
|
TMVCRESTClient = class(TInterfacedObject, IMVCRESTClient)
|
2017-04-24 00:19:53 +02:00
|
|
|
|
private
|
2020-08-09 02:18:36 +02:00
|
|
|
|
fRttiContext: TRttiContext;
|
2020-08-04 00:48:35 +02:00
|
|
|
|
fRESTClient: TCustomRESTClient;
|
|
|
|
|
fRESTRequest: TCustomRESTRequest;
|
|
|
|
|
fRESTResponse: TCustomRESTResponse;
|
|
|
|
|
fSerializer: IMVCSerializer;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
fNextRequestIsAsync: Boolean;
|
|
|
|
|
fAsyncCompletionHandler: TProc<IMVCRESTResponse>;
|
|
|
|
|
fAsyncCompletionHandlerWithError: TProc<Exception>;
|
2020-08-21 02:45:51 +02:00
|
|
|
|
fAsyncSynchronized: Boolean;
|
2020-08-18 23:19:17 +02:00
|
|
|
|
|
2020-08-09 02:18:36 +02:00
|
|
|
|
procedure ClearRESTParams(const aRESTParamKind: TRESTRequestParameterKind);
|
|
|
|
|
function ConvertMVCPathParamsToRESTParams(const aResource: string): string;
|
2020-08-26 01:07:21 +02:00
|
|
|
|
function CloneRESTClient: IMVCRESTClient;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
function ObjectIsList(aObject: TObject): Boolean;
|
|
|
|
|
function SerializeObject(aObject: TObject): string;
|
2020-08-26 14:05:12 +02:00
|
|
|
|
function PreEncodeURL(const aURL: string): string;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
{$IF not defined(SYDNEYORBETTER)}
|
|
|
|
|
function GetResponseCookies: TArray<TCookie>;
|
|
|
|
|
{$ENDIF}
|
2020-08-20 20:08:41 +02:00
|
|
|
|
procedure ExecuteAsyncRESTRequest;
|
|
|
|
|
function ExecuteRESTRequest(const aMethod: TRESTRequestMethod): 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 RaiseExceptionOn500(const aRaiseExceptionOn500: Boolean): IMVCRESTClient; overload;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function RaiseExceptionOn500: Boolean; overload;
|
|
|
|
|
|
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-08-26 22:13:18 +02:00
|
|
|
|
function SecureProtocols(const aSecureProtocols: THTTPSecureProtocols): IMVCRESTClient; overload;
|
|
|
|
|
function SecureProtocols: THTTPSecureProtocols; overload;
|
|
|
|
|
|
2020-08-20 20:08:41 +02:00
|
|
|
|
function UserAgent(const aUserAgent: string): IMVCRESTClient; overload;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function UserAgent: string; overload;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
/// <summary>
|
2020-08-26 01:07:21 +02:00
|
|
|
|
/// Clears all parameters, except authorization headers and cookies. This method is executed after each request
|
|
|
|
|
/// is completed.
|
2020-08-21 16:22:23 +02:00
|
|
|
|
/// </summary>
|
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-08-18 23:19:17 +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-08-18 23:19:17 +02:00
|
|
|
|
function SetBearerAuthorization(const aToken: string): 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-08-20 20:08:41 +02:00
|
|
|
|
function AddHeader(const aName, aValue: string; const aDoNotEncode: Boolean = False): IMVCRESTClient; overload;
|
2020-08-26 14:05:12 +02:00
|
|
|
|
function HeaderValue(const aName: string): string;
|
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;
|
|
|
|
|
|
|
|
|
|
/// <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-08-26 01:07:21 +02:00
|
|
|
|
/// Clear all cookies.
|
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;
|
|
|
|
|
|
|
|
|
|
function FallbackCharsetEncoding(const aFallbackCharsetEncoding: string): IMVCRESTClient; overload;
|
|
|
|
|
function FallbackCharsetEncoding: 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;
|
|
|
|
|
|
2020-08-20 20:08:41 +02:00
|
|
|
|
function URLAlreadyEncoded(const aURLAlreadyEncoded: Boolean): IMVCRESTClient; overload;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function URLAlreadyEncoded: Boolean; overload;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Add a body to the requisition.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="aBody">
|
|
|
|
|
/// Body in string format.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="aContentType">
|
|
|
|
|
/// Body content type.
|
|
|
|
|
/// </param>
|
2020-08-20 20:08:41 +02:00
|
|
|
|
function AddBody(const aBody: string; const aDoNotEncode: Boolean = False;
|
|
|
|
|
const aContentType: TRESTContentType = TRESTContentType.ctNone): 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>
|
|
|
|
|
function AddBody(aBodyStream: TStream; const aContentType: TRESTContentType = TRESTContentType.ctNone;
|
2020-08-18 23:19:17 +02:00
|
|
|
|
const aOwnsStream: Boolean = True): 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-18 23:19:17 +02:00
|
|
|
|
function ClearBody: IMVCRESTClient;
|
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-09 02:18:36 +02:00
|
|
|
|
function AddFile(const aName, aFileName: string;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
const aContentType: TRESTContentType = TRESTContentType.ctNone): IMVCRESTClient; overload;
|
|
|
|
|
function AddFile(const aFileName: string;
|
|
|
|
|
const aContentType: TRESTContentType = TRESTContentType.ctNone): IMVCRESTClient; overload;
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function ClearFiles: 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-08-21 16:22:23 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Execute a Post request.
|
|
|
|
|
/// </summary>
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function Post(const aResource: string; aBody: TObject; const aOwnsBody: Boolean = True): IMVCRESTResponse; overload;
|
2020-08-26 01:07:21 +02:00
|
|
|
|
function Post(const aResource: string; const aBody: string = ''; const aDoNotEncode: Boolean = False;
|
|
|
|
|
const aContentType: TRESTContentType = TRESTContentType.ctAPPLICATION_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-08-26 01:07:21 +02:00
|
|
|
|
function Patch(const aResource: string; const aBody: string = ''; const aDoNotEncode: Boolean = False;
|
|
|
|
|
const aContentType: TRESTContentType = TRESTContentType.ctAPPLICATION_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-08-26 01:07:21 +02:00
|
|
|
|
function Put(const aResource: string; const aBody: string = ''; const aDoNotEncode: Boolean = False;
|
|
|
|
|
const aContentType: TRESTContentType = TRESTContentType.ctAPPLICATION_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-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-08-26 14:05:12 +02:00
|
|
|
|
function DataSetUpdate(const aResource, aKeyValue: 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>
|
|
|
|
|
/// 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
|
|
|
|
|
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-08-18 23:19:17 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides access to the REST request response.
|
|
|
|
|
/// </summary>
|
|
|
|
|
TMVCRESTResponse = class(TInterfacedObject, IMVCRESTResponse)
|
2016-04-24 19:08:21 +02:00
|
|
|
|
private
|
2020-08-04 00:48:35 +02:00
|
|
|
|
fSuccess: Boolean;
|
|
|
|
|
fStatusCode: Integer;
|
|
|
|
|
fStatusText: string;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
fErrorMessage: string;
|
2020-08-04 00:48:35 +02:00
|
|
|
|
fHeaders: TStrings;
|
2020-08-21 02:45:51 +02:00
|
|
|
|
fCookies: TCookies;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
fServer: string;
|
|
|
|
|
fFullRequestURI: string;
|
2020-08-04 00:48:35 +02:00
|
|
|
|
fContentType: string;
|
|
|
|
|
fContentEncoding: string;
|
|
|
|
|
fContentLength: Integer;
|
|
|
|
|
fContent: string;
|
|
|
|
|
fRawBytes: TBytes;
|
2020-08-18 23:19:17 +02:00
|
|
|
|
|
2020-08-09 02:18:36 +02:00
|
|
|
|
procedure FillRESTResponse(aRESTResponse: TCustomRESTResponse);
|
2015-12-18 20:59:40 +01:00
|
|
|
|
public
|
2020-08-09 02:18:36 +02:00
|
|
|
|
constructor Create(aRESTResponse: TCustomRESTResponse);
|
2015-12-18 20:59:40 +01:00
|
|
|
|
destructor Destroy; override;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
{$IF not defined(SYDNEYORBETTER)}
|
|
|
|
|
procedure SetCookies(aCookies: TArray<TCookie>);
|
|
|
|
|
{$ENDIF}
|
2020-08-18 23:19:17 +02:00
|
|
|
|
{ IMVCRESTResponse }
|
2020-08-09 02:18:36 +02:00
|
|
|
|
function Success: Boolean;
|
|
|
|
|
function StatusCode: Integer;
|
|
|
|
|
function StatusText: string;
|
|
|
|
|
function ErrorMessage: string;
|
|
|
|
|
function Headers: TStrings;
|
2020-08-26 01:07:21 +02:00
|
|
|
|
function HeaderValue(const aName: string): string;
|
2020-08-21 02:45:51 +02:00
|
|
|
|
function Cookies: TCookies;
|
2020-08-26 01:07:21 +02:00
|
|
|
|
function CookieByName(const aName: string): TCookie;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
function Server: string;
|
|
|
|
|
function FullRequestURI: string;
|
|
|
|
|
function ContentType: string;
|
|
|
|
|
function ContentEncoding: string;
|
|
|
|
|
function ContentLength: Integer;
|
|
|
|
|
function Content: string;
|
|
|
|
|
function RawBytes: TBytes;
|
|
|
|
|
procedure SaveContentToStream(aStream: TStream);
|
|
|
|
|
procedure SaveContentToFile(const aFileName: string);
|
2016-06-23 12:11:01 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
EMVCRESTClientException = class(Exception);
|
2020-08-09 02:18:36 +02:00
|
|
|
|
|
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
|
|
|
|
|
MVCFramework.Serializer.JsonDataObjects,
|
2020-08-09 02:18:36 +02:00
|
|
|
|
System.NetEncoding,
|
2020-08-21 16:22:23 +02:00
|
|
|
|
System.RegularExpressions,
|
|
|
|
|
REST.HttpClient;
|
2016-04-24 19:08:21 +02:00
|
|
|
|
|
2020-08-21 00:00:54 +02:00
|
|
|
|
const
|
2020-08-26 01:07:21 +02:00
|
|
|
|
DEFAULT_ACCEPT_ENCODING = 'gzip';
|
2020-08-21 00:00:54 +02:00
|
|
|
|
DEFAULT_BODY_NAME = 'body';
|
|
|
|
|
DEFAULT_FILE_NAME = 'file';
|
|
|
|
|
AUTHORIZATION_HEADER = 'Authorization';
|
|
|
|
|
BASIC_AUTH_PREFIX = 'Basic ';
|
|
|
|
|
BEARER_AUTH_PREFIX = 'Bearer ';
|
2020-08-21 16:22:23 +02:00
|
|
|
|
HEADER_RESPONSE_COOKIES = 'Cookies';
|
2020-08-26 01:07:21 +02:00
|
|
|
|
PATH_UNSAFE_CHARS: TURLEncoding.TUnsafeChars = [Ord('"'), Ord('<'), Ord('>'), Ord('^'), Ord('`'), Ord('{'),
|
|
|
|
|
Ord('}'), Ord('|'), Ord('/'), Ord('\'), Ord('?'), Ord('#'), Ord('+'), Ord('.')];
|
|
|
|
|
|
|
|
|
|
type
|
|
|
|
|
THackCustomRESTRequest = class(TCustomRESTRequest)
|
|
|
|
|
private
|
|
|
|
|
protected
|
|
|
|
|
procedure DoApplyURLSegments(const aParamList: TRESTRequestParameterArray; var aURL: string); override;
|
|
|
|
|
end;
|
2020-08-21 00:00:54 +02:00
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
{ TMVCRESTClient }
|
2016-06-23 12:11:01 +02:00
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.Accept(const aAccept: string): IMVCRESTClient;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2020-08-09 02:18:36 +02:00
|
|
|
|
Result := Self;
|
|
|
|
|
fRESTRequest.Accept := aAccept;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.Accept: string;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2020-08-09 02:18:36 +02:00
|
|
|
|
Result := fRESTRequest.Accept;
|
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
|
2020-08-09 02:18:36 +02:00
|
|
|
|
Result := fRESTRequest.AcceptCharset;
|
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
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := Self;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
fRESTRequest.AcceptCharset := aAcceptCharset;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.AcceptEncoding: string;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2020-08-09 02:18:36 +02:00
|
|
|
|
Result := fRESTRequest.AcceptEncoding;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.AcceptEncoding(const aAcceptEncoding: string): IMVCRESTClient;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2020-08-09 02:18:36 +02:00
|
|
|
|
Result := Self;
|
|
|
|
|
fRESTRequest.AcceptEncoding := aAcceptEncoding;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-20 20:08:41 +02:00
|
|
|
|
function TMVCRESTClient.AddBody(const aBody: string; const aDoNotEncode: Boolean;
|
|
|
|
|
const aContentType: TRESTContentType): IMVCRESTClient;
|
|
|
|
|
var
|
|
|
|
|
lBodyName: string;
|
|
|
|
|
lOptions: TRESTRequestParameterOptions;
|
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
|
|
|
|
|
|
|
|
|
// A body does not have a specific name, but as names need to be unique, we are using a GUID here
|
|
|
|
|
lBodyName := TGUID.NewGuid.ToString;
|
|
|
|
|
lBodyName := lBodyName.Replace('{', '', [rfReplaceAll]);
|
|
|
|
|
lBodyName := lBodyName.Replace('}', '', [rfReplaceAll]);
|
|
|
|
|
lBodyName := lBodyName.Replace('-', '', [rfReplaceAll]);
|
2020-08-21 00:00:54 +02:00
|
|
|
|
lBodyName := DEFAULT_BODY_NAME + lBodyName;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
|
|
|
|
|
if aDoNotEncode then
|
|
|
|
|
lOptions := [TRESTRequestParameterOption.poDoNotEncode]
|
|
|
|
|
else
|
|
|
|
|
lOptions := [];
|
|
|
|
|
|
2020-08-26 01:07:21 +02:00
|
|
|
|
fRESTRequest.Params.AddItem(lBodyName, aBody, TRESTRequestParameterKind.pkREQUESTBODY, lOptions, aContentType);
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function TMVCRESTClient.AddBody(aBodyStream: TStream; const aContentType: TRESTContentType;
|
2020-08-18 23:19:17 +02:00
|
|
|
|
const aOwnsStream: Boolean): IMVCRESTClient;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
var
|
|
|
|
|
lOwnsStream: TRESTObjectOwnership;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2020-08-21 16:22:23 +02:00
|
|
|
|
if aBodyStream = nil then
|
2020-08-18 23:19:17 +02:00
|
|
|
|
raise EMVCRESTClientException.Create('You need a valid body!');
|
2015-12-18 20:59:40 +01:00
|
|
|
|
|
2018-05-16 19:46:29 +02:00
|
|
|
|
Result := Self;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
|
|
|
|
|
if aOwnsStream then
|
|
|
|
|
lOwnsStream := TRESTObjectOwnership.ooREST
|
|
|
|
|
else
|
|
|
|
|
lOwnsStream := TRESTObjectOwnership.ooApp;
|
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
fRESTRequest.AddBody(aBodyStream, aContentType, lOwnsStream);
|
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-08-21 16:22:23 +02:00
|
|
|
|
if aBodyObject = nil then
|
2020-08-18 23:19:17 +02:00
|
|
|
|
raise EMVCRESTClientException.Create('You need a valid body!');
|
2020-08-09 02:18:36 +02:00
|
|
|
|
|
|
|
|
|
Result := Self;
|
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
AddBody(SerializeObject(aBodyObject), False, TRESTContentType.ctAPPLICATION_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-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.AddCookie(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-08-09 02:18:36 +02:00
|
|
|
|
fRESTRequest.AddParameter(aName, aValue, TRESTRequestParameterKind.pkCOOKIE);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.AddFile(const aName, aFileName: string; const aContentType: TRESTContentType): IMVCRESTClient;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2020-08-09 02:18:36 +02:00
|
|
|
|
Result := Self;
|
|
|
|
|
fRESTRequest.AddFile(aName, aFileName, aContentType);
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.AddFile(const aFileName: string; const aContentType: TRESTContentType): IMVCRESTClient;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2020-08-21 00:00:54 +02:00
|
|
|
|
Result := AddFile(DEFAULT_FILE_NAME, aFileName, aContentType);
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-20 20:08:41 +02:00
|
|
|
|
function TMVCRESTClient.AddHeader(const aName, aValue: string; const aDoNotEncode: Boolean): IMVCRESTClient;
|
|
|
|
|
var
|
|
|
|
|
lOptions: TRESTRequestParameterOptions;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-08-09 02:18:36 +02:00
|
|
|
|
Result := Self;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
|
|
|
|
|
if aDoNotEncode then
|
|
|
|
|
lOptions := [TRESTRequestParameterOption.poDoNotEncode]
|
|
|
|
|
else
|
|
|
|
|
lOptions := [];
|
|
|
|
|
|
|
|
|
|
fRESTRequest.AddParameter(aName, aValue, TRESTRequestParameterKind.pkHTTPHEADER, lOptions);
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.AddPathParam(const aName: string; aValue: TGUID): IMVCRESTClient;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2020-08-09 02:18:36 +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.AddPathParam(const aName: string; aValue: Int64): IMVCRESTClient;
|
2014-04-10 13:56:23 +02: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: string; aValue: Integer): 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-08-26 01:07:21 +02:00
|
|
|
|
fRESTRequest.AddParameter(aName, aValue, TRESTRequestParameterKind.pkURLSEGMENT);
|
2014-04-10 13:56:23 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.AddPathParam(const aName: string; aValue: Double): IMVCRESTClient;
|
2014-04-10 13:56:23 +02:00
|
|
|
|
begin
|
2020-08-09 02:18:36 +02:00
|
|
|
|
Result := AddPathParam(aName, aValue.ToString);
|
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-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.AddPathParam(const aName: string; aValue: TDate): IMVCRESTClient;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-08-09 02:18:36 +02:00
|
|
|
|
Result := AddPathParam(aName, DateToISODate(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: TDate): IMVCRESTClient;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2020-08-09 02:18:36 +02:00
|
|
|
|
Result := AddQueryStringParam(aName, DateToISODate(aValue));
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.AddQueryStringParam(const aName: string; aValue: TDateTime): IMVCRESTClient;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2020-08-09 02:18:36 +02:00
|
|
|
|
Result := AddQueryStringParam(aName, DateTimeToISOTimeStamp(aValue));
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.AddQueryStringParam(const aName: string; aValue: TTime): IMVCRESTClient;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2020-08-09 02:18:36 +02:00
|
|
|
|
Result := AddQueryStringParam(aName, TimeToISOTime(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: Double): IMVCRESTClient;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-08-09 02:18:36 +02:00
|
|
|
|
Result := AddQueryStringParam(aName, aValue.ToString);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function TMVCRESTClient.AllowCookies: Boolean;
|
|
|
|
|
begin
|
|
|
|
|
Result := fRESTClient.AllowCookies;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTClient.AllowCookies(const aAllowCookies: Boolean): IMVCRESTClient;
|
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
|
|
|
|
fRESTClient.AllowCookies := aAllowCookies;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-21 02:45:51 +02:00
|
|
|
|
function TMVCRESTClient.Async(aCompletionHandler: TProc<IMVCRESTResponse>; aCompletionHandlerWithError: TProc<Exception>;
|
|
|
|
|
const aSynchronized: Boolean): IMVCRESTClient;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
|
|
|
|
fNextRequestIsAsync := True;
|
|
|
|
|
fAsyncCompletionHandler := aCompletionHandler;
|
|
|
|
|
fAsyncCompletionHandlerWithError := aCompletionHandlerWithError;
|
2020-08-21 02:45:51 +02:00
|
|
|
|
fAsyncSynchronized := aSynchronized;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTClient.BaseURL(const aHost: string; const aPort: Integer): IMVCRESTClient;
|
|
|
|
|
begin
|
|
|
|
|
Result := BaseURL(aHost + ':' + aPort.ToString);
|
2020-08-20 20:08:41 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function TMVCRESTClient.BaseURL(const aBaseURL: string): IMVCRESTClient;
|
|
|
|
|
var
|
|
|
|
|
lBaseURL: string;
|
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
|
|
|
|
|
|
|
|
|
lBaseURL := aBaseURL;
|
|
|
|
|
if not lBaseURL.Contains('://') then
|
|
|
|
|
lBaseURL := 'http://' + lBaseURL;
|
|
|
|
|
|
2020-08-26 14:05:12 +02:00
|
|
|
|
fRESTClient.BaseURL := PreEncodeURL(lBaseURL);
|
2020-08-21 16:22:23 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTClient.BaseURL: string;
|
|
|
|
|
begin
|
|
|
|
|
Result := fRESTClient.BaseURL;
|
|
|
|
|
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-08-26 01:07:21 +02:00
|
|
|
|
fRESTRequest.AddParameter(aName, aValue, TRESTRequestParameterKind.pkQUERY);
|
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-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.AddQueryStringParam(const aName: string; aValue: TGUID): IMVCRESTClient;
|
2015-12-22 12:17:13 +01:00
|
|
|
|
begin
|
2020-08-09 02:18:36 +02:00
|
|
|
|
Result := AddQueryStringParam(aName, aValue.ToString);
|
2015-12-22 12:17:13 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.AddQueryStringParam(const aName: string; aValue: Int64): IMVCRESTClient;
|
2015-12-22 12:17:13 +01:00
|
|
|
|
begin
|
2020-08-09 02:18:36 +02:00
|
|
|
|
Result := AddQueryStringParam(aName, aValue.ToString);
|
2015-12-22 12:17:13 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-20 20:08:41 +02:00
|
|
|
|
function TMVCRESTClient.ClearAllParams: IMVCRESTClient;
|
2020-08-21 02:45:51 +02:00
|
|
|
|
var
|
|
|
|
|
lAuthHeader: TRESTRequestParameter;
|
2020-08-26 14:05:12 +02:00
|
|
|
|
lLastResource: string;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
|
|
|
|
|
|
|
|
|
fRESTClient.HandleRedirects := True;
|
|
|
|
|
fRESTClient.RaiseExceptionOn500 := False;
|
2020-08-21 02:45:51 +02:00
|
|
|
|
fRESTClient.AllowCookies := True;
|
|
|
|
|
|
|
|
|
|
// If the authorization header exists, it will be extracted from the list of parameters to be added later.
|
|
|
|
|
lAuthHeader := fRESTRequest.Params.ParameterByName(AUTHORIZATION_HEADER);
|
|
|
|
|
if Assigned(lAuthHeader) then
|
|
|
|
|
lAuthHeader.Collection := nil;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
|
2020-08-26 14:05:12 +02:00
|
|
|
|
// Saves the last resource of the request
|
|
|
|
|
lLastResource := fRESTRequest.Resource;
|
|
|
|
|
|
2020-08-20 20:08:41 +02:00
|
|
|
|
fRESTRequest.ResetToDefaults;
|
|
|
|
|
fRESTRequest.AutoCreateParams := False;
|
2020-08-21 00:00:54 +02:00
|
|
|
|
fRESTRequest.AcceptEncoding := DEFAULT_ACCEPT_ENCODING;
|
2020-08-20 23:35:28 +02:00
|
|
|
|
|
2020-08-21 02:45:51 +02:00
|
|
|
|
if Assigned(lAuthHeader) then
|
|
|
|
|
lAuthHeader.Collection := fRESTRequest.Params;
|
|
|
|
|
|
2020-08-26 14:05:12 +02:00
|
|
|
|
fRESTRequest.Resource := lLastResource;
|
|
|
|
|
|
2020-08-20 23:35:28 +02:00
|
|
|
|
fNextRequestIsAsync := False;;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
fAsyncCompletionHandler := nil;
|
|
|
|
|
fAsyncCompletionHandlerWithError := nil;
|
2020-08-21 02:45:51 +02:00
|
|
|
|
fAsyncSynchronized := False;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.ClearBody: IMVCRESTClient;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
|
|
|
|
fRESTRequest.ClearBody;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-26 01:07:21 +02:00
|
|
|
|
type
|
|
|
|
|
THackRESTClient = class(TCustomRESTClient);
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.ClearCookies: IMVCRESTClient;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := Self;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
ClearRESTParams(TRESTRequestParameterKind.pkCOOKIE);
|
2020-08-26 14:05:12 +02:00
|
|
|
|
// It is necessary to recreate the RESTClient's internal HttpClient so that stored cookies are deleted.
|
2020-08-26 01:07:21 +02:00
|
|
|
|
THackRESTClient(fRESTClient).CreateHttpClient;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.ClearFiles: IMVCRESTClient;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
|
|
|
|
ClearRESTParams(TRESTRequestParameterKind.pkFILE);
|
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-08-09 02:18:36 +02:00
|
|
|
|
ClearRESTParams(TRESTRequestParameterKind.pkHTTPHEADER);
|
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-08-09 02:18:36 +02:00
|
|
|
|
ClearRESTParams(TRESTRequestParameterKind.pkURLSEGMENT);
|
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;
|
|
|
|
|
ClearRESTParams(TRESTRequestParameterKind.pkQUERY);
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
procedure TMVCRESTClient.ClearRESTParams(const aRESTParamKind: TRESTRequestParameterKind);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
var
|
2020-08-04 00:48:35 +02:00
|
|
|
|
I: Integer;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
for I := Pred(fRESTRequest.Params.Count) downto 0 do
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-08-09 02:18:36 +02:00
|
|
|
|
if (fRESTRequest.Params[I].Kind = aRESTParamKind) then
|
2020-08-04 00:48:35 +02:00
|
|
|
|
fRESTRequest.Params.Delete(I);
|
2013-11-11 12:23:49 +01:00
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-20 23:35:28 +02:00
|
|
|
|
function TMVCRESTClient.CloneRESTClient: IMVCRESTClient;
|
|
|
|
|
begin
|
|
|
|
|
Result := TMVCRESTClient.New
|
|
|
|
|
.BaseURL(BaseURL)
|
|
|
|
|
.RaiseExceptionOn500(RaiseExceptionOn500)
|
|
|
|
|
.ProxyServer(ProxyServer)
|
|
|
|
|
.ProxyPort(ProxyPort)
|
|
|
|
|
.ProxyUsername(ProxyUsername)
|
|
|
|
|
.ProxyPassword(ProxyPassword)
|
|
|
|
|
.ProxyServer(ProxyServer)
|
|
|
|
|
.UserAgent(UserAgent)
|
2020-08-21 16:22:23 +02:00
|
|
|
|
.ConnectTimeout(ConnectTimeout)
|
|
|
|
|
.ReadTimeout(ReadTimeout)
|
2020-08-20 23:35:28 +02:00
|
|
|
|
.Accept(Accept)
|
|
|
|
|
.AcceptCharset(AcceptCharset)
|
|
|
|
|
.AcceptEncoding(AcceptEncoding)
|
|
|
|
|
.HandleRedirects(HandleRedirects)
|
2020-08-26 01:07:21 +02:00
|
|
|
|
.FallbackCharsetEncoding(FallbackCharsetEncoding)
|
2020-08-21 16:22:23 +02:00
|
|
|
|
.AllowCookies(AllowCookies)
|
2020-08-20 23:35:28 +02:00
|
|
|
|
.Resource(Resource)
|
|
|
|
|
.URLAlreadyEncoded(URLAlreadyEncoded);
|
2020-08-26 01:07:21 +02:00
|
|
|
|
TMVCRESTClient(Result).fRESTRequest.Method := fRESTRequest.Method;
|
2020-08-20 23:35:28 +02:00
|
|
|
|
TMVCRESTClient(Result).fRESTRequest.Params.Assign(fRESTRequest.Params);
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function TMVCRESTClient.ConnectTimeout(const aConnectTimeout: Integer): IMVCRESTClient;
|
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
|
|
|
|
{$IF defined(SYDNEYORBETTER)}
|
|
|
|
|
fRESTRequest.ConnectTimeout := aConnectTimeout;
|
|
|
|
|
{$ELSE}
|
|
|
|
|
fRESTRequest.Timeout := aConnectTimeout;
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTClient.ConnectTimeout: Integer;
|
|
|
|
|
begin
|
|
|
|
|
{$IF defined(SYDNEYORBETTER)}
|
|
|
|
|
Result := fRESTRequest.ConnectTimeout;
|
|
|
|
|
{$ELSE}
|
|
|
|
|
Result := fRESTRequest.Timeout;
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.ConvertMVCPathParamsToRESTParams(const aResource: string): string;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := TRegEx.Replace(aResource, '(\([($])([\w_]+)([)])', '{\2}', [roIgnoreCase]);
|
|
|
|
|
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-08-04 00:48:35 +02:00
|
|
|
|
fRESTClient := TCustomRESTClient.Create(nil);
|
2020-08-26 01:07:21 +02:00
|
|
|
|
fRESTRequest := THackCustomRESTRequest.Create(nil);
|
2020-08-04 00:48:35 +02:00
|
|
|
|
fRESTResponse := TCustomRESTResponse.Create(nil);
|
2015-12-22 12:17:13 +01:00
|
|
|
|
|
2020-08-04 00:48:35 +02:00
|
|
|
|
fRESTRequest.Client := fRESTClient;
|
|
|
|
|
fRESTRequest.Response := fRESTResponse;
|
2014-04-01 02:12:34 +02:00
|
|
|
|
|
2020-08-04 00:48:35 +02:00
|
|
|
|
fSerializer := TMVCJsonDataObjectsSerializer.Create;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
fRttiContext := TRttiContext.Create;
|
2020-08-20 20:08:41 +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;
|
|
|
|
|
var
|
|
|
|
|
lResource: string;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
2020-08-26 14:05:12 +02:00
|
|
|
|
lResource := aResource + '/' + aKeyValue;
|
|
|
|
|
Result := Delete(lResource);
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +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
|
|
|
|
|
Result := Post(aResource, fSerializer.SerializeDataSetRecord(aDataSet, aIgnoredFields, aNameCase));
|
|
|
|
|
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;
|
|
|
|
|
var
|
|
|
|
|
lResource: string;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
2020-08-26 14:05:12 +02:00
|
|
|
|
lResource := aResource + '/' + aKeyValue;
|
|
|
|
|
|
|
|
|
|
Result := Put(lResource, fSerializer.SerializeDataSetRecord(aDataSet, aIgnoredFields, aNameCase));
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.Delete: IMVCRESTResponse;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := ExecuteRESTRequest(TRESTRequestMethod.rmDELETE);
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.Delete(const aResource: string): IMVCRESTResponse;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
Resource(aResource);
|
|
|
|
|
Result := Delete;
|
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-08-04 00:48:35 +02:00
|
|
|
|
fRESTResponse.Free;
|
|
|
|
|
fRESTRequest.Free;
|
|
|
|
|
fRESTClient.Free;
|
|
|
|
|
fSerializer := nil;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
fRttiContext.Free;
|
2013-11-11 12:23:49 +01:00
|
|
|
|
|
2020-08-04 00:48:35 +02:00
|
|
|
|
inherited Destroy;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-20 20:08:41 +02:00
|
|
|
|
procedure TMVCRESTClient.ExecuteAsyncRESTRequest;
|
2020-08-20 23:35:28 +02:00
|
|
|
|
var
|
|
|
|
|
lMVCRESTClient: IMVCRESTClient;
|
|
|
|
|
lAsyncCompletionHandler: TProc<IMVCRESTResponse>;
|
|
|
|
|
lAsyncSynchronized: Boolean;
|
|
|
|
|
lAsyncCompletionHandlerWithError: TProc<Exception>;
|
|
|
|
|
begin
|
|
|
|
|
// For asynchronous execution, a clone of RESTClient is created to be executed in an anonymous thread
|
|
|
|
|
lMVCRESTClient := CloneRESTClient;
|
|
|
|
|
|
|
|
|
|
lAsyncCompletionHandler := fAsyncCompletionHandler;
|
|
|
|
|
lAsyncSynchronized := fAsyncSynchronized;
|
|
|
|
|
lAsyncCompletionHandlerWithError := fAsyncCompletionHandlerWithError;
|
|
|
|
|
|
|
|
|
|
TThread.CreateAnonymousThread(
|
|
|
|
|
procedure
|
|
|
|
|
var
|
|
|
|
|
lMVCRESTResponse: IMVCRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
try
|
|
|
|
|
TMVCRESTClient(lMVCRESTClient).fRESTRequest.Execute;
|
|
|
|
|
lMVCRESTResponse := TMVCRESTResponse.Create(TMVCRESTClient(lMVCRESTClient).fRESTResponse);
|
2020-08-21 16:22:23 +02:00
|
|
|
|
{$IF not defined(SYDNEYORBETTER)}
|
|
|
|
|
TMVCRESTResponse(lMVCRESTResponse).SetCookies(TMVCRESTClient(lMVCRESTClient).GetResponseCookies);
|
|
|
|
|
{$ENDIF}
|
2020-08-26 01:07:21 +02:00
|
|
|
|
TMonitor.Enter(TObject(lMVCRESTResponse));
|
|
|
|
|
try
|
|
|
|
|
if Assigned(lAsyncCompletionHandler) then
|
|
|
|
|
begin
|
|
|
|
|
if lAsyncSynchronized then
|
|
|
|
|
TThread.Synchronize(TThread.CurrentThread,
|
|
|
|
|
procedure
|
|
|
|
|
begin
|
|
|
|
|
lAsyncCompletionHandler(lMVCRESTResponse)
|
|
|
|
|
end
|
|
|
|
|
)
|
|
|
|
|
else
|
|
|
|
|
lAsyncCompletionHandler(lMVCRESTResponse);
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
TMonitor.Exit(TObject(lMVCRESTResponse));
|
2020-08-20 23:35:28 +02:00
|
|
|
|
end;
|
|
|
|
|
except
|
|
|
|
|
on E: Exception do
|
|
|
|
|
begin
|
|
|
|
|
if Assigned(lAsyncCompletionHandlerWithError) then
|
|
|
|
|
begin
|
|
|
|
|
if fAsyncSynchronized then
|
|
|
|
|
TThread.Synchronize(nil,
|
|
|
|
|
procedure
|
|
|
|
|
begin
|
|
|
|
|
lAsyncCompletionHandlerWithError(E)
|
|
|
|
|
end
|
|
|
|
|
)
|
|
|
|
|
else
|
|
|
|
|
lAsyncCompletionHandlerWithError(E);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end).Start;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.ExecuteRESTRequest(const aMethod: TRESTRequestMethod): IMVCRESTResponse;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
fRESTRequest.Method := aMethod;
|
|
|
|
|
|
2020-08-20 20:08:41 +02:00
|
|
|
|
if fNextRequestIsAsync then
|
|
|
|
|
begin
|
|
|
|
|
Result := nil;
|
|
|
|
|
ExecuteAsyncRESTRequest;
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
fRESTRequest.Execute;
|
|
|
|
|
Result := TMVCRESTResponse.Create(fRESTResponse);
|
2020-08-21 16:22:23 +02:00
|
|
|
|
{$IF not defined(SYDNEYORBETTER)}
|
|
|
|
|
TMVCRESTResponse(Result).SetCookies(GetResponseCookies);
|
|
|
|
|
{$ENDIF}
|
2020-08-26 01:07:21 +02:00
|
|
|
|
ClearAllParams;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
end;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function TMVCRESTClient.FallbackCharsetEncoding: string;
|
|
|
|
|
begin
|
|
|
|
|
Result := fRESTClient.FallbackCharsetEncoding;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTClient.FallbackCharsetEncoding(const aFallbackCharsetEncoding: string): IMVCRESTClient;
|
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
|
|
|
|
fRESTClient.FallbackCharsetEncoding := aFallbackCharsetEncoding;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.Get(const aResource: string): IMVCRESTResponse;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
Resource(aResource);
|
|
|
|
|
Result := ExecuteRESTRequest(TRESTRequestMethod.rmGET);
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-20 20:08:41 +02:00
|
|
|
|
function TMVCRESTClient.HandleRedirects(const aHandleRedirects: Boolean): IMVCRESTClient;
|
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
|
|
|
|
fRESTRequest.HandleRedirects := aHandleRedirects;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTClient.HandleRedirects: Boolean;
|
|
|
|
|
begin
|
|
|
|
|
Result := fRESTRequest.HandleRedirects;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-26 14:05:12 +02:00
|
|
|
|
function TMVCRESTClient.HeaderValue(const aName: string): string;
|
|
|
|
|
var
|
|
|
|
|
lParam: TRESTRequestParameter;
|
|
|
|
|
begin
|
|
|
|
|
Result := '';
|
|
|
|
|
lParam := fRESTRequest.Params.ParameterByName(aName);
|
|
|
|
|
if Assigned(lParam) and (lParam.Kind = TRESTRequestParameterKind.pkHTTPHEADER) then
|
|
|
|
|
Result := lParam.Value;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
class function TMVCRESTClient.New: IMVCRESTClient;
|
2020-08-14 00:43:25 +02:00
|
|
|
|
begin
|
2020-08-18 23:19:17 +02:00
|
|
|
|
Result := TMVCRESTClient.Create;
|
2020-08-14 00:43:25 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.ObjectIsList(aObject: TObject): Boolean;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := fRttiContext.GetType(aObject.ClassType).GetMethod('GetEnumerator') <> nil;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.Get: IMVCRESTResponse;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := ExecuteRESTRequest(TRESTRequestMethod.rmGET);
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-26 01:07:21 +02:00
|
|
|
|
function TMVCRESTClient.Patch(const aResource, aBody: string; const aDoNotEncode: Boolean;
|
2020-08-26 14:05:12 +02:00
|
|
|
|
const aContentType: TRESTContentType): IMVCRESTResponse;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
Resource(aResource);
|
|
|
|
|
if not aBody.isEmpty then
|
|
|
|
|
begin
|
|
|
|
|
ClearBody;
|
2020-08-26 01:07:21 +02:00
|
|
|
|
AddBody(aBody, aDoNotEncode, aContentType);
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
Result := Patch;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.Patch: IMVCRESTResponse;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := ExecuteRESTRequest(TRESTRequestMethod.rmPATCH);
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.Patch(const aResource: string; aBody: TObject; const aOwnsBody: Boolean): IMVCRESTResponse;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
if aBody = nil then
|
2020-08-18 23:19:17 +02:00
|
|
|
|
raise EMVCRESTClientException.Create('You need a valid body!');
|
2020-08-09 02:18:36 +02:00
|
|
|
|
|
|
|
|
|
Result := Patch(aResource, SerializeObject(aBody));
|
|
|
|
|
|
|
|
|
|
if aOwnsBody then
|
|
|
|
|
aBody.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.Post(const aResource: string; aBody: TObject; const aOwnsBody: Boolean): IMVCRESTResponse;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
if aBody = nil then
|
2020-08-18 23:19:17 +02:00
|
|
|
|
raise EMVCRESTClientException.Create('You need a valid body!');
|
2020-08-09 02:18:36 +02:00
|
|
|
|
|
|
|
|
|
Result := Post(aResource, SerializeObject(aBody));
|
|
|
|
|
|
|
|
|
|
if aOwnsBody then
|
|
|
|
|
aBody.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-26 01:07:21 +02:00
|
|
|
|
function TMVCRESTClient.Post(const aResource, aBody: string; const aDoNotEncode: Boolean;
|
2020-08-26 14:05:12 +02:00
|
|
|
|
const aContentType: TRESTContentType): IMVCRESTResponse;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
Resource(aResource);
|
|
|
|
|
if not aBody.IsEmpty then
|
|
|
|
|
begin
|
|
|
|
|
ClearBody;
|
2020-08-26 01:07:21 +02:00
|
|
|
|
AddBody(aBody, aDoNotEncode, aContentType);
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
Result := Post;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.Post: IMVCRESTResponse;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := ExecuteRESTRequest(TRESTRequestMethod.rmPOST);
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.ProxyPassword(const aProxyPassword: string): IMVCRESTClient;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := Self;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
fRESTClient.ProxyPassword := aProxyPassword;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-26 14:05:12 +02:00
|
|
|
|
function TMVCRESTClient.PreEncodeURL(const aURL: string): string;
|
|
|
|
|
begin
|
|
|
|
|
// 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
|
|
|
|
|
Result := aURL;
|
|
|
|
|
Result := Result.Replace('\', '/', [rfReplaceAll]);
|
|
|
|
|
Result := Result.Replace('../', '%2E%2E/', [rfReplaceAll]);
|
|
|
|
|
Result := Result.Replace('./', '%2E/', [rfReplaceAll]);
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.ProxyPassword: string;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := fRESTClient.ProxyPassword;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.ProxyPort: Integer;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := fRESTClient.ProxyPort;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.ProxyPort(const aProxyPort: Integer): IMVCRESTClient;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := Self;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
fRESTClient.ProxyPort := aProxyPort;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.ProxyServer(const aProxyServer: string): IMVCRESTClient;
|
2015-04-01 17:01:23 +02:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := Self;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
fRESTClient.ProxyServer := aProxyServer;
|
2015-04-01 17:01:23 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.ProxyServer: string;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := fRESTClient.ProxyServer;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.ProxyUsername: string;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := fRESTClient.ProxyUsername;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +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-08-09 02:18:36 +02:00
|
|
|
|
fRESTClient.ProxyUsername := aProxyUsername;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.Put(const aResource: string; aBody: TObject; const aOwnsBody: Boolean): IMVCRESTResponse;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
if aBody = nil then
|
2020-08-18 23:19:17 +02:00
|
|
|
|
raise EMVCRESTClientException.Create('You need a valid body!');
|
2020-08-09 02:18:36 +02:00
|
|
|
|
|
|
|
|
|
Result := Put(aResource, SerializeObject(aBody));
|
|
|
|
|
|
|
|
|
|
if aOwnsBody then
|
|
|
|
|
aBody.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-26 01:07:21 +02:00
|
|
|
|
function TMVCRESTClient.Put(const aResource, aBody: string; const aDoNotEncode: Boolean;
|
2020-08-26 14:05:12 +02:00
|
|
|
|
const aContentType: TRESTContentType): IMVCRESTResponse;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
Resource(aResource);
|
|
|
|
|
if not aBody.IsEmpty then
|
|
|
|
|
begin
|
|
|
|
|
ClearBody;
|
2020-08-26 01:07:21 +02:00
|
|
|
|
AddBody(aBody, aDoNotEncode, aContentType);
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
Result := Put;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.Put: IMVCRESTResponse;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := ExecuteRESTRequest(TRESTRequestMethod.rmPUT);
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.RaiseExceptionOn500(const aRaiseExceptionOn500: Boolean): IMVCRESTClient;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
|
|
|
|
fRESTClient.RaiseExceptionOn500 := aRaiseExceptionOn500;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.Resource: string;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := fRESTRequest.Resource;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
{$IF not defined(SYDNEYORBETTER)}
|
2020-08-26 14:05:12 +02:00
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function TMVCRESTClient.GetResponseCookies: TArray<TCookie>;
|
|
|
|
|
var
|
|
|
|
|
lRttiType: TRttiType;
|
|
|
|
|
lRttiField: TRttiField;
|
|
|
|
|
lRestHttp: TRESTHTTP;
|
|
|
|
|
lHttpResponse: IHTTPResponse;
|
|
|
|
|
begin
|
|
|
|
|
SetLength(Result, 0);
|
|
|
|
|
lRttiType := fRttiContext.GetType(fRESTClient.ClassType);
|
|
|
|
|
lRttiField := lRttiType.GetField('FHttpClient');
|
|
|
|
|
if not Assigned(lRttiField) then
|
|
|
|
|
Exit;
|
|
|
|
|
|
2020-08-26 14:05:12 +02:00
|
|
|
|
lRestHttp := lRttiField.GetValue(fRESTClient).AsObject as TRESTHTTP;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
lRttiType := fRttiContext.GetType(lRestHttp.ClassType);
|
|
|
|
|
lRttiField := lRttiType.GetField('FHTTPResponse');
|
|
|
|
|
|
|
|
|
|
if not Assigned(lRttiField) then
|
|
|
|
|
Exit;
|
|
|
|
|
|
|
|
|
|
lHttpResponse := lRttiField.GetValue(lRestHttp).AsInterface as IHTTPResponse;
|
|
|
|
|
|
|
|
|
|
Result := lHttpResponse.Cookies.ToArray;
|
|
|
|
|
end;
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
2020-08-26 14:05:12 +02:00
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.RegisterTypeSerializer(const aTypeInfo: PTypeInfo;
|
2020-08-20 23:35:28 +02:00
|
|
|
|
aInstance: IMVCTypeSerializer): IMVCRESTClient;
|
2020-08-18 23:19:17 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
|
|
|
|
fSerializer.RegisterTypeSerializer(aTypeInfo, aInstance);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTClient.Resource(const aResource: string): IMVCRESTClient;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
|
2020-08-26 14:05:12 +02:00
|
|
|
|
fRESTRequest.Resource := PreEncodeURL(ConvertMVCPathParamsToRESTParams(aResource));
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.RaiseExceptionOn500: Boolean;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := fRESTClient.RaiseExceptionOn500;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function TMVCRESTClient.ReadTimeout: Integer;
|
2018-10-24 23:07:31 +02:00
|
|
|
|
begin
|
2020-08-21 16:22:23 +02:00
|
|
|
|
{$IF defined(SYDNEYORBETTER)}
|
|
|
|
|
Result := fRESTRequest.ReadTimeout;
|
|
|
|
|
{$ELSE}
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := fRESTRequest.Timeout;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
{$ENDIF}
|
2018-10-24 23:07:31 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
function TMVCRESTClient.ReadTimeout(const aReadTimeout: Integer): IMVCRESTClient;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := Self;
|
2020-08-21 16:22:23 +02:00
|
|
|
|
{$IF defined(SYDNEYORBETTER)}
|
|
|
|
|
fRESTRequest.ReadTimeout := aReadTimeout;
|
|
|
|
|
{$ELSE}
|
|
|
|
|
fRESTRequest.Timeout := aReadTimeout;
|
|
|
|
|
{$ENDIF}
|
2020-08-09 02:18:36 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-20 20:08:41 +02:00
|
|
|
|
function TMVCRESTClient.URLAlreadyEncoded(const aURLAlreadyEncoded: Boolean): IMVCRESTClient;
|
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
|
|
|
|
fRESTRequest.URLAlreadyEncoded := aURLAlreadyEncoded;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTClient.UserAgent(const aUserAgent: string): IMVCRESTClient;
|
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
|
|
|
|
fRESTClient.UserAgent := aUserAgent;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTClient.UserAgent: string;
|
|
|
|
|
begin
|
|
|
|
|
Result := fRESTClient.UserAgent;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-20 20:38:32 +02:00
|
|
|
|
function TMVCRESTClient.URLAlreadyEncoded: Boolean;
|
2020-08-20 20:08:41 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := fRESTRequest.URLAlreadyEncoded;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-26 22:13:18 +02:00
|
|
|
|
function TMVCRESTClient.SecureProtocols: THTTPSecureProtocols;
|
|
|
|
|
begin
|
|
|
|
|
Result := fRESTClient.SecureProtocols;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRESTClient.SecureProtocols(const aSecureProtocols: THTTPSecureProtocols): IMVCRESTClient;
|
|
|
|
|
begin
|
|
|
|
|
Result := Self;
|
|
|
|
|
fRESTClient.SecureProtocols := aSecureProtocols;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.SerializeObject(aObject: TObject): string;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
if ObjectIsList(aObject) then
|
|
|
|
|
Result := fSerializer.SerializeCollection(aObject)
|
|
|
|
|
else
|
|
|
|
|
Result := fSerializer.SerializeObject(aObject);
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.SetBasicAuthorization(const aUsername, aPassword: string): IMVCRESTClient;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
var
|
2020-08-04 00:48:35 +02:00
|
|
|
|
LBase64: TNetEncoding;
|
|
|
|
|
LAuthValue: string;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := Self;
|
|
|
|
|
// Do not use TNetEncoding.Base64 here, because it may break long line
|
|
|
|
|
LBase64 := TBase64Encoding.Create(0, '');
|
2016-04-24 19:08:21 +02:00
|
|
|
|
try
|
2020-08-21 00:00:54 +02:00
|
|
|
|
LAuthValue := BASIC_AUTH_PREFIX + LBase64.Encode(aUsername + ':' + aPassword);
|
2016-04-24 19:08:21 +02:00
|
|
|
|
finally
|
2020-08-04 00:48:35 +02:00
|
|
|
|
LBase64.Free;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
2020-08-21 00:00:54 +02:00
|
|
|
|
AddHeader(AUTHORIZATION_HEADER, LAuthValue, True)
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTClient.SetBearerAuthorization(const aToken: string): IMVCRESTClient;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := Self;
|
2020-08-21 00:00:54 +02:00
|
|
|
|
AddHeader(AUTHORIZATION_HEADER, BEARER_AUTH_PREFIX + aToken, True);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
{ TMVCRESTResponse }
|
2015-12-22 12:17:13 +01:00
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTResponse.Content: string;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := fContent;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTResponse.ContentEncoding: string;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := fContentEncoding;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTResponse.ContentLength: Integer;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := fContentLength;
|
2015-12-18 20:59:40 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTResponse.ContentType: string;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := fContentType;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-26 01:07:21 +02:00
|
|
|
|
function TMVCRESTResponse.CookieByName(const aName: string): TCookie;
|
|
|
|
|
var
|
|
|
|
|
lCookie: TCookie;
|
|
|
|
|
begin
|
2020-08-26 14:05:12 +02:00
|
|
|
|
Result := Default (TCookie);
|
2020-08-26 01:07:21 +02:00
|
|
|
|
for lCookie in fCookies do
|
|
|
|
|
begin
|
|
|
|
|
if SameText(lCookie.Name, aName) then
|
2020-08-26 14:05:12 +02:00
|
|
|
|
Exit(lCookie);
|
2020-08-26 01:07:21 +02:00
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-21 02:45:51 +02:00
|
|
|
|
function TMVCRESTResponse.Cookies: TCookies;
|
|
|
|
|
begin
|
|
|
|
|
Result := fCookies;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
constructor TMVCRESTResponse.Create(aRESTResponse: TCustomRESTResponse);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
inherited Create;
|
|
|
|
|
fHeaders := TStringList.Create;
|
|
|
|
|
SetLength(fRawBytes, 0);
|
2020-08-21 02:45:51 +02:00
|
|
|
|
fCookies := TCookies.Create;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
|
|
|
|
|
FillRESTResponse(aRESTResponse);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
destructor TMVCRESTResponse.Destroy;
|
2015-04-01 17:01:23 +02:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
SetLength(fRawBytes, 0);
|
|
|
|
|
fHeaders.Free;
|
2020-08-21 02:45:51 +02:00
|
|
|
|
fCookies.Free;
|
2020-08-04 00:48:35 +02:00
|
|
|
|
inherited Destroy;
|
2015-04-01 17:01:23 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTResponse.ErrorMessage: string;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-08-09 02:18:36 +02:00
|
|
|
|
Result := fErrorMessage;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
procedure TMVCRESTResponse.FillRESTResponse(aRESTResponse: TCustomRESTResponse);
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
fSuccess := aRESTResponse.Status.Success;
|
|
|
|
|
fStatusCode := aRESTResponse.StatusCode;
|
|
|
|
|
fStatusText := aRESTResponse.StatusText;
|
|
|
|
|
fErrorMessage := aRESTResponse.ErrorMessage;
|
2020-08-14 00:43:25 +02:00
|
|
|
|
fHeaders.Assign(aRESTResponse.Headers);
|
2020-08-21 02:45:51 +02:00
|
|
|
|
{$IF defined(SYDNEYORBETTER)}
|
|
|
|
|
fCookies.AddRange(aRESTResponse.Cookies.ToArray);
|
|
|
|
|
{$ENDIF}
|
2020-08-09 02:18:36 +02:00
|
|
|
|
fServer := aRESTResponse.Server;
|
|
|
|
|
fFullRequestURI := aRESTResponse.FullRequestURI;
|
|
|
|
|
fContent := aRESTResponse.Content;
|
|
|
|
|
fRawBytes := aRESTResponse.RawBytes;
|
|
|
|
|
fContentType := aRESTResponse.ContentType;
|
|
|
|
|
fContentEncoding := aRESTResponse.ContentEncoding;
|
|
|
|
|
fContentLength := aRESTResponse.ContentLength;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTResponse.FullRequestURI: string;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := fFullRequestURI;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-26 01:07:21 +02:00
|
|
|
|
function TMVCRESTResponse.HeaderValue(const aName: string): string;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := fHeaders.Values[aName];
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTResponse.Headers: TStrings;
|
2016-03-03 17:07:34 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := fHeaders;
|
2016-03-03 17:07:34 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTResponse.RawBytes: TBytes;
|
2016-03-03 17:07:34 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := fRawBytes;
|
2016-03-03 17:07:34 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
procedure TMVCRESTResponse.SaveContentToFile(const aFileName: string);
|
2020-08-09 02:18:36 +02:00
|
|
|
|
var
|
|
|
|
|
lStream: TMemoryStream;
|
2018-03-10 23:50:44 +01:00
|
|
|
|
begin
|
2020-08-09 02:18:36 +02:00
|
|
|
|
lStream := TMemoryStream.Create;
|
|
|
|
|
try
|
|
|
|
|
lStream.Write(fRawBytes, Length(fRawBytes));
|
|
|
|
|
lStream.Position := 0;
|
|
|
|
|
lStream.SaveToFile(aFileName);
|
|
|
|
|
finally
|
|
|
|
|
lStream.Free;
|
|
|
|
|
end;
|
2018-03-10 23:50:44 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
procedure TMVCRESTResponse.SaveContentToStream(aStream: TStream);
|
2018-03-10 23:50:44 +01:00
|
|
|
|
begin
|
2020-08-09 02:18:36 +02:00
|
|
|
|
if aStream = nil then
|
2020-08-18 23:19:17 +02:00
|
|
|
|
raise EMVCRESTClientException.Create('Stream not assigned!');
|
2020-08-09 02:18:36 +02:00
|
|
|
|
|
|
|
|
|
aStream.Write(fRawBytes, Length(fRawBytes));
|
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTResponse.Server: string;
|
2020-08-09 02:18:36 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := fServer;
|
2018-03-10 23:50:44 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
{$IF not defined(SYDNEYORBETTER)}
|
2020-08-26 14:05:12 +02:00
|
|
|
|
|
2020-08-21 16:22:23 +02:00
|
|
|
|
procedure TMVCRESTResponse.SetCookies(aCookies: TArray<TCookie>);
|
|
|
|
|
var
|
|
|
|
|
i: Integer;
|
|
|
|
|
lCookies: string;
|
|
|
|
|
begin
|
|
|
|
|
fCookies.AddRange(aCookies);
|
|
|
|
|
|
2020-08-26 14:05:12 +02:00
|
|
|
|
if (fHeaders.IndexOfName(HEADER_RESPONSE_COOKIES) = - 1) and (fCookies.Count > 0) then
|
2020-08-21 16:22:23 +02:00
|
|
|
|
begin
|
|
|
|
|
lCookies := '';
|
|
|
|
|
for i := 0 to fCookies.Count - 1 do
|
|
|
|
|
lCookies := lCookies + '; ' + fCookies[i].ToString;
|
|
|
|
|
fHeaders.Add(HEADER_RESPONSE_COOKIES + '=' + lCookies.Substring(2));
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
2020-08-26 14:05:12 +02:00
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTResponse.StatusCode: Integer;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := fStatusCode;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTResponse.StatusText: string;
|
2013-11-05 14:57:50 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := fStatusText;
|
2013-11-05 14:57:50 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-18 23:19:17 +02:00
|
|
|
|
function TMVCRESTResponse.Success: Boolean;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-08-04 00:48:35 +02:00
|
|
|
|
Result := fSuccess;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-08-26 01:07:21 +02:00
|
|
|
|
{ THackCustomRESTRequest }
|
|
|
|
|
|
|
|
|
|
procedure THackCustomRESTRequest.DoApplyURLSegments(const aParamList: TRESTRequestParameterArray; var aURL: string);
|
|
|
|
|
var
|
|
|
|
|
lParameter: TRESTRequestParameter;
|
|
|
|
|
lReplace: string;
|
|
|
|
|
lEncodedParam: string;
|
|
|
|
|
begin
|
|
|
|
|
for lParameter in aParamList do
|
|
|
|
|
if lParameter.Kind = TRESTRequestParameterKind.pkURLSEGMENT then
|
|
|
|
|
begin
|
|
|
|
|
lReplace := '{' + lParameter.Name + '}';
|
|
|
|
|
lEncodedParam := TNetEncoding.URL.Encode(lParameter.Value, PATH_UNSAFE_CHARS,
|
|
|
|
|
[TURLEncoding.TEncodeOption.EncodePercent]);
|
|
|
|
|
|
|
|
|
|
aURL := StringReplace(aURL, lReplace, lEncodedParam, [rfReplaceAll, rfIgnoreCase]);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end.
|