Package deviceatlas :: Package cloud :: Module device :: Class Client
[frames] | no frames]

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.

>>> # Import the DeviceAtlas Cloud API module.
>>> from deviceatlas.cloud.device import Client
>>>
>>> # Get data by passing headers (headers named are not case sensitive and may include "_" or start with "HTTP_").
>>> 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'
>>> }
>>>
>>> # Instantiate a client for the DeviceAtlas Cloud API.
>>> client = Client()
>>>
>>> # Set the licence key used by the DeviceAtlas Cloud API.
>>> client.LICENCE_KEY = 'ENTER-YOUR-LICENCE-KEY'
>>>
>>> try:
>>>     results = client.getDeviceData(headers)
>>>
>>>     # If DEBUG = True and errors happened while fetching data.
>>>     error = results.get(client.KEY_ERROR, None)
>>>
>>>     # The User-Agent used in the lookup.
>>>     user_agent = results.get(client.KEY_USERAGENT, '')
>>>
>>>     # The source of the data - cache, cloud or None.
>>>     data_source = results.get(client.KEY_SOURCE, '')
>>>
>>>     # The actual device properties.
>>>     properties = results.get(client.KEY_PROPERTIES, {})
>>>
>>> except Exception as err:
>>>     # Handle all errors.

Example 2:

Passing a User-Agent.

>>> # Import the DeviceAtlas Cloud API module.
>>> 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'
>>>
>>> # Instantiate a client for the DeviceAtlas Cloud API.
>>> client = Client()
>>>
>>> # Set the licence key used by the DeviceAtlas Cloud API.
>>> client.LICENCE_KEY = 'ENTER-YOUR-LICENCE-KEY'
>>>
>>> try:
>>>     results = client.getDeviceData(user_agent=ua)
>>>
>>>     # If DEBUG = True and errors happened while fetching data.
>>>     error = results.get(client.KEY_ERROR, None)
>>>
>>>     # The User-Agent used in the lookup.
>>>     user_agent = results.get(client.KEY_USERAGENT, '')
>>>
>>>     # The source of the data - cache, cloud or None.
>>>     data_source = results.get(client.KEY_SOURCE, '')
>>>
>>>     # The actual device properties.
>>>     properties = results.get(client.KEY_PROPERTIES, {})
>>>
>>> except Exception as err:
>>>     # Handle all errors

Example 3:

Passing set of HTTP headers from a framework such a Django.

>>> # Import the DeviceAtlas Cloud API module.
>>> from deviceatlas.cloud.device import Client
>>>
>>> # Instantiate a client for the DeviceAtlas Cloud API.
>>> client = Client()
>>>
>>> # Set the licence key used by the DeviceAtlas Cloud API.
>>> client.LICENCE_KEY = 'ENTER-YOUR-LICENCE-KEY'
>>>
>>> try:
>>>     results = client.getDeviceData(request.META)
>>>
>>>     # If DEBUG = True and errors happened while fetching data.
>>>     error = results.get(client.KEY_ERROR, None)
>>>
>>>     # The User-Agent used in the lookup.
>>>     user_agent = results.get(client.KEY_USERAGENT, '')
>>>
>>>     # The source of the data - cache, cloud or None.
>>>     data_source = results.get(client.KEY_SOURCE, '')
>>>
>>>     # The actual device properties.
>>>     properties = results.get(client.KEY_PROPERTIES, {})
>>>
>>> except Exception as err:
>>>     # Handle all errors.

Example 4:

Using the built-in User-Agent.

>>> # Import the DeviceAtlas Cloud API module.
>>> from deviceatlas.cloud.device import Client
>>>
>>> # Instantiate a client for the DeviceAtlas Cloud API.
>>> client = Client()
>>>
>>> # Set the licence key used by the DeviceAtlas Cloud API.
>>> client.LICENCE_KEY = 'ENTER-YOUR-LICENCE-KEY'
>>>
>>> try:
>>>     results = client.getDeviceData(test_mode=True)
>>>
>>>     # if DEBUG = True and errors happened while fetching data.
>>>     error = results.get(client.KEY_ERROR, None)
>>>
>>>     # The User-Agent used in the lookup.
>>>     user_agent = results.get(client.KEY_USERAGENT, '')
>>>
>>>     # The source of the data - cache, cloud or None.
>>>     data_source = results.get(client.KEY_SOURCE, '')
>>>
>>>     # The actual device properties.
>>>     properties = results.get(client.KEY_PROPERTIES, {})
>>>
>>> except Exception as err:
>>>     # Handle all errors.
Instance Methods
dict
getDeviceData(self, headers=None, test_mode=False, user_agent='')
Get device data from DeviceAtlas Cloud service.
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__

Class Variables
  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.0.0'
  SOURCE_FILE_CACHE = 'cache'
  SOURCE_CLOUD = 'cloud'
  SOURCE_NONE = 'none'
  DA_HEADER_PREFIX = 'X-DA-'
  CLIENT_COOKIE_HEADER = 'Client-Properties'
  CLOUD_PATH = '/v1/detect/properties?licencekey=%s&useragent=%s'
  FAILOVER_NOT_REQUIRED = 0
  FAILOVER_STOP = 1
  FAILOVER_CONTINUE = 2
  ESSENTIAL_HEADERS = ('x-profile', 'x-wap-profile', 'x-att-devi...
  ESSENTIAL_USER_AGENT_HEADERS = ('x-device-user-agent', 'x-orig...
  EXTRA_HEADERS = ('client-ip', 'x-forwarded-for', 'x-forwarded'...
  rankingStatus = None
  headers = None
Properties

Inherited from object: __class__

Method Details

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.

getCacheBasePath(self)

 

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.

getServers(self)

 

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.

getCloudUrl(self)

 

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:

  1. If device properties come from cache.
  2. If licence is wrong, expired or exceeded quota.
  3. None end-point were reached or could return device properties.
Returns: str
The end-point URL.

Class Variable Details

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'

ESSENTIAL_HEADERS

Value:
('x-profile',
 'x-wap-profile',
 'x-att-deviceid',
 'accept',
 'accept-language')

ESSENTIAL_USER_AGENT_HEADERS

Value:
('x-device-user-agent',
 'x-original-user-agent',
 'x-operamini-phone-ua',
 'x-skyfire-phone',
 'x-bolt-phone-ua',
 'device-stock-ua',
 'x-ucbrowser-ua',
 'x-ucbrowser-device-ua',
...

EXTRA_HEADERS

Value:
('client-ip',
 'x-forwarded-for',
 'x-forwarded',
 'forwarded-for',
 'forwarded',
 'proxy-client-ip',
 'wl-proxy-client-ip',
 'remote-addr')