forked from Al-Muhandis/fp-telegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtgbasehttpclient.pas
101 lines (85 loc) · 4.15 KB
/
tgbasehttpclient.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
unit tgbasehttpclient;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
type
EHTTPClient = class(Exception);
TBaseClientClass = class of TBaseHTTPClient;
{ TBaseHTTPClient }
TBaseHTTPClient=class(TComponent)
private
FUserAgent: String;
protected
function GetAllowRedirect: Boolean; virtual; abstract;
function GetCookies: TStrings; virtual; abstract;
function GetHTTPProxyHost: String; virtual; abstract;
function GetHTTPProxyPassword: String; virtual; abstract;
function GetHTTPProxyPort: Word; virtual; abstract;
function GetHTTPProxyUsername: String; virtual; abstract;
function GetInternalHTTPClient: TObject; virtual; abstract;
function GetRequestBody: TStream; virtual; abstract;
function GetRequestHeaders: TStrings; virtual; abstract;
function GetResponseHeaders: TStrings; virtual; abstract;
function GetResponseStatusCode: Integer; virtual; abstract;
function GetResponseStatusText: String; virtual; abstract;
function GetIOTimeout: Integer; virtual; abstract;
procedure SetAllowRedirect(AValue: Boolean); virtual; abstract;
procedure SetCookies(AValue: TStrings); virtual; abstract;
procedure SetHTTPProxyHost(AValue: String); virtual; abstract;
procedure SetHTTPProxyPassword(AValue: String); virtual; abstract;
procedure SetHTTPProxyPort(AValue: Word); virtual; abstract;
procedure SetHTTPProxyUsername(AValue: String); virtual; abstract;
procedure SetRequestBody(AValue: TStream); virtual; abstract;
procedure SetRequestHeaders(AValue: TStrings); virtual; abstract;
procedure SetIOTimeout(AValue: Integer); virtual; abstract;
public
procedure AddHeader(Const AHeader,AValue : String); virtual; abstract;
class function EncodeUrlElement(S: String): String; virtual; abstract;
procedure FileFormPost(const AURL: string; FormData: TStrings; AFieldName, AFileName: string;
const Response: TStream); virtual; abstract;
function FormPost(const URL: string; FormData : TStrings): String; virtual; abstract;
function Get(const AUrl: String): String; virtual; abstract;
function Post(const URL: string) : String; virtual; abstract;
procedure StreamFormPost(const AURL: string; FormData: TStrings;
const AFieldName, AFileName: string; const AStream: TStream;
const Response: TStream); virtual; abstract;
class function GetClientClass: TBaseClientClass;
class procedure RegisterClientClass;
class procedure UnregisterClientClass;
property AllowRedirect: Boolean read GetAllowRedirect write SetAllowRedirect;
property Cookies: TStrings read GetCookies write SetCookies;
property RequestBody: TStream read GetRequestBody write SetRequestBody;
property RequestHeaders: TStrings read GetRequestHeaders write SetRequestHeaders;
property ResponseHeaders: TStrings read GetResponseHeaders;
property ResponseStatusCode: Integer read GetResponseStatusCode;
property ResponseStatusText: String read GetResponseStatusText;
property HTTPProxyHost: String read GetHTTPProxyHost write SetHTTPProxyHost;
property HTTPProxyPort: Word read GetHTTPProxyPort write SetHTTPProxyPort;
property HTTPProxyUsername: String read GetHTTPProxyUsername write SetHTTPProxyUsername;
property HTTPProxyPassword: String read GetHTTPProxyPassword write SetHTTPProxyPassword;
Property IOTimeout : Integer read GetIOTimeout write SetIOTimeout;
property InternalHTTPClient: TObject read GetInternalHTTPClient;
property UserAgent: String read FUserAgent write FUserAgent;
end;
implementation
var
_BaseHTTPClientClass: TBaseClientClass = nil;
{ TBaseHTTPClient }
class procedure TBaseHTTPClient.RegisterClientClass;
begin
if Assigned(_BaseHTTPClientClass) then
raise EHTTPClient.Create('HTTP client class already registered!');
_BaseHTTPClientClass := Self;
end;
class procedure TBaseHTTPClient.UnregisterClientClass;
begin
_BaseHTTPClientClass := nil;
end;
class function TBaseHTTPClient.GetClientClass: TBaseClientClass;
begin
if not Assigned(_BaseHTTPClientClass) then
raise EHTTPClient.Create('No HTTP client class registered! Please use RegisterClientClass procedure');
Result:=_BaseHTTPClientClass;
end;
end.