aur/client.v

78 lines
1.3 KiB
Coq
Raw Normal View History

2022-06-22 11:42:16 +02:00
module aur
import net.urllib
import net.http
import json
pub struct Client {
2022-06-22 11:51:03 +02:00
url string = aur_rpc_url
2022-06-22 11:42:16 +02:00
}
pub fn new() &Client {
2022-06-22 11:51:03 +02:00
return &Client{}
2022-06-22 11:42:16 +02:00
}
pub fn new_with_url(url string) &Client {
2022-06-22 11:51:03 +02:00
if !url.ends_with('/') {
return &Client{
url: '$url/'
}
}
return &Client{
url: url
}
2022-06-22 11:42:16 +02:00
}
struct SuccessResponse {
2022-06-22 11:51:03 +02:00
result_count int [json: resultCount]
results []Package
2022-06-22 11:42:16 +02:00
}
2022-06-22 13:42:55 +02:00
fn (c Client) request(params map[string][]string) ?[]Package {
mut param_strings := []string{}
2022-06-22 11:51:03 +02:00
for k, v in params {
2022-06-22 13:42:55 +02:00
param_strings << v.map(urllib.query_escape(it)).map('$k=$it')
2022-06-22 11:51:03 +02:00
}
2022-06-22 11:42:16 +02:00
2022-06-22 13:42:55 +02:00
url := '$c.url?${param_strings.join('&')}'
2022-06-22 11:42:16 +02:00
2022-06-22 11:51:03 +02:00
res := http.get(url)?
2022-06-22 11:42:16 +02:00
2022-06-22 11:51:03 +02:00
if res.status_code < 200 || res.status_code >= 300 {
// temporary
return error('error')
}
2022-06-22 11:42:16 +02:00
2022-06-22 11:51:03 +02:00
data := json.decode(SuccessResponse, res.body)?
2022-06-22 11:42:16 +02:00
2022-06-22 11:51:03 +02:00
return data.results
2022-06-22 11:42:16 +02:00
}
2022-06-22 13:42:55 +02:00
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)
}