Package mobi :: Package mtld :: Package da :: Package device :: Module device_api :: Class DeviceApi
[hide private]
[frames] | no frames]

Class DeviceApi

object --+
         |
        DeviceApi

The DeviceAtlas Device Detection API provides a way to detect devices based on the HTTP headers. Using the headers, the API returns device information such as screen width, screen height, is mobile, vendor, model etc.

DeviceApi.get_properties(user_agent_or_headers, client_side_properties)

To get the most accurate results: 1- Pass the whole HTTP headers. 2- Use the DeviceAtlas client-side-component and pass the result.

Example usage:

>>> headers = {}
>>> device_api = DeviceApi()
>>> device_api.load_data_from_file("/path/to/datafile.json")
>>>
>>> # get all properties from the headers
>>> properties = device_api.get_properties(headers)
>>>
>>> # .... use the properties ....
>>>
>>> if properties.contains("isMobilePhone", True):
>>>  # it is a mobile phone
>>>
>>> if "model" in properties:
>>>  device_model = str(properties.get("model"))
Instance Methods [hide private]
 
__init__(self, config=None)
Constructs a DeviceApi instance with default configs.
 
set_config(self, c)
Applies a configuration object to the api instance, resetting the cache and to the loaded tree if exists
 
load_data_from_file(self, json_data_file_path)
Load the DeviceAtlas device detection data into the API from a JSON file.
 
load_data_from_string(self, json_data_string)
Load the DeviceAtlas device detection data into the API from a string.
 
get_property_names(self)
Get a set of available device property names.
 
get_data_version(self)
Get the device data (JSON file) version.
 
get_data_revision(self)
Get the device data (JSON file) revision.
 
get_data_creation_timestamp(self)
Get the device data (JSON file) creation timestamp.
 
get_properties(self, user_agent_or_headers, client_side_properties=None)
Get known properties from a User-Agent or HTTP headers optionally merged with properties from the client side component.
 
__put_cache(self, key, properties)
 
__get_properties_from_headers(self, user_agent_or_headers, client_side_properties, properties)

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Static Methods [hide private]
 
__add_language_properties(accept_language, properties)
Get the Accept-Language header and add language properties to the property list.
 
__get_property_as_byte(type_char)
 
__dict_to_str(dictionary)
 
__normalise_keys(headers)
 
__get_utf8_string(string)
Class Variables [hide private]
  api_version = u'DEVICE_API_VERSION'
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, config=None)
(Constructor)

 

Constructs a DeviceApi instance with default configs. You can see the default configs in the class "Config".

Parameters:
  • config - Instance of Config. You can change the DeviceAtlas API configs by creating an instance or Configuration and setting your custom config values then passing the instance to the DeviceApi constructor.
Overrides: object.__init__

set_config(self, c)

 

Applies a configuration object to the api instance, resetting the cache and to the loaded tree if exists

:param c: configuration instance :return:

load_data_from_file(self, json_data_file_path)

 

Load the DeviceAtlas device detection data into the API from a JSON file. The JSON data file is provided from the DeviceAtlas web-site.

Parameters:
  • json_data_file_path - Path to the JSON data file.

load_data_from_string(self, json_data_string)

 

Load the DeviceAtlas device detection data into the API from a string.

Parameters:
  • json_data_string - JSON data string.

get_property_names(self)

 

Get a set of available device property names. It returns a list of PropertyName objects.

get_properties(self, user_agent_or_headers, client_side_properties=None)

 

Get known properties from a User-Agent or HTTP headers optionally merged with properties from the client side component. The client side component (JS) sets a cookie with collected properties. The client properties will over-ride any properties discovered from the main JSON data file.

Parameters:
  • user_agent_or_headers - User-Agent string or array of HTTP headers.
  • client_side_properties - String of client side properties with the format the client side component provides. It returns a list of Property objects

__add_language_properties(accept_language, properties)
Static Method

 

Get the Accept-Language header and add language properties to the property list.

Parameters:
  • accept_language - Accept-Language header.