forked from vieter-v/vieter
refactor(client): streamline code & improve error propagation
This commit is contained in:
parent
0bd5158608
commit
0727d0fd25
6 changed files with 29 additions and 67 deletions
|
|
@ -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<T>(method Method, url string, params map[string]stri
|
|||
// output as a Response<T> object.
|
||||
fn (c &Client) send_request_with_body<T>(method Method, url string, params map[string]string, body string) !Response<T> {
|
||||
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<string>
|
||||
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<string>, 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<T>, res.body)!
|
||||
|
|
|
|||
|
|
@ -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<string> {
|
||||
data := c.send_request<string>(.post, '/api/v1/jobs/queue', {
|
||||
pub fn (c &Client) queue_job(target_id int, arch string, force bool) ! {
|
||||
c.send_request<string>(.post, '/api/v1/jobs/queue', {
|
||||
'target': target_id.str()
|
||||
'arch': arch
|
||||
'force': force.str()
|
||||
})!
|
||||
|
||||
return data
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<BuildLog> {
|
||||
pub fn (c &Client) get_build_log(id int) !BuildLog {
|
||||
data := c.send_request<BuildLog>(Method.get, '/api/v1/logs/$id', {})!
|
||||
|
||||
return data
|
||||
return data.data
|
||||
}
|
||||
|
||||
// get_build_log_content returns the contents of the build log file.
|
||||
|
|
|
|||
|
|
@ -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<int> {
|
||||
pub fn (c &Client) add_target(t NewTarget) !int {
|
||||
params := models.params_from<NewTarget>(t)
|
||||
data := c.send_request<int>(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<string> {
|
||||
pub fn (c &Client) remove_target(id int) !string {
|
||||
data := c.send_request<string>(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<string> {
|
||||
pub fn (c &Client) patch_target(id int, params map[string]string) !string {
|
||||
data := c.send_request<string>(Method.patch, '/api/v1/targets/$id', params)!
|
||||
|
||||
return data
|
||||
return data.data
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue