# DeviceAtlas Device Identification API #
This document describes the DeviceAtlas API and how it is used.
The DeviceAtlas Device Identification API consists of two libraries. A core
library containing all of the main identification logic and a secondary
extension library to allow for easier integration into a web application
context.
## Data File ##
### Loading the data file ###
The DeviceAtlas API provides multiple methods to load the data file.
#### Downloading and loading the data file ####
The following examples download the data file, save it to the file system,
load it into the DeviceAtlas API and schedule a background task to download and load
a fresh data file at a configured time in the future.
Please see the [Data File Configuration documentation](README.DataFileConfig.html)
to obtain the data file download URL and for data file configuration options.
```csharp
using Com.DeviceAtlas.Device;
using Com.DeviceAtlas.DataFile;
DeviceApi deviceApi = new DeviceApi();
string dataFileUrl = ;
// Load using custom configuration
DataFileConfig dataFileConfig = DataFileConfigBuilder.Create().FileDirectory().BuildDevice();
deviceApi.DownloadAndLoadDataFile(dataFileConfig);
// Load using default configuration
deviceApi.DownloadAndLoadDataFile(dataFileUrl);
```
#### Loading an existing data file ####
The following examples show how to load a data file that is already on the filesystem.
```csharp
using Com.DeviceAtlas.Device;
DeviceApi deviceApi = new DeviceApi();
// Load data from file path
deviceApi.LoadDataFromFile();
// Load data from a Stream
deviceApi.LoadDataFromStream();
```
The exception DataLoadingException is thrown by the above methods in case the data file cannot be loaded.
## Client Side Library - Apple Device Identification ##
In addition to the server-side API, an optional client-side Javascript library
is available. This library is important for two reasons:
1. It facilitates identification of Apple devices. Apple devices are _not_
identifiable using only HTTP User-Agent data.
2. It allows for the collection of dynamic or changing properties.
When the client-side library is embedded in a web page it collects additional
properties from the client's web browser. This data is then sent back to the
server and must be passed to the DeviceAtlas API along with the HTTP headers.
The combination of the client data and the server side data allow for accurate
identification of Apple devices.
The client-side library may be included on a webpage by adding the following
snippet:
```Javascript
```
By default, the client-side library returns the data via a cookie. If this is
present in the `HttpRequest` object it will be automatically used. The
cookie name is configurable via the `Config` class.
Alternatively, the client data may be returned via AJAX and passed to the server
side API manually.
For additional information, please see the [Client Side Library](https://docs.deviceatlas.com/apis/clientside/latest/README.ClientSide.html) documentation.
### Usage Examples ###
The following sections shows how to call the DeviceApi. Please note that
exception handling has been omitted for brevity.
#### EXAMPLE 1: Basic Usage - Single User-Agent ####
```csharp
DeviceApi deviceApi = new DeviceApi();
DataFileConfig deviceDataFileConfig = DataFileConfigBuilder.Create().FileDirectory().BuildDevice();
deviceApi.DownloadAndLoadDataFile(deviceDataFileConfig);
// pass an identifier to the API
string userAgent = "Mozilla/5.0 (Linux; Android 11; SM-G998B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Mobile Safari/537.36";
Properties properties = deviceApi.GetProperties(userAgent);
// print all properties
foreach (KeyValuePair kv : properties)
{
Console.WriteLine(kv.Key + ": " +kv.Value.Value() + " (" + kv.Value.GetDataType() + ")");
}
// use a property
if (properties.ContainsKey("mobileDevice"))
{
bool mobileDevice = properties.Get("mobileDevice").AsBoolean();
// do something
}
// or get the property value as a string with an internal check to verify if the property exists in the set,
// otherwise the defaultValue is returned instead.
string browserName = properties.GetOrDefault("browserName", "");
```
#### EXAMPLE 2: Detection via HttpRequest ####
To use the API in a web application where a `HttpRequest` object is
available the following two step approach could be taken. Please note the
following:
> **Note 1:** This will automatically use HTTP Client Hints if they are present in
the `HttpRequest` object.
>
> **Note 2:** This will automatically read the client side library cookie if it
is available and use the data to detect Apple devices.
##### Step 1: Create an API instance and load the data file when the servlet starts:
```csharp
public class Startup
{
public void ConfigureServices(IServiceCollection service)
{
// load the data file in a try/catch block as exceptions may be thrown
DeviceApiWeb deviceApi = new DeviceApiWeb();
DataFileConfig deviceDataFileConfig = DataFileConfigBuilder.Create().FileDirectory().BuildDevice();
deviceApi.DownloadAndLoadDataFile(deviceDataFileConfig);
// store the DeviceApiWeb instance on the servlet context
service.AddSingleton(deviceApiWeb);
}
}
```
##### Step 2: Lookup the device properties when processing requests:
```csharp
public class DeviceAtlasController : Controller
{
public HttpRequest req => HttpContext.Request;
[Route(""), HttpGet]
public ActionResult Index()
{
DeviceApiWeb deviceApi = HttpContext.RequestServices.GetService();
Properties properties = deviceApi.GetProperties(request); // pass HttpRequest object
// use the properties
}
}
```
#### EXAMPLE 3: Passing Multiple Headers Without A HttpRequest Object ####
In some cases, it may not be possible to use a `HttpRequest` object. In
this scenario, an `IDictionary` of headers may be passed to the `DeviceApi`
`GetProperties()` method. It is recommended to pass all of the HTTP headers
provided by the client. The below example shows passing a User-Agent along with
a Client Hint header. In the example, the device properties are detected from
the User-Agent and the browser properties are detected from the Client Hint
header.
```csharp
DeviceApi deviceApi = new DeviceApi();
DataFileConfig deviceDataFileConfig = DataFileConfigBuilder.Create().FileDirectory().BuildDevice();
deviceApi.DownloadAndLoadDataFile(deviceDataFileConfig);
// pass a collection of HTTP headers
IDictionary headers = new Dictionary();
headers["User-Agent"] = "Mozilla/5.0 (Linux; Android 12; SM-G998B) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/102.0.0.0 Mobile Safari/537.36";
headers["Sec-CH-UA-Full-Version-List"] = "\" Not A;Brand\";v=\"99.0.0.0\", \"Chromium\";v=\"102.0.5005.125\", \"Google Chrome\";v=\"102.0.5005.25\"";
Properties properties = deviceApi.GetProperties(headers);
// use the properties
if (properties.ContainsKey("browserVersion"))
{
string browserVersion = properties.Get("browserVersion").AsString();
// do something
}
// or get the property value as a string with an internal check to verify if the property exists in the set,
// otherwise the defaultValue is returned instead.
string browserName = properties.GetOrDefault("browserName", "");
```
#### EXAMPLE 4: Passing Client Side Data ####
The data collected by the DeviceAtlas Client-side Library is automatically
passed when a `HttpRequest` is used. If a `HttpRequest` object
is not available the client-side data can be passed manually as a second
parameter to the `GetProperties()` method.
```csharp
DeviceApi deviceApi = new DeviceApi();
DataFileConfig deviceDataFileConfig = DataFileConfigBuilder.Create().FileDirectory().BuildDevice();
deviceApi.DownloadAndLoadDataFile(deviceDataFileConfig);
string userAgent = "Mozilla/5.0 (Linux; Android 12; SM-G998B) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/102.0.0.0 Mobile Safari/537.36";
string clientSideData = "scsVersion:2.1|bcookieSupport:1|bcss.animations:1|bcss.columns:1|bcss.transforms:1|bcss.transitions:1|sdeviceAspectRatio:1795/1010"
Properties properties = deviceApi.GetProperties(userAgent, clientSideData);
// use the properties
```
#### EXAMPLE 5: Configuration ####
During instantiation of the `DeviceApi` object an optional config parameter may
be passed. The example below shows the setting of a custom cookie name that the
DeviceAtlas client-side data should be found in.
```csharp
Config config = new Config();
config.CookieName = "DeviceAtlasClientSide"; // override default cookie name
config.MaxCacheEntries = 4096; // override default cache size
DeviceApi deviceApi = new DeviceApi(config);
deviceApi.LoadDataFromFile("/path/to/datafile.json");
// ... use the API as per previous examples
```
#### Additional Examples ####
Please find more complete examples in the /Examples directory.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
_ Copyright (c) DeviceAtlas Limited 2022. All Rights Reserved. _
_ https://deviceatlas.com _