Switch from open library API to Inventaire API
This commit is contained in:
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