From 5dd1076c13de919079bf3f1812ebe5d23413fa5f Mon Sep 17 00:00:00 2001 From: Son Luong Ngoc Date: Wed, 17 May 2017 07:15:56 +0700 Subject: [PATCH 1/3] Implemented Regional API URL in config instead of hardcoded --- cognitive_face/__init__.py | 1 + cognitive_face/tests/__init__.py | 2 ++ cognitive_face/tests/config.sample.py | 3 +++ cognitive_face/util.py | 38 +++++++++++++++++++++++---- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/cognitive_face/__init__.py b/cognitive_face/__init__.py index cc18328..01f271c 100644 --- a/cognitive_face/__init__.py +++ b/cognitive_face/__init__.py @@ -12,3 +12,4 @@ from . import util from .util import CognitiveFaceException from .util import Key +from .util import Region diff --git a/cognitive_face/tests/__init__.py b/cognitive_face/tests/__init__.py index 75cb521..41eb62b 100644 --- a/cognitive_face/tests/__init__.py +++ b/cognitive_face/tests/__init__.py @@ -23,9 +23,11 @@ def setUpModule(): """Setup for the whole unitests. - Set Subscription Key. + - Set API Region. - Setup needed data for unitests. """ CF.Key.set(config.KEY) + CF.Region.set(config.REGION) util.DataStore.setup_person_group() util.DataStore.setup_face_list() util.DataStore.setup_face() diff --git a/cognitive_face/tests/config.sample.py b/cognitive_face/tests/config.sample.py index 8a031fd..a6bde66 100644 --- a/cognitive_face/tests/config.sample.py +++ b/cognitive_face/tests/config.sample.py @@ -11,6 +11,9 @@ # Subscription Key for calling the Cognitive Face API. KEY = '' +# Region for calling the Cognitive Face API. +REGION = '' + # Time (in seconds) for sleep between each call to avoid exceeding quota. # Default to 3 as free subscription have limit of 20 calls per minute. TIME_SLEEP = 3 diff --git a/cognitive_face/util.py b/cognitive_face/util.py index 4addf28..d978a86 100644 --- a/cognitive_face/util.py +++ b/cognitive_face/util.py @@ -11,7 +11,35 @@ import cognitive_face as CF -_BASE_URL = 'https://westus.api.cognitive.microsoft.com/face/v1.0/' + +class Region(object): + """Manage API region""" + + @classmethod + def set(cls, region): + """Set the API region""" + cls.region = region + + @classmethod + def get(cls): + """Get the API region""" + if not hasattr(cls, 'region'): + cls.region = 'uswest' + return cls.region + + +class BaseUrl(object): + + @classmethod + def set(cls, url): + cls.url = url + + @classmethod + def get(cls): + if not hasattr(cls, 'url'): + cls.url = 'https://' + Region.get() + '.api.cognitive.microsoft.com/face/v1.0/' + return cls.url + TIME_SLEEP = 1 @@ -58,9 +86,9 @@ def request(method, url, data=None, json=None, headers=None, params=None): # pylint: disable=too-many-arguments """Universal interface for request.""" - # Make it possible to call only with short name (without _BASE_URL). + # Make it possible to call only with short name (without BaseUrl). if not url.startswith('https://'): - url = _BASE_URL + url + url = BaseUrl.get() + url # Setup the headers with default Content-Type and Subscription Key. headers = headers or {} @@ -87,7 +115,7 @@ def request(method, url, data=None, json=None, headers=None, params=None): error_msg.get('code'), error_msg.get('message')) - # Prevent `reponse.json()` complains about empty response. + # Prevent `response.json()` complains about empty response. if response.text: result = response.json() else: @@ -117,7 +145,7 @@ def parse_image(image): headers = {'Content-Type': 'application/octet-stream'} data = open(image, 'rb').read() return headers, data, None - else: # Defailt treat it as a URL (string). + else: # Default treat it as a URL (string). headers = {'Content-Type': 'application/json'} json = {'url': image} return headers, None, json From 69728d650c8ed0f3092a0d785c1ae2891e6de152 Mon Sep 17 00:00:00 2001 From: Son Luong Ngoc Date: Wed, 17 May 2017 10:39:54 +0700 Subject: [PATCH 2/3] Update Minimal Usage section with Regional API config --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 9222aa8..ac0ecc3 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,9 @@ import cognitive_face as CF KEY = 'subscription key' # Replace with a valid Subscription Key here. CF.Key.set(KEY) +REGION = 'westus' # Replace with a valid region: westus, eastus2, westcentralus, westeurope, southeastasia +CF.Region.set(REGION) # If not set, default will be 'westus' + img_url = 'https://raw.githubusercontent.com/Microsoft/Cognitive-Face-Windows/master/Data/detection1.jpg' result = CF.face.detect(img_url) print result From bcc77e325481e700d9f4327058a880ce96f60c74 Mon Sep 17 00:00:00 2001 From: Son Luong Ngoc Date: Wed, 17 May 2017 14:57:30 +0700 Subject: [PATCH 3/3] Modified solution from using Region to use BaseUrl instead --- README.md | 4 ++-- cognitive_face/__init__.py | 2 +- cognitive_face/tests/__init__.py | 4 ++-- cognitive_face/tests/config.sample.py | 5 +++-- cognitive_face/util.py | 27 ++++++--------------------- 5 files changed, 14 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index ac0ecc3..c024f90 100644 --- a/README.md +++ b/README.md @@ -33,8 +33,8 @@ import cognitive_face as CF KEY = 'subscription key' # Replace with a valid Subscription Key here. CF.Key.set(KEY) -REGION = 'westus' # Replace with a valid region: westus, eastus2, westcentralus, westeurope, southeastasia -CF.Region.set(REGION) # If not set, default will be 'westus' +BASE_URL = 'https://westus.api.cognitive.microsoft.com/face/v1.0/' # Replace with your regional Base URL +CF.BaseUrl.set(BASE_URL) img_url = 'https://raw.githubusercontent.com/Microsoft/Cognitive-Face-Windows/master/Data/detection1.jpg' result = CF.face.detect(img_url) diff --git a/cognitive_face/__init__.py b/cognitive_face/__init__.py index 01f271c..683a619 100644 --- a/cognitive_face/__init__.py +++ b/cognitive_face/__init__.py @@ -12,4 +12,4 @@ from . import util from .util import CognitiveFaceException from .util import Key -from .util import Region +from .util import BaseUrl diff --git a/cognitive_face/tests/__init__.py b/cognitive_face/tests/__init__.py index 41eb62b..44fc995 100644 --- a/cognitive_face/tests/__init__.py +++ b/cognitive_face/tests/__init__.py @@ -23,11 +23,11 @@ def setUpModule(): """Setup for the whole unitests. - Set Subscription Key. - - Set API Region. + - Set Base URL. - Setup needed data for unitests. """ CF.Key.set(config.KEY) - CF.Region.set(config.REGION) + CF.BaseUrl.set(config.BASE_URL) util.DataStore.setup_person_group() util.DataStore.setup_face_list() util.DataStore.setup_face() diff --git a/cognitive_face/tests/config.sample.py b/cognitive_face/tests/config.sample.py index a6bde66..93985b4 100644 --- a/cognitive_face/tests/config.sample.py +++ b/cognitive_face/tests/config.sample.py @@ -11,8 +11,9 @@ # Subscription Key for calling the Cognitive Face API. KEY = '' -# Region for calling the Cognitive Face API. -REGION = '' +# Base URL for calling the Cognitive Face API. +# default is 'https://westus.api.cognitive.microsoft.com/face/v1.0/' +BASE_URL = '' # Time (in seconds) for sleep between each call to avoid exceeding quota. # Default to 3 as free subscription have limit of 20 calls per minute. diff --git a/cognitive_face/util.py b/cognitive_face/util.py index d978a86..e758a56 100644 --- a/cognitive_face/util.py +++ b/cognitive_face/util.py @@ -11,34 +11,19 @@ import cognitive_face as CF - -class Region(object): - """Manage API region""" - - @classmethod - def set(cls, region): - """Set the API region""" - cls.region = region - - @classmethod - def get(cls): - """Get the API region""" - if not hasattr(cls, 'region'): - cls.region = 'uswest' - return cls.region - +DEFAULT_BASE_URL = 'https://westus.api.cognitive.microsoft.com/face/v1.0/' class BaseUrl(object): @classmethod - def set(cls, url): - cls.url = url + def set(cls, base_url): + cls.base_url = base_url @classmethod def get(cls): - if not hasattr(cls, 'url'): - cls.url = 'https://' + Region.get() + '.api.cognitive.microsoft.com/face/v1.0/' - return cls.url + if not hasattr(cls, 'base_url'): + cls.base_url = DEFAULT_BASE_URL + return cls.base_url TIME_SLEEP = 1