From 54c66f062cef98f929dfcdbb89cc2480eaab9f3f Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 23 Jun 2022 20:59:14 +0200 Subject: [PATCH] WIP: migrate existing functions to new query builder --- containers.v | 47 ++++++++++++++++++++++++++++++++--------------- request.v | 21 +++++++++++++++++++++ 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/containers.v b/containers.v index 0e69c05..4da092a 100644 --- a/containers.v +++ b/containers.v @@ -37,18 +37,11 @@ pub: } pub fn (mut d DockerConn) container_create(c NewContainer) ?CreatedContainer { - d.send_request_with_json(Method.post, '/containers/create', c)? - head, res := d.read_response()? + d.post('/containers/create') + d.json(c) + d.send()? - if head.status_code != 201 { - data := json.decode(DockerError, res)? - - return error(data.message) - } - - data := json.decode(CreatedContainer, res)? - - return data + return d.read_json_response() } // start_container starts the container with the given id. @@ -103,8 +96,18 @@ pub fn (mut d DockerConn) container_inspect(id string) ?ContainerInspect { return data } -pub fn (mut d DockerConn) container_remove(id string) ? { - d.send_request(Method.delete, '/containers/$id')? +[params] +pub struct ContainerRemoveConfig { + v bool + force bool + link bool +} + +pub fn (mut d DockerConn) container_remove(id string, c ContainerRemoveConfig) ? { + d.request(Method.delete, '/containers/$id') + d.params(c) + d.send()? + head, body := d.read_response()? if head.status_code != 204 { @@ -114,8 +117,22 @@ pub fn (mut d DockerConn) container_remove(id string) ? { } } -pub fn (mut d DockerConn) container_get_logs(id string) ?&StreamFormatReader { - d.send_request(Method.get, '/containers/$id/logs?stdout=true&stderr=true')? +[params] +pub struct ContainerGetLogsConfig { + follow bool + stdout bool + stderr bool + since int + until int + timestamps bool + tail string = "all" +} + +pub fn (mut d DockerConn) container_get_logs(id string, c ContainerGetLogsConfig) ?&StreamFormatReader { + d.get('/containers/$id/logs') + d.params(c) + d.send()? + head := d.read_response_head()? if head.status_code != 200 { diff --git a/request.v b/request.v index 14dd468..658d7d4 100644 --- a/request.v +++ b/request.v @@ -3,6 +3,7 @@ module docker import net.http import net.urllib import io +import json fn (mut d DockerConn) request(method http.Method, url_str string) { d.method = method @@ -16,6 +17,14 @@ fn (mut d DockerConn) get(url_str string) { d.request(http.Method.get, url_str) } +fn (mut d DockerConn) post(url_str string) { + d.request(http.Method.post, url_str) +} + +fn (mut d DockerConn) delete(url_str string) { + d.request(http.Method.delete, url_str) +} + fn (mut d DockerConn) params(o T) { $for field in T.fields { v := o.$(field.name) @@ -26,6 +35,18 @@ fn (mut d DockerConn) params(o T) { } } +fn (mut d DockerConn) body(content_type string, body string) { + d.content_type = content_type + d.body = body +} + +fn (mut d DockerConn) json(o T) { + body := json.encode(o) + + d.content_type = 'application/json' + d.body = body +} + fn (mut d DockerConn) send() ? { mut full_url := d.url