Skip to content

Commit a72f096

Browse files
committed
feat(scripting): Adding HTTP Support
1 parent 88c66d7 commit a72f096

File tree

9 files changed

+607
-1
lines changed

9 files changed

+607
-1
lines changed

.vscode/settings.json

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,26 @@
7878
"filesystem": "cpp",
7979
"*.inc": "cpp",
8080
"csignal": "cpp",
81-
"future": "cpp"
81+
"future": "cpp",
82+
"xhash": "cpp",
83+
"xstring": "cpp",
84+
"xtree": "cpp",
85+
"xutility": "cpp",
86+
"ios": "cpp",
87+
"locale": "cpp",
88+
"ranges": "cpp",
89+
"stack": "cpp",
90+
"xfacet": "cpp",
91+
"xiosbase": "cpp",
92+
"xlocale": "cpp",
93+
"xlocbuf": "cpp",
94+
"xlocinfo": "cpp",
95+
"xlocmes": "cpp",
96+
"xlocmon": "cpp",
97+
"xlocnum": "cpp",
98+
"xloctime": "cpp",
99+
"xmemory": "cpp",
100+
"xtr1common": "cpp"
82101
},
83102
"C_Cpp.default.compilerPath": "C:\\msys64\\mingw64\\bin\\g++.exe"
84103
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "http.h"
2+
3+
HTTP::HTTP() {}
4+
5+
HTTPRequest *HTTP::GenerateRequest(const char *domain)
6+
{
7+
HTTPRequest *request = new HTTPRequest(domain);
8+
return request;
9+
}
10+
11+
void HTTP::CloseRequest(HTTPRequest *request)
12+
{
13+
request->Close();
14+
delete request;
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef _swiftly_http_h
2+
#define _swiftly_http_h
3+
4+
#include "http/HTTPRequest.h"
5+
6+
class HTTP
7+
{
8+
private:
9+
public:
10+
HTTP();
11+
12+
HTTPRequest *GenerateRequest(const char *domain);
13+
void CloseRequest(HTTPRequest *request);
14+
};
15+
16+
extern HTTP *http;
17+
18+
#endif
Lines changed: 315 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
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

Comments
 (0)