Skip to content
This repository was archived by the owner on Nov 8, 2021. It is now read-only.

Commit 7a5862a

Browse files
sluongnghuxuan
authored andcommitted
Add BaseUrl configuration support. (#32) Fix #31 Fix #29
1 parent 9f0a1dc commit 7a5862a

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ import cognitive_face as CF
3333
KEY = 'subscription key' # Replace with a valid Subscription Key here.
3434
CF.Key.set(KEY)
3535

36+
BASE_URL = 'https://westus.api.cognitive.microsoft.com/face/v1.0/' # Replace with your regional Base URL
37+
CF.BaseUrl.set(BASE_URL)
38+
3639
img_url = 'https://raw.githubusercontent.com/Microsoft/Cognitive-Face-Windows/master/Data/detection1.jpg'
3740
result = CF.face.detect(img_url)
3841
print result

cognitive_face/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
from . import util
1313
from .util import CognitiveFaceException
1414
from .util import Key
15+
from .util import BaseUrl

cognitive_face/tests/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ def setUpModule():
2323
"""Setup for the whole unitests.
2424
2525
- Set Subscription Key.
26+
- Set Base URL.
2627
- Setup needed data for unitests.
2728
"""
2829
CF.Key.set(config.KEY)
30+
CF.BaseUrl.set(config.BASE_URL)
2931
util.DataStore.setup_person_group()
3032
util.DataStore.setup_face_list()
3133
util.DataStore.setup_face()

cognitive_face/tests/config.sample.py

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
# Subscription Key for calling the Cognitive Face API.
1212
KEY = ''
1313

14+
# Base URL for calling the Cognitive Face API.
15+
# default is 'https://westus.api.cognitive.microsoft.com/face/v1.0/'
16+
BASE_URL = ''
17+
1418
# Time (in seconds) for sleep between each call to avoid exceeding quota.
1519
# Default to 3 as free subscription have limit of 20 calls per minute.
1620
TIME_SLEEP = 3

cognitive_face/util.py

+18-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,20 @@
1111

1212
import cognitive_face as CF
1313

14-
_BASE_URL = 'https://westus.api.cognitive.microsoft.com/face/v1.0/'
14+
DEFAULT_BASE_URL = 'https://westus.api.cognitive.microsoft.com/face/v1.0/'
15+
16+
class BaseUrl(object):
17+
18+
@classmethod
19+
def set(cls, base_url):
20+
cls.base_url = base_url
21+
22+
@classmethod
23+
def get(cls):
24+
if not hasattr(cls, 'base_url'):
25+
cls.base_url = DEFAULT_BASE_URL
26+
return cls.base_url
27+
1528
TIME_SLEEP = 1
1629

1730

@@ -58,9 +71,9 @@ def request(method, url, data=None, json=None, headers=None, params=None):
5871
# pylint: disable=too-many-arguments
5972
"""Universal interface for request."""
6073

61-
# Make it possible to call only with short name (without _BASE_URL).
74+
# Make it possible to call only with short name (without BaseUrl).
6275
if not url.startswith('https://'):
63-
url = _BASE_URL + url
76+
url = BaseUrl.get() + url
6477

6578
# Setup the headers with default Content-Type and Subscription Key.
6679
headers = headers or {}
@@ -87,7 +100,7 @@ def request(method, url, data=None, json=None, headers=None, params=None):
87100
error_msg.get('code'),
88101
error_msg.get('message'))
89102

90-
# Prevent `reponse.json()` complains about empty response.
103+
# Prevent `response.json()` complains about empty response.
91104
if response.text:
92105
result = response.json()
93106
else:
@@ -117,7 +130,7 @@ def parse_image(image):
117130
headers = {'Content-Type': 'application/octet-stream'}
118131
data = open(image, 'rb').read()
119132
return headers, data, None
120-
else: # Defailt treat it as a URL (string).
133+
else: # Default treat it as a URL (string).
121134
headers = {'Content-Type': 'application/json'}
122135
json = {'url': image}
123136
return headers, None, json

0 commit comments

Comments
 (0)