Skip to content

Commit

Permalink
DO NOT COMMIT: testing endpoint only. Drop before merging.
Browse files Browse the repository at this point in the history
  • Loading branch information
scottbarnes committed May 15, 2024
1 parent 30c6dc5 commit 467b795
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion openlibrary/plugins/importapi/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

from infogami.plugins.api.code import add_hook
from infogami.infobase.client import ClientException

from openlibrary.catalog.add_book.load_book import (
east_in_by_statement,
import_author,
remove_author_honorifics,
)
from openlibrary.plugins.openlibrary.code import can_write
from openlibrary.catalog.marc.marc_binary import MarcBinary, MarcException
from openlibrary.catalog.marc.marc_xml import MarcXml
Expand Down Expand Up @@ -115,6 +119,41 @@ def parse_data(data: bytes) -> tuple[dict | None, str | None]:
return edition_builder.get_dict(), format


class AuthorAPI:
"""
/api/author
For testing author name resolution.
Takes a `rec` with, at minimum, the `authors` key:
curl -X POST http://localhost:8080/api/author \
-H "Content-Type: application/json" \
-b ~/cookies.txt \
-d '{
"authors": [{"name": "epictetus"}]
}'
{'type': {'key': '/type/edition'}, 'authors': [<Author: '/authors/OL5A'>]}
"""

def POST(self):
web.header('Content-Type', 'application/json')
if not can_write():
raise web.HTTPError('403 Forbidden')

rec = json.loads(web.data())

book = {
'type': {'key': '/type/edition'},
}
if authors := rec.get("authors"):
book['authors'] = []
for author in authors:
author['name'] = remove_author_honorifics(author['name'])
east = east_in_by_statement(rec, author)
book['authors'].append(import_author(author, eastern=east))

return book


class importapi:
"""/api/import endpoint for general data formats."""

Expand Down Expand Up @@ -740,6 +779,7 @@ def POST(self):
raise self.error(i, "upload failed")


add_hook("author", AuthorAPI)
add_hook("import", importapi)
add_hook("ils_search", ils_search)
add_hook("ils_cover_upload", ils_cover_upload)
Expand Down

0 comments on commit 467b795

Please sign in to comment.