|
| 1 | +package http |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/suborbital/sdk/tinygo/internal/ffi" |
| 5 | + |
| 6 | + "github.com/suborbital/sdk/tinygo/http/method" |
| 7 | +) |
| 8 | + |
| 9 | +func GET(url string, headers map[string]string) ([]byte, error) { |
| 10 | + return do(method.GET, url, nil, headers) |
| 11 | +} |
| 12 | + |
| 13 | +func HEAD(url string, headers map[string]string) ([]byte, error) { |
| 14 | + return do(method.HEAD, url, nil, headers) |
| 15 | +} |
| 16 | + |
| 17 | +func OPTIONS(url string, headers map[string]string) ([]byte, error) { |
| 18 | + return do(method.OPTIONS, url, nil, headers) |
| 19 | +} |
| 20 | + |
| 21 | +func POST(url string, body []byte, headers map[string]string) ([]byte, error) { |
| 22 | + return do(method.POST, url, body, headers) |
| 23 | +} |
| 24 | + |
| 25 | +func PUT(url string, body []byte, headers map[string]string) ([]byte, error) { |
| 26 | + return do(method.PUT, url, body, headers) |
| 27 | +} |
| 28 | + |
| 29 | +func PATCH(url string, body []byte, headers map[string]string) ([]byte, error) { |
| 30 | + return do(method.PATCH, url, body, headers) |
| 31 | +} |
| 32 | + |
| 33 | +func DELETE(url string, headers map[string]string) ([]byte, error) { |
| 34 | + return do(method.DELETE, url, nil, headers) |
| 35 | +} |
| 36 | + |
| 37 | +// Remark: The URL gets encoded with headers added on the end, seperated by :: |
| 38 | +// eg. https://google.com/somepage::authorization:bearer qdouwrnvgoquwnrg::anotherheader:nicetomeetyou |
| 39 | +func do(method method.MethodType, url string, body []byte, headers map[string]string) ([]byte, error) { |
| 40 | + urlStr := url |
| 41 | + |
| 42 | + if headers != nil { |
| 43 | + headerStr := renderHeaderString(headers) |
| 44 | + if headerStr != "" { |
| 45 | + urlStr += "::" + headerStr |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return ffi.DoHTTPRequest(int32(method), urlStr, body, headers) |
| 50 | +} |
| 51 | + |
| 52 | +func renderHeaderString(headers map[string]string) string { |
| 53 | + out := "" |
| 54 | + |
| 55 | + for key, value := range headers { |
| 56 | + out += key + ":" + value |
| 57 | + out += "::" |
| 58 | + } |
| 59 | + |
| 60 | + return out[:len(out)-2] |
| 61 | +} |
0 commit comments