Switch from open library API to Inventaire API
This commit is contained in:
41
internal/inventaire/inventaire.go
Normal file
41
internal/inventaire/inventaire.go
Normal file
@@ -0,0 +1,41 @@
|
||||
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"`
|
||||
}
|
||||
|
||||
func computeInventaireUrl(paths ...string) (*url.URL, error) {
|
||||
baseUrl := "https://inventaire.io/api"
|
||||
return callapiutils.ComputeUrl(baseUrl, paths...)
|
||||
}
|
||||
|
||||
func CallInventaireSearch(searchterm string, limit int, offset int) (InventaireSearchResult, error) {
|
||||
var queryResult InventaireSearchResult
|
||||
u, err := computeInventaireUrl("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
|
||||
}
|
||||
46
internal/inventaire/inventaire_test.go
Normal file
46
internal/inventaire/inventaire_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package inventaire
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCallInventaireSearch_NoLimit(t *testing.T) {
|
||||
result, err := CallInventaireSearch("salammbo", 0, 0)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.Equal(t, 17, result.Total)
|
||||
assert.Equal(t, 10, len(result.Results))
|
||||
}
|
||||
|
||||
func TestCallInventaireSearch_Limit(t *testing.T) {
|
||||
result, err := CallInventaireSearch("salammbo", 5, 0)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.Equal(t, 17, result.Total)
|
||||
assert.Equal(t, 5, len(result.Results))
|
||||
}
|
||||
|
||||
func TestCallInventaireSearch_Offset(t *testing.T) {
|
||||
result, err := CallInventaireSearch("salammbo", 0, 15)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.Equal(t, 17, result.Total)
|
||||
assert.Equal(t, 2, len(result.Results))
|
||||
}
|
||||
|
||||
func TestCallInventaireBook_BraveNewWorld(t *testing.T) {
|
||||
result, err := CallInventaireBook("Q191949", "fr")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.Equal(t, "Le Meilleur des mondes", result.Title)
|
||||
assert.Equal(t, "roman de Aldous Huxley", result.Description)
|
||||
assert.Equal(t, "Q81447", result.Author.ID)
|
||||
assert.Equal(t, "Aldous Huxley", result.Author.Name)
|
||||
assert.Equal(t, "écrivain, romancier et philosophe britannique (1894–1963)", result.Author.Description)
|
||||
}
|
||||
102
internal/inventaire/inventairebook.go
Normal file
102
internal/inventaire/inventairebook.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package inventaire
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/callapiutils"
|
||||
)
|
||||
|
||||
type InventaireBookResult struct {
|
||||
ID string
|
||||
Lang string
|
||||
Title string
|
||||
Description string
|
||||
Author *InventaireAuthorResult
|
||||
}
|
||||
|
||||
type InventaireAuthorResult struct {
|
||||
ID string
|
||||
Name string
|
||||
Description string
|
||||
}
|
||||
|
||||
func (i *InventaireBookResult) UnmarshalJSON(b []byte) error {
|
||||
var parsed struct {
|
||||
Entities map[string]json.RawMessage
|
||||
}
|
||||
err := json.Unmarshal(b, &parsed)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, entity := range parsed.Entities {
|
||||
|
||||
var inventaireEntity struct {
|
||||
WdId string `json:"wdId"`
|
||||
Type string `json:"type"`
|
||||
Labels map[string]json.RawMessage `json:"labels"`
|
||||
Descriptions map[string]json.RawMessage `json:"descriptions"`
|
||||
}
|
||||
|
||||
err := json.Unmarshal(entity, &inventaireEntity)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if inventaireEntity.WdId == i.ID {
|
||||
title, err := findLangageField(inventaireEntity.Labels, i.Lang)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
i.Title = title
|
||||
description, err := findLangageField(inventaireEntity.Descriptions, i.Lang)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
i.Description = description
|
||||
} else if inventaireEntity.Type == "human" {
|
||||
a := InventaireAuthorResult{ID: inventaireEntity.WdId}
|
||||
name, err := findLangageField(inventaireEntity.Labels, i.Lang)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
a.Name = name
|
||||
desc, err := findLangageField(inventaireEntity.Descriptions, i.Lang)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
a.Description = desc
|
||||
i.Author = &a
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func findLangageField(multipleMessageFields map[string]json.RawMessage, lang string) (string, error) {
|
||||
fieldToParse, ok := multipleMessageFields[lang]
|
||||
if ok {
|
||||
var parsedField string
|
||||
err := json.Unmarshal(fieldToParse, &parsedField)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return parsedField, err
|
||||
} else {
|
||||
return "", errors.New("multi lang field could not be parsed")
|
||||
}
|
||||
}
|
||||
|
||||
func CallInventaireBook(inventaireId string, lang string) (InventaireBookResult, error) {
|
||||
queryResult := InventaireBookResult{ID: inventaireId, Lang: lang}
|
||||
u, err := computeInventaireUrl("entities")
|
||||
if err != nil {
|
||||
return queryResult, err
|
||||
}
|
||||
callapiutils.AddQueryParam(u, "action", "by-uris")
|
||||
callapiutils.AddQueryParam(u, "relatives", "wdt:P50")
|
||||
callapiutils.AddQueryParam(u, "lang", lang)
|
||||
callapiutils.AddQueryParam(u, "uris", "wd:"+inventaireId)
|
||||
|
||||
err = callapiutils.FetchAndParseResult(u, &queryResult)
|
||||
return queryResult, err
|
||||
}
|
||||
Reference in New Issue
Block a user