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:
id u32 [json: ID]
name string [json: Name]
package_base_id u32 [json: PackageBaseID]
id u32 [json: ID]
name string [json: Name]
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
pub struct Client {
url string = aur_rpc_url
url string = aur_rpc_url
}
pub fn new() &Client {
return &Client{}
return &Client{}
}
pub fn new_with_url(url string) &Client {
if !url.ends_with('/') {
return &Client{url: '$url/'}
}
if !url.ends_with('/') {
return &Client{
url: '$url/'
}
}
return &Client{url: url}
return &Client{
url: url
}
}
struct SuccessResponse {
result_count int [json: resultCount]
results []Package
result_count int [json: resultCount]
results []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 {
params_escaped[k] = urllib.query_escape(v)
}
for k, v in params {
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 {
// temporary
return error('error')
}
if res.status_code < 200 || res.status_code >= 300 {
// temporary
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
pub enum SearchType {
name
name_desc
maintainer
depends
make_depends
opt_depends
check_depends
name
name_desc
maintainer
depends
make_depends
opt_depends
check_depends
}
pub fn (st SearchType) str() string {
return match st {
.name { 'name' }
.name_desc { 'name-desc' }
.maintainer { 'maintainer' }
.depends { 'depends' }
.make_depends { 'makedepends' }
.opt_depends { 'optdepends' }
.check_depends { 'checkdepends' }
}
return match st {
.name { 'name' }
.name_desc { 'name-desc' }
.maintainer { 'maintainer' }
.depends { 'depends' }
.make_depends { 'makedepends' }
.opt_depends { 'optdepends' }
.check_depends { 'checkdepends' }
}
}
pub fn (c Client) search_by(arg string, st SearchType) ?[]Package {
params := {
'v': '5'
'type': 'search'
'by': '$st'
'arg': arg
}
params := {
'v': '5'
'type': 'search'
'by': '$st'
'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)
}