2022-05-07 16:10:27 +02:00
|
|
|
module client
|
|
|
|
|
2022-12-15 10:01:45 +01:00
|
|
|
import net.http { Method }
|
2022-05-07 19:38:28 +02:00
|
|
|
import net.urllib
|
2022-12-15 09:46:48 +01:00
|
|
|
import web.response { Response, new_data_response }
|
2022-05-07 16:10:27 +02:00
|
|
|
import json
|
|
|
|
|
|
|
|
pub struct Client {
|
|
|
|
pub:
|
|
|
|
address string
|
|
|
|
api_key string
|
|
|
|
}
|
|
|
|
|
2022-05-07 22:06:17 +02:00
|
|
|
// new creates a new Client instance.
|
2022-05-07 16:10:27 +02:00
|
|
|
pub fn new(address string, api_key string) Client {
|
|
|
|
return Client{
|
|
|
|
address: address
|
|
|
|
api_key: api_key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-07 22:06:17 +02:00
|
|
|
// send_request_raw sends an HTTP request, returning the http.Response object.
|
|
|
|
// It encodes the params so that they're safe to pass as HTTP query parameters.
|
2022-11-01 21:10:45 +01:00
|
|
|
fn (c &Client) send_request_raw(method Method, url string, params map[string]string, body string) !http.Response {
|
2022-05-07 19:38:28 +02:00
|
|
|
mut full_url := '$c.address$url'
|
2022-05-07 16:10:27 +02:00
|
|
|
|
|
|
|
if params.len > 0 {
|
2022-05-07 19:38:28 +02:00
|
|
|
mut params_escaped := map[string]string{}
|
|
|
|
|
|
|
|
// Escape each query param
|
|
|
|
for k, v in params {
|
2022-05-19 07:54:33 +02:00
|
|
|
// An empty parameter should be the same as not providing it at all
|
2022-09-11 22:20:02 +02:00
|
|
|
params_escaped[k] = urllib.query_escape(v)
|
2022-05-07 19:38:28 +02:00
|
|
|
}
|
|
|
|
|
2022-09-11 22:20:02 +02:00
|
|
|
params_str := params_escaped.keys().map('$it=${params_escaped[it]}').join('&')
|
2022-05-07 16:10:27 +02:00
|
|
|
|
|
|
|
full_url = '$full_url?$params_str'
|
|
|
|
}
|
|
|
|
|
2022-11-01 21:10:45 +01:00
|
|
|
// Looking at the source code, this function doesn't actually fail, so I'm
|
|
|
|
// not sure why it returns an optional
|
|
|
|
mut req := http.new_request(method, full_url, body) or { return error('') }
|
|
|
|
req.add_custom_header('X-Api-Key', c.api_key)!
|
2022-05-07 16:10:27 +02:00
|
|
|
|
2022-11-01 21:10:45 +01:00
|
|
|
res := req.do()!
|
2022-05-07 21:50:20 +02:00
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// send_request<T> just calls send_request_with_body<T> with an empty body.
|
2022-11-01 21:10:45 +01:00
|
|
|
fn (c &Client) send_request<T>(method Method, url string, params map[string]string) !Response<T> {
|
2022-05-07 21:50:20 +02:00
|
|
|
return c.send_request_with_body<T>(method, url, params, '')
|
|
|
|
}
|
|
|
|
|
2022-05-07 22:06:17 +02:00
|
|
|
// send_request_with_body<T> calls send_request_raw_response & parses its
|
|
|
|
// output as a Response<T> object.
|
2022-11-01 21:10:45 +01:00
|
|
|
fn (c &Client) send_request_with_body<T>(method Method, url string, params map[string]string, body string) !Response<T> {
|
2022-12-15 09:46:48 +01:00
|
|
|
res := c.send_request_raw(method, url, params, body)!
|
2022-12-15 10:01:45 +01:00
|
|
|
status := http.status_from_int(res.status_code)
|
2022-12-15 09:46:48 +01:00
|
|
|
|
|
|
|
// Just return an empty successful response
|
2022-12-15 10:01:45 +01:00
|
|
|
if status.is_success() && res.body == '' {
|
2022-12-15 09:46:48 +01:00
|
|
|
return new_data_response(T{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Non-successful requests are expected to return either an empty body or
|
|
|
|
// Response<string>
|
2022-12-15 10:01:45 +01:00
|
|
|
if status.is_error() {
|
2022-12-15 09:46:48 +01:00
|
|
|
// A non-successful status call will have an empty body
|
|
|
|
if res.body == '' {
|
2022-12-15 10:01:45 +01:00
|
|
|
return error('Error $res.status_code ($status.str()): (empty response)')
|
2022-12-15 09:46:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
data := json.decode(Response<string>, res.body)!
|
|
|
|
|
2022-12-15 10:01:45 +01:00
|
|
|
return error('Status $res.status_code ($status.str()): $data.message')
|
2022-12-15 09:46:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
data := json.decode(Response<T>, res.body)!
|
2022-05-07 16:10:27 +02:00
|
|
|
|
|
|
|
return data
|
|
|
|
}
|
2022-05-07 21:50:20 +02:00
|
|
|
|
2022-05-07 22:06:17 +02:00
|
|
|
// send_request_raw_response returns the raw text response for an HTTP request.
|
2022-11-01 21:10:45 +01:00
|
|
|
fn (c &Client) send_request_raw_response(method Method, url string, params map[string]string, body string) !string {
|
|
|
|
res := c.send_request_raw(method, url, params, body)!
|
2022-05-07 21:50:20 +02:00
|
|
|
|
2022-06-05 22:21:54 +02:00
|
|
|
return res.body
|
2022-05-07 21:50:20 +02:00
|
|
|
}
|