Skip to content

Commit

Permalink
Fix nanopub request encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
MarekSuchanek committed Jan 23, 2022
1 parent a9a50cf commit 2f551af
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion nanopub_submitter/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 18 additions & 13 deletions nanopub_submitter/nanopub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
Expand Down

0 comments on commit 2f551af

Please sign in to comment.