aboutsummaryrefslogtreecommitdiff
path: root/examples/http/http.tm
diff options
context:
space:
mode:
Diffstat (limited to 'examples/http/http.tm')
-rw-r--r--examples/http/http.tm50
1 files changed, 25 insertions, 25 deletions
diff --git a/examples/http/http.tm b/examples/http/http.tm
index 3fe41ae2..8d111904 100644
--- a/examples/http/http.tm
+++ b/examples/http/http.tm
@@ -10,73 +10,73 @@ 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(C_code:Text(Text$from_strn(@chunk, @size*@n)))
+ chunks.insert(C_code:Text`Text$from_strn(@chunk, @size*@n)`)
return n*size
- C_code {
+ C_code `
CURL *curl = curl_easy_init();
struct curl_slist *chunk = NULL;
curl_easy_setopt(curl, CURLOPT_URL, @(CString(url)));
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, @save_chunk.fn);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, @save_chunk.userdata);
- }
+ `
defer
- C_code {
+ C_code `
if (chunk)
curl_slist_free_all(chunk);
curl_easy_cleanup(curl);
- }
+ `
when method is POST
- C_code {
+ C_code `
curl_easy_setopt(curl, CURLOPT_POST, 1L);
- }
+ `
if posting := data
- C_code {
+ C_code `
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, @(CString(posting)));
- }
+ `
is PUT
- C_code {
+ C_code `
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
- }
+ `
if putting := data
- C_code {
+ C_code `
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, @(CString(putting)));
- }
+ `
is PATCH
- C_code {
+ C_code `
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH");
- }
+ `
if patching := data
- C_code {
+ C_code `
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, @(CString(patching)));
- }
+ `
is DELETE
- C_code {
+ C_code `
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
- }
+ `
else
pass
for header in headers
- C_code {
+ C_code `
chunk = curl_slist_append(chunk, @(CString(header)));
- }
+ `
- C_code {
+ C_code `
if (chunk)
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
- }
+ `
code := Int64(0)
- C_code {
+ C_code ```
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);
- }
+ ```
return HTTPResponse(Int(code), "".join(chunks))
func get(url:Text, headers:[Text]=[] -> HTTPResponse)