Add a config to get inventaire.io URL
This commit is contained in:
@@ -18,18 +18,14 @@ type InventaireSearchBook struct {
|
||||
Image string `json:"image"`
|
||||
}
|
||||
|
||||
func GetBaseInventaireUrl() string {
|
||||
return "https://inventaire.io"
|
||||
}
|
||||
|
||||
func computeInventaireApiUrl(paths ...string) (*url.URL, error) {
|
||||
baseUrl := GetBaseInventaireUrl() + "/api"
|
||||
func computeInventaireApiUrl(inventaireUrl string, paths ...string) (*url.URL, error) {
|
||||
baseUrl := inventaireUrl + "/api"
|
||||
return callapiutils.ComputeUrl(baseUrl, paths...)
|
||||
}
|
||||
|
||||
func CallInventaireSearch(searchterm string, lang string, limit int, offset int) (InventaireSearchResult, error) {
|
||||
func CallInventaireSearch(inventaireUrl string, searchterm string, lang string, limit int, offset int) (InventaireSearchResult, error) {
|
||||
var queryResult InventaireSearchResult
|
||||
u, err := computeInventaireApiUrl("search")
|
||||
u, err := computeInventaireApiUrl(inventaireUrl, "search")
|
||||
if err != nil {
|
||||
return queryResult, err
|
||||
}
|
||||
|
||||
@@ -6,8 +6,12 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func getBaseInventaireUrl() string {
|
||||
return "https://inventaire.io"
|
||||
}
|
||||
|
||||
func TestCallInventaireSearch_NoParameters(t *testing.T) {
|
||||
result, err := CallInventaireSearch("salammbo", "", 0, 0)
|
||||
result, err := CallInventaireSearch(getBaseInventaireUrl(), "salammbo", "", 0, 0)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -16,7 +20,7 @@ func TestCallInventaireSearch_NoParameters(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCallInventaireSearch_NoLimit(t *testing.T) {
|
||||
result, err := CallInventaireSearch("salammbo", "fr", 0, 0)
|
||||
result, err := CallInventaireSearch(getBaseInventaireUrl(), "salammbo", "fr", 0, 0)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -25,7 +29,7 @@ func TestCallInventaireSearch_NoLimit(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCallInventaireSearch_Limit(t *testing.T) {
|
||||
result, err := CallInventaireSearch("salammbo", "fr", 5, 0)
|
||||
result, err := CallInventaireSearch(getBaseInventaireUrl(), "salammbo", "fr", 5, 0)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -34,7 +38,7 @@ func TestCallInventaireSearch_Limit(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCallInventaireSearch_Offset(t *testing.T) {
|
||||
result, err := CallInventaireSearch("salammbo", "fr", 0, 15)
|
||||
result, err := CallInventaireSearch(getBaseInventaireUrl(), "salammbo", "fr", 0, 15)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -43,7 +47,7 @@ func TestCallInventaireSearch_Offset(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCallInventaireBook_BraveNewWorld(t *testing.T) {
|
||||
result, err := callInventaireBook("wd:Q191949", "fr")
|
||||
result, err := callInventaireBook(getBaseInventaireUrl(), "wd:Q191949", "fr")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -55,7 +59,7 @@ func TestCallInventaireBook_BraveNewWorld(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCallInventaireEditionFromWork_TestLimit(t *testing.T) {
|
||||
result, err := CallInventaireEditionFromWork("wd:Q339761", "fr", 10, 0)
|
||||
result, err := CallInventaireEditionFromWork(getBaseInventaireUrl(), "wd:Q339761", "fr", 10, 0)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -64,7 +68,7 @@ func TestCallInventaireEditionFromWork_TestLimit(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCallInventaireEditionFromWork_TestOffset(t *testing.T) {
|
||||
result, err := CallInventaireEditionFromWork("wd:Q3213142", "fr", 0, 0)
|
||||
result, err := CallInventaireEditionFromWork(getBaseInventaireUrl(), "wd:Q3213142", "fr", 0, 0)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -103,7 +107,7 @@ func TestCallInventaireEditionFromWork_TestOffset(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCallInventaireEdition(t *testing.T) {
|
||||
result, err := CallInventaireEdition("isbn:9782266003698", "fr")
|
||||
result, err := CallInventaireEdition(getBaseInventaireUrl(), "isbn:9782266003698", "fr")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -127,7 +131,7 @@ func TestCallInventaireEdition(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCallInventaireEditionFromISBN(t *testing.T) {
|
||||
result, err := CallInventaireFromISBN("9782070379248", "fr")
|
||||
result, err := CallInventaireFromISBN(getBaseInventaireUrl(), "9782070379248", "fr")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
@@ -94,9 +94,9 @@ func findLangageField(multipleMessageFields map[string]json.RawMessage, lang str
|
||||
return parsedField, err
|
||||
}
|
||||
|
||||
func callInventaireBook(inventaireId string, lang string) (InventaireBookResult, error) {
|
||||
func callInventaireBook(inventaireUrl string, inventaireId string, lang string) (InventaireBookResult, error) {
|
||||
queryResult := InventaireBookResult{ID: inventaireId, Lang: lang}
|
||||
u, err := computeInventaireApiUrl("entities")
|
||||
u, err := computeInventaireApiUrl(inventaireUrl, "entities")
|
||||
if err != nil {
|
||||
return queryResult, err
|
||||
}
|
||||
|
||||
@@ -9,7 +9,8 @@ import (
|
||||
)
|
||||
|
||||
type inventaireEditionQueryResult struct {
|
||||
Entities []inventaireEditionQueryEntity
|
||||
Entities []inventaireEditionQueryEntity
|
||||
inventaireUrl string
|
||||
}
|
||||
|
||||
type inventaireEditionQueryEntity struct {
|
||||
@@ -75,7 +76,7 @@ func (i *inventaireEditionQueryResult) UnmarshalJSON(b []byte) error {
|
||||
return err
|
||||
}
|
||||
if image != "" {
|
||||
image = GetBaseInventaireUrl() + image
|
||||
image = i.inventaireUrl + image
|
||||
}
|
||||
}
|
||||
i.Entities = append(i.Entities, inventaireEditionQueryEntity{
|
||||
@@ -111,9 +112,9 @@ func parseStringArrayFieldInJsonRaw(jsonRawMap map[string]json.RawMessage, key s
|
||||
return s, err
|
||||
}
|
||||
|
||||
func callInventaireEditionEntities(uris []string) (inventaireEditionQueryResult, error) {
|
||||
var queryResult inventaireEditionQueryResult
|
||||
u, err := getInventaireEditionEntitiesUri(strings.Join(uris, "|"))
|
||||
func callInventaireEditionEntities(inventaireUrl string, uris []string) (inventaireEditionQueryResult, error) {
|
||||
queryResult := inventaireEditionQueryResult{inventaireUrl: inventaireUrl}
|
||||
u, err := getInventaireEditionEntitiesUri(inventaireUrl, strings.Join(uris, "|"))
|
||||
if err != nil {
|
||||
return queryResult, err
|
||||
}
|
||||
@@ -121,8 +122,8 @@ func callInventaireEditionEntities(uris []string) (inventaireEditionQueryResult,
|
||||
return queryResult, err
|
||||
}
|
||||
|
||||
func getInventaireEditionEntitiesUri(uris string) (*url.URL, error) {
|
||||
u, err := computeInventaireApiUrl("entities")
|
||||
func getInventaireEditionEntitiesUri(inventaireUrl string, uris string) (*url.URL, error) {
|
||||
u, err := computeInventaireApiUrl(inventaireUrl, "entities")
|
||||
if err != nil {
|
||||
return u, err
|
||||
}
|
||||
@@ -162,9 +163,9 @@ func (i *inventaireEditionPublisherResult) UnmarshalJSON(b []byte) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func callInventairePublisherGetName(editionId string, lang string) (string, error) {
|
||||
func callInventairePublisherGetName(inventaireUrl string, editionId string, lang string) (string, error) {
|
||||
var queryResult inventaireEditionPublisherResult
|
||||
u, err := computeInventaireApiUrl("entities")
|
||||
u, err := computeInventaireApiUrl(inventaireUrl, "entities")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -26,9 +26,9 @@ type inventaireReverseClaimsResult struct {
|
||||
Uris []string `json:"uris"`
|
||||
}
|
||||
|
||||
func CallInventaireEditionFromWork(workId string, lang string, limit int, offset int) (InventaireEditionResult, error) {
|
||||
func CallInventaireEditionFromWork(inventaireUrl string, workId string, lang string, limit int, offset int) (InventaireEditionResult, error) {
|
||||
var queryResult InventaireEditionResult
|
||||
uris, err := callInventaireUris(workId)
|
||||
uris, err := callInventaireUris(inventaireUrl, workId)
|
||||
if err != nil {
|
||||
return queryResult, err
|
||||
}
|
||||
@@ -41,7 +41,7 @@ func CallInventaireEditionFromWork(workId string, lang string, limit int, offset
|
||||
endIndex := int(math.Min(float64(limit+offset), float64(l)))
|
||||
limitedUris = uris.Uris[startIndex:endIndex]
|
||||
}
|
||||
editionEntities, err := callInventaireEditionEntities(limitedUris)
|
||||
editionEntities, err := callInventaireEditionEntities(inventaireUrl, limitedUris)
|
||||
|
||||
if err != nil {
|
||||
return queryResult, err
|
||||
@@ -55,7 +55,7 @@ func CallInventaireEditionFromWork(workId string, lang string, limit int, offset
|
||||
for _, entity := range sortedEntities {
|
||||
publisher := ""
|
||||
if entity.EditionId != "" {
|
||||
publisher, err = callInventairePublisherGetName(entity.EditionId, lang)
|
||||
publisher, err = callInventairePublisherGetName(inventaireUrl, entity.EditionId, lang)
|
||||
if err != nil {
|
||||
return queryResult, err
|
||||
}
|
||||
@@ -73,9 +73,9 @@ func CallInventaireEditionFromWork(workId string, lang string, limit int, offset
|
||||
return queryResult, err
|
||||
}
|
||||
|
||||
func callInventaireUris(workId string) (inventaireReverseClaimsResult, error) {
|
||||
func callInventaireUris(inventaireUrl string, workId string) (inventaireReverseClaimsResult, error) {
|
||||
var queryResult inventaireReverseClaimsResult
|
||||
u, err := computeInventaireApiUrl("entities")
|
||||
u, err := computeInventaireApiUrl(inventaireUrl, "entities")
|
||||
if err != nil {
|
||||
return queryResult, err
|
||||
}
|
||||
|
||||
@@ -24,9 +24,9 @@ func (e *ErrorEditionNotFound) Error() string {
|
||||
return fmt.Sprintf("No edition found on inventaire for id %s\n", e.InventaireId)
|
||||
}
|
||||
|
||||
func CallInventaireEdition(inventaireId string, lang string) (InventaireEditionDetailedSingleResult, error) {
|
||||
func CallInventaireEdition(inventaireUrl string, inventaireId string, lang string) (InventaireEditionDetailedSingleResult, error) {
|
||||
var result InventaireEditionDetailedSingleResult
|
||||
editionQueryResults, err := callInventaireEditionEntities([]string{inventaireId})
|
||||
editionQueryResults, err := callInventaireEditionEntities(inventaireUrl, []string{inventaireId})
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
@@ -39,7 +39,7 @@ func CallInventaireEdition(inventaireId string, lang string) (InventaireEditionD
|
||||
|
||||
var publisher string
|
||||
if editionQueryResult.EditionId != "" {
|
||||
publisher, err = callInventairePublisherGetName(editionQueryResult.EditionId, lang)
|
||||
publisher, err = callInventairePublisherGetName(inventaireUrl, editionQueryResult.EditionId, lang)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
@@ -48,7 +48,7 @@ func CallInventaireEdition(inventaireId string, lang string) (InventaireEditionD
|
||||
var author *InventaireAuthorResult
|
||||
var description string
|
||||
if editionQueryResult.WorkId != "" {
|
||||
workQueryResult, err := callInventaireBook(editionQueryResult.WorkId, lang)
|
||||
workQueryResult, err := callInventaireBook(inventaireUrl, editionQueryResult.WorkId, lang)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@ package inventaire
|
||||
|
||||
import "errors"
|
||||
|
||||
func CallInventaireFromISBN(isbn string, lang string) (*InventaireEditionDetailedSingleResult, error) {
|
||||
inventaireInfo, err := CallInventaireEdition("isbn:"+isbn, lang)
|
||||
func CallInventaireFromISBN(inventaireUrl string, isbn string, lang string) (*InventaireEditionDetailedSingleResult, error) {
|
||||
inventaireInfo, err := CallInventaireEdition(inventaireUrl, "isbn:"+isbn, lang)
|
||||
if err != nil {
|
||||
if errors.Is(err, &ErrorEditionNotFound{}) {
|
||||
//suppress the not found error, returns nil instead
|
||||
|
||||
Reference in New Issue
Block a user