Versioned API endpoints (closes #91)

main^2
Jef Roosens 2022-02-21 17:49:10 +01:00
parent 96416585bc
commit 0cdd4e7b4b
Signed by: Jef Roosens
GPG Key ID: B580B976584B5F30
2 changed files with 15 additions and 9 deletions

View File

@ -10,7 +10,7 @@ struct Container {
// containers returns a list of all currently running containers
pub fn containers() ?[]Container {
res := request('GET', urllib.parse('/containers/json') ?) ?
res := request('GET', urllib.parse('/v1.41/containers/json') ?) ?
return json.decode([]Container, res.text) or {}
}
@ -29,7 +29,7 @@ struct CreatedContainer {
// create_container creates a container defined by the given configuration. If
// successful, it returns the ID of the newly created container.
pub fn create_container(c &NewContainer) ?string {
res := request_with_json('POST', urllib.parse('/containers/create') ?, c) ?
res := request_with_json('POST', urllib.parse('/v1.41/containers/create') ?, c) ?
if res.status_code != 201 {
return error('Failed to create container.')
@ -41,7 +41,7 @@ pub fn create_container(c &NewContainer) ?string {
// start_container starts a container with a given ID. It returns whether the
// container was started or not.
pub fn start_container(id string) ?bool {
res := request('POST', urllib.parse('/containers/$id/start') ?) ?
res := request('POST', urllib.parse('/v1.41/containers/$id/start') ?) ?
return res.status_code == 204
}
@ -59,7 +59,7 @@ pub:
// inspect_container returns the result of inspecting a container with a given
// ID.
pub fn inspect_container(id string) ?ContainerInspect {
res := request('GET', urllib.parse('/containers/$id/json') ?) ?
res := request('GET', urllib.parse('/v1.41/containers/$id/json') ?) ?
if res.status_code != 200 {
return error('Failed to inspect container.')
@ -70,7 +70,7 @@ pub fn inspect_container(id string) ?ContainerInspect {
// remove_container removes a container with a given ID.
pub fn remove_container(id string) ?bool {
res := request('DELETE', urllib.parse('/containers/$id') ?) ?
res := request('DELETE', urllib.parse('/v1.41/containers/$id') ?) ?
return res.status_code == 204
}

View File

@ -11,7 +11,9 @@ const buf_len = 1024
fn send(req &string) ?http.Response {
// Open a connection to the socket
mut s := unix.connect_stream(docker.socket) or { return error('Failed to connect to socket ${docker.socket}.') }
mut s := unix.connect_stream(docker.socket) or {
return error('Failed to connect to socket ${docker.socket}.')
}
defer {
// This or is required because otherwise, the V compiler segfaults for
@ -40,7 +42,9 @@ fn send(req &string) ?http.Response {
// After reading the first part of the response, we parse it into an HTTP
// response. If it isn't chunked, we return early with the data.
parsed := http.parse_response(res.bytestr()) or { return error('Failed to parse HTTP response from socket ${docker.socket}.') }
parsed := http.parse_response(res.bytestr()) or {
return error('Failed to parse HTTP response from socket ${docker.socket}.')
}
if parsed.header.get(http.CommonHeader.transfer_encoding) or { '' } != 'chunked' {
return parsed
@ -53,7 +57,9 @@ fn send(req &string) ?http.Response {
s.wait_for_write() ?
for {
c = s.read(mut buf) or { return error('Failed to read data from socket ${docker.socket}.') }
c = s.read(mut buf) or {
return error('Failed to read data from socket ${docker.socket}.')
}
res << buf[..c]
if c < docker.buf_len {
@ -88,5 +94,5 @@ pub fn request_with_json<T>(method string, url urllib.URL, data &T) ?http.Respon
// pull_image pulls tries to pull the image for the given image & tag
pub fn pull_image(image string, tag string) ?http.Response {
return request('POST', urllib.parse('/images/create?fromImage=$image&tag=$tag') ?)
return request('POST', urllib.parse('/v1.41/images/create?fromImage=$image&tag=$tag') ?)
}