56 lines
793 B
V
56 lines
793 B
V
module gitea
|
|
|
|
import net.http
|
|
import json
|
|
|
|
const api_prefix = '/api/v1'
|
|
|
|
pub struct Client {
|
|
base_url string
|
|
api_key string
|
|
}
|
|
|
|
[params]
|
|
pub struct PagingConfig {
|
|
page int
|
|
limit int
|
|
}
|
|
|
|
fn (p PagingConfig) str() string {
|
|
if p.page == 0 && p.limit == 0 {
|
|
return ''
|
|
}
|
|
|
|
mut parts := []string{}
|
|
|
|
if p.page != 0 {
|
|
parts << 'page=$p.page'
|
|
}
|
|
|
|
if p.limit != 0 {
|
|
parts << 'limit=$p.limit'
|
|
}
|
|
|
|
return parts.join('&')
|
|
}
|
|
|
|
pub fn new(base_url string, api_key string) Client {
|
|
return Client{
|
|
base_url: base_url.trim_right('/') + gitea.api_prefix
|
|
api_key: api_key
|
|
}
|
|
}
|
|
|
|
fn (c &Client) get<T>(path string, conf PagingConfig) !T {
|
|
mut url := '$c.base_url$path'
|
|
|
|
if conf.str() != '' {
|
|
url += '&$conf.str()'
|
|
}
|
|
|
|
res := http.get(url)!
|
|
data := json.decode(T, res.body)!
|
|
|
|
return data
|
|
}
|