83 lines
1.3 KiB
V
83 lines
1.3 KiB
V
module aur
|
|
|
|
import net.urllib
|
|
import net.http
|
|
import json
|
|
|
|
pub struct Client {
|
|
url string = aur_rpc_url
|
|
}
|
|
|
|
pub fn new() &Client {
|
|
return &Client{}
|
|
}
|
|
|
|
pub fn new_with_url(url string) &Client {
|
|
if !url.ends_with('/') {
|
|
return &Client{
|
|
url: '${url}/'
|
|
}
|
|
}
|
|
|
|
return &Client{
|
|
url: url
|
|
}
|
|
}
|
|
|
|
struct Response {
|
|
@type string [json: 'type']
|
|
error string
|
|
results []Package
|
|
}
|
|
|
|
fn (c Client) request(params map[string][]string) ![]Package {
|
|
mut param_strings := []string{}
|
|
|
|
for k, v in params {
|
|
param_strings << v.map(urllib.query_escape(it)).map('${k}=${it}')
|
|
}
|
|
|
|
url := '${c.url}?${param_strings.join('&')}'
|
|
|
|
res := http.get(url)!
|
|
|
|
if res.status_code < 200 || res.status_code >= 300 {
|
|
// temporary
|
|
return error('error')
|
|
}
|
|
|
|
data := json.decode(Response, res.body)!
|
|
|
|
if data.@type == 'error' {
|
|
return error('Server responded with an error: ${data.error}')
|
|
}
|
|
|
|
return data.results
|
|
}
|
|
|
|
pub fn (c Client) search_by(arg string, st SearchType) ![]Package {
|
|
params := {
|
|
'v': ['5']
|
|
'type': ['search']
|
|
'by': ['${st}']
|
|
'arg': [arg]
|
|
}
|
|
|
|
return c.request(params)
|
|
}
|
|
|
|
// search performs a search by name_desc.
|
|
pub fn (c Client) search(arg string) ![]Package {
|
|
return c.search_by(arg, .name_desc)
|
|
}
|
|
|
|
pub fn (c Client) info(pkgs []string) ![]Package {
|
|
params := {
|
|
'v': ['5']
|
|
'type': ['info']
|
|
'arg[]': pkgs
|
|
}
|
|
|
|
return c.request(params)
|
|
}
|