|
6 | 6 | [](https://sonarcloud.io/dashboard/index/yassine_ipdata-java-client) |
7 | 7 |
|
8 | 8 |
|
9 | | -An <i>ipdata.co</i> java client. |
| 9 | +An 100% compliant [ipdata.co](https://ipdata.co) API java client. |
10 | 10 |
|
11 | | -Recipes: |
12 | | -1. I wanna call the api without caching results : |
13 | | -```java |
14 | | -URL url = new URL("https://api.ipdata.co"); |
15 | | -IpdataService ipdataService = Ipdata.builder().url(url) |
16 | | - .noCache() |
17 | | - .key("MY_KEY") |
18 | | - .get(); |
19 | | -IpdataModel model = ipdataService.get("1.1.1.1"); |
| 11 | +**Table of Contents** |
| 12 | + |
| 13 | +- [Install](#install) |
| 14 | +- [Use](#use) |
| 15 | + - [Configuration](#configuration) |
| 16 | + - [API](#create-an-instance) |
| 17 | + - [Basic Usage](#basic-usage) |
| 18 | + - [Single Field Selection](#single-field-selection) |
| 19 | + - [Multiple Field Selection](#multiple-field-selection) |
| 20 | + - [Bulk data](#bulk-data) |
| 21 | + |
| 22 | + |
| 23 | +## Install |
| 24 | +You can have the library from Maven Central. |
| 25 | +``` |
| 26 | +<dependency> |
| 27 | + <groupId>co.ipdata.client</groupId> |
| 28 | + <artifactId>ipdata-java-client</artifactId> |
| 29 | + <version>TBD</version> |
| 30 | +</dependency> |
20 | 31 | ``` |
21 | | -2. I wanna call the api while caching results for further calls on the same IP (caching will yield you a latency of less than 1ms for requests that hit the cache): |
| 32 | + |
| 33 | +## Use |
| 34 | + |
| 35 | +### Configuration |
| 36 | +A builder is available to help configure your client configuration, you'll have to provide the API endpoint and an [API key](https://ipdata.co/pricing.html): |
22 | 37 | ```java |
| 38 | +import io.ipdata.client.Ipdata; |
| 39 | + |
| 40 | +/.../ |
23 | 41 | URL url = new URL("https://api.ipdata.co"); |
24 | 42 | IpdataService ipdataService = Ipdata.builder().url(url) |
25 | | - .withDefaultCache() |
26 | 43 | .key("MY_KEY").get(); |
27 | | -IpdataModel model = ipdataService.get("1.1.1.1"); //cache miss here |
28 | | -ipdataService.get("1.1.1.1"); //cache hit from here on ip address "1.1.1.1" |
| 44 | +/.../ |
29 | 45 | ``` |
| 46 | +Optionally, you can configure a cache for faster access (less than 1ms latency on requests that hit the cache). |
30 | 47 |
|
31 | | -3. My 50% user session length is 30 minutes, and I don't want to have more than 8 * 1024 items in cache, |
32 | | -how can I do that? |
| 48 | +The cache is configurable for time and space eviction policies: |
33 | 49 |
|
34 | 50 | ```java |
35 | 51 | URL url = new URL("https://api.ipdata.co"); |
36 | 52 | IpdataService ipdataService = Ipdata.builder().url(url) |
37 | 53 | .withCache() |
38 | | - .timeout(30, TimeUnit.MINUTES) |
39 | | - .maxSize(8 * 1024) |
| 54 | + .timeout(30, TimeUnit.MINUTES) //ttl after first write |
| 55 | + .maxSize(8 * 1024) //no more than 8*1024 items shall be stored in cache |
40 | 56 | .registerCacheConfig() |
41 | 57 | .key("MY_KEY") |
42 | 58 | .get(); |
43 | | -IpdataModel model = ipdataService.get("1.1.1.1"); //cache miss here |
44 | | -ipdataService.get("1.1.1.1"); //cache hit from here on ip address "1.1.1.1" |
| 59 | +IpdataModel model = ipdataService.ipdata("1.1.1.1"); //cache miss here |
| 60 | +ipdataService.ipdata("1.1.1.1"); //cache hit from now on on ip address "1.1.1.1" |
| 61 | +``` |
| 62 | + |
| 63 | +### API |
| 64 | +The client is fully compliant with the API. The data model of the api is available under the package ``io.ipdata.client.model``. |
| 65 | +Interaction with the API is captured by the Service Interface ``io.ipdata.client.service.IpdataService``: |
| 66 | + |
| 67 | +#### Basic Usage |
| 68 | +To get all available information about a given IP address, you can use the ``get`` method of the service Interface: |
| 69 | +```java |
| 70 | +IpdataModel model = ipdataService.ipdata("1.1.1.1"); |
| 71 | +System.out.println(jsonSerialize(model)); |
45 | 72 | ``` |
46 | 73 |
|
| 74 | +#### Single Field Selection |
| 75 | +If you're interested in only one field from the model capturing an IP address information, The service interface |
| 76 | +exposes some a method on each available field: |
| 77 | + |
| 78 | +```java |
| 79 | +boolean isEu = ipdataService.isEu("1.1.1.1"); |
| 80 | +AsnModel asn = ipdataService.asn("1.1.1.1"); |
| 81 | +TimeZone tz = ipdataService.timeZone("1.1.1.1"); |
| 82 | +ThreatModel threat = ipdataService.threat("1.1.1.1"); |
| 83 | +/*...*/ |
| 84 | +``` |
| 85 | +The list of available fields is available [here](https://docs.ipdata.co/api-reference/response-fields) |
| 86 | + |
| 87 | +#### Multiple Field Selection |
| 88 | +If you're interested by multiple fields for a given IP address, you'll use the ``getFields`` method: |
| 89 | +```java |
| 90 | +import io.ipdata.client.service.IpdataField; |
| 91 | +import io.ipdata.client.service.IpdataService; |
| 92 | + |
| 93 | +/* The model will be hydrated by the selected fields only */ |
| 94 | +IpdataModel model = ipdataService.getFields("1.1.1.1", IpdataField.ASN, IpdataField.CURRENCY); |
| 95 | + |
| 96 | +``` |
| 97 | + |
| 98 | +### Bulk data |
| 99 | +You can as well get multiple responses at once by using the ``bulk`` api: |
| 100 | + |
| 101 | +```java |
| 102 | +List<IpdataModel> models = ipdataService.bulkIpdata(Arrays.asList("1.1.1.1", "8.8.8.8")); |
| 103 | +``` |
| 104 | + |
| 105 | + |
47 | 106 |
|
0 commit comments