aboutsummaryrefslogtreecommitdiff
path: root/examples/http/http.tm
blob: f36fac606c769455e1fad72f07f7056519ccb3a2 (plain)
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
# A simple HTTP library built using CURL

use libcurl.so
use <curl/curl.h>

struct HTTPResponse(code:Int, body:Text)

enum _Method(GET, POST, PUT, PATCH, DELETE)

func _send(method:_Method, url:Text, data:Text?, headers=[:Text] -> HTTPResponse):
    chunks := @[:Text]
    save_chunk := func(chunk:CString, size:Int64, n:Int64):
        chunks:insert(inline C:Text {
            Text$format("%.*s", $size*$n, $chunk)
        })
        return n*size

    inline C {
        CURL *curl = curl_easy_init();
        struct curl_slist *chunk = NULL;
        curl_easy_setopt(curl, CURLOPT_URL, Text$as_c_string($url));
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, $save_chunk.fn);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, $save_chunk.userdata);
    }

    when method is POST:
        inline C {
            curl_easy_setopt(curl, CURLOPT_POST, 1L);
        }
        if posting := data:
            inline C {
                curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Text$as_c_string($posting));
            }
    is PUT:
        inline C {
            curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
        }
        if putting := data:
            inline C {
                curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Text$as_c_string($putting));
            }
    is PATCH:
        inline C {
            curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH");
        }
        if patching := data:
            inline C {
                curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Text$as_c_string($patching));
            }
    is DELETE:
        inline C {
            curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
        }
    else:
        pass

    for header in headers:
        inline C {
            chunk = curl_slist_append(chunk, Text$as_c_string($header));
        }

    inline C {
        if (chunk)
            curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
    }

    code := Int64(0)
    inline C {
        CURLcode res = curl_easy_perform(curl);
        if (res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));

        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &$code);
        if (chunk)
            curl_slist_free_all(chunk);
        curl_easy_cleanup(curl);
    }
    return HTTPResponse(Int(code), "":join(chunks))

func get(url:Text, headers=[:Text] -> HTTPResponse):
    return _send(GET, url, NONE, headers)

func post(url:Text, data="", headers=["Content-Type: application/json", "Accept: application/json"] -> HTTPResponse):
    return _send(POST, url, data, headers)

func put(url:Text, data="", headers=["Content-Type: application/json", "Accept: application/json"] -> HTTPResponse):
    return _send(PUT, url, data, headers)

func patch(url:Text, data="", headers=["Content-Type: application/json", "Accept: application/json"] -> HTTPResponse):
    return _send(PATCH, url, data, headers)

func delete(url:Text, data=NONE:Text, headers=["Content-Type: application/json", "Accept: application/json"] -> HTTPResponse):
    return _send(DELETE, url, data, headers)

func main():
    !! GET:
    say(get("https://httpbin.org/get").body)
    !! Waiting 1sec...
    sleep(1)
    !! POST:
    say(post("https://httpbin.org/post", `{"key": "value"}`).body)
    !! Waiting 1sec...
    sleep(1)
    !! PUT:
    say(put("https://httpbin.org/put", `{"key": "value"}`).body)
    !! Waiting 1sec...
    sleep(1)
    !! DELETE:
    say(delete("https://httpbin.org/delete").body)