diff --git a/README.md b/README.md index 9222aa8..c024f90 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) +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) print result diff --git a/cognitive_face/__init__.py b/cognitive_face/__init__.py index cc18328..683a619 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 BaseUrl diff --git a/cognitive_face/tests/__init__.py b/cognitive_face/tests/__init__.py index 75cb521..44fc995 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 Base URL. - Setup needed data for unitests. """ CF.Key.set(config.KEY) + 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 8a031fd..93985b4 100644 --- a/cognitive_face/tests/config.sample.py +++ b/cognitive_face/tests/config.sample.py @@ -11,6 +11,10 @@ # Subscription Key for calling the Cognitive Face API. KEY = '' +# 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. TIME_SLEEP = 3 diff --git a/cognitive_face/util.py b/cognitive_face/util.py index 4addf28..e758a56 100644 --- a/cognitive_face/util.py +++ b/cognitive_face/util.py @@ -11,7 +11,20 @@ import cognitive_face as CF -_BASE_URL = 'https://westus.api.cognitive.microsoft.com/face/v1.0/' +DEFAULT_BASE_URL = 'https://westus.api.cognitive.microsoft.com/face/v1.0/' + +class BaseUrl(object): + + @classmethod + def set(cls, base_url): + cls.base_url = base_url + + @classmethod + def get(cls): + if not hasattr(cls, 'base_url'): + cls.base_url = DEFAULT_BASE_URL + return cls.base_url + TIME_SLEEP = 1 @@ -58,9 +71,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 +100,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 +130,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