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

Class DeviceApi

source code

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:

>>> 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.
source code
 
load_data_from_file(self, json_data_file_path)
Load the DeviceAtlas device detection data into the API from a JSON file.
source code
 
load_data_from_string(self, json_data_string)
Load the DeviceAtlas device detection data into the API from a string.
source code
 
get_property_names(self)
Get a set of available device property names.
source code
 
get_data_version(self)
Get the device data (JSON file) version.
source code
 
get_data_revision(self)
Get the device data (JSON file) revision.
source code
 
get_data_creation_timestamp(self)
Get the device data (JSON file) creation timestamp.
source code
 
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.
source code
 
__add_language_properties(self, accept_language)
Get the Accept-Language header and add language properties to the property list.
source code
 
__get_property_as_byte(self, type_char) source code
 
__get_properties_from_headers(self, headers, client_side_properties) source code
 
__normalise_keys(self, headers) source code

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

Class Variables [hide private]
  api_version = '2.0'
  __config = None
hash(x)
  __tree = None
hash(x)
  __cached_client_side_properties = None
hash(x)
  __cached_user_agent = None
hash(x)
  __cached_headers = None
hash(x)
  __properties = None
hash(x)
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, config=None)
(Constructor)

source code 

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

Parameters:
  • config - Instance of Configuration. 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__

load_data_from_file(self, json_data_file_path)

source code 

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)

source code 

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

Parameters:
  • json_data_string - JSON data string.

get_property_names(self)

source code 

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)

source code 

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(self, accept_language)

source code 

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

Parameters:
  • accept_language - Accept-Language header.