From 2f551af79026ec9f384e1cced1d34b3ba2683406 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Such=C3=A1nek?= Date: Sun, 23 Jan 2022 21:12:40 +0100 Subject: [PATCH] Fix nanopub request encoding --- nanopub_submitter/api.py | 2 +- nanopub_submitter/nanopub.py | 31 ++++++++++++++++++------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/nanopub_submitter/api.py b/nanopub_submitter/api.py index a113864..97cb023 100644 --- a/nanopub_submitter/api.py +++ b/nanopub_submitter/api.py @@ -96,7 +96,7 @@ async def submit_nanopub(request: fastapi.Request): LOG.error(f'[{submission_id}] Unexpected processing error: {str(e)}') return fastapi.responses.PlainTextResponse( status_code=fastapi.status.HTTP_500_INTERNAL_SERVER_ERROR, - content='Failed to process the nanopublication', + content=f'Failed to process the nanopublication: {str(e)}', ) # (4) Mail Mailer.get().notice(nanopub_uri=result.location) diff --git a/nanopub_submitter/nanopub.py b/nanopub_submitter/nanopub.py index 0f357f3..7df351f 100644 --- a/nanopub_submitter/nanopub.py +++ b/nanopub_submitter/nanopub.py @@ -120,20 +120,25 @@ def _publish_nanopub(nanopub_bundle: str, ctx: NanopubProcessingContext) -> list ctx.debug(f'Submitting to: {server}') ok = True for nanopub in nanopubs: - r = requests.post( - url=server, - data=nanopub, - headers={ - 'Content-Type': f'application/trig; charset={DEFAULT_ENCODING}', - 'User-Agent': f'{PACKAGE_NAME}/{PACKAGE_VERSION}', - }, - timeout=10, - ) - if not r.ok: + try: + r = requests.post( + url=server, + data=nanopub.encode(encoding=DEFAULT_ENCODING), + headers={ + 'Content-Type': f'application/trig; charset={DEFAULT_ENCODING}', + 'User-Agent': f'{PACKAGE_NAME}/{PACKAGE_VERSION}', + }, + timeout=10, + ) + if not r.ok: + ok = False + ctx.warn(f'Failed to publish nanopub via {server}') + ctx.debug(f'status={r.status_code}') + ctx.debug(r.text) + break + except Exception as e: ok = False - ctx.warn(f'Failed to publish nanopub via {server}') - ctx.debug(f'status={r.status_code}') - ctx.debug(r.text) + ctx.warn(f'Failed to publish nanopub via {server}: {str(e)}') break if ok: ctx.info(f'Nanopub published via {server}')