refactor(docker): use http.Method instead of strings

main
Jef Roosens 2022-05-16 15:02:57 +02:00
parent d4c803c41c
commit 850cba6ab9
Signed by untrusted user: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 14 additions and 14 deletions

View File

@ -3,7 +3,7 @@ module docker
import json
import net.urllib
import time
import net.http
import net.http { Method }
struct DockerError {
message string
@ -16,7 +16,7 @@ struct Container {
// containers returns a list of all containers.
pub fn (mut d DockerConn) containers() ?[]Container {
d.send_request('GET', urllib.parse('/v1.41/containers/json')?)?
d.send_request(Method.get, urllib.parse('/v1.41/containers/json')?)?
head, res := d.read_response()?
if head.status_code != 200 {
@ -47,7 +47,7 @@ pub:
// create_container creates a new container with the given config.
pub fn (mut d DockerConn) create_container(c NewContainer) ?CreatedContainer {
d.send_request_with_json('POST', urllib.parse('/v1.41/containers/create')?, c)?
d.send_request_with_json(Method.post, urllib.parse('/v1.41/containers/create')?, c)?
head, res := d.read_response()?
if head.status_code != 201 {
@ -63,7 +63,7 @@ pub fn (mut d DockerConn) create_container(c NewContainer) ?CreatedContainer {
// start_container starts the container with the given id.
pub fn (mut d DockerConn) start_container(id string) ? {
d.send_request('POST', urllib.parse('/v1.41/containers/$id/start')?)?
d.send_request(Method.post, urllib.parse('/v1.41/containers/$id/start')?)?
head, body := d.read_response()?
if head.status_code != 204 {
@ -93,7 +93,7 @@ pub mut:
// inspect_container returns detailed information for a given container.
pub fn (mut d DockerConn) inspect_container(id string) ?ContainerInspect {
d.send_request('GET', urllib.parse('/v1.41/containers/$id/json')?)?
d.send_request(Method.get, urllib.parse('/v1.41/containers/$id/json')?)?
head, body := d.read_response()?
if head.status_code != 200 {
@ -115,7 +115,7 @@ pub fn (mut d DockerConn) inspect_container(id string) ?ContainerInspect {
// remove_container removes the container with the given id.
pub fn (mut d DockerConn) remove_container(id string) ? {
d.send_request('DELETE', urllib.parse('/v1.41/containers/$id')?)?
d.send_request(Method.delete, urllib.parse('/v1.41/containers/$id')?)?
head, body := d.read_response()?
if head.status_code != 204 {
@ -128,7 +128,7 @@ pub fn (mut d DockerConn) remove_container(id string) ? {
// get_container_logs returns a reader object allowing access to the
// container's logs.
pub fn (mut d DockerConn) get_container_logs(id string) ?&StreamFormatReader {
d.send_request('GET', urllib.parse('/v1.41/containers/$id/logs?stdout=true&stderr=true')?)?
d.send_request(Method.get, urllib.parse('/v1.41/containers/$id/logs?stdout=true&stderr=true')?)?
head := d.read_response_head()?
if head.status_code != 200 {

View File

@ -39,7 +39,7 @@ pub fn (mut d DockerConn) close() ? {
}
// send_request sends an HTTP request without body.
pub fn (mut d DockerConn) send_request(method string, url urllib.URL) ? {
pub fn (mut d DockerConn) send_request(method http.Method, url urllib.URL) ? {
req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\n\n'
d.socket.write_string(req)?
@ -49,7 +49,7 @@ pub fn (mut d DockerConn) send_request(method string, url urllib.URL) ? {
}
// send_request_with_body sends an HTTP request with the given body.
pub fn (mut d DockerConn) send_request_with_body(method string, url urllib.URL, content_type string, body string) ? {
pub fn (mut d DockerConn) send_request_with_body(method http.Method, url urllib.URL, content_type string, body string) ? {
req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\nContent-Type: $content_type\nContent-Length: $body.len\n\n$body\n\n'
d.socket.write_string(req)?
@ -60,7 +60,7 @@ pub fn (mut d DockerConn) send_request_with_body(method string, url urllib.URL,
// send_request_with_json<T> is a convenience wrapper around
// send_request_with_body that encodes the input as JSON.
pub fn (mut d DockerConn) send_request_with_json<T>(method string, url urllib.URL, data &T) ? {
pub fn (mut d DockerConn) send_request_with_json<T>(method http.Method, url urllib.URL, data &T) ? {
body := json.encode(data)
return d.send_request_with_body(method, url, 'application/json', body)

View File

@ -1,6 +1,6 @@
module docker
import net.http
import net.http { Method }
import net.urllib
import json
@ -11,7 +11,7 @@ pub:
// pull_image pulls the given image:tag.
pub fn (mut d DockerConn) pull_image(image string, tag string) ? {
d.send_request('POST', urllib.parse('/v1.41/images/create?fromImage=$image&tag=$tag')?)?
d.send_request(Method.post, urllib.parse('/v1.41/images/create?fromImage=$image&tag=$tag')?)?
head := d.read_response_head()?
if head.status_code != 200 {
@ -34,7 +34,7 @@ pub fn (mut d DockerConn) pull_image(image string, tag string) ? {
// create_image_from_container creates a new image from a container.
pub fn (mut d DockerConn) create_image_from_container(id string, repo string, tag string) ?Image {
d.send_request('POST', urllib.parse('/v1.41/commit?container=$id&repo=$repo&tag=$tag')?)?
d.send_request(Method.post, urllib.parse('/v1.41/commit?container=$id&repo=$repo&tag=$tag')?)?
head, body := d.read_response()?
if head.status_code != 201 {
@ -50,7 +50,7 @@ pub fn (mut d DockerConn) create_image_from_container(id string, repo string, ta
// remove_image removes the image with the given id.
pub fn (mut d DockerConn) remove_image(id string) ? {
d.send_request('DELETE', urllib.parse('/v1.41/images/$id')?)?
d.send_request(Method.delete, urllib.parse('/v1.41/images/$id')?)?
head, body := d.read_response()?
if head.status_code != 200 {