This repository was archived by the owner on Mar 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttps.pas
227 lines (200 loc) · 6.58 KB
/
https.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
unit https;
{ coolsms class interface를 만들어 아래 implementation에서 구현 }
interface
uses
IdURI, System.Json, IdHash, IdHMACMD5, IDGlobal, DateUtils, IdException, IdHTTP, IdSSLOpenSSL, IdMultipartFormData, Classes, System.SysUtils;
type
handler = class
private
class var api_key: String;
class var api_secret: String;
published
{ set api_key and api_secret }
Constructor Create(key: String; secret: String);
public
{ request(post) }
class function postRequest(url: String; data: TStringList): TJSONObject;
{ request(get) }
class function request(url: String; data: TStringList): TJSONObject;
end;
implementation
{ api request를 위해 Adata 값을 md5로 encrypt }
function hashHmacMd5(const AData, AKey: string): string;
var
HMACMD5: TIdHMACMD5;
KeyBytes, DataBytes, ResBytes: TIdBytes;
i: integer;
begin
HMACMD5 := TIdHMACMD5.Create;
try
HMACMD5.Key := ToBytes(AKey);
ResBytes := HMACMD5.HashValue(ToBytes(AData));
Result := '';
for i := low(ResBytes) to high(ResBytes) do
Result := Result + inttohex(ResBytes[i], 2);
finally
HMACMD5.Free;
end;
end;
{ get random string }
function randomString(): string;
var
str: string;
begin
Randomize;
//string with all possible chars
str := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
Result := '';
repeat
Result := Result + str[Random(Length(str)) + 1];
until (Length(Result) = 10)
end;
{ image를 제외한 string data 들을 data에 delimiter로 분리해서 넣는다 }
function split(delimiter: Char; input: string; data: TIdMultiPartFormDataStream): TIdMultiPartFormDataStream ;
var
string_list: TStringList;
begin
string_list := TSTringList.Create;
string_list.Delimiter := delimiter;
string_list.DelimitedText := input;
// 해당 string data가 image가 아니면 data에 add
if string_list[0] <> 'image' then
begin
data.AddFormField(string_list[0], string_list[1]);
end;
string_list.Clear;
Result := data;
end;
{ set basic request data }
function setBasicReuqestData(data: TStringList): TStringList;
var
signature, salt, timestamp: String;
begin
salt := randomString();
timestamp := IntToStr(DateTimetoUnix(IncHour(Now, -9)));
signature := hashHmacMd5(timestamp + salt, handler.api_secret);
data.Values['api_key'] := handler.api_key;
data.Values['salt'] := salt;
data.Values['signature'] := signature;
data.Values['timestamp'] := timestamp;
Result := data;
end;
{ request(POST) }
class function handler.postRequest(url: String; data: TStringList): TJSONObject;
var
params: TStringList;
http: TIdHTTP;
idSSLIOHandlerSocketOpenSSL: TIdSSLIOHandlerSocketOpenSSL;
jsonObject: TJSONObject;
response, data_string: String;
multi_data: TIdMultiPartFormDataStream;
begin
params := TStringList.Create;
http := TIdHTTP.Create();
jsonObject := TJSonObject.Create;
idSSLIOHandlerSocketOpenSSL := TIdSSLIOHandlerSocketOpenSSL.Create;
try
// 기본설정
data := setBasicReuqestData(data);
http.IOHandler := idSSLIOHandlerSocketOpenSSL;
http.Request.ContentType := 'application/x-www-form-urlencoded';
http.Request.UserAgent := 'Coolsms REST API/ DelPhi SDK 1.0';
// image가 있으면 TStringList가 아닌 TIdMultiPartFormDataStream으로 data를 전송한다.
if data.Values['image'].IsEmpty = FALSE then
begin
multi_data := TIdMultiPartFormDataStream.Create;
for data_string in data do
multi_data := split('=', data_string, multi_data);
multi_data.AddFile('image', data.Values['image']);
response := http.Post(url, multi_data);
end
else
begin
response := http.Post(url, data);
end;
if response.Equals('') = FALSE then
begin
// response가 있다면
jsonObject := TJSONObject.ParseJSONValue(response) as TJSONObject;
end;
jsonObject.AddPair(TJSONPair.Create('status', TJSONTrue.Create));
Result := jsonObject;
except
// http exception 처리
on E: EIdHTTPProtocolException do
begin
jsonObject := TJSONObject.ParseJSONValue(E.ErrorMessage) as TJSONObject;
jsonObject.AddPair(TJSONPair.Create('status', TJSONFalse.Create));
Result := jsonObject;
end;
// 그외 exception 처리
on E: Exception do
begin
jsonObject.AddPair(TJSONPair.Create('message', E.Message));
jsonObject.AddPair(TJSONPair.Create('status', TJSONFalse.Create));
Result := jsonObject;
end;
end;
http.Free;
end;
{ request(GET) }
class function handler.request(url: String; data: TStringList): TJSONObject;
var
http: TIdHTTP;
jsonObject: TJSONObject;
data_string, response: String;
jsonArray: TJSONArray;
begin
http := TIdHTTP.Create();
jsonObject := TJSONObject.Create;
try
if data = nil then data := TStringList.Create;
// 기본설정
data := setBasicReuqestData(data);
for data_string in data Do
url := url + data_string + '&';
delete(url, length(url), 1);
url := TIdURI.URLEncode(url);
response := http.get(url);
try
jsonObject := TJSONObject.ParseJSONValue(response) as TJSONObject;
jsonObject.AddPair(TJSONPair.Create('status', TJSONTrue.Create));
except
begin
try
jsonArray := TJSONObject.ParseJSONValue(response) as TJSONArray;
jsonObject.AddPair('data', jsonArray);
jsonObject.AddPair(TJSONPair.Create('status', TJSONTrue.Create));
except
on E: Exception do
begin
jsonObject.AddPair(TJSONPair.Create('status', TJSONFalse.Create));
jsonObject.AddPair('message', E.Message);
end;
end;
end;
end;
Result := jsonObject;
except
on E: EIdHTTPProtocolException do
begin
jsonObject := TJSONObject.ParseJSONValue(E.ErrorMessage) as TJSONObject;
jsonObject.AddPair(TJSONPair.Create('status', TJSONFalse.Create));
Result := jsonObject;
end;
on E: Exception do
begin
jsonObject.AddPair(TJSONPair.Create('message', E.Message));
jsonObject.AddPair(TJSONPair.Create('status', TJSONFalse.Create));
Result := jsonObject;
end;
end;
http.Free;
end;
{ set api_key and api_secret }
constructor handler.Create(key: String; secret: String);
begin
api_key := key;
api_secret := secret;
end;
end.