2022-06-23 20:26:45 +02:00
|
|
|
module docker
|
|
|
|
|
|
|
|
import net.http
|
|
|
|
import net.urllib
|
|
|
|
import io
|
2023-01-05 12:53:30 +01:00
|
|
|
import json
|
2022-06-23 20:26:45 +02:00
|
|
|
|
2023-01-05 16:31:48 +01:00
|
|
|
fn (mut d DockerConn) request(method http.Method, url string) {
|
2022-06-23 20:26:45 +02:00
|
|
|
d.method = method
|
2023-01-05 12:53:30 +01:00
|
|
|
d.url = url
|
2022-06-23 20:26:45 +02:00
|
|
|
d.content_type = ''
|
|
|
|
d.body = ''
|
2023-01-05 12:53:30 +01:00
|
|
|
|
|
|
|
d.params.clear()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut d DockerConn) body(content_type string, body string) {
|
|
|
|
d.content_type = content_type
|
|
|
|
d.body = body
|
2022-06-23 20:26:45 +02:00
|
|
|
}
|
|
|
|
|
2023-01-05 12:53:30 +01:00
|
|
|
fn (mut d DockerConn) body_json<T>(data T) {
|
|
|
|
d.content_type = 'application/json'
|
|
|
|
d.body = json.encode(data)
|
2022-06-23 20:26:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut d DockerConn) params<T>(o T) {
|
2023-01-05 16:31:48 +01:00
|
|
|
$if T is map[string]string {
|
|
|
|
for key, value in o {
|
|
|
|
d.params[key] = urllib.query_escape(value.replace("'", '"'))
|
|
|
|
}
|
|
|
|
} $else {
|
|
|
|
$for field in T.fields {
|
|
|
|
v := o.$(field.name)
|
2022-06-23 20:26:45 +02:00
|
|
|
|
2023-01-05 16:31:48 +01:00
|
|
|
if !isnil(v) {
|
|
|
|
d.params[field.name] = urllib.query_escape(v.str().replace("'", '"'))
|
|
|
|
}
|
2022-06-23 20:26:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 20:47:28 +01:00
|
|
|
fn (mut d DockerConn) send() ! {
|
2022-06-23 20:26:45 +02:00
|
|
|
mut full_url := d.url
|
|
|
|
|
|
|
|
if d.params.len > 0 {
|
|
|
|
params_str := d.params.keys().map('$it=${d.params[it]}').join('&')
|
|
|
|
full_url += '?$params_str'
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is to make sure we actually created a valid URL
|
2022-11-01 20:47:28 +01:00
|
|
|
parsed_url := urllib.parse(full_url)!
|
2022-06-23 20:26:45 +02:00
|
|
|
final_url := parsed_url.request_uri()
|
|
|
|
|
|
|
|
req := if d.body == '' {
|
|
|
|
'$d.method $final_url HTTP/1.1\nHost: localhost\n\n'
|
|
|
|
} else {
|
|
|
|
'$d.method $final_url HTTP/1.1\nHost: localhost\nContent-Type: $d.content_type\nContent-Length: $d.body.len\n\n$d.body\n\n'
|
|
|
|
}
|
|
|
|
|
2022-11-01 20:47:28 +01:00
|
|
|
d.socket.write_string(req)!
|
2022-06-23 20:26:45 +02:00
|
|
|
|
|
|
|
// When starting a new request, the reader needs to be reset.
|
|
|
|
d.reader = io.new_buffered_reader(reader: d.socket)
|
|
|
|
}
|