2022-04-07 12:07:56 +02:00
|
|
|
module git
|
|
|
|
|
|
|
|
import json
|
|
|
|
import response { Response }
|
|
|
|
import net.http
|
2022-05-03 16:16:56 +02:00
|
|
|
import db
|
2022-04-07 12:07:56 +02:00
|
|
|
|
2022-04-30 20:22:03 +02:00
|
|
|
// 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.
|
2022-04-07 14:28:21 +02:00
|
|
|
fn send_request<T>(method http.Method, address string, url string, api_key string, params map[string]string) ?Response<T> {
|
|
|
|
mut full_url := '$address$url'
|
|
|
|
|
|
|
|
if params.len > 0 {
|
|
|
|
params_str := params.keys().map('$it=${params[it]}').join('&')
|
|
|
|
|
2022-04-07 14:50:07 +02:00
|
|
|
full_url = '$full_url?$params_str'
|
2022-04-07 14:28:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mut req := http.new_request(method, full_url, '') ?
|
2022-04-07 12:07:56 +02:00
|
|
|
req.add_custom_header('X-API-Key', api_key) ?
|
|
|
|
|
|
|
|
res := req.do() ?
|
2022-04-07 14:28:21 +02:00
|
|
|
data := json.decode(Response<T>, res.text) ?
|
|
|
|
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
// get_repos returns the current list of repos.
|
2022-05-03 16:16:56 +02:00
|
|
|
pub fn get_repos(address string, api_key string) ?[]db.GitRepo {
|
|
|
|
data := send_request<[]db.GitRepo>(http.Method.get, address, '/api/repos',
|
|
|
|
api_key, {}) ?
|
|
|
|
|
|
|
|
return data.data
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_repo(address string, api_key string, id int) ?db.GitRepo {
|
|
|
|
data := send_request<db.GitRepo>(http.Method.get, address, '/api/repos/$id', api_key,
|
2022-04-07 14:50:07 +02:00
|
|
|
{}) ?
|
2022-04-07 12:07:56 +02:00
|
|
|
|
|
|
|
return data.data
|
|
|
|
}
|
|
|
|
|
2022-04-07 12:10:37 +02:00
|
|
|
// add_repo adds a new repo to the server.
|
2022-04-07 14:28:21 +02:00
|
|
|
pub fn add_repo(address string, api_key string, url string, branch string, repo string, arch []string) ?Response<string> {
|
2022-05-01 12:44:54 +02:00
|
|
|
mut params := {
|
2022-04-07 14:50:07 +02:00
|
|
|
'url': url
|
2022-04-07 14:28:21 +02:00
|
|
|
'branch': branch
|
2022-04-07 14:50:07 +02:00
|
|
|
'repo': repo
|
2022-04-07 14:28:21 +02:00
|
|
|
}
|
2022-05-01 12:44:54 +02:00
|
|
|
|
|
|
|
if arch.len > 0 {
|
|
|
|
params['arch'] = arch.join(',')
|
|
|
|
}
|
|
|
|
|
2022-04-07 14:28:21 +02:00
|
|
|
data := send_request<string>(http.Method.post, address, '/api/repos', api_key, params) ?
|
2022-04-07 12:07:56 +02:00
|
|
|
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2022-04-07 12:10:37 +02:00
|
|
|
// remove_repo removes the repo with the given ID from the server.
|
2022-05-03 16:16:56 +02:00
|
|
|
pub fn remove_repo(address string, api_key string, id int) ?Response<string> {
|
2022-04-07 14:50:07 +02:00
|
|
|
data := send_request<string>(http.Method.delete, address, '/api/repos/$id', api_key,
|
|
|
|
{}) ?
|
2022-04-07 12:07:56 +02:00
|
|
|
|
|
|
|
return data
|
|
|
|
}
|
2022-04-07 15:08:27 +02:00
|
|
|
|
|
|
|
// patch_repo sends a PATCH request to the given repo with the params as
|
|
|
|
// payload.
|
2022-05-03 16:16:56 +02:00
|
|
|
pub fn patch_repo(address string, api_key string, id int, params map[string]string) ?Response<string> {
|
2022-04-07 15:08:27 +02:00
|
|
|
data := send_request<string>(http.Method.patch, address, '/api/repos/$id', api_key,
|
|
|
|
params) ?
|
|
|
|
|
|
|
|
return data
|
|
|
|
}
|