forked from vieter-v/vieter
refactor: moved client code into own module
This commit is contained in:
parent
f42d3fd8b0
commit
407b226955
7 changed files with 110 additions and 91 deletions
40
src/client/client.v
Normal file
40
src/client/client.v
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
module client
|
||||
|
||||
import net.http
|
||||
import response { Response }
|
||||
import json
|
||||
|
||||
pub struct Client {
|
||||
pub:
|
||||
address string
|
||||
api_key string
|
||||
}
|
||||
|
||||
pub fn new(address string, api_key string) Client {
|
||||
return Client{
|
||||
address: address
|
||||
api_key: api_key
|
||||
}
|
||||
}
|
||||
|
||||
// send_request<T> is a convenience method for sending requests to the repos
|
||||
// API. It mostly does string manipulation to create a query string containing
|
||||
// the provided params.
|
||||
fn (c &Client) send_request<T>(method http.Method, url string, params map[string]string) ?Response<T> {
|
||||
mut full_url := '${c.address}$url'
|
||||
|
||||
if params.len > 0 {
|
||||
params_str := params.keys().map('$it=${params[it]}').join('&')
|
||||
|
||||
full_url = '$full_url?$params_str'
|
||||
}
|
||||
|
||||
mut req := http.new_request(method, full_url, '') ?
|
||||
req.add_custom_header('X-API-Key', c.api_key) ?
|
||||
|
||||
res := req.do() ?
|
||||
data := json.decode(Response<T>, res.text) ?
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
51
src/client/git.v
Normal file
51
src/client/git.v
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
module client
|
||||
|
||||
import db
|
||||
import net.http
|
||||
import response { Response }
|
||||
|
||||
// get_repos returns the current list of repos.
|
||||
pub fn (c &Client) get_git_repos() ?[]db.GitRepo {
|
||||
data := c.send_request<[]db.GitRepo>(http.Method.get, '/api/repos', {}) ?
|
||||
|
||||
return data.data
|
||||
}
|
||||
|
||||
// get_repo returns the repo for a specific ID.
|
||||
pub fn (c &Client) get_git_repo(id int) ?db.GitRepo {
|
||||
data := c.send_request<db.GitRepo>(http.Method.get, '/api/repos/$id', {}) ?
|
||||
|
||||
return data.data
|
||||
}
|
||||
|
||||
// add_repo adds a new repo to the server.
|
||||
pub fn (c &Client) add_git_repo(url string, branch string, repo string, arch []string) ?Response<string> {
|
||||
mut params := {
|
||||
'url': url
|
||||
'branch': branch
|
||||
'repo': repo
|
||||
}
|
||||
|
||||
if arch.len > 0 {
|
||||
params['arch'] = arch.join(',')
|
||||
}
|
||||
|
||||
data := c.send_request<string>(http.Method.post, '/api/repos', params) ?
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
// remove_repo removes the repo with the given ID from the server.
|
||||
pub fn (c &Client) remove_git_repo(id int) ?Response<string> {
|
||||
data := c.send_request<string>(http.Method.delete, '/api/repos/$id', {}) ?
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
// patch_repo sends a PATCH request to the given repo with the params as
|
||||
// payload.
|
||||
pub fn (c &Client) patch_git_repo(id int, params map[string]string) ?Response<string> {
|
||||
data := c.send_request<string>(http.Method.patch, '/api/repos/$id', params) ?
|
||||
|
||||
return data
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue