-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrequests.odin
62 lines (51 loc) · 1.7 KB
/
requests.odin
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
package opm_cli
//
import "core:encoding/json"
import "./external/curl"
import "core:fmt"
import "core:strings"
import "core:runtime"
import "core:mem"
post_json :: proc(url: string, request_json: any, $T: typeid) -> (ret: T, code: int) {
using curl
url_cstr := strings.clone_to_cstring(url);defer delete(url_cstr)
json_data, merr := json.marshal(request_json);assert(merr == nil)
assert(len(json_data) > 0, "NO DATA IN BODY")
defer delete(json_data)
h := easy_init();defer easy_cleanup(h)
headers: ^curl_slist
headers = slist_append(nil, "content-type: application/json")
headers = slist_append(headers, "Accept: application/json")
headers = slist_append(headers, "charset: utf-8")
defer slist_free_all(headers)
easy_setopt(h, CURLoption.URL, url_cstr)
easy_setopt(h, CURLoption.HTTPHEADER, headers)
easy_setopt(h, CURLoption.POST, 1)
easy_setopt(h, CURLoption.POSTFIELDS, &json_data[0])
easy_setopt(h, CURLoption.POSTFIELDSIZE, len(json_data))
easy_setopt(h, CURLoption.SSL_VERIFYPEER, 0)
easy_setopt(h, CURLoption.WRITEFUNCTION, write_callback)
data := DataContext{nil, context}
easy_setopt(h, .WRITEDATA, &data)
result := easy_perform(h)
if result != CURLcode.OK {
fmt.println("Error occurred: ", result)
} else {
json.unmarshal(data.data, &ret)
fmt.println(ret.message)
}
return
}
DataContext :: struct {
data: []u8,
ctx: runtime.Context,
}
write_callback :: proc "c" (contents: rawptr, size: uint, nmemb: uint, userp: rawptr) -> uint {
dc := transmute(^DataContext)userp
context = dc.ctx
total_size := size * nmemb
content_str := transmute([^]u8)contents
dc.data = make([]u8, int(total_size)) // <-- ALLOCATION
mem.copy(&dc.data[0], content_str, int(total_size))
return total_size
}