Skip to content

Commit

Permalink
Allow set NP servers by headers
Browse files Browse the repository at this point in the history
  • Loading branch information
MarekSuchanek committed Jan 19, 2022
1 parent 43e38ef commit b420568
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
15 changes: 13 additions & 2 deletions nanopub_submitter/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from typing import Tuple

from nanopub_submitter.config import cfg_parser
from nanopub_submitter.config import cfg_parser, RequestConfig
from nanopub_submitter.consts import NICE_NAME, VERSION, BUILD_INFO,\
ENV_CONFIG, DEFAULT_CONFIG, DEFAULT_ENCODING
from nanopub_submitter.logger import LOG, init_default_logging, init_config_logging
Expand Down Expand Up @@ -43,6 +43,12 @@ def _extract_content_type(header: str) -> Tuple[str, str]:
return input_format, DEFAULT_ENCODING


def _extract_servers(header: str) -> list[str]:
if header == '':
return []
return list(map(lambda x: x.strip(), header.split(',')))


@app.get(path='/')
async def get_info():
return fastapi.responses.JSONResponse(
Expand All @@ -62,7 +68,11 @@ async def submit_nanopub(request: fastapi.Request):
# (2) Extract data
submission_id = str(uuid.uuid4())
data = await request.body()
input_format, encoding = _extract_content_type(request.headers.get('Content-Type'))
input_format, encoding = _extract_content_type(request.headers.get('Content-Type', ''))
req_cfg = RequestConfig(
servers=_extract_servers(request.headers.get('X-NP-Servers', '')),
uri_replace=request.headers.get('X-URI-Replace', None)
)
if input_format != 'application/trig':
return fastapi.responses.Response(
status_code=fastapi.status.HTTP_400_BAD_REQUEST,
Expand All @@ -73,6 +83,7 @@ async def submit_nanopub(request: fastapi.Request):
try:
result = process(
cfg=cfg,
req_cfg=req_cfg,
submission_id=submission_id,
data=data.decode(encoding),
)
Expand Down
7 changes: 7 additions & 0 deletions nanopub_submitter/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,10 @@ def config(self) -> SubmitterConfig:


cfg_parser = SubmitterConfigParser()


class RequestConfig:

def __init__(self, servers: list[str], uri_replace: Optional[str]):
self.servers = servers
self.uri_replace = uri_replace
35 changes: 28 additions & 7 deletions nanopub_submitter/nanopub.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from typing import Optional, Tuple

from nanopub_submitter.config import SubmitterConfig
from nanopub_submitter.config import SubmitterConfig, RequestConfig
from nanopub_submitter.consts import DEFAULT_ENCODING, PACKAGE_NAME, PACKAGE_VERSION
from nanopub_submitter.logger import LOG
from nanopub_submitter.triple_store import store_to_triple_store
Expand Down Expand Up @@ -40,9 +40,11 @@ def __str__(self):

class NanopubProcessingContext:

def __init__(self, submission_id: str, cfg: SubmitterConfig):
def __init__(self, submission_id: str, cfg: SubmitterConfig,
req_cfg: RequestConfig):
self.id = submission_id
self.cfg = cfg
self.req_cfg = req_cfg
self.uri = None

def cleanup(self):
Expand All @@ -54,6 +56,20 @@ def cleanup(self):
except Exception:
pass

@property
def target_servers(self) -> list[str]:
if len(self.req_cfg.servers) > 0:
return self.req_cfg.servers
return self.cfg.nanopub.target_servers

@property
def uri_replace(self) -> Optional[str]:
if self.req_cfg.uri_replace is None:
return self.cfg.nanopub.uri_replace
if '|' in self.req_cfg.uri_replace:
return self.req_cfg.uri_replace
return None

@property
def input_file(self) -> str:
return f'{self.id}.trig'
Expand Down Expand Up @@ -100,7 +116,7 @@ def _split_nanopubs(nanopub_bundle: str) -> list[str]:
def _publish_nanopub(nanopub_bundle: str, ctx: NanopubProcessingContext) -> list[str]:
success = []
nanopubs = _split_nanopubs(nanopub_bundle)
for server in ctx.cfg.nanopub.target_servers:
for server in ctx.target_servers:
ctx.debug(f'Submitting to: {server}')
ok = True
for nanopub in nanopubs:
Expand Down Expand Up @@ -192,8 +208,13 @@ def _extract_np_uri(nanopub: str) -> Optional[str]:
return last_this_prefix


def process(cfg: SubmitterConfig, submission_id: str, data: str) -> NanopubSubmissionResult:
ctx = NanopubProcessingContext(submission_id=submission_id, cfg=cfg)
def process(cfg: SubmitterConfig, req_cfg: RequestConfig,
submission_id: str, data: str) -> NanopubSubmissionResult:
ctx = NanopubProcessingContext(
submission_id=submission_id,
cfg=cfg,
req_cfg=req_cfg,
)
ctx.debug('Preprocessing nanopublication as RDF')
try:
graph = rdflib.ConjunctiveGraph()
Expand Down Expand Up @@ -234,8 +255,8 @@ def process(cfg: SubmitterConfig, submission_id: str, data: str) -> NanopubSubmi
ctx.cleanup()
raise NanopubProcessingError(400, 'Failed to get nanopub URI')

if cfg.nanopub.uri_replace is not None:
old, new = cfg.nanopub.uri_replace.split('|', maxsplit=1)
if ctx.uri_replace is not None:
old, new = ctx.uri_replace.split('|', maxsplit=1)
new_uri = nanopub_uri.replace(old, new)
LOG.debug(f'Replacing {nanopub_uri} with {new_uri}')
nanopub_uri = new_uri
Expand Down

0 comments on commit b420568

Please sign in to comment.