From 0727d0fd2517cb5dd67aa6f25d94b651c0f351f1 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Thu, 15 Dec 2022 10:01:45 +0100 Subject: [PATCH] refactor(client): streamline code & improve error propagation --- src/client/client.v | 13 ++++++------- src/client/jobs.v | 7 ++----- src/client/logs.v | 19 ++++--------------- src/client/targets.v | 13 ++++++------- src/console/logs/logs.v | 12 ++---------- src/console/targets/targets.v | 32 +++++++++----------------------- 6 files changed, 29 insertions(+), 67 deletions(-) diff --git a/src/client/client.v b/src/client/client.v index 3541555..5f24197 100644 --- a/src/client/client.v +++ b/src/client/client.v @@ -1,6 +1,6 @@ module client -import net.http { Method, Status } +import net.http { Method } import net.urllib import web.response { Response, new_data_response } import json @@ -57,25 +57,24 @@ fn (c &Client) send_request(method Method, url string, params map[string]stri // 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 res.status_code == Status.no_content.int() { + 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 res.status_code < 200 || res.status_code > 299 { - status_string := http.status_from_int(res.status_code).str() - + if status.is_error() { // A non-successful status call will have an empty body if res.body == '' { - return error('Error $res.status_code ($status_string): (empty response)') + return error('Error $res.status_code ($status.str()): (empty response)') } data := json.decode(Response, res.body)! - return error('Status $res.status_code ($status_string): $data.message') + return error('Status $res.status_code ($status.str()): $data.message') } data := json.decode(Response, res.body)! diff --git a/src/client/jobs.v b/src/client/jobs.v index 440affa..a545499 100644 --- a/src/client/jobs.v +++ b/src/client/jobs.v @@ -1,7 +1,6 @@ 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 { @@ -15,12 +14,10 @@ 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) !Response { - data := c.send_request(.post, '/api/v1/jobs/queue', { +pub fn (c &Client) queue_job(target_id int, arch string, force bool) ! { + 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 eaddc8c..85063bc 100644 --- a/src/client/logs.v +++ b/src/client/logs.v @@ -6,29 +6,18 @@ import web.response { Response } import time // get_build_logs returns all build logs. -pub fn (c &Client) get_build_logs(filter BuildLogFilter) !Response<[]BuildLog> { +pub fn (c &Client) get_build_logs(filter BuildLogFilter) ![]BuildLog { params := models.params_from(filter) data := c.send_request<[]BuildLog>(Method.get, '/api/v1/logs', params)! - 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 + return data.data } // get_build_log returns a specific build log. -pub fn (c &Client) get_build_log(id int) !Response { +pub fn (c &Client) get_build_log(id int) !BuildLog { data := c.send_request(Method.get, '/api/v1/logs/$id', {})! - return data + return data.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 fd4254c..40bfdae 100644 --- a/src/client/targets.v +++ b/src/client/targets.v @@ -2,7 +2,6 @@ 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 { @@ -49,24 +48,24 @@ pub struct NewTarget { } // add_target adds a new target to the server. -pub fn (c &Client) add_target(t NewTarget) !Response { +pub fn (c &Client) add_target(t NewTarget) !int { params := models.params_from(t) data := c.send_request(Method.post, '/api/v1/targets', params)! - return data + return data.data } // remove_target removes the target with the given id from the server. -pub fn (c &Client) remove_target(id int) !Response { +pub fn (c &Client) remove_target(id int) !string { data := c.send_request(Method.delete, '/api/v1/targets/$id', {})! - return data + return data.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) !Response { +pub fn (c &Client) patch_target(id int, params map[string]string) !string { data := c.send_request(Method.patch, '/api/v1/targets/$id', params)! - return data + return data.data } diff --git a/src/console/logs/logs.v b/src/console/logs/logs.v index 1330dd0..3064a58 100644 --- a/src/console/logs/logs.v +++ b/src/console/logs/logs.v @@ -183,15 +183,7 @@ 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)!.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 + logs := c.get_build_logs(filter)! print_log_list(logs, raw)! } @@ -199,7 +191,7 @@ fn list_for_target(conf Config, target_id int, 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)!.data + log := c.get_build_log(id)! print(log) } diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index b527896..b277410 100644 --- a/src/console/targets/targets.v +++ b/src/console/targets/targets.v @@ -215,8 +215,7 @@ pub fn cmd() cli.Command { } c := client.new(conf.address, conf.api_key) - res := c.queue_job(target_id, arch, force)! - println(res.message) + c.queue_job(target_id, arch, force)! } else { build(conf, target_id, force)! } @@ -245,23 +244,19 @@ 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) - res := c.add_target(t)! + target_id := c.add_target(t)! if raw { - println(res.data) + println(target_id) } else { - println('Target added with id $res.data') + println('Target added with id $target_id') } } // remove removes a repository from the server's list. fn remove(conf Config, id string) ! { - id_int := id.int() - - if id_int != 0 { - c := client.new(conf.address, conf.api_key) - c.remove_target(id_int)! - } + c := client.new(conf.address, conf.api_key) + c.remove_target(id.int())! } // patch patches a given repository with the provided params. @@ -274,22 +269,13 @@ fn patch(conf Config, id string, params map[string]string) ! { } } - id_int := id.int() - if id_int != 0 { - c := client.new(conf.address, conf.api_key) - c.patch_target(id_int, params)! - } + 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) }