-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
ksAwsHttpIntf.pas
211 lines (181 loc) · 7.08 KB
/
ksAwsHttpIntf.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
{*******************************************************************************
* *
* ksAwsHttpIntf - ksAws HTTP Interface *
* *
* https://github.com/gmurt/ksAws *
* *
* Copyright 2020 Graham Murt *
* *
* email: [email protected] *
* *
* 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. *
* *
*******************************************************************************}
unit ksAwsHttpIntf;
interface
uses Classes;
type
TksApiHttpImplementation = (ksAwsHttpNetClient, ksAwsHttpIndy);
IksAwsHttpResponse = interface
['{4703F21B-BD13-42EE-A1DC-40742F8F2469}']
function GetContentStream: TStream;
function GetContentAsString: string;
function GetETag: string;
function GetLastModified: string;
function GetStatusCode: integer;
function GetHeaderValue(AName: string): string;
procedure SetContentStream(const Value: TStream);
procedure SetETag(const Value: string);
procedure SetLastModified(const Value: string);
procedure SetStatusCode(const Value: integer);
procedure SetHeaderValue(AName: string; const Value: string);
property ContentStream: TStream read GetContentStream write SetContentStream;
property StatusCode: integer read GetStatusCode write SetStatusCode;
property ETag: string read GetETag write SetETag;
property LastModified: string read GetLastModified write SetLastModified;
property ContentAsString: string read GetContentAsString;
property HeaderValue[AName: string]: string read GetHeaderValue write SetHeaderValue;
end;
IksAwsHttp = interface
['{AFEEA848-BDE5-423B-8144-1DA0A7A1F036}']
function Head(AUrl: string; AHeaders: TStrings): IksAwsHttpResponse;
function Get(AUrl: string; AHeaders: TStrings; const AResponseStream: TStream = nil): IksAwsHttpResponse;
function Put(AUrl: string; APayload: TStream; AHeaders: TStrings; const AResponseStream: TStream = nil): IksAwsHttpResponse;
function Post(AUrl, APayload: string; AHeaders: TStrings; const AResponseStream: TStream = nil): IksAwsHttpResponse; overload;
function Post(AUrl: string; APayload: TStream; AHeaders: TStrings; const AResponseStream: TStream = nil): IksAwsHttpResponse; overload;
function Delete(AUrl: string; AHeaders: TStrings; const AResponseStream: TStream = nil): IksAwsHttpResponse;
end;
function CreateAwsHttp: IksAwsHttp;
function CreateAwsHttpResponse: IksAwsHttpResponse;
implementation
{$I include.inc}
uses
SysUtils,
{$IFDEF USE_INDY}
ksAwsHttpIndy;
{$ELSE}
ksAwsHttpNetClient;
{$ENDIF}
type
TksAwsHttpResponse = class(TInterfacedObject, IksAwsHttpResponse)
private
FContentStream: TStream;
FStatusCode: integer;
FETag: string;
FLastModified: string;
FHeaderValues: TStrings;
function GetContentStream: TStream;
function GetETag: string;
function GetLastModified: string;
function GetStatusCode: integer;
procedure SetContentStream(const Value: TStream);
procedure SetETag(const Value: string);
procedure SetLastModified(const Value: string);
procedure SetStatusCode(const Value: integer);
function GetContentAsString: string;
function GetHeaderValue(AName: string): string;
procedure SetHeaderValue(AName: string; const Value: string);
public
constructor Create; virtual;
destructor Destroy; override;
property ContentStream: TStream read GetContentStream write SetContentStream;
property StatusCode: integer read GetStatusCode write SetStatusCode;
property ETag: string read GetETag write SetETag;
property LastModified: string read GetLastModified write SetLastModified;
property ContentAsString: string read GetContentAsString;
property HeaderValue[AName: string]: string read GetHeaderValue write SetHeaderValue;
end;
function CreateAwsHttp: IksAwsHttp;
begin
{$IFDEF USE_INDY}
Result := CreateksAwsHttpIndy;
{$ELSE}
Result := CreateksAwsHttpNetClient;
{$ENDIF}
end;
function CreateAwsHttpResponse: IksAwsHttpResponse;
begin
Result := TksAwsHttpResponse.Create;
end;
{ TksAwsHttpResponse }
constructor TksAwsHttpResponse.Create;
begin
FContentStream := TMemoryStream.Create;
FHeaderValues := TStringList.Create;
end;
destructor TksAwsHttpResponse.Destroy;
begin
FContentStream.Free;
FHeaderValues.Free;
inherited;
end;
function TksAwsHttpResponse.GetContentAsString: string;
var
AStream: TStringStream;
begin
AStream := TStringStream.Create;
try
FContentStream.Position := 0;
AStream.CopyFrom(FContentStream, FContentStream.Size);
Result := AStream.DataString;
finally
AStream.Free;
end;
end;
function TksAwsHttpResponse.GetContentStream: TStream;
begin
Result := FContentStream;
end;
function TksAwsHttpResponse.GetETag: string;
begin
Result := FETag;
end;
function TksAwsHttpResponse.GetHeaderValue(AName: string): string;
begin
Result := FHeaderValues.Values[AName];
end;
function TksAwsHttpResponse.GetLastModified: string;
begin
Result := FLastModified;
end;
function TksAwsHttpResponse.GetStatusCode: integer;
begin
Result := FStatusCode;
end;
procedure TksAwsHttpResponse.SetContentStream(const Value: TStream);
begin
TMemoryStream(FContentStream).Clear;
if Value <> nil then
begin
Value.Position := 0;
FContentStream.CopyFrom(Value, Value.Size);
FContentStream.Position := 0;
end;
end;
procedure TksAwsHttpResponse.SetETag(const Value: string);
begin
FETag := Value;
end;
procedure TksAwsHttpResponse.SetHeaderValue(AName: string; const Value: string);
begin
FHeaderValues.Values[AName] := Value;
end;
procedure TksAwsHttpResponse.SetLastModified(const Value: string);
begin
FLastModified := Value;
end;
procedure TksAwsHttpResponse.SetStatusCode(const Value: integer);
begin
FStatusCode := Value;
end;
end.