diff --git a/containers.v b/containers.v index 545cce1..6981a2c 100644 --- a/containers.v +++ b/containers.v @@ -12,7 +12,7 @@ pub struct ContainerListConfig { } pub fn (mut d DockerConn) container_list(c ContainerListConfig) ![]ContainerListItem { - d.request(.get, '/containers/json', {}) + d.request(.get, '/containers/json') d.params(c) d.send()! @@ -35,7 +35,7 @@ pub: } pub fn (mut d DockerConn) container_create(c NewContainer) !CreatedContainer { - d.request(.post, '/containers/create', {}) + d.request(.post, '/containers/create') d.body_json(c) d.send()! @@ -44,7 +44,7 @@ pub fn (mut d DockerConn) container_create(c NewContainer) !CreatedContainer { // start_container starts the container with the given id. pub fn (mut d DockerConn) container_start(id string) ! { - d.request(.post, '/containers/$id/start', {}) + d.request(.post, '/containers/$id/start') d.send()! d.read_response()! } @@ -68,7 +68,7 @@ pub mut: } pub fn (mut d DockerConn) container_inspect(id string) !ContainerInspect { - d.request(.get, '/containers/$id/json', {}) + d.request(.get, '/containers/$id/json') d.send()! mut data := d.read_json_response()! @@ -84,13 +84,14 @@ pub fn (mut d DockerConn) container_inspect(id string) !ContainerInspect { } pub fn (mut d DockerConn) container_remove(id string) ! { - d.request(.delete, '/containers/$id', {}) + d.request(.delete, '/containers/$id') d.send()! d.read_response()! } pub fn (mut d DockerConn) container_get_logs(id string) !&StreamFormatReader { - d.request(.get, '/containers/$id/logs', { + d.request(.get, '/containers/$id/logs') + d.params({ 'stdout': 'true' 'stderr': 'true' }) diff --git a/images.v b/images.v index 1da31c7..76df20d 100644 --- a/images.v +++ b/images.v @@ -3,7 +3,7 @@ module docker import types { Image } pub fn (mut d DockerConn) image_inspect(image string) !Image { - d.request(.get, '/images/$image/json', {}) + d.request(.get, '/images/$image/json') d.send()! data := d.read_json_response()! @@ -13,7 +13,8 @@ pub fn (mut d DockerConn) image_inspect(image string) !Image { // image_pull pulls the given image:tag. pub fn (mut d DockerConn) image_pull(image string, tag string) ! { - d.request(.post, '/images/create', { + d.request(.post, '/images/create') + d.params({ 'fromImage': image 'tag': tag }) @@ -33,7 +34,8 @@ pub fn (mut d DockerConn) image_pull(image string, tag string) ! { // create_image_from_container creates a new image from a container. pub fn (mut d DockerConn) image_from_container(id string, repo string, tag string) !Image { - d.request(.post, '/commit', { + d.request(.post, '/commit') + d.params({ 'container': id 'repo': repo 'tag': tag @@ -45,13 +47,17 @@ pub fn (mut d DockerConn) image_from_container(id string, repo string, tag strin // remove_image removes the image with the given id. pub fn (mut d DockerConn) image_remove(id string) ! { - d.request(.delete, '/images/$id', {}) + d.request(.delete, '/images/$id') d.send()! d.read_response()! } pub fn (mut d DockerConn) image_tag(name string, repo string, tag string) ! { - d.request(.post, '/images/$name/tag', {'repo': repo, 'tag': tag}) - d.send()! - d.read_response()! + d.request(.post, '/images/$name/tag') + d.params({ + 'repo': repo + 'tag': tag + }) + d.send()! + d.read_response()! } diff --git a/request.v b/request.v index f844717..92d7073 100644 --- a/request.v +++ b/request.v @@ -5,17 +5,13 @@ import net.urllib import io import json -fn (mut d DockerConn) request(method http.Method, url string, params map[string]string) { +fn (mut d DockerConn) request(method http.Method, url string) { d.method = method d.url = url d.content_type = '' d.body = '' d.params.clear() - - for key, value in params { - d.params[key] = urllib.query_escape(value.replace("'", '"')) - } } fn (mut d DockerConn) body(content_type string, body string) { @@ -29,11 +25,17 @@ fn (mut d DockerConn) body_json(data T) { } fn (mut d DockerConn) params(o T) { - $for field in T.fields { - v := o.$(field.name) + $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) - if !isnil(v) { - d.params[field.name] = urllib.query_escape(v.str().replace("'", '"')) + if !isnil(v) { + d.params[field.name] = urllib.query_escape(v.str().replace("'", '"')) + } } } } diff --git a/volumes.v b/volumes.v index 8442ef0..be648c8 100644 --- a/volumes.v +++ b/volumes.v @@ -17,7 +17,7 @@ struct VolumeListResponse { } pub fn (mut d DockerConn) volume_list() !VolumeListResponse { - d.request(.get, '/volumes', {}) + d.request(.get, '/volumes') d.send()! mut data := d.read_json_response()!