Skip to content

Commit

Permalink
Improve Utils adding an API platform guesser - Merge #88
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts committed Aug 23, 2019
2 parents 9c513fe + 7d4482c commit 4a4342c
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 23 deletions.
2 changes: 1 addition & 1 deletion isogeo_pysdk/api/routes_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ def download_hosted(self, link: Link, encode_clean: bool = 1) -> tuple:
filename = re.sub(r"[^\w\-_\. ]", "", filename)

# end of method
return (req_download_hosted, filename, utils._convert_octets(link.size))
return (req_download_hosted, filename, utils.convert_octets(link.size))

@ApiDecorators._check_bearer_validity
def upload_hosted(
Expand Down
94 changes: 84 additions & 10 deletions isogeo_pysdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ def set_base_url(self, platform: str = "prod"):
self.ssl,
)

def _convert_octets(self, octets: int) -> str:
@classmethod
def convert_octets(self, octets: int) -> str:
"""Convert a mount of octets in readable size.
:param int octets: mount of octets to convert
Expand All @@ -197,6 +198,7 @@ def _convert_octets(self, octets: int) -> str:

return "%s %s" % (s, size_name[i])

@classmethod
def convert_uuid(self, in_uuid: str = str, mode: bool = 0):
"""Convert a metadata UUID to its URI equivalent. And conversely.
Expand Down Expand Up @@ -238,6 +240,7 @@ def convert_uuid(self, in_uuid: str = str, mode: bool = 0):
else:
raise ValueError("'mode' must be one of: 0 | 1 | 2")

@classmethod
def encoded_words_to_text(self, in_encoded_words: str):
"""Pull out the character set, encoding, and encoded text from the input
encoded words. Next, it decodes the encoded words into a byte string,
Expand All @@ -247,8 +250,8 @@ def encoded_words_to_text(self, in_encoded_words: str):
See:
- https://github.com/isogeo/isogeo-api-py-minsdk/issues/32
- https://dmorgan.info/posts/encoded-word-syntax/
- https://github.com/isogeo/isogeo-api-py-minsdk/issues/32
- https://dmorgan.info/posts/encoded-word-syntax/
:param str in_encoded_words: base64 or quori encoded character string.
"""
Expand Down Expand Up @@ -415,7 +418,46 @@ def get_url_base_from_url_token(
)
return api_url_base.geturl()

@classmethod
def guess_platform_from_url(self, url: str = "https://api.isogeo.com/") -> str:
"""Returns the Isogeo platform from a given URL.
:param url str: URL string to guess from
:rtype: str
:returns: "prod" or "qa" or "unknown"
:Example:
.. code-block:: python
IsogeoUtils.guess_platform_from_url("https://api.isogeo.com")
>>> "prod"
IsogeoUtils.guess_platform_from_url("https://api.qa.isogeo.com")
>>> "qa"
IsogeoUtils.guess_platform_from_url("https://api.isogeo.ratp.local")
>>> "unknown"
"""
if "api.isogeo.com" in url:
api_platform = "prod"
elif "app.isogeo.com" in url:
api_platform = "prod"
elif "api.qa.isogeo.com" in url:
api_platform = "qa"
elif "qa." in url and "isogeo" in url:
api_platform = "qa"
elif "qa-" in url and "isogeo" in url:
# https://qa-isogeo-app.azurewebsites.net
# https://qa-isogeo-open.azurewebsites.net
api_platform = "qa"
else:
api_platform = "unknown" # not recognized. on-premises?

return api_platform

# -- SEARCH --------------------------------------------------------------
@classmethod
def pages_counter(self, total: int, page_size: int = 100) -> int:
"""Simple helper to handle pagination. Returns the number of pages for a
given number of results.
Expand Down Expand Up @@ -720,8 +762,33 @@ def share_extender(self, share: dict, results_filtered: dict):
def credentials_loader(self, in_credentials: str = "client_secrets.json") -> dict:
"""Loads API credentials from a file, JSON or INI.
:param str in_credentials: path to the credentials file. By default,
look for a client_secrets.json file.
:param str in_credentials: path to the credentials file. By default, `./client_secrets.json`
:rtype: dict
:returns: a dictionary with credentials (ID, secret, URLs, platform...)
:Example:
.. code-block:: python
api_credentials = IsogeoUtils.credentials_loader("./_auth/client_secrets.json")
pprint.pprint(api_credentials)
>>> {
'auth_mode': 'group',
'client_id': 'python-minimalist-sdk-test-uuid-1a2b3c4d5e6f7g8h9i0j11k12l',
'client_secret': 'application-secret-1a2b3c4d5e6f7g8h9i0j11k12l13m14n15o16p17Q18rS',
'kind': None,
'platform': 'prod',
'scopes': ['resources:read'],
'staff': None,
'type': None,
'uri_auth': 'https://id.api.isogeo.com/oauth/authorize',
'uri_base': 'https://api.isogeo.com',
'uri_redirect': None,
'uri_token': 'https://id.api.isogeo.com/oauth/token'
}
"""
accepted_extensions = (".ini", ".json")
in_credentials_path = Path(in_credentials) # load path
Expand Down Expand Up @@ -813,12 +880,16 @@ def credentials_loader(self, in_credentials: str = "client_secrets.json") -> dic
),
"uri_redirect": auth_settings.get("URI_REDIRECT"),
}

# add guessed platform
out_auth["platform"] = self.guess_platform_from_url(out_auth.get("uri_base"))

# method ending
return out_auth

# -- HELPERS ---------------------------------------------------------------
@classmethod
def hlpr_date_as_datetime(cls, in_date: str) -> datetime:
def hlpr_datetimes(cls, in_date: str) -> datetime:
"""Helper to handle differnts dates formats.
See: https://github.com/isogeo/isogeo-api-py-minsdk/issues/85
Expand All @@ -832,11 +903,14 @@ def hlpr_date_as_datetime(cls, in_date: str) -> datetime:
.. code-block:: python
# for an event date
event_date = Metadata.hlpr_date_as_datetime("2018-06-04T00:00:00+00:00")
IsogeoUtils.hlpr_datetimes"2018-06-04T00:00:00+00:00")
>>> 2018-06-04 00:00:00
# for a metadata creation date with 6 digits as milliseconds
md_date = Metadata.hlpr_date_as_datetime("2019-05-17T13:01:08.559123+00:00")
IsogeoUtils.hlpr_datetimes"2019-05-17T13:01:08.559123+00:00")
>>> 2019-05-17 13:01:08.559123
# for a metadata creation date with more than 6 digits as milliseconds
md_date_larger = Metadata.hlpr_date_as_datetime("2019-06-13T16:21:38.1917618+00:00")
IsogeoUtils.hlpr_datetimes"2019-06-13T16:21:38.1917618+00:00")
>>> 2019-06-13 16:21:38.191761
"""
if len(in_date) == 10:
Expand All @@ -851,7 +925,7 @@ def hlpr_date_as_datetime(cls, in_date: str) -> datetime:
) # metadata timestamps with 6 milliseconds
elif len(in_date) >= 32 and "." in in_date:
milliseconds = re.search(_regex_milliseconds, in_date)
return cls.hlpr_date_as_datetime(
return cls.hlpr_datetimes(
in_date.replace(milliseconds.group(1), milliseconds.group(1)[:6])
)
else:
Expand Down
6 changes: 3 additions & 3 deletions tests/test_metadatas.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ def test_metadatas_in_search_results(self):

# -- HELPERS
# dates
md_date_creation = utils.hlpr_date_as_datetime(metadata._created)
md_date_creation = utils.hlpr_datetimes(metadata._created)
self.assertEqual(int(metadata._created[:4]), md_date_creation.year)
md_date_modification = utils.hlpr_date_as_datetime(metadata._modified)
md_date_modification = utils.hlpr_datetimes(metadata._modified)
self.assertEqual(int(metadata._modified[:4]), md_date_modification.year)
if metadata.created:
ds_date_creation = utils.hlpr_date_as_datetime(metadata.created)
ds_date_creation = utils.hlpr_datetimes(metadata.created)
self.assertEqual(int(metadata.created[:4]), ds_date_creation.year)

# def test_search_specific_mds_bad(self):
Expand Down
44 changes: 35 additions & 9 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,33 +349,59 @@ def test_pages_counter(self):
self.assertEqual(p_default, 8)

# -- Methods helpers
def test_guess_platform(self):
"""Test class method to guess platform from url."""
# prod
prod_api = IsogeoUtils.guess_platform_from_url("https://api.isogeo.com/about/")
self.assertEqual(prod_api, "prod")
prod_api = IsogeoUtils.guess_platform_from_url(
"https://v1.api.isogeo.com/about/"
)
self.assertEqual(prod_api, "prod")
prod_app = IsogeoUtils.guess_platform_from_url("https://app.isogeo.com/")
self.assertEqual(prod_app, "prod")

# qa
qa_api = IsogeoUtils.guess_platform_from_url("https://api.qa.isogeo.com/about")
self.assertEqual(qa_api, "qa")
qa_api = IsogeoUtils.guess_platform_from_url(
"https://v1.api.qa.isogeo.com/about"
)
self.assertEqual(qa_api, "qa")
qa_app = IsogeoUtils.guess_platform_from_url(
"https://qa-isogeo-app.azurewebsites.net/"
)
self.assertEqual(qa_app, "qa")

# unknown
unknown_api = IsogeoUtils.guess_platform_from_url(
"https://api.isogeo.ratp.local/about"
)
self.assertEqual(unknown_api, "unknown")

def test_helper_datetimes(self):
"""Test class method to help formatting dates."""
# simple dates str
simple_date = self.utils.hlpr_date_as_datetime("2019-08-09")
simple_date = IsogeoUtils.hlpr_datetimes("2019-08-09")
self.assertIsInstance(simple_date, datetime)
self.assertEqual(simple_date.year, 2019)

# events datetimes str
event_date = self.utils.hlpr_date_as_datetime("2018-06-04T00:00:00+00:00")
event_date = IsogeoUtils.hlpr_datetimes("2018-06-04T00:00:00+00:00")
self.assertIsInstance(event_date, datetime)
self.assertEqual(event_date.year, 2018)

# metadata timestamps str - 6 milliseconds
md_date = self.utils.hlpr_date_as_datetime("2019-05-17T13:01:08.559123+00:00")
md_date = IsogeoUtils.hlpr_datetimes("2019-05-17T13:01:08.559123+00:00")
self.assertIsInstance(md_date, datetime)
self.assertEqual(md_date.year, 2019)

# metadata timestamps str - 6 milliseconds
md_date_lesser = self.utils.hlpr_date_as_datetime(
"2017-12-01T16:36:28.74561+00:00"
)
md_date_lesser = IsogeoUtils.hlpr_datetimes("2017-12-01T16:36:28.74561+00:00")
self.assertIsInstance(md_date_lesser, datetime)
self.assertEqual(md_date_lesser.year, 2017)

# metadata timestamps str - more than 6 milliseconds
md_date_larger = self.utils.hlpr_date_as_datetime(
"2019-06-13T16:21:38.1917618+00:00"
)
md_date_larger = IsogeoUtils.hlpr_datetimes("2019-06-13T16:21:38.1917618+00:00")
self.assertIsInstance(md_date_larger, datetime)
self.assertEqual(md_date_larger.year, 2019)
2 changes: 2 additions & 0 deletions tests/test_utils_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ def test_credentials_loader_json_group(self):
self.assertIn("kind", creds_json_group)
self.assertIn("type", creds_json_group)
self.assertIn("staff", creds_json_group)
self.assertIn("platform", creds_json_group)
# values
self.assertEqual(creds_json_group.get("auth_mode"), "group")
self.assertEqual(
Expand All @@ -166,6 +167,7 @@ def test_credentials_loader_json_group(self):
"https://id.api.isogeo.com/oauth/token",
)
self.assertEqual(creds_json_group.get("uri_base"), "https://api.isogeo.com")
self.assertEqual(creds_json_group.get("platform"), "prod")
self.assertIsNone(creds_json_group.get("uri_redirect"))

def test_credentials_loader_ini(self):
Expand Down

0 comments on commit 4a4342c

Please sign in to comment.