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

Class DeviceApi

object --+
         |
        DeviceApi

The DeviceAtlas device detection API provides a way to detect devices based on the HTTP headers. Using these headers, the API returns device properties 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 all of the HTTP headers.
  2. Use the DeviceAtlas Client-side-component and pass the result.

Example usage:

>>> # Import the DeviceAtlas DeviceApi module.
>>> from mobi.mtld.da.device.device_api import DeviceApi
>>>
>>> headers = {
>>>     "User-Agent": "...",
>>>     "Accept-Language": "...",
>>>     # Add any other available header...
>>> }
>>>
>>> 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"))
>>>
>>> if "yearReleased" in properties:
>>>     year_released = int(properties.get("yearReleased"))

Copyright: Copyright (c) DeviceAtlas Limited 2021. All Rights Reserved.

Author: DeviceAtlas Limited

Instance Methods
 
__init__(self, config=None)
Constructs a DeviceApi instance with default or an optional custom config.
 
set_config(self, c)
Updates the DeviceApi instance with a new config.
 
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 list of all possible property names.
 
get_data_version(self)
Returns the device data (JSON file) version.
 
get_data_revision(self)
Returns the device data (JSON file) revision.
 
get_data_creation_timestamp(self)
Returns the device data (JSON file) creation timestamp.
 
get_properties(self, user_agent_or_headers, client_side_properties=None)
Get the properties for a User-Agent or a dictionary of HTTP headers optionally merged with the client side properties (provided by the DeviceAtlas Client-side component).

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

Class Variables
  api_version = u'2.2.0'
Properties

Inherited from object: __class__

Method Details

__init__(self, config=None)
(Constructor)

 

Constructs a DeviceApi instance with default or an optional custom config.

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

See Also: You can see the default configs in the class Config.

set_config(self, c)

 

Updates the DeviceApi instance with a new config.

Parameters:
  • c - An instance of Config, you can change the DeviceAtlas API config by creating an instance or Config and setting your custom config values then passing the instance to the DeviceApi constructor.

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 website.

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

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 file represented as a string.

get_property_names(self)

 

Get a list of all possible property names. Note that the client side properties are not included in this list.

Returns:
A list of all possible device property names as PropertyName objects. A PropertyName object holds a property-name and its data-type.

get_data_version(self)

 

Returns the device data (JSON file) version.

Returns:
The version of the data file.

get_data_revision(self)

 

Returns the device data (JSON file) revision.

Returns:
The revision of the data file.

get_data_creation_timestamp(self)

 

Returns the device data (JSON file) creation timestamp.

Returns:
The creation date.

get_properties(self, user_agent_or_headers, client_side_properties=None)

 

Get the properties for a User-Agent or a dictionary of HTTP headers optionally merged with the client side properties (provided by the DeviceAtlas Client-side component). The DeviceAtlas Client-side component (JavaScript library) collects the client-side properties and puts them in a cookie. The content of this cookie can be passed to this method. The client properties will override any properties discovered from the main JSON data file.

Parameters:
  • user_agent_or_headers - User-Agent string or a dictionary of HTTP headers.
  • client_side_properties - The content of the cookie containing the client side properties. If None is passed, the API will try to get the cookie from the headers.
Returns:
A dictionary of properties (Property objects) wrapped in a Properties object.