# DeviceAtlas Cloud Client API # This section deals with usage of the DeviceAtlas Cloud API. ## 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 <!-- Adding the DeviceAtlas client side component to get client side properties --> <script type="text/javascript" src="https://cs.deviceatlas-cdn.com/dacs.js" async></script> ``` By default, the client-side library returns the data via a cookie. If this is present in the `HttpServletRequest` 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. ### Basic Usage ### The DeviceAtlas Cloud Client API can be used as follows: * Import: ```java import com.deviceatlas.cloud.deviceidentification.client.Client; import com.deviceatlas.cloud.deviceidentification.client.Result; import com.deviceatlas.cloud.deviceidentification.client.Properties; import com.deviceatlas.cloud.deviceidentification.client.ClientException; import com.deviceatlas.cloud.deviceidentification.cacheprovider.CacheException; import com.deviceatlas.cloud.deviceidentification.cacheprovider.CacheProvider; import com.deviceatlas.cloud.deviceidentification.cacheprovider.FileCacheProvider; import java.util.HashMap; import java.util.Map; ``` * Get an instance of the API (singleton), set your DeviceAtlas licence key and request properties from DeviceAtlas Cloud. In a servlet container with a HttpServletRequest object: ```java try { /* First time usage, the cache provider needs to be set */ Client client = Client.getInstance(new FileCacheProvider()); /* for further use get the API instance (singleton) */ Client client = Client.getInstance(); /* set your licence key */ client.setLicenceKey(LICENCE_KEY); /* get the device properties - by request object */ Result result = client.getResult(request); Map<String, String> headers = result.getHeaders(); Properties properties = result.getProperties(); } catch (ClientException ex) { /* handle the errors */ } catch (CacheException ex) { /* handle the errors */ } ``` Detecting based on a set of HTTP headers: ```java Map<String, String> headers = new HashMap<String, String>(); headers.put("user-agent", "THE USER AGENT ..."); /* add more headers .*/ try { /* get the API instance (singleton) */ Client client = Client.getInstance(new FileCacheProvider()); /* set your licence key */ client.setLicenceKey(LICENCE_KEY); /* get device properties - by passing HTTP headers */ Result result = client.getResultByHeaders(headers); Properties properties = result.getProperties(); } catch (ClientException ex) { /* handle the errors */ } catch (CacheException ex) { /* handle the errors */ } ``` Detecting based on a User-Agent string: ```java String userAgent = "THE USER AGENT ..."; try { /* get the API instance (singleton) */ Client client = Client.getInstance(new FileCacheProvider()); /* set your licence key */ client.setLicenceKey(LICENCE_KEY); /* get device properties - by passing a user agent string */ Result result = client.getResultByUserAgent(userAgent); Properties properties = result.getProperties(); } catch (ClientException ex) { /* handle the errors */ } catch (CacheException ex) { /* handle the errors */ } ``` * Use the device properties: ```java if (properties.containsKey("mobileDevice") && properties.get("mobileDevice").asBoolean()) { /* example 1: Get the screen width for image optimization */ int displayWidth = properties.containsKey("displayWidth")? properties.get("displayWidth").asInteger(): 100; /* example 2: Get the device vendor name */ String vendor = properties.containsKey("vendor")? properties.get("vendor").asString(): ""; /* example 3: Touch screen optimization */ boolean useBiggerIcons = properties.containsKey("touchScreen")? properties.get("touchScreen").asBoolean(): false; /* example 4: Send Geo Location JS to client? */ boolean supportsGeoLocation = properties.containsKey("js.geoLocation")? properties.get("js.geoLocation").asBoolean(): false; } ``` See the list of all property names here: https://deviceatlas.com/resources/available-properties The availability of a property depends on the device and your licence, before accessing a property always check if it exists in the set or not. ### Additional Examples ### Please find more complete examples in the /Examples directory. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - _ Copyright (c) DeviceAtlas Limited 2023. All Rights Reserved. _ _ https://deviceatlas.com _ <!-- HTML+JS for document formatting when opened in browser --> <div class="btn-group" id="main-menu" style="float:right"><a class="btn dropdown-toggle" data-toggle="dropdown" href="#">Menu<span class="caret"></span></a><ul class="dropdown-menu"><li><a href="README.html">Cloud Client API</a></li><li><a href="README.Installation.html">Cloud Client API Installation</a></li><li><a href="README.Config.html">Cloud Client API Configuration</a></li><li><a href="./Javadoc/index.html">DeviceAtlas Cloud Client API doc</a></li></ul></div>