Skip to content

Commit

Permalink
sub resources handle resource type
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts committed Apr 2, 2018
1 parent 6de3744 commit 1be24ee
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 85 deletions.
70 changes: 70 additions & 0 deletions isogeo_pysdk/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@
"metadata": ",".join(FILTER_TYPES),
}

_SUBRESOURCES_MD = ("_creator",
"conditions",
"contacts",
"coordinate-system",
"events",
"feature-attributes",
"keywords",
"layers",
"limitations",
"links",
"operations",
"serviceLayers",
"specifications",
"tags",
)

_SUBRESOURCES_KW = ("count",
)

# ##############################################################################
# ########## Classes ###############
# ##################################
Expand Down Expand Up @@ -322,10 +341,61 @@ def check_edit_tab(self, tab, md_type):
else:
return True

def _check_filter_specific_md(self, specific_md):
"""Check if specific_md parameter is valid.
:param list specific_md: list of specific metadata UUID to check
"""
if isinstance(specific_md, list):
if len(specific_md) > 0:
# checking UUIDs and poping bad ones
for md in specific_md:
if not self.check_is_uuid(md):
specific_md.remove(md)
logging.error("Metadata UUID is not correct: {}"
.format(md))
# joining survivors
specific_md = ",".join(specific_md)
else:
specific_md = ""
else:
raise TypeError("'specific_md' expects a list")
return specific_md

def _check_filter_sub_resources(self, sub_resources, resource="metadata"):
"""Check if specific_resources parameter is valid.
:param list sub_resources: sub resources to check
:param str resource: resource type to check sub resources.
Must be one of: metadata | keyword.
"""
# check resource parameter
if resource == "metadata":
ref_subresources = _SUBRESOURCES_MD
elif resource == "keyword":
ref_subresources = _SUBRESOURCES_KW
else:
raise ValueError("Must be one of: metadata | keyword.")

# sub resources manager
if isinstance(sub_resources, string_types)\
and sub_resources.lower() == "all":
sub_resources = ",".join(ref_subresources)
elif isinstance(sub_resources, list):
if len(sub_resources) > 0:
sub_resources = ",".join(sub_resources)
else:
sub_resources = ""
else:
raise TypeError("'sub_resources' expect a list or a str='all'")
return sub_resources


# ##############################################################################
# ##### Stand alone program ########
# ##################################
if __name__ == '__main__':
"""Standalone execution."""
checker = IsogeoChecker()
subresources = checker._check_filter_sub_resources(sub_resources="all")
print(subresources)
80 changes: 6 additions & 74 deletions isogeo_pysdk/isogeo_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,6 @@ class Isogeo(object):
"user_public",
}

SUBRESOURCES = ("_creator",
"conditions",
"contacts",
"coordinate-system",
"events",
"feature-attributes",
"keywords",
"layers",
"limitations",
"links",
"operations",
"serviceLayers",
"specifications",
"tags",
)

_THESAURI_DICT = {"isogeo": "1616597fbc4348c8b11ef9d59cf594c8",
"inspire-theme": "926c676c380046d7af99bcae343ac813",
"iso19115-topic": "926f969ee2bb470a84066625f68b96bb"
Expand Down Expand Up @@ -311,32 +295,10 @@ def search(self,
self.ct))

# specific resources specific parsing
if isinstance(specific_md, list):
if len(specific_md) > 0:
# checking UUIDs and poping bad ones
for md in specific_md:
if not checker.check_is_uuid(md):
specific_md.remove(md)
logging.error("Metadata UUID is not correct: {}"
.format(md))
# joining survivors
specific_md = ",".join(specific_md)
else:
specific_md = ""
else:
raise TypeError("'specific_md' expects a list")
specific_md = checker._check_filter_specific_md(specific_md)

# sub resources specific parsing
if isinstance(sub_resources, string_types)\
and sub_resources.lower() == "all":
sub_resources = self.SUBRESOURCES
elif isinstance(sub_resources, list):
if len(sub_resources) > 0:
sub_resources = ",".join(sub_resources)
else:
sub_resources = ""
else:
raise TypeError("'sub_resources' expect a list or a str='all'")
sub_resources = checker._check_filter_sub_resources(sub_resources)

# handling request parameters
payload = {'_id': specific_md,
Expand Down Expand Up @@ -427,16 +389,7 @@ def resource(self, token, id_resource, sub_resources=[], prot="https"):
self.ct))

# sub resources specific parsing
if isinstance(sub_resources, string_types)\
and sub_resources.lower() == "all":
sub_resources = self.SUBRESOURCES
elif isinstance(sub_resources, list):
if len(sub_resources) > 0:
sub_resources = ",".join(sub_resources)
else:
sub_resources = ""
else:
raise TypeError("'sub_resources' expect a list or a str='all'")
sub_resources = checker._check_filter_sub_resources(sub_resources)

# handling request parameters
payload = {"id": id_resource,
Expand Down Expand Up @@ -678,20 +631,7 @@ def keywords(self,
self.ct))

# specific resources specific parsing
if isinstance(specific_md, list):
if len(specific_md) > 0:
# checking UUIDs and poping bad ones
for md in specific_md:
if not checker.check_is_uuid(md):
specific_md.remove(md)
logging.error("Metadata UUID is not correct: {}"
.format(md))
# joining survivors
specific_md = ",".join(specific_md)
else:
specific_md = ""
else:
raise TypeError("'specific_md' expects a list")
specific_md = checker._check_filter_specific_md(specific_md)

# specific tag specific parsing
if isinstance(specific_tag, list):
Expand All @@ -703,16 +643,8 @@ def keywords(self,
raise TypeError("'specific_tag' expects a list")

# sub resources specific parsing
if isinstance(sub_resources, string_types)\
and sub_resources.lower() == "all":
sub_resources = self.SUBRESOURCES
elif isinstance(sub_resources, list):
if len(sub_resources) > 0:
sub_resources = ",".join(sub_resources)
else:
sub_resources = ""
else:
raise TypeError("'sub_resources' expect a list or a str='all'")
sub_resources = checker._check_filter_sub_resources(sub_resources,
"keyword")

# handling request parameters
payload = {'_id': specific_md,
Expand Down
17 changes: 6 additions & 11 deletions isogeo_pysdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,15 @@ def get_isogeo_version(self, component="api", prot="https"):
elif component == "app" and self.platform == "qa":
version_url = "https://qa-isogeo-app.azurewebsites.net/about"
else:
raise ValueError("Component value is one theses values:"
raise ValueError("Component value must be one of: "
"api [default], db, app.")

# send request
try:
version_req = requests.get(version_url,
proxies=self.proxies
)
except requests.exceptions.SSLError as e:
logging.error(e)
version_req = requests.get(version_url,
proxies=self.proxies,
verify=False
)
version_req = requests.get(version_url,
proxies=self.proxies,
verify=self.ssl
)

# checking response
checker.check_api_response(version_req)

Expand Down
17 changes: 17 additions & 0 deletions tests/test_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,23 @@ def test_check_edit_tab_bad(self):
with self.assertRaises(ValueError):
checker.check_edit_tab(tab="attributes", md_type="service")

def test_check_filter_sub_resources_ok(self):
"""Return"""
# metadata sub resources
subresources = checker._check_filter_sub_resources(sub_resources="all",
resource="metadata")
self.assertIsInstance(subresources, str)
# keyword sub resources
subresources = checker._check_filter_sub_resources(sub_resources="all",
resource="keyword")
self.assertIsInstance(subresources, str)

def test_check_filter_sub_resources_bad(self):
"""Raise errors"""
with self.assertRaises(ValueError):
checker._check_filter_sub_resources(sub_resources="all",
resource="Metadata")


# #############################################################################
# ######## Standalone ##############
Expand Down

0 comments on commit 1be24ee

Please sign in to comment.