Skip to content

Commit

Permalink
add a generic webapp URL generator handling OpenCatalog and pixup por…
Browse files Browse the repository at this point in the history
…tal #22
  • Loading branch information
Guts committed Mar 28, 2018
1 parent d947914 commit bb56eca
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions isogeo_pysdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ class IsogeoUtils(object):
# "int": "api.int.hq.isogeo.fr"
}

WEBAPPS = {"oc": {"args": ("md_id", "share_id", "oc_token"),
"url": "https://open.isogeo.com/s/{share_id}"
"/{oc_token}/r/{md_id}"
},
"pixup_portal": {"args": ("md_id", "portal_url", ),
"url": "http://{portal_url}/?muid={md_id}"
},
}

def __init__(self, proxies=dict()):
"""Instanciate IsogeoUtils module.
Expand Down Expand Up @@ -192,6 +201,57 @@ def get_edit_url(self, md_id=str, md_type=str, owner_id=str, tab="identification
"/resources/{}" \
"/{}".format(owner_id, md_id, tab)

def get_view_url(self, webapp="oc", **kwargs):
"""Constructs the view URL of a metadata.
:param str webapp: web app destination.
:param dict \**kwargs: web app specific parameters.
For example see WEBAPPS
"""
# build wbeapp URL depending on choosen webapp
if webapp in self.WEBAPPS:
webapp_args = self.WEBAPPS.get(webapp).get("args")
# check kwargs parameters
if set(webapp_args) <= set(kwargs):
# construct and return url
url = self.WEBAPPS.get(webapp).get("url")

return url.format(**kwargs)
else:
webapp_args = self.WEBAPPS.get(webapp).get("args")
raise TypeError("'{}' webapp expects {} argument(s):"
" {}"
" Args passed: {}"
.format(webapp,
len(webapp_args),
webapp_args,
kwargs))
else:
raise ValueError("'{}' is not a recognized webapp among: {}."
" Try to register it."
.format(self.WEBAPPS.keys(), webapp))

def register_webapp(self, webapp_name, webapp_args, webapp_url):
"""Register a new WEBAPP to use with the view URL builder.
:param str webapp_name: name of the web app to register
:param list webapp_args: dynamic arguments to complete the URL.
Typically 'md_id'.
:param str webapp_url: URL of the web app to register with
args tags to replace. Example:
'https://www.ppige-npdc.fr/portail/geocatalogue?uuid={md_id}'
"""
# check parameters
for arg in webapp_args:
if arg not in webapp_url:
raise ValueError("Inconsistent web app arguments and URL."
" It should contain arguments to replace"
" dynamically. Example: 'http://webapp.com"
"/isogeo?metadata={md_id}'")
# register
self.WEBAPPS[webapp_name] = {"args": webapp_args,
"url": webapp_url}

# -- API AUTH ------------------------------------------------------------
def credentials_loader(self, f_json, f_ini, e_vars):
"""Loads API credentials from a file or environment variables.
Expand Down

0 comments on commit bb56eca

Please sign in to comment.