Skip to content

Commit 9783776

Browse files
committed
updated docs, renamed some service methods
1 parent 07fe1da commit 9783776

7 files changed

Lines changed: 88 additions & 28 deletions

File tree

README.md

Lines changed: 79 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,101 @@
66
[![Reliability](https://sonarcloud.io/api/project_badges/measure?metric=reliability_rating&project=yassine_ipdata-java-client)](https://sonarcloud.io/dashboard/index/yassine_ipdata-java-client)
77

88

9-
An <i>ipdata.co</i> java client.
9+
An 100% compliant [ipdata.co](https://ipdata.co) API java client.
1010

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>
2031
```
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):
2237
```java
38+
import io.ipdata.client.Ipdata;
39+
40+
/.../
2341
URL url = new URL("https://api.ipdata.co");
2442
IpdataService ipdataService = Ipdata.builder().url(url)
25-
.withDefaultCache()
2643
.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+
/.../
2945
```
46+
Optionally, you can configure a cache for faster access (less than 1ms latency on requests that hit the cache).
3047

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:
3349

3450
```java
3551
URL url = new URL("https://api.ipdata.co");
3652
IpdataService ipdataService = Ipdata.builder().url(url)
3753
.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
4056
.registerCacheConfig()
4157
.key("MY_KEY")
4258
.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));
4572
```
4673

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+
47106

pom.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ project {
2727
'version.client.feign' '9.7.0'
2828
'version.build.jacoco' '0.8.4'
2929
'version.build.surefire' '3.0.0-M3'
30+
'version.build.sonar' '3.7.0.1746'
3031
'sonar.organization' 'yassine-github'
3132
'sonar.coverage.jacoco.xmlReportPaths' '${project.build.directory}/site/code-coverage/jacoco.xml'
3233
'sonar.links.homepage' 'https://github.com/yassine/ipdata-java-client'
@@ -112,7 +113,7 @@ project {
112113
argLine '${surefireArgLine}'
113114
}
114115
}
115-
plugin('org.codehaus.mojo:sonar-maven-plugin:3.6.0.1398')
116+
plugin('org.codehaus.mojo:sonar-maven-plugin:${version.build.sonar}')
116117
plugin('org.apache.maven.plugins:maven-source-plugin:3.2.1') {
117118
executions {
118119
execution('attach-sources') {

src/main/java/io/ipdata/client/Ipdata.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public Builder noCache(){
6868
*/
6969
public Builder withDefaultCache() {
7070
return new CacheConfigBuilder(this)
71-
.maxSize(Long.MAX_VALUE)
71+
.maxSize(1024)
7272
.timeout(4, TimeUnit.HOURS)
7373
.registerCacheConfig();
7474
}

src/main/java/io/ipdata/client/service/IpdataInternalClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface IpdataInternalClient {
1313
IpdataModel ipdata(@Param("ip") String ip) throws IpdataException;
1414

1515
@RequestLine("POST /bulk")
16-
List<IpdataModel> bulkIpdata(List<String> ips) throws IpdataException;
16+
List<IpdataModel> bulk(List<String> ips) throws IpdataException;
1717

1818
@Cacheable
1919
@RequestLine("GET /{ip}?fields={fields}")

src/main/java/io/ipdata/client/service/IpdataService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ public interface IpdataService extends IpdataInternalSingleFieldClient {
99

1010
IpdataModel ipdata(String ip) throws IpdataException;
1111

12-
List<IpdataModel> bulkIpdata(List<String> ips) throws IpdataException;
12+
List<IpdataModel> bulk(List<String> ips) throws IpdataException;
1313

14-
IpdataModel[] bulkIpdataAsArray(List<String> ips) throws IpdataException;
14+
IpdataModel[] bulkAsArray(List<String> ips) throws IpdataException;
1515

1616
IpdataModel getFields(String ip, IpdataField<?>... fields) throws IpdataException;
1717
}

src/main/java/io/ipdata/client/service/IpdataServiceSupport.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ private IpdataInternalSingleFieldClient getApi() {
3030
}
3131

3232
@Override
33-
public IpdataModel[] bulkIpdataAsArray(List<String> ips) throws IpdataException {
34-
return bulkIpdata(ips).toArray(new IpdataModel[0]);
33+
public IpdataModel[] bulkAsArray(List<String> ips) throws IpdataException {
34+
return bulk(ips).toArray(new IpdataModel[0]);
3535
}
3636

3737
@Override

src/test/java/io/ipdata/client/IpdataFunctionalTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public void testSingleFields() {
135135
@SneakyThrows
136136
@Test
137137
public void testBulkResponse() {
138-
List<IpdataModel> ipdataModels = ipdataService.bulkIpdata(Arrays.asList("8.8.8.8", "1.1.1.1"));
138+
List<IpdataModel> ipdataModels = ipdataService.bulk(Arrays.asList("8.8.8.8", "1.1.1.1"));
139139
String serialized = MAPPER.writeValueAsString(ipdataModels);
140140
String expected = TestUtils.post(HTTP_CLIENT, "/bulk", "[\"8.8.8.8\",\"1.1.1.1\"]", null);
141141
expected = MAPPER.writeValueAsString(MAPPER.readValue(expected, IpdataModel[].class));

0 commit comments

Comments
 (0)