forked from vieter-v/vieter
refactor: renamed api routes & client code to 'targets'
parent
6b79f7b5ed
commit
4d581da7bf
|
@ -1,73 +0,0 @@
|
||||||
module client
|
|
||||||
|
|
||||||
import models { GitRepo, GitRepoFilter }
|
|
||||||
import net.http { Method }
|
|
||||||
import response { Response }
|
|
||||||
|
|
||||||
// get_git_repos returns a list of GitRepo's, given a filter object.
|
|
||||||
pub fn (c &Client) get_git_repos(filter GitRepoFilter) ?[]GitRepo {
|
|
||||||
params := models.params_from(filter)
|
|
||||||
data := c.send_request<[]GitRepo>(Method.get, '/api/v1/repos', params)?
|
|
||||||
|
|
||||||
return data.data
|
|
||||||
}
|
|
||||||
|
|
||||||
// get_all_git_repos retrieves *all* GitRepo's from the API using the default
|
|
||||||
// limit.
|
|
||||||
pub fn (c &Client) get_all_git_repos() ?[]GitRepo {
|
|
||||||
mut repos := []GitRepo{}
|
|
||||||
mut offset := u64(0)
|
|
||||||
|
|
||||||
for {
|
|
||||||
sub_repos := c.get_git_repos(offset: offset)?
|
|
||||||
|
|
||||||
if sub_repos.len == 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
repos << sub_repos
|
|
||||||
|
|
||||||
offset += u64(sub_repos.len)
|
|
||||||
}
|
|
||||||
|
|
||||||
return repos
|
|
||||||
}
|
|
||||||
|
|
||||||
// get_git_repo returns the repo for a specific ID.
|
|
||||||
pub fn (c &Client) get_git_repo(id int) ?GitRepo {
|
|
||||||
data := c.send_request<GitRepo>(Method.get, '/api/v1/repos/$id', {})?
|
|
||||||
|
|
||||||
return data.data
|
|
||||||
}
|
|
||||||
|
|
||||||
// add_git_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>(Method.post, '/api/v1/repos', params)?
|
|
||||||
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
|
|
||||||
// remove_git_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>(Method.delete, '/api/v1/repos/$id', {})?
|
|
||||||
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
|
|
||||||
// patch_git_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>(Method.patch, '/api/v1/repos/$id', params)?
|
|
||||||
|
|
||||||
return data
|
|
||||||
}
|
|
|
@ -0,0 +1,73 @@
|
||||||
|
module client
|
||||||
|
|
||||||
|
import models { GitRepo, GitRepoFilter }
|
||||||
|
import net.http { Method }
|
||||||
|
import response { Response }
|
||||||
|
|
||||||
|
// get_targets returns a list of GitRepo's, given a filter object.
|
||||||
|
pub fn (c &Client) get_targets(filter GitRepoFilter) ?[]GitRepo {
|
||||||
|
params := models.params_from(filter)
|
||||||
|
data := c.send_request<[]GitRepo>(Method.get, '/api/v1/targets', params)?
|
||||||
|
|
||||||
|
return data.data
|
||||||
|
}
|
||||||
|
|
||||||
|
// get_all_targets retrieves *all* GitRepo's from the API using the default
|
||||||
|
// limit.
|
||||||
|
pub fn (c &Client) get_all_targets() ?[]GitRepo {
|
||||||
|
mut repos := []GitRepo{}
|
||||||
|
mut offset := u64(0)
|
||||||
|
|
||||||
|
for {
|
||||||
|
sub_repos := c.get_targets(offset: offset)?
|
||||||
|
|
||||||
|
if sub_repos.len == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
repos << sub_repos
|
||||||
|
|
||||||
|
offset += u64(sub_repos.len)
|
||||||
|
}
|
||||||
|
|
||||||
|
return repos
|
||||||
|
}
|
||||||
|
|
||||||
|
// get_target returns the repo for a specific ID.
|
||||||
|
pub fn (c &Client) get_target(id int) ?GitRepo {
|
||||||
|
data := c.send_request<GitRepo>(Method.get, '/api/v1/targets/$id', {})?
|
||||||
|
|
||||||
|
return data.data
|
||||||
|
}
|
||||||
|
|
||||||
|
// add_target adds a new repo to the server.
|
||||||
|
pub fn (c &Client) add_target(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>(Method.post, '/api/v1/targets', params)?
|
||||||
|
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove_target removes the repo with the given ID from the server.
|
||||||
|
pub fn (c &Client) remove_target(id int) ?Response<string> {
|
||||||
|
data := c.send_request<string>(Method.delete, '/api/v1/targets/$id', {})?
|
||||||
|
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
// patch_target sends a PATCH request to the given repo with the params as
|
||||||
|
// payload.
|
||||||
|
pub fn (c &Client) patch_target(id int, params map[string]string) ?Response<string> {
|
||||||
|
data := c.send_request<string>(Method.patch, '/api/v1/targets/$id', params)?
|
||||||
|
|
||||||
|
return data
|
||||||
|
}
|
|
@ -8,7 +8,7 @@ import build
|
||||||
// build builds every Git repo in the server's list.
|
// build builds every Git repo in the server's list.
|
||||||
fn build(conf Config, repo_id int) ? {
|
fn build(conf Config, repo_id int) ? {
|
||||||
c := client.new(conf.address, conf.api_key)
|
c := client.new(conf.address, conf.api_key)
|
||||||
repo := c.get_git_repo(repo_id)?
|
repo := c.get_target(repo_id)?
|
||||||
|
|
||||||
build_arch := os.uname().machine
|
build_arch := os.uname().machine
|
||||||
|
|
||||||
|
|
|
@ -170,7 +170,7 @@ pub fn cmd() cli.Command {
|
||||||
// list prints out a list of all repositories.
|
// list prints out a list of all repositories.
|
||||||
fn list(conf Config, filter GitRepoFilter) ? {
|
fn list(conf Config, filter GitRepoFilter) ? {
|
||||||
c := client.new(conf.address, conf.api_key)
|
c := client.new(conf.address, conf.api_key)
|
||||||
repos := c.get_git_repos(filter)?
|
repos := c.get_targets(filter)?
|
||||||
data := repos.map([it.id.str(), it.url, it.branch, it.repo])
|
data := repos.map([it.id.str(), it.url, it.branch, it.repo])
|
||||||
|
|
||||||
println(console.pretty_table(['id', 'url', 'branch', 'repo'], data)?)
|
println(console.pretty_table(['id', 'url', 'branch', 'repo'], data)?)
|
||||||
|
@ -179,7 +179,7 @@ fn list(conf Config, filter GitRepoFilter) ? {
|
||||||
// add adds a new repository to the server's list.
|
// add adds a new repository to the server's list.
|
||||||
fn add(conf Config, url string, branch string, repo string) ? {
|
fn add(conf Config, url string, branch string, repo string) ? {
|
||||||
c := client.new(conf.address, conf.api_key)
|
c := client.new(conf.address, conf.api_key)
|
||||||
res := c.add_git_repo(url, branch, repo, [])?
|
res := c.add_target(url, branch, repo, [])?
|
||||||
|
|
||||||
println(res.message)
|
println(res.message)
|
||||||
}
|
}
|
||||||
|
@ -191,7 +191,7 @@ fn remove(conf Config, id string) ? {
|
||||||
|
|
||||||
if id_int != 0 {
|
if id_int != 0 {
|
||||||
c := client.new(conf.address, conf.api_key)
|
c := client.new(conf.address, conf.api_key)
|
||||||
res := c.remove_git_repo(id_int)?
|
res := c.remove_target(id_int)?
|
||||||
println(res.message)
|
println(res.message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -209,7 +209,7 @@ fn patch(conf Config, id string, params map[string]string) ? {
|
||||||
id_int := id.int()
|
id_int := id.int()
|
||||||
if id_int != 0 {
|
if id_int != 0 {
|
||||||
c := client.new(conf.address, conf.api_key)
|
c := client.new(conf.address, conf.api_key)
|
||||||
res := c.patch_git_repo(id_int, params)?
|
res := c.patch_target(id_int, params)?
|
||||||
|
|
||||||
println(res.message)
|
println(res.message)
|
||||||
}
|
}
|
||||||
|
@ -224,6 +224,6 @@ fn info(conf Config, id string) ? {
|
||||||
}
|
}
|
||||||
|
|
||||||
c := client.new(conf.address, conf.api_key)
|
c := client.new(conf.address, conf.api_key)
|
||||||
repo := c.get_git_repo(id_int)?
|
repo := c.get_target(id_int)?
|
||||||
println(repo)
|
println(repo)
|
||||||
}
|
}
|
||||||
|
|
|
@ -178,7 +178,7 @@ fn (mut d Daemon) schedule_build(repo GitRepo) {
|
||||||
fn (mut d Daemon) renew_repos() {
|
fn (mut d Daemon) renew_repos() {
|
||||||
d.linfo('Renewing repos...')
|
d.linfo('Renewing repos...')
|
||||||
|
|
||||||
mut new_repos := d.client.get_all_git_repos() or {
|
mut new_repos := d.client.get_all_targets() or {
|
||||||
d.lerror('Failed to renew repos. Retrying in ${daemon.api_update_retry_timeout}s...')
|
d.lerror('Failed to renew repos. Retrying in ${daemon.api_update_retry_timeout}s...')
|
||||||
d.api_update_timestamp = time.now().add_seconds(daemon.api_update_retry_timeout)
|
d.api_update_timestamp = time.now().add_seconds(daemon.api_update_retry_timeout)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
This module contains the Vieter HTTP server, consisting of the repository
|
||||||
|
implementation & the REST API.
|
||||||
|
|
||||||
|
**NOTE**: vweb defines the priority order of routes by the file names in this
|
||||||
|
module. Therefore, it's very important that all API routes are defined in files
|
||||||
|
prefixed with `api_`, as this is before the word `routes` alphabetically.
|
|
@ -6,9 +6,9 @@ import response { new_data_response, new_response }
|
||||||
import db
|
import db
|
||||||
import models { GitRepo, GitRepoArch, GitRepoFilter }
|
import models { GitRepo, GitRepoArch, GitRepoFilter }
|
||||||
|
|
||||||
// v1_get_repos returns the current list of repos.
|
// v1_get_targets returns the current list of repos.
|
||||||
['/api/v1/repos'; get]
|
['/api/v1/targets'; get]
|
||||||
fn (mut app App) v1_get_repos() web.Result {
|
fn (mut app App) v1_get_targets() web.Result {
|
||||||
if !app.is_authorized() {
|
if !app.is_authorized() {
|
||||||
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
||||||
}
|
}
|
||||||
|
@ -21,9 +21,9 @@ fn (mut app App) v1_get_repos() web.Result {
|
||||||
return app.json(http.Status.ok, new_data_response(repos))
|
return app.json(http.Status.ok, new_data_response(repos))
|
||||||
}
|
}
|
||||||
|
|
||||||
// v1_get_single_repo returns the information for a single repo.
|
// v1_get_single_target returns the information for a single repo.
|
||||||
['/api/v1/repos/:id'; get]
|
['/api/v1/targets/:id'; get]
|
||||||
fn (mut app App) v1_get_single_repo(id int) web.Result {
|
fn (mut app App) v1_get_single_target(id int) web.Result {
|
||||||
if !app.is_authorized() {
|
if !app.is_authorized() {
|
||||||
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
||||||
}
|
}
|
||||||
|
@ -33,9 +33,9 @@ fn (mut app App) v1_get_single_repo(id int) web.Result {
|
||||||
return app.json(http.Status.ok, new_data_response(repo))
|
return app.json(http.Status.ok, new_data_response(repo))
|
||||||
}
|
}
|
||||||
|
|
||||||
// v1_post_repo creates a new repo from the provided query string.
|
// v1_post_target creates a new repo from the provided query string.
|
||||||
['/api/v1/repos'; post]
|
['/api/v1/targets'; post]
|
||||||
fn (mut app App) v1_post_repo() web.Result {
|
fn (mut app App) v1_post_target() web.Result {
|
||||||
if !app.is_authorized() {
|
if !app.is_authorized() {
|
||||||
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
||||||
}
|
}
|
||||||
|
@ -57,9 +57,9 @@ fn (mut app App) v1_post_repo() web.Result {
|
||||||
return app.json(http.Status.ok, new_response('Repo added successfully.'))
|
return app.json(http.Status.ok, new_response('Repo added successfully.'))
|
||||||
}
|
}
|
||||||
|
|
||||||
// v1_delete_repo removes a given repo from the server's list.
|
// v1_delete_target removes a given repo from the server's list.
|
||||||
['/api/v1/repos/:id'; delete]
|
['/api/v1/targets/:id'; delete]
|
||||||
fn (mut app App) v1_delete_repo(id int) web.Result {
|
fn (mut app App) v1_delete_target(id int) web.Result {
|
||||||
if !app.is_authorized() {
|
if !app.is_authorized() {
|
||||||
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
||||||
}
|
}
|
||||||
|
@ -69,9 +69,9 @@ fn (mut app App) v1_delete_repo(id int) web.Result {
|
||||||
return app.json(http.Status.ok, new_response('Repo removed successfully.'))
|
return app.json(http.Status.ok, new_response('Repo removed successfully.'))
|
||||||
}
|
}
|
||||||
|
|
||||||
// v1_patch_repo updates a repo's data with the given query params.
|
// v1_patch_target updates a repo's data with the given query params.
|
||||||
['/api/v1/repos/:id'; patch]
|
['/api/v1/targets/:id'; patch]
|
||||||
fn (mut app App) v1_patch_repo(id int) web.Result {
|
fn (mut app App) v1_patch_target(id int) web.Result {
|
||||||
if !app.is_authorized() {
|
if !app.is_authorized() {
|
||||||
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
||||||
}
|
}
|
Loading…
Reference in New Issue