diff --git a/openlibrary/plugins/importapi/code.py b/openlibrary/plugins/importapi/code.py index b8a4ad511474..920a24dc9bbe 100644 --- a/openlibrary/plugins/importapi/code.py +++ b/openlibrary/plugins/importapi/code.py @@ -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 @@ -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': []} + """ + + 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.""" @@ -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)