47 lines
1.2 KiB
Go
47 lines
1.2 KiB
Go
package inventaire
|
|
|
|
import (
|
|
"net/url"
|
|
|
|
"git.artlef.fr/PersonalLibraryManager/internal/callapiutils"
|
|
)
|
|
|
|
type InventaireSearchResult struct {
|
|
Results []InventaireSearchBook `json:"results"`
|
|
Total int `json:"total"`
|
|
}
|
|
|
|
type InventaireSearchBook struct {
|
|
ID string `json:"id"`
|
|
Label string `json:"label"`
|
|
Description string `json:"description"`
|
|
Image string `json:"image"`
|
|
}
|
|
|
|
func GetBaseInventaireUrl() string {
|
|
return "https://inventaire.io"
|
|
}
|
|
|
|
func computeInventaireApiUrl(paths ...string) (*url.URL, error) {
|
|
baseUrl := GetBaseInventaireUrl() + "/api"
|
|
return callapiutils.ComputeUrl(baseUrl, paths...)
|
|
}
|
|
|
|
func CallInventaireSearch(searchterm string, limit int, offset int) (InventaireSearchResult, error) {
|
|
var queryResult InventaireSearchResult
|
|
u, err := computeInventaireApiUrl("search")
|
|
if err != nil {
|
|
return queryResult, err
|
|
}
|
|
if limit != 0 {
|
|
callapiutils.AddQueryParamInt(u, "limit", limit)
|
|
}
|
|
if offset != 0 {
|
|
callapiutils.AddQueryParamInt(u, "offset", offset)
|
|
}
|
|
callapiutils.AddQueryParam(u, "types", "works")
|
|
callapiutils.AddQueryParam(u, "search", searchterm)
|
|
err = callapiutils.FetchAndParseResult(u, &queryResult)
|
|
return queryResult, err
|
|
}
|