-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapbox.jule
126 lines (106 loc) · 2.35 KB
/
snapbox.jule
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
/*
snapbox
HTTP Client Library for Jule
https://github.com/adamperkowski/snapbox
Copyright (c) 2025, Adam Perkowski
BSD 3-Clause License
*/
#pass "-lcurl"
use "header"
use "status"
cpp use "internal/curlwrapper.hpp"
#typedef
cpp struct Response {
body: str
status: status::StatusCode
}
/* args: url, headers, method */
cpp fn request(str, []str, int): cpp.Response
/* args: url, data, headers, method */
cpp fn post(str, str, []str, int): cpp.Response
/* args: url, path */
cpp fn download(str, str): bool
// Request structure for non-data requests.
struct Request {
url: str
mut headers: header::HeaderMap
method: int // 0 = GET, 1 = HEAD
}
impl Request {
// Executes the request and returns the response.
fn Send(self): cpp.Response {
headers := header::Slice(self.headers)
ret cpp.request(self.url, headers, self.method)
}
// Sets headers for the request.
fn Headers(self, headers: header::HeaderMap): Request {
self.headers = headers
ret self
}
}
// Request structure for data requests.
struct DataRequest {
url: str
mut data: str
mut headers: header::HeaderMap
method: int // 0 = POST, 1 = PUT, 2 = DELETE
}
impl DataRequest {
// Executes the request and returns the response.
fn Send(self): cpp.Response {
headers := header::Slice(self.headers)
ret cpp.post(self.url, self.data, headers, self.method)
}
// Sets headers for the request.
fn Headers(self, headers: header::HeaderMap): DataRequest {
self.headers = headers
ret self
}
// Sets data for the request.
fn Data(self, data: str): DataRequest {
self.data = data
ret self
}
}
// Builds a GET request object.
fn GET(url: str): Request {
ret Request{
url: url,
method: 0,
}
}
// Builds a HEAD request object.
fn HEAD(url: str): Request {
ret Request{
url: url,
method: 1,
}
}
// Builds a POST request object.
fn POST(url: str): DataRequest {
ret DataRequest{
url: url,
method: 0,
}
}
// Builds a PUT request object.
fn PUT(url: str): DataRequest {
ret DataRequest{
url: url,
method: 1,
}
}
// Builds a DELETE request object.
fn DELETE(url: str): DataRequest {
ret DataRequest{
url: url,
method: 2,
}
}
// Uses the internal C++ function to download a file from the given URL and save it to the given path.
fn Download(url: str, path: str)! {
response := cpp.download(url, path)
if !response {
panic("Download failed")
}
}