Add `BaseUrl` configuration support. (#32) Fix #31 Fix #29

This commit is contained in:
Son Luong Ngoc 2017-05-17 15:34:29 +07:00 коммит произвёл Xuan Hu (Sean)
Родитель 9f0a1dc113
Коммит 7a5862a7de
5 изменённых файлов: 28 добавлений и 5 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -12,3 +12,4 @@ from . import person_group
from . import util
from .util import CognitiveFaceException
from .util import Key
from .util import BaseUrl

Просмотреть файл

@ -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()

Просмотреть файл

@ -11,6 +11,10 @@ Description: unittest configuration for Python SDK of the Cognitive Face API.
# 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

Просмотреть файл

@ -11,7 +11,20 @@ import requests
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