1
+ #include " HTTPRequest.h"
2
+ #include " ../swiftly.h"
3
+
4
+ #define NOT_SUPPORTED (func_name ) print(" [Swiftly] This version of Swiftly is not supporting %s.\n " , func_name)
5
+
6
+ HTTPRequest::HTTPRequest (const char *domain)
7
+ {
8
+ void *http_CreateRequest = FetchFunctionPtr (nullptr , " scripting_HTTP_CreateRequest" );
9
+ if (http_CreateRequest)
10
+ this ->requestID = reinterpret_cast <HTTP_CreateRequest>(http_CreateRequest)(domain);
11
+ else
12
+ NOT_SUPPORTED (" scripting_HTTP_CreateRequest" );
13
+ }
14
+
15
+ HTTPRequest *HTTPRequest::SetBody (const char *body)
16
+ {
17
+ if (this ->requestID == 0 )
18
+ {
19
+ print (" [Swiftly] You can't use %s because the request couldn't be created.\n " , __FUNCTION__);
20
+ return this ;
21
+ }
22
+
23
+ void *http_SetBody = FetchFunctionPtr (nullptr , " scripting_HTTP_SetBody" );
24
+ if (http_SetBody)
25
+ reinterpret_cast <HTTP_SetBody>(http_SetBody)(this ->requestID , body);
26
+ else
27
+ NOT_SUPPORTED (" scripting_HTTP_SetBody" );
28
+
29
+ return this ;
30
+ }
31
+
32
+ HTTPRequest *HTTPRequest::AddHeader (const char *key, const char *value)
33
+ {
34
+ if (this ->requestID == 0 )
35
+ {
36
+ print (" [Swiftly] You can't use %s because the request couldn't be created.\n " , __FUNCTION__);
37
+ return this ;
38
+ }
39
+
40
+ void *http_AddHeader = FetchFunctionPtr (nullptr , " scripting_HTTP_AddHeader" );
41
+ if (http_AddHeader)
42
+ reinterpret_cast <HTTP_AddHeader>(http_AddHeader)(this ->requestID , key, value);
43
+ else
44
+ NOT_SUPPORTED (" scripting_HTTP_AddHeader" );
45
+
46
+ return this ;
47
+ }
48
+
49
+ HTTPRequest *HTTPRequest::DeleteHeader (const char *key)
50
+ {
51
+ if (this ->requestID == 0 )
52
+ {
53
+ print (" [Swiftly] You can't use %s because the request couldn't be created.\n " , __FUNCTION__);
54
+ return this ;
55
+ }
56
+
57
+ void *http_DeleteHeader = FetchFunctionPtr (nullptr , " scripting_HTTP_DeleteHeader" );
58
+ if (http_DeleteHeader)
59
+ reinterpret_cast <HTTP_DeleteHeader>(http_DeleteHeader)(this ->requestID , key);
60
+ else
61
+ NOT_SUPPORTED (" scripting_HTTP_DeleteHeader" );
62
+
63
+ return this ;
64
+ }
65
+
66
+ HTTPRequest *HTTPRequest::AddMultipartFile (const char *field, const char *content, const char *filename, const char *file_content_type)
67
+ {
68
+ if (this ->requestID == 0 )
69
+ {
70
+ print (" [Swiftly] You can't use %s because the request couldn't be created.\n " , __FUNCTION__);
71
+ return this ;
72
+ }
73
+
74
+ void *http_AddMultipartFile = FetchFunctionPtr (nullptr , " scripting_HTTP_AddMultipartFile" );
75
+ if (http_AddMultipartFile)
76
+ reinterpret_cast <HTTP_AddMultipartFile>(http_AddMultipartFile)(this ->requestID , field, content, filename, file_content_type);
77
+ else
78
+ NOT_SUPPORTED (" scripting_HTTP_AddMultipartFile" );
79
+
80
+ return this ;
81
+ }
82
+
83
+ HTTPRequest *HTTPRequest::SetContentType (ContentType content_type)
84
+ {
85
+ if (this ->requestID == 0 )
86
+ {
87
+ print (" [Swiftly] You can't use %s because the request couldn't be created.\n " , __FUNCTION__);
88
+ return this ;
89
+ }
90
+
91
+ void *http_SetContentType = FetchFunctionPtr (nullptr , " scripting_HTTP_SetContentType" );
92
+ if (http_SetContentType)
93
+ reinterpret_cast <HTTP_SetContentType>(http_SetContentType)(this ->requestID , content_type);
94
+ else
95
+ NOT_SUPPORTED (" scripting_HTTP_SetContentType" );
96
+
97
+ return this ;
98
+ }
99
+
100
+ HTTPRequest *HTTPRequest::SetBasicAuthentication (const char *username, const char *password)
101
+ {
102
+ if (this ->requestID == 0 )
103
+ {
104
+ print (" [Swiftly] You can't use %s because the request couldn't be created.\n " , __FUNCTION__);
105
+ return this ;
106
+ }
107
+
108
+ void *http_SetBasicAuthentication = FetchFunctionPtr (nullptr , " scripting_HTTP_SetBasicAuthentication" );
109
+ if (http_SetBasicAuthentication)
110
+ reinterpret_cast <HTTP_SetBasicAuthentication>(http_SetBasicAuthentication)(this ->requestID , username, password);
111
+ else
112
+ NOT_SUPPORTED (" scripting_HTTP_SetBasicAuthentication" );
113
+
114
+ return this ;
115
+ }
116
+
117
+ HTTPRequest *HTTPRequest::SetBearerAuthenticationToken (const char *token)
118
+ {
119
+ if (this ->requestID == 0 )
120
+ {
121
+ print (" [Swiftly] You can't use %s because the request couldn't be created.\n " , __FUNCTION__);
122
+ return this ;
123
+ }
124
+
125
+ void *http_SetBearerAuthenticationToken = FetchFunctionPtr (nullptr , " scripting_HTTP_SetBearerAuthenticationToken" );
126
+ if (http_SetBearerAuthenticationToken)
127
+ reinterpret_cast <HTTP_SetBearerAuthenticationToken>(http_SetBearerAuthenticationToken)(this ->requestID , token);
128
+ else
129
+ NOT_SUPPORTED (" scripting_HTTP_SetBearerAuthenticationToken" );
130
+
131
+ return this ;
132
+ }
133
+
134
+ HTTPRequest *HTTPRequest::SetFollowRedirect (bool follow)
135
+ {
136
+ if (this ->requestID == 0 )
137
+ {
138
+ print (" [Swiftly] You can't use %s because the request couldn't be created.\n " , __FUNCTION__);
139
+ return this ;
140
+ }
141
+
142
+ void *http_SetFollowRedirect = FetchFunctionPtr (nullptr , " scripting_HTTP_SetFollowRedirect" );
143
+ if (http_SetFollowRedirect)
144
+ reinterpret_cast <HTTP_SetFollowRedirect>(http_SetFollowRedirect)(this ->requestID , follow);
145
+ else
146
+ NOT_SUPPORTED (" scripting_HTTP_SetFollowRedirect" );
147
+
148
+ return this ;
149
+ }
150
+
151
+ const char *HTTPRequest::GetBody ()
152
+ {
153
+ if (!this ->executed )
154
+ {
155
+ print (" [Swiftly] You can't use %s because the request was not executed. (Get, Post, ...)\n " , __FUNCTION__);
156
+ return " " ;
157
+ }
158
+
159
+ void *http_GetBody = FetchFunctionPtr (nullptr , " scripting_HTTP_GetBody" );
160
+ if (http_GetBody)
161
+ return reinterpret_cast <HTTP_GetBody>(http_GetBody)(this ->requestID );
162
+ else
163
+ {
164
+ NOT_SUPPORTED (" scripting_HTTP_GetBody" );
165
+ return " " ;
166
+ }
167
+ }
168
+
169
+ int HTTPRequest::GetStatusCode ()
170
+ {
171
+ if (!this ->executed )
172
+ {
173
+ print (" [Swiftly] You can't use %s because the request was not executed. (Get, Post, ...)\n " , __FUNCTION__);
174
+ return 0 ;
175
+ }
176
+
177
+ void *http_GetStatusCode = FetchFunctionPtr (nullptr , " scripting_HTTP_GetStatusCode" );
178
+ if (http_GetStatusCode)
179
+ return reinterpret_cast <HTTP_GetStatusCode>(http_GetStatusCode)(this ->requestID );
180
+ else
181
+ {
182
+ NOT_SUPPORTED (" scripting_HTTP_GetStatusCode" );
183
+ return 0 ;
184
+ }
185
+ }
186
+
187
+ const char *HTTPRequest::GetError ()
188
+ {
189
+ if (!this ->executed )
190
+ {
191
+ print (" [Swiftly] You can't use %s because the request was not executed. (Get, Post, ...)\n " , __FUNCTION__);
192
+ return " " ;
193
+ }
194
+
195
+ void *http_GetError = FetchFunctionPtr (nullptr , " scripting_HTTP_GetError" );
196
+ if (http_GetError)
197
+ return reinterpret_cast <HTTP_GetError>(http_GetError)(this ->requestID );
198
+ else
199
+ {
200
+ NOT_SUPPORTED (" scripting_HTTP_GetError" );
201
+ return " " ;
202
+ }
203
+ }
204
+
205
+ HTTPRequest *HTTPRequest::Get (const char *path)
206
+ {
207
+ if (this ->requestID == 0 )
208
+ {
209
+ print (" [Swiftly] You can't use %s because the request couldn't be created.\n " , __FUNCTION__);
210
+ return this ;
211
+ }
212
+
213
+ void *http_Get = FetchFunctionPtr (nullptr , " scripting_HTTP_Get" );
214
+ if (http_Get)
215
+ {
216
+ reinterpret_cast <HTTP_Get>(http_Get)(this ->requestID , path);
217
+ this ->executed = true ;
218
+ }
219
+ else
220
+ NOT_SUPPORTED (" scripting_HTTP_Get" );
221
+
222
+ return this ;
223
+ }
224
+
225
+ HTTPRequest *HTTPRequest::Delete (const char *path)
226
+ {
227
+ if (this ->requestID == 0 )
228
+ {
229
+ print (" [Swiftly] You can't use %s because the request couldn't be created.\n " , __FUNCTION__);
230
+ return this ;
231
+ }
232
+
233
+ void *http_Delete = FetchFunctionPtr (nullptr , " scripting_HTTP_Delete" );
234
+ if (http_Delete)
235
+ {
236
+ reinterpret_cast <HTTP_Delete>(http_Delete)(this ->requestID , path);
237
+ this ->executed = true ;
238
+ }
239
+ else
240
+ NOT_SUPPORTED (" scripting_HTTP_Delete" );
241
+
242
+ return this ;
243
+ }
244
+
245
+ HTTPRequest *HTTPRequest::Post (const char *path)
246
+ {
247
+ if (this ->requestID == 0 )
248
+ {
249
+ print (" [Swiftly] You can't use %s because the request couldn't be created.\n " , __FUNCTION__);
250
+ return this ;
251
+ }
252
+
253
+ void *http_Post = FetchFunctionPtr (nullptr , " scripting_HTTP_Post" );
254
+ if (http_Post)
255
+ {
256
+ reinterpret_cast <HTTP_Post>(http_Post)(this ->requestID , path);
257
+ this ->executed = true ;
258
+ }
259
+ else
260
+ NOT_SUPPORTED (" scripting_HTTP_Post" );
261
+
262
+ return this ;
263
+ }
264
+
265
+ HTTPRequest *HTTPRequest::Put (const char *path)
266
+ {
267
+ if (this ->requestID == 0 )
268
+ {
269
+ print (" [Swiftly] You can't use %s because the request couldn't be created.\n " , __FUNCTION__);
270
+ return this ;
271
+ }
272
+
273
+ void *http_Put = FetchFunctionPtr (nullptr , " scripting_HTTP_Put" );
274
+ if (http_Put)
275
+ {
276
+ reinterpret_cast <HTTP_Put>(http_Put)(this ->requestID , path);
277
+ this ->executed = true ;
278
+ }
279
+ else
280
+ NOT_SUPPORTED (" scripting_HTTP_Put" );
281
+
282
+ return this ;
283
+ }
284
+
285
+ HTTPRequest *HTTPRequest::Patch (const char *path)
286
+ {
287
+ if (this ->requestID == 0 )
288
+ {
289
+ print (" [Swiftly] You can't use %s because the request couldn't be created.\n " , __FUNCTION__);
290
+ return this ;
291
+ }
292
+
293
+ void *http_Patch = FetchFunctionPtr (nullptr , " scripting_HTTP_Patch" );
294
+ if (http_Patch)
295
+ {
296
+ reinterpret_cast <HTTP_Patch>(http_Patch)(this ->requestID , path);
297
+ this ->executed = true ;
298
+ }
299
+ else
300
+ NOT_SUPPORTED (" scripting_HTTP_Patch" );
301
+
302
+ return this ;
303
+ }
304
+
305
+ void HTTPRequest::Close ()
306
+ {
307
+ if (this ->requestID == 0 )
308
+ return print (" [Swiftly] You can't use %s because the request couldn't be created.\n " , __FUNCTION__);
309
+
310
+ void *http_Close = FetchFunctionPtr (nullptr , " scripting_HTTP_Close" );
311
+ if (http_Close)
312
+ reinterpret_cast <HTTP_Close>(http_Close)(this ->requestID );
313
+ else
314
+ NOT_SUPPORTED (" scripting_HTTP_Close" );
315
+ }
0 commit comments