Class Client
object --+
|
Client
DeviceAtlas Cloud API. This client library can be used to easily get
device data from the DeviceAtlas Cloud service. To reduce cloud calls and
improve performance the API locally caches the data returned from the
cloud service. This API caches data on the disk. If the cache path is not
writable the API will not proceed (even if the device cache is turned
off).
It is recommended to set DEBUG = True
during
implementation so if the cache path is not writable the errors will not
be ignored. The client is queried by passing either a set (dictionary) of
HTTP headers to it (if being used in a Django application
request.META
can be passed to the API) or a User-Agent
string. The device properties will then be returned.
Example 1:
Passing a custom set of HTTP headers.
>>>
>>> from deviceatlas.cloud.device import Client
>>>
>>>
>>> headers = {
>>> 'User-Agent': 'Mozilla/5.0 (SymbianOS/9.2; U; Series60/3.1 NokiaN95_8GB/15.0.015; Profile/MIDP-2.0 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413',
>>> 'X-Profile': 'http://nds.nokia.com/uaprof/NN95_8GB-1r100.xml',
>>> 'Accept': 'text/html,text/css',
>>> 'Accept-Language': 'en-us,en;q=0.5'
>>> }
>>>
>>>
>>> client = Client()
>>>
>>>
>>> client.LICENCE_KEY = 'ENTER-YOUR-LICENCE-KEY'
>>>
>>> try:
>>> results = client.getDeviceData(headers)
>>>
>>>
>>> error = results.get(client.KEY_ERROR, None)
>>>
>>>
>>> user_agent = results.get(client.KEY_USERAGENT, '')
>>>
>>>
>>> data_source = results.get(client.KEY_SOURCE, '')
>>>
>>>
>>> properties = results.get(client.KEY_PROPERTIES, {})
>>>
>>> except Exception as err:
>>>
Example 2:
Passing a User-Agent.
>>>
>>> from deviceatlas.cloud.device import Client
>>>
>>> ua = 'Mozilla/5.0 (SymbianOS/9.2; U; Series60/3.1 NokiaN95_8GB/15.0.015; Profile/MIDP-2.0 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413'
>>>
>>>
>>> client = Client()
>>>
>>>
>>> client.LICENCE_KEY = 'ENTER-YOUR-LICENCE-KEY'
>>>
>>> try:
>>> results = client.getDeviceData(user_agent=ua)
>>>
>>>
>>> error = results.get(client.KEY_ERROR, None)
>>>
>>>
>>> user_agent = results.get(client.KEY_USERAGENT, '')
>>>
>>>
>>> data_source = results.get(client.KEY_SOURCE, '')
>>>
>>>
>>> properties = results.get(client.KEY_PROPERTIES, {})
>>>
>>> except Exception as err:
>>>
Example 3:
Passing set of HTTP headers from a framework such a Django.
>>>
>>> from deviceatlas.cloud.device import Client
>>>
>>>
>>> client = Client()
>>>
>>>
>>> client.LICENCE_KEY = 'ENTER-YOUR-LICENCE-KEY'
>>>
>>> try:
>>> results = client.getDeviceData(request.META)
>>>
>>>
>>> error = results.get(client.KEY_ERROR, None)
>>>
>>>
>>> user_agent = results.get(client.KEY_USERAGENT, '')
>>>
>>>
>>> data_source = results.get(client.KEY_SOURCE, '')
>>>
>>>
>>> properties = results.get(client.KEY_PROPERTIES, {})
>>>
>>> except Exception as err:
>>>
Example 4:
Using the built-in User-Agent.
>>>
>>> from deviceatlas.cloud.device import Client
>>>
>>>
>>> client = Client()
>>>
>>>
>>> client.LICENCE_KEY = 'ENTER-YOUR-LICENCE-KEY'
>>>
>>> try:
>>> results = client.getDeviceData(test_mode=True)
>>>
>>>
>>> error = results.get(client.KEY_ERROR, None)
>>>
>>>
>>> user_agent = results.get(client.KEY_USERAGENT, '')
>>>
>>>
>>> data_source = results.get(client.KEY_SOURCE, '')
>>>
>>>
>>> properties = results.get(client.KEY_PROPERTIES, {})
>>>
>>> except Exception as err:
>>>
dict
|
getDeviceData(self,
headers=None,
test_mode=False,
user_agent='
' )
Get device data from DeviceAtlas Cloud service. |
|
|
|
get_cached_headers(self)
Collect headers based on the DA Headers |
|
|
|
get_da_headers_from_file_cache(self)
Read header from cache file if present |
|
|
str
|
getCacheBasePath(self)
Get the directory path in which the DeviceAtlas API puts cache files
inside it (device data cache, server fail-over list). |
|
|
list
|
getServersLatencies(self,
numRequests=3)
Get a list of cloud end-points and their service latencies. |
|
|
list
|
getServers(self)
Get DeviceAtlas cloud end-point list. |
|
|
list
|
rankServers(self,
servers=None)
If auto-ranking is on then rank the DeviceAtlas cloud end-points and
put in cache. |
|
|
str
|
getCloudUrl(self)
Get the URL of the latest DeviceAtlas cloud end-point that returned
device properties successfully. |
|
|
|
|
Inherited from object :
__delattr__ ,
__format__ ,
__getattribute__ ,
__hash__ ,
__init__ ,
__new__ ,
__reduce__ ,
__reduce_ex__ ,
__repr__ ,
__setattr__ ,
__sizeof__ ,
__str__ ,
__subclasshook__
|
|
LICENCE_KEY = ' ENTER-YOUR-LICENCE-KEY '
|
|
DEBUG_MODE = True
|
|
AUTO_SERVER_RANKING = True
|
|
SERVERS = [ { ' host ' : ' region0.deviceatlascloud.com ' , ' port ' : 80...
|
|
TEST_USERAGENT = ' Mozilla/5.0 (Linux; U; Android 2.3.3; en-gb; ...
|
|
CLOUD_SERVICE_TIMEOUT = 2
|
|
USE_CLIENT_COOKIE = True
|
|
USE_FILE_CACHE = True
|
|
CACHE_ITEM_EXPIRY_SEC = 2592000
|
|
CACHE_NAME = ' deviceatlas_cache_py '
|
|
USE_SYSTEM_TEMP_DIR = True
|
|
CUSTOM_CACHE_DIR = ' /path/to/your/cache/ '
|
|
SEND_EXTRA_HEADERS = False
|
|
CLIENT_COOKIE_NAME = ' DAPROPS '
|
|
AUTO_SERVER_RANKING_MAX_FAILURE = 1
|
|
AUTO_SERVER_RANKING_NUM_REQUESTS = 3
|
|
AUTO_SERVER_RANKING_LIFETIME = 1440
|
|
SERVER_PHASEOUT_LIFETIME = 1440
|
|
KEY_USERAGENT = ' useragent '
|
|
KEY_SOURCE = ' source '
|
|
KEY_ERROR = ' error '
|
|
KEY_PROPERTIES = ' properties '
|
|
API_VERSION = ' 2.2.1 '
|
|
SOURCE_FILE_CACHE = ' cache '
|
|
SOURCE_CLOUD = ' cloud '
|
|
SOURCE_NONE = ' none '
|
|
DA_HEADER_PREFIX = ' X-DA- '
|
|
CLIENT_COOKIE_HEADER = ' Client-Properties '
|
|
DA_HEADERS_NAME = ' da-headers '
|
|
COOKIE_HEADERS_NAME = ' cookie '
|
|
USER_AGENT_HEADERS_NAME = ' user-agent '
|
|
CLOUD_SERVICE_PATH = ' /v1/detect '
|
|
CLOUD_SERVICE_PROPERTIES_PATH = ' /v1/detect/properties?licence ...
|
|
CLOUD_SERVICE_HEADERS_PATH = ' /v1/detect/headers?licencekey=%s '
|
|
CLOUD_SERVICE_HEADERS_NAME_KEY = ' name '
|
|
CLOUD_SERVICE_HEADERS_REQUIRED_KEY = ' required '
|
|
FAILOVER_NOT_REQUIRED = 0
|
|
FAILOVER_STOP = 1
|
|
FAILOVER_CONTINUE = 2
|
|
rankingStatus = None
|
|
headers = None
|
Inherited from object :
__class__
|
getDeviceData(self,
headers=None,
test_mode=False,
user_agent='
' )
|
|
Get device data from DeviceAtlas Cloud service.
If using cache is not turned off the device data will be cached after
each cloud call, if cached data exists for a device it will be used over
the cloud.
If device data provided by "DeviceAtlas Client Side
Component" exists in a cookie then cloud data will be merged with
the cookie data.
>>> client = Client()
>>> client.LICENCE_KEY = 'ENTER-YOUR-LICENCE-KEY'
>>> client.getDeviceData(user_agent='User-Agent')
{'source': 'data-source','properties': {'property_name': property_value, ...},'useragent': 'user-agent', _error: 'if-any-happens'}}
- Parameters:
headers (dict) - A collection of HTTP headers.
test_mode (bool) - Get the device data using the built in test User-Agent
user_agent (str) - UTF-8 encoded User-Agent string.
- Returns: dict
- A collection of device data.
|
Get the directory path in which the DeviceAtlas API puts cache files
inside it (device data cache, server fail-over list).
- Returns: str
- Returns the path of the cache directory.
|
getServersLatencies(self,
numRequests=3)
|
|
Get a list of cloud end-points and their service latencies.
>>> client = Client()
>>> client.LICENCE_KEY = 'ENTER-YOUR-LICENCE-KEY'
>>> client.getServersLatencies()
{'host':'region0.deviceatlascloud.com','avg':0.123456789,'port':80,'latencies':[0.123456789, 0.123456789, 0.123456789]},...}
- Parameters:
numRequests (int) - The number of times to send request to an end-point per test.
- Returns: list
- A list of Cloud end-points.
|
Get DeviceAtlas cloud end-point list.
If auto-ranking is on then the ranked end-point list is returned
otherwise the manual or default original or fail-over list will be
returned.
If auto-ranking is on but no valid end-point list is found inside the
cache then the end-points will be ranked and cached.
>>> client = Client()
>>> client.LICENCE_KEY = 'ENTER-YOUR-LICENCE-KEY'
>>> client.getServers()
{'host':'region0.deviceatlascloud.com','avg':0.123456789,'port':80,'latencies':[0.123456789, 0.123456789, 0.123456789]},...}
- Returns: list
- DeviceAtlas Cloud end-point list.
|
rankServers(self,
servers=None)
|
|
If auto-ranking is on then rank the DeviceAtlas cloud end-points and
put in cache.
>>> client = Client()
>>> client.LICENCE_KEY = 'ENTER-YOUR-LICENCE-KEY'
>>> client.rankServers()
{'host':'region0.deviceatlascloud.com','avg':0.123456789,'port':80,'latencies':[0.123456789, 0.123456789, 0.123456789]},...}
- Parameters:
servers (list) - List of servers end-point. Possible values:
-
servers=None, rank and cache automatically.
-
servers=list, cache the given server list without ranking.
- Returns: list
- The ranked or re-ordered end-point list.
- Raises:
err - Exception when unable to create cache file or directory.
|
Get the URL of the latest DeviceAtlas cloud end-point that returned
device properties successfully.
There are three cases where this function will return None:
-
If device properties come from cache.
-
If licence is wrong, expired or exceeded quota.
-
None end-point were reached or could return device properties.
- Returns: str
- The end-point URL.
|
SERVERS
- Value:
[ { ' host ' : ' region0.deviceatlascloud.com ' , ' port ' : 80} ,
{ ' host ' : ' region1.deviceatlascloud.com ' , ' port ' : 80} ,
{ ' host ' : ' region2.deviceatlascloud.com ' , ' port ' : 80} ,
{ ' host ' : ' region3.deviceatlascloud.com ' , ' port ' : 80} ]
|
|
TEST_USERAGENT
- Value:
' Mozilla/5.0 (Linux; U; Android 2.3.3; en-gb; GT-I9100 Build/GINGERBRE
AD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/53
3.1 '
|
|
CLOUD_SERVICE_PROPERTIES_PATH
- Value:
' /v1/detect/properties?licencekey=%s&useragent=%s '
|
|