-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapbox.jule
111 lines (91 loc) · 1.87 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
// 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, headerCount, method
cpp fn request(str, []str, int, int): cpp.response
// args: url, data, headers, headerCount, method
cpp fn post(str, str, []str, int, int): cpp.response
// args: url, path
cpp fn download(str, str): bool
struct request {
url: str
mut headers: header::HeaderMap
method: int // 0 = GET, 1 = HEAD
}
impl request {
fn Send(self): cpp.response {
headers := header::Slice(self.headers)
ret cpp.request(self.url, headers, len(headers), self.method)
}
fn Headers(self, headers: header::HeaderMap): request {
self.headers = headers
ret self
}
}
struct postRequest {
url: str
mut data: str
mut headers: header::HeaderMap
method: int // 0 = POST, 1 = PUT, 2 = DELETE
}
impl postRequest {
fn Send(self): cpp.response {
headers := header::Slice(self.headers)
ret cpp.post(self.url, self.data, headers, len(headers), self.method)
}
fn Headers(self, headers: header::HeaderMap): postRequest {
self.headers = headers
ret self
}
fn Data(self, data: str): postRequest {
self.data = data
ret self
}
}
fn GET(url: str): request {
ret request{
url: url,
method: 0,
}
}
fn HEAD(url: str): request {
ret request{
url: url,
method: 1,
}
}
fn POST(url: str): postRequest {
ret postRequest{
url: url,
method: 0,
}
}
fn PUT(url: str): postRequest {
ret postRequest{
url: url,
method: 1,
}
}
fn DELETE(url: str): postRequest {
ret postRequest{
url: url,
method: 2,
}
}
fn Download(url: str, path: str)! {
response := cpp.download(url, path)
if !response {
panic("Download failed")
}
}