50 lines
921 B
V
50 lines
921 B
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 SuccessResponse {
|
|
result_count int [json: resultCount]
|
|
results []Package
|
|
}
|
|
|
|
fn (c Client) request(params map[string]string) ?[]Package {
|
|
mut params_escaped := map[string]string{}
|
|
|
|
for k, v in params {
|
|
params_escaped[k] = urllib.query_escape(v)
|
|
}
|
|
|
|
params_str := params_escaped.keys().map('$it=${params[it]}').join('&')
|
|
|
|
url := '$c.url?$params_str'
|
|
|
|
res := http.get(url)?
|
|
|
|
if res.status_code < 200 || res.status_code >= 300 {
|
|
// temporary
|
|
return error('error')
|
|
}
|
|
|
|
data := json.decode(SuccessResponse, res.body)?
|
|
|
|
return data.results
|
|
}
|