diff --git a/src/client/client.v b/src/client/client.v index 5f24197..aa6094a 100644 --- a/src/client/client.v +++ b/src/client/client.v @@ -2,7 +2,7 @@ module client import net.http { Method } import net.urllib -import web.response { Response, new_data_response } +import web.response { Response } import json pub struct Client { @@ -56,28 +56,8 @@ fn (c &Client) send_request(method Method, url string, params map[string]stri // send_request_with_body calls send_request_raw_response & parses its // output as a Response object. fn (c &Client) send_request_with_body(method Method, url string, params map[string]string, body string) !Response { - res := c.send_request_raw(method, url, params, body)! - status := http.status_from_int(res.status_code) - - // Just return an empty successful response - if status.is_success() && res.body == '' { - return new_data_response(T{}) - } - - // Non-successful requests are expected to return either an empty body or - // Response - if status.is_error() { - // A non-successful status call will have an empty body - if res.body == '' { - return error('Error $res.status_code ($status.str()): (empty response)') - } - - data := json.decode(Response, res.body)! - - return error('Status $res.status_code ($status.str()): $data.message') - } - - data := json.decode(Response, res.body)! + res_text := c.send_request_raw_response(method, url, params, body)! + data := json.decode(Response, res_text)! return data } diff --git a/src/client/jobs.v b/src/client/jobs.v index a545499..440affa 100644 --- a/src/client/jobs.v +++ b/src/client/jobs.v @@ -1,6 +1,7 @@ module client import build { BuildConfig } +import web.response { Response } // poll_jobs requests a list of new build jobs from the server. pub fn (c &Client) poll_jobs(arch string, max int) ![]BuildConfig { @@ -14,10 +15,12 @@ pub fn (c &Client) poll_jobs(arch string, max int) ![]BuildConfig { // queue_job adds a new one-time build job for the given target to the job // queue. -pub fn (c &Client) queue_job(target_id int, arch string, force bool) ! { - c.send_request(.post, '/api/v1/jobs/queue', { +pub fn (c &Client) queue_job(target_id int, arch string, force bool) !Response { + data := c.send_request(.post, '/api/v1/jobs/queue', { 'target': target_id.str() 'arch': arch 'force': force.str() })! + + return data } diff --git a/src/client/logs.v b/src/client/logs.v index 85063bc..eaddc8c 100644 --- a/src/client/logs.v +++ b/src/client/logs.v @@ -6,18 +6,29 @@ import web.response { Response } import time // get_build_logs returns all build logs. -pub fn (c &Client) get_build_logs(filter BuildLogFilter) ![]BuildLog { +pub fn (c &Client) get_build_logs(filter BuildLogFilter) !Response<[]BuildLog> { params := models.params_from(filter) data := c.send_request<[]BuildLog>(Method.get, '/api/v1/logs', params)! - return data.data + return data +} + +// get_build_logs_for_target returns all build logs for a given target. +pub fn (c &Client) get_build_logs_for_target(target_id int) !Response<[]BuildLog> { + params := { + 'repo': target_id.str() + } + + data := c.send_request<[]BuildLog>(Method.get, '/api/v1/logs', params)! + + return data } // get_build_log returns a specific build log. -pub fn (c &Client) get_build_log(id int) !BuildLog { +pub fn (c &Client) get_build_log(id int) !Response { data := c.send_request(Method.get, '/api/v1/logs/$id', {})! - return data.data + return data } // get_build_log_content returns the contents of the build log file. diff --git a/src/client/targets.v b/src/client/targets.v index 40bfdae..fd4254c 100644 --- a/src/client/targets.v +++ b/src/client/targets.v @@ -2,6 +2,7 @@ module client import models { Target, TargetFilter } import net.http { Method } +import web.response { Response } // get_targets returns a list of targets, given a filter object. pub fn (c &Client) get_targets(filter TargetFilter) ![]Target { @@ -48,24 +49,24 @@ pub struct NewTarget { } // add_target adds a new target to the server. -pub fn (c &Client) add_target(t NewTarget) !int { +pub fn (c &Client) add_target(t NewTarget) !Response { params := models.params_from(t) data := c.send_request(Method.post, '/api/v1/targets', params)! - return data.data + return data } // remove_target removes the target with the given id from the server. -pub fn (c &Client) remove_target(id int) !string { +pub fn (c &Client) remove_target(id int) !Response { data := c.send_request(Method.delete, '/api/v1/targets/$id', {})! - return data.data + return data } // patch_target sends a PATCH request to the given target with the params as // payload. -pub fn (c &Client) patch_target(id int, params map[string]string) !string { +pub fn (c &Client) patch_target(id int, params map[string]string) !Response { data := c.send_request(Method.patch, '/api/v1/targets/$id', params)! - return data.data + return data } diff --git a/src/console/logs/logs.v b/src/console/logs/logs.v index 3064a58..1330dd0 100644 --- a/src/console/logs/logs.v +++ b/src/console/logs/logs.v @@ -183,7 +183,15 @@ fn print_log_list(logs []BuildLog, raw bool) ! { // list prints a list of all build logs. fn list(conf Config, filter BuildLogFilter, raw bool) ! { c := client.new(conf.address, conf.api_key) - logs := c.get_build_logs(filter)! + logs := c.get_build_logs(filter)!.data + + print_log_list(logs, raw)! +} + +// list prints a list of all build logs for a given target. +fn list_for_target(conf Config, target_id int, raw bool) ! { + c := client.new(conf.address, conf.api_key) + logs := c.get_build_logs_for_target(target_id)!.data print_log_list(logs, raw)! } @@ -191,7 +199,7 @@ fn list(conf Config, filter BuildLogFilter, raw bool) ! { // info print the detailed info for a given build log. fn info(conf Config, id int) ! { c := client.new(conf.address, conf.api_key) - log := c.get_build_log(id)! + log := c.get_build_log(id)!.data print(log) } diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index b277410..b527896 100644 --- a/src/console/targets/targets.v +++ b/src/console/targets/targets.v @@ -215,7 +215,8 @@ pub fn cmd() cli.Command { } c := client.new(conf.address, conf.api_key) - c.queue_job(target_id, arch, force)! + res := c.queue_job(target_id, arch, force)! + println(res.message) } else { build(conf, target_id, force)! } @@ -244,19 +245,23 @@ fn list(conf Config, filter TargetFilter, raw bool) ! { // add adds a new repository to the server's list. fn add(conf Config, t &NewTarget, raw bool) ! { c := client.new(conf.address, conf.api_key) - target_id := c.add_target(t)! + res := c.add_target(t)! if raw { - println(target_id) + println(res.data) } else { - println('Target added with id $target_id') + println('Target added with id $res.data') } } // remove removes a repository from the server's list. fn remove(conf Config, id string) ! { - c := client.new(conf.address, conf.api_key) - c.remove_target(id.int())! + id_int := id.int() + + if id_int != 0 { + c := client.new(conf.address, conf.api_key) + c.remove_target(id_int)! + } } // patch patches a given repository with the provided params. @@ -269,13 +274,22 @@ fn patch(conf Config, id string, params map[string]string) ! { } } - c := client.new(conf.address, conf.api_key) - c.patch_target(id.int(), params)! + id_int := id.int() + if id_int != 0 { + c := client.new(conf.address, conf.api_key) + c.patch_target(id_int, params)! + } } // info shows detailed information for a given repo. fn info(conf Config, id string) ! { + id_int := id.int() + + if id_int == 0 { + return + } + c := client.new(conf.address, conf.api_key) - repo := c.get_target(id.int())! + repo := c.get_target(id_int)! println(repo) }