Skip to content

Commit

Permalink
code improvments
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts committed Mar 29, 2018
1 parent 14f4c5a commit 57ee93d
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 41 deletions.
65 changes: 33 additions & 32 deletions isogeo_pysdk/isogeo_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ def __init__(self, client_id, client_secret, proxy=None, auth_mode="group",

# checking internet connection
if not checker.check_internet_connection():
logging.error("Internet connection doesn't work.")
raise EnvironmentError("Internet connection issue.")
else:
pass
Expand Down Expand Up @@ -156,15 +155,16 @@ def __init__(self, client_id, client_secret, proxy=None, auth_mode="group",

# handling proxy parameters
# see: http://docs.python-requests.org/en/latest/user/advanced/#proxies
if proxy and type(proxy) is dict and 'http' in list(proxy.keys()):
logging.info("Proxy activated")
if proxy and isinstance(proxy, dict) and 'http' in proxy:
logging.debug("Proxy activated")
self.proxies = proxy
elif proxy and type(proxy) is not dict:
logging.info("Proxy syntax error. Must be a dict: { 'protocol': "
"'http://user:password@proxy_url:port' }."
"e.g.: {'http': 'http://martin:1234@10.1.68.1:5678',"
"'https': 'http://martin:p4ssW0rde@10.1.68.1:5678'}")
return
elif proxy and not isinstance(proxy, dict):
raise TypeError("Proxy syntax error. Must be a dict:"
"{ 'protocol': "
"'http://user:password@proxy_url:port' }."
" e.g.: "
"{'http': 'http://martin:1234@10.1.68.1:5678',"
"'https': 'http://martin:p4ssW0rde@10.1.68.1:5678'}")
else:
self.proxies = {}
logging.info("No proxy set. Use default configuration.")
Expand Down Expand Up @@ -207,7 +207,7 @@ def connect(self, client_id=None, client_secret=None):
headers=head,
data=payload,
proxies=self.proxies)
except ConnectionError:
except requests.exceptions.ConnectionError:
return "No internet connection"
except requests.exceptions.SSLError as e:
logging.error(e)
Expand Down Expand Up @@ -243,8 +243,8 @@ def search(self,
page_size=100,
offset=0,
share=None,
specific_md=None,
sub_resources=None,
specific_md=[],
sub_resources=[],
whole_share=True,
check=True,
augment=False,
Expand Down Expand Up @@ -316,31 +316,32 @@ def search(self,
self.ct))

# specific resources specific parsing
if type(specific_md) is list and 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)
elif specific_md is None:
specific_md = ""
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:
specific_md = ""
raise TypeError("'specific_md' expects a list")

# sub resources specific parsing
if isinstance(sub_resources, string_types) and sub_resources.lower() == "all":
if isinstance(sub_resources, string_types)\
and sub_resources.lower() == "all":
sub_resources = self.SUBRESOURCES
elif type(sub_resources) is list and len(sub_resources) > 0:
sub_resources = ",".join(sub_resources)
elif sub_resources is None:
sub_resources = ""
elif isinstance(sub_resources, list):
if len(sub_resources) > 0:
sub_resources = ",".join(sub_resources)
elif sub_resources:
sub_resources = ""
else:
sub_resources = ""
raise ValueError("Error: sub_resources argument must be a list,"
"'all' or empty")
raise TypeError("'sub_resources' expect a list or a str='all'")

# handling request parameters
payload = {'_id': specific_md,
Expand Down
16 changes: 16 additions & 0 deletions tests/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ def test_bad_id_secret(self):
with self.assertRaises(ValueError):
isogeo.connect()

def test_other_language(self):
"""Try to get other language."""
isogeo = Isogeo(client_id=app_id,
client_secret=app_token,
lang="ES")
# if other language passed the English is applied
self.assertEqual(isogeo.lang, "en")

def test_bad_platform(self):
"""Bad platform value."""
with self.assertRaises(ValueError):
Expand All @@ -81,6 +89,14 @@ def test_bad_auth_mode(self):
)
del isogeo

def test_bad_proxy(self):
"""Bad auth mode value."""
with self.assertRaises(TypeError):
Isogeo(client_id=app_id,
client_secret=app_token,
proxy="this_is_my_string_proxy",
)

def test_successed_auth(self):
"""When a search works, check the response structure."""
isogeo = Isogeo(client_id=app_id,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
# ##################################


class AuthBadCodes(unittest.TestCase):
class IsogeoChecker(unittest.TestCase):
"""Test authentication process."""
if not app_id or not app_token:
logging.critical("No API credentials set as env variables.")
Expand Down
91 changes: 91 additions & 0 deletions tests/test_download_hosted.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# -*- coding: UTF-8 -*-
#!/usr/bin/env python
from __future__ import (absolute_import, print_function, unicode_literals)

# #############################################################################
# ########## Libraries #############
# ##################################

# Standard library
import logging
from os import environ
from sys import exit
from tempfile import mkstemp
import unittest

# module target
from isogeo_pysdk import Isogeo


# #############################################################################
# ######## Globals #################
# ##################################

# API access
app_id = environ.get('ISOGEO_API_DEV_ID')
app_token = environ.get('ISOGEO_API_DEV_SECRET')

# #############################################################################
# ########## Classes ###############
# ##################################


class DownloadHosted(unittest.TestCase):
"""Test download hosted data through Isogeo API."""
if not app_id or not app_token:
logging.critical("No API credentials set as env variables.")
exit()
else:
pass

# standard methods
def setUp(self):
"""Executed before each test."""
self.isogeo = Isogeo(client_id=app_id,
client_secret=app_token)
self.bearer = self.isogeo.connect()

def tearDown(self):
"""Executed after each test."""
pass

# # metadata models
# def test_dl_hosted(self):
# """Download an hosted data from Isogeo metadata."""
# search = self.isogeo.search(self.bearer, whole_share=0,
# query="action:download type:dataset",
# sub_resources=["links", ],
# page_size=100)
# # get an hosted link
# for md in search.get("results"):
# for link in md.get("links"):
# if link.get("type") == "hosted":
# target_link = link
# else:
# continue

# # download the XML version
# print(target_link)
# dl_stream = self.isogeo.dl_hosted(self.bearer,
# id_resource=md.get("_id"),
# resource_link=target_link)

# # create tempfile and fill with downloaded XML
# # tmp_output = mkstemp(prefix="IsogeoPySDK_" + md.get("_id")[:5])
# with open(dl_stream[1], 'wb') as fd:
# for block in dl_stream[0].iter_content(1024):
# fd.write(block)

def test_dl_hosted_bad(self):
"""Test errors raised by download method"""
with self.assertRaises(ValueError):
self.isogeo.dl_hosted(self.bearer,
id_resource="trust_me_its_an_uuid",
resource_link={})


# ##############################################################################
# ##### Stand alone program ########
# ##################################
if __name__ == '__main__':
unittest.main()
8 changes: 7 additions & 1 deletion tests/test_export_XML19139.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
# ##################################


class Search(unittest.TestCase):
class ExportXML19139(unittest.TestCase):
"""Test search to Isogeo API."""
if not app_id or not app_token:
logging.critical("No API credentials set as env variables.")
Expand Down Expand Up @@ -89,6 +89,12 @@ def test_export_XML19139(self):
self.assertEqual(root[11].tag, "{http://www.isotc211.org/2005/gmd}distributionInfo")
self.assertEqual(root[12].tag, "{http://www.isotc211.org/2005/gmd}dataQualityInfo")

def test_export_bad(self):
"""Test errors raised by export function"""
with self.assertRaises(ValueError):
self.isogeo.xml19139(self.bearer,
"trust_me_its_an_uuid")


# ##############################################################################
# ##### Stand alone program ########
Expand Down
3 changes: 2 additions & 1 deletion tests/test_metadata_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
# ##################################


class Search(unittest.TestCase):
class MetadataModels(unittest.TestCase):
"""Test search to Isogeo API."""
if not app_id or not app_token:
logging.critical("No API credentials set as env variables.")
Expand Down Expand Up @@ -646,6 +646,7 @@ def test_share(self):
self.assertIsInstance(share_app.get("type"), str)
self.assertIsInstance(share_app.get("url"), str)


# ##############################################################################
# ##### Stand alone program ########
# ##################################
Expand Down

0 comments on commit 57ee93d

Please sign in to comment.