From 4d581da7bf08fa73510c29aff9e11b1741dcce1d Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 14 Jun 2022 20:31:00 +0200 Subject: [PATCH] refactor: renamed api routes & client code to 'targets' --- src/client/git.v | 73 ----------------------------- src/client/targets.v | 73 +++++++++++++++++++++++++++++ src/console/git/build.v | 2 +- src/console/git/git.v | 10 ++-- src/cron/daemon/daemon.v | 2 +- src/server/README.md | 6 +++ src/server/{logs.v => api_logs.v} | 0 src/server/{git.v => api_targets.v} | 30 ++++++------ 8 files changed, 101 insertions(+), 95 deletions(-) delete mode 100644 src/client/git.v create mode 100644 src/client/targets.v create mode 100644 src/server/README.md rename src/server/{logs.v => api_logs.v} (100%) rename src/server/{git.v => api_targets.v} (72%) diff --git a/src/client/git.v b/src/client/git.v deleted file mode 100644 index 7b5e52e..0000000 --- a/src/client/git.v +++ /dev/null @@ -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(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 { - mut params := { - 'url': url - 'branch': branch - 'repo': repo - } - - if arch.len > 0 { - params['arch'] = arch.join(',') - } - - data := c.send_request(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 { - data := c.send_request(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 { - data := c.send_request(Method.patch, '/api/v1/repos/$id', params)? - - return data -} diff --git a/src/client/targets.v b/src/client/targets.v new file mode 100644 index 0000000..90ff1a2 --- /dev/null +++ b/src/client/targets.v @@ -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(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 { + mut params := { + 'url': url + 'branch': branch + 'repo': repo + } + + if arch.len > 0 { + params['arch'] = arch.join(',') + } + + data := c.send_request(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 { + data := c.send_request(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 { + data := c.send_request(Method.patch, '/api/v1/targets/$id', params)? + + return data +} diff --git a/src/console/git/build.v b/src/console/git/build.v index fac760d..b3c46a1 100644 --- a/src/console/git/build.v +++ b/src/console/git/build.v @@ -8,7 +8,7 @@ import build // build builds every Git repo in the server's list. fn build(conf Config, repo_id int) ? { 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 diff --git a/src/console/git/git.v b/src/console/git/git.v index 54cfaaa..6a4b9a8 100644 --- a/src/console/git/git.v +++ b/src/console/git/git.v @@ -170,7 +170,7 @@ pub fn cmd() cli.Command { // list prints out a list of all repositories. fn list(conf Config, filter GitRepoFilter) ? { 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]) 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. fn add(conf Config, url string, branch string, repo string) ? { 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) } @@ -191,7 +191,7 @@ fn remove(conf Config, id string) ? { if id_int != 0 { c := client.new(conf.address, conf.api_key) - res := c.remove_git_repo(id_int)? + res := c.remove_target(id_int)? println(res.message) } } @@ -209,7 +209,7 @@ 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) - res := c.patch_git_repo(id_int, params)? + res := c.patch_target(id_int, params)? println(res.message) } @@ -224,6 +224,6 @@ fn info(conf Config, id string) ? { } c := client.new(conf.address, conf.api_key) - repo := c.get_git_repo(id_int)? + repo := c.get_target(id_int)? println(repo) } diff --git a/src/cron/daemon/daemon.v b/src/cron/daemon/daemon.v index f1206d6..1f36e74 100644 --- a/src/cron/daemon/daemon.v +++ b/src/cron/daemon/daemon.v @@ -178,7 +178,7 @@ fn (mut d Daemon) schedule_build(repo GitRepo) { fn (mut d Daemon) renew_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.api_update_timestamp = time.now().add_seconds(daemon.api_update_retry_timeout) diff --git a/src/server/README.md b/src/server/README.md new file mode 100644 index 0000000..ded9985 --- /dev/null +++ b/src/server/README.md @@ -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. diff --git a/src/server/logs.v b/src/server/api_logs.v similarity index 100% rename from src/server/logs.v rename to src/server/api_logs.v diff --git a/src/server/git.v b/src/server/api_targets.v similarity index 72% rename from src/server/git.v rename to src/server/api_targets.v index f3fe010..67117b9 100644 --- a/src/server/git.v +++ b/src/server/api_targets.v @@ -6,9 +6,9 @@ import response { new_data_response, new_response } import db import models { GitRepo, GitRepoArch, GitRepoFilter } -// v1_get_repos returns the current list of repos. -['/api/v1/repos'; get] -fn (mut app App) v1_get_repos() web.Result { +// v1_get_targets returns the current list of repos. +['/api/v1/targets'; get] +fn (mut app App) v1_get_targets() web.Result { if !app.is_authorized() { 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)) } -// v1_get_single_repo returns the information for a single repo. -['/api/v1/repos/:id'; get] -fn (mut app App) v1_get_single_repo(id int) web.Result { +// v1_get_single_target returns the information for a single repo. +['/api/v1/targets/:id'; get] +fn (mut app App) v1_get_single_target(id int) web.Result { if !app.is_authorized() { 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)) } -// v1_post_repo creates a new repo from the provided query string. -['/api/v1/repos'; post] -fn (mut app App) v1_post_repo() web.Result { +// v1_post_target creates a new repo from the provided query string. +['/api/v1/targets'; post] +fn (mut app App) v1_post_target() web.Result { if !app.is_authorized() { 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.')) } -// v1_delete_repo removes a given repo from the server's list. -['/api/v1/repos/:id'; delete] -fn (mut app App) v1_delete_repo(id int) web.Result { +// v1_delete_target removes a given repo from the server's list. +['/api/v1/targets/:id'; delete] +fn (mut app App) v1_delete_target(id int) web.Result { if !app.is_authorized() { 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.')) } -// v1_patch_repo updates a repo's data with the given query params. -['/api/v1/repos/:id'; patch] -fn (mut app App) v1_patch_repo(id int) web.Result { +// v1_patch_target updates a repo's data with the given query params. +['/api/v1/targets/:id'; patch] +fn (mut app App) v1_patch_target(id int) web.Result { if !app.is_authorized() { return app.json(http.Status.unauthorized, new_response('Unauthorized.')) }