feat: completed Package struct

main
Jef Roosens 2022-06-22 11:51:03 +02:00
parent 442e01c53f
commit 3d39132646
Signed by: Jef Roosens
GPG Key ID: B580B976584B5F30
3 changed files with 77 additions and 48 deletions

26
aur.v
View File

@ -4,7 +4,27 @@ const aur_rpc_url = 'https://aur.archlinux.org/rpc/'
pub struct Package { pub struct Package {
pub: pub:
id u32 [json: ID] id u32 [json: ID]
name string [json: Name] name string [json: Name]
package_base_id u32 [json: PackageBaseID] package_base_id u32 [json: PackageBaseID]
version string [json: Version]
description string [json: Description]
url string [json: URL]
num_votes u32 [json: NumVotes]
popularity f64 [json: Popularity]
out_of_date i64 [json: OutOfDate]
maintainer string [json: Maintainer]
first_submitted i64 [json: FirstSubmitted]
last_modified i64 [json: LastModified]
url_path string [json: URLPath]
groups []string [json: Groups]
depends []string [json: Depends]
make_depends []string [json: MakeDepends]
opt_depends []string [json: OptDepends]
check_depends []string [json: CheckDepends]
conflicts []string [json: Conflicts]
replaces []string [json: Replaces]
provides []string [json: Provides]
license []string [json: License]
keywords []string [json: Keywords]
} }

View File

@ -5,45 +5,49 @@ import net.http
import json import json
pub struct Client { pub struct Client {
url string = aur_rpc_url url string = aur_rpc_url
} }
pub fn new() &Client { pub fn new() &Client {
return &Client{} return &Client{}
} }
pub fn new_with_url(url string) &Client { pub fn new_with_url(url string) &Client {
if !url.ends_with('/') { if !url.ends_with('/') {
return &Client{url: '$url/'} return &Client{
} url: '$url/'
}
}
return &Client{url: url} return &Client{
url: url
}
} }
struct SuccessResponse { struct SuccessResponse {
result_count int [json: resultCount] result_count int [json: resultCount]
results []Package results []Package
} }
fn (c Client) request(params map[string]string) ?[]Package { fn (c Client) request(params map[string]string) ?[]Package {
mut params_escaped := map[string]string{} mut params_escaped := map[string]string{}
for k, v in params { for k, v in params {
params_escaped[k] = urllib.query_escape(v) params_escaped[k] = urllib.query_escape(v)
} }
params_str := params_escaped.keys().map('$it=${params[it]}').join('&') params_str := params_escaped.keys().map('$it=${params[it]}').join('&')
url := '$c.url?$params_str' url := '$c.url?$params_str'
res := http.get(url)? res := http.get(url)?
if res.status_code < 200 || res.status_code >= 300 { if res.status_code < 200 || res.status_code >= 300 {
// temporary // temporary
return error('error') return error('error')
} }
data := json.decode(SuccessResponse, res.body)? data := json.decode(SuccessResponse, res.body)?
return data.results return data.results
} }

View File

@ -1,34 +1,39 @@
module aur module aur
pub enum SearchType { pub enum SearchType {
name name
name_desc name_desc
maintainer maintainer
depends depends
make_depends make_depends
opt_depends opt_depends
check_depends check_depends
} }
pub fn (st SearchType) str() string { pub fn (st SearchType) str() string {
return match st { return match st {
.name { 'name' } .name { 'name' }
.name_desc { 'name-desc' } .name_desc { 'name-desc' }
.maintainer { 'maintainer' } .maintainer { 'maintainer' }
.depends { 'depends' } .depends { 'depends' }
.make_depends { 'makedepends' } .make_depends { 'makedepends' }
.opt_depends { 'optdepends' } .opt_depends { 'optdepends' }
.check_depends { 'checkdepends' } .check_depends { 'checkdepends' }
} }
} }
pub fn (c Client) search_by(arg string, st SearchType) ?[]Package { pub fn (c Client) search_by(arg string, st SearchType) ?[]Package {
params := { params := {
'v': '5' 'v': '5'
'type': 'search' 'type': 'search'
'by': '$st' 'by': '$st'
'arg': arg 'arg': arg
} }
return c.request(params) 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)
} }