Skip to content

Commit 03118c1

Browse files
committed
Initial commit
0 parents  commit 03118c1

31 files changed

Lines changed: 1008 additions & 0 deletions

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Editor configuration, see https://editorconfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
indent_style = space
7+
indent_size = 2
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
max_line_length = off
13+
trim_trailing_whitespace = false

.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
target/
2+
pom.xml.tag
3+
pom.xml.releaseBackup
4+
pom.xml.versionsBackup
5+
pom.xml.next
6+
release.properties
7+
dependency-reduced-pom.xml
8+
buildNumber.properties
9+
.mvn/timing.properties
10+
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
11+
.mvn/wrapper/maven-wrapper.jar
12+
13+
# IntelliJ project files
14+
.idea
15+
*.iml
16+
out
17+
gen
18+
NOTES

.mvn/extensions.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<extensions>
3+
<extension>
4+
<groupId>io.takari.polyglot</groupId>
5+
<artifactId>polyglot-groovy</artifactId>
6+
<version>0.4.3</version>
7+
</extension>
8+
</extensions>
9+

lombok.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lombok.addLombokGeneratedAnnotation = true

pom.groovy

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
project {
2+
modelVersion '4.0.0'
3+
groupId 'io.ipdata.client'
4+
artifactId 'ipdata-client'
5+
version '0.1.0-SNAPSHOT'
6+
properties {
7+
'project.build.sourceEncoding' 'UTF-8'
8+
'maven.compiler.source' '6'
9+
'maven.compiler.target' '6'
10+
}
11+
dependencies{
12+
dependency ('io.github.openfeign:feign-core:9.7.0')
13+
dependency ('io.github.openfeign:feign-jackson:9.7.0')
14+
dependency ('io.github.openfeign:feign-httpclient:9.7.0')
15+
dependency ('com.google.guava:guava:20.0')
16+
dependency ('org.slf4j:slf4j-api:1.7.30')
17+
dependency ('org.slf4j:slf4j-log4j12:1.7.30')
18+
dependency ('org.projectlombok:lombok:1.18.10')
19+
/* testing */
20+
dependency('org.hamcrest:hamcrest:2.2:test')
21+
dependency('junit:junit:4.13')
22+
}
23+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package io.ipdata.client;
2+
3+
import io.ipdata.client.service.CacheConfig;
4+
import java.util.concurrent.TimeUnit;
5+
import lombok.AccessLevel;
6+
import lombok.RequiredArgsConstructor;
7+
import lombok.experimental.Accessors;
8+
9+
@Accessors(fluent = true)
10+
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
11+
public class CacheConfigBuilder {
12+
13+
private final Ipdata.Builder parent;
14+
private long maxSize;
15+
private int timeout;
16+
private TimeUnit unit;
17+
18+
/**
19+
* Configures the cache maximum size.
20+
* @param maxSize The maximum of items to keep in Cache
21+
* @return this builder
22+
*/
23+
public CacheConfigBuilder maxSize(long maxSize) {
24+
if (maxSize <= 0) {
25+
throw new IllegalArgumentException("cache size must be greater than 0");
26+
}
27+
this.maxSize = maxSize;
28+
return this;
29+
}
30+
31+
/**
32+
* The maximum duration before invalidating a cache entry.
33+
* @param timeout The duration
34+
* @param unit The duration unit
35+
* @return
36+
*/
37+
public CacheConfigBuilder timeout(int timeout, TimeUnit unit) {
38+
if (timeout <= 0) {
39+
throw new IllegalArgumentException("cache timeout must be greater than 0");
40+
}
41+
if (unit == null) {
42+
throw new IllegalArgumentException("timeout unit should not be null");
43+
}
44+
this.timeout = timeout;
45+
this.unit = unit;
46+
return this;
47+
}
48+
49+
/**
50+
* Builds A <code>CacheConfig</code> instance and registers it in the <code>Ipdata.Builder</code>
51+
* @return The <code>Ipdata.Builder</code> that created this <code>CacheConfigBuilder</code>
52+
*/
53+
public Ipdata.Builder registerCacheConfig() {
54+
return parent.cacheConfig(CacheConfig.builder()
55+
.maxSize(maxSize)
56+
.timeout(timeout)
57+
.unit(unit)
58+
.build());
59+
}
60+
61+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package io.ipdata.client;
2+
3+
import feign.Client;
4+
import io.ipdata.client.service.CacheConfig;
5+
import io.ipdata.client.service.IpdataService;
6+
import io.ipdata.client.service.IpdataServiceBuilder;
7+
import java.net.URL;
8+
import java.util.concurrent.TimeUnit;
9+
import lombok.AccessLevel;
10+
import lombok.NoArgsConstructor;
11+
import lombok.Setter;
12+
import lombok.experimental.Accessors;
13+
import lombok.experimental.UtilityClass;
14+
import org.slf4j.Logger;
15+
16+
@UtilityClass
17+
public final class Ipdata {
18+
19+
public static Ipdata.Builder builder() {
20+
return new Builder();
21+
}
22+
23+
@Setter
24+
@Accessors(fluent = true, chain = true)
25+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
26+
public static class Builder {
27+
28+
/**
29+
* The Ipdata API key
30+
*/
31+
@Setter
32+
private String key;
33+
/**
34+
* The target URL for the client
35+
*/
36+
@Setter
37+
private URL url;
38+
/**
39+
* (Optional) To Cache api calls, a cache configuration can be provided
40+
*/
41+
@Setter(AccessLevel.PACKAGE)
42+
private CacheConfig cacheConfig;
43+
/**
44+
* (Optional) Configures a logger to use by the client
45+
*/
46+
@Setter
47+
private Logger logger;
48+
/**
49+
* (Optional) Configures a custom Feign client (If you wanna use this with Ribbon or Hystrix for example).
50+
*/
51+
@Setter
52+
private Client feignClient;
53+
54+
/**
55+
* Configures a cache with default configuration parameters.
56+
* Note: Overrides any previously configured cache.
57+
*
58+
* @return this builder
59+
*/
60+
public Builder withDefaultCache() {
61+
return new CacheConfigBuilder(this)
62+
.maxSize(Long.MAX_VALUE)
63+
.timeout(4, TimeUnit.HOURS)
64+
.registerCacheConfig();
65+
}
66+
67+
/**
68+
* Allows for customizing the configuration of the cache.
69+
* Note: Overrides any previously configured cache.
70+
*
71+
* @return a cache configuration builder
72+
* @see CacheConfigBuilder
73+
*/
74+
public CacheConfigBuilder withCache() {
75+
return new CacheConfigBuilder(this);
76+
}
77+
78+
/**
79+
* @return An Ipdata api facade
80+
* @see io.ipdata.client.service.IpdataService
81+
*/
82+
public IpdataService get() {
83+
if (key == null) {
84+
throw new IllegalArgumentException("Key can't be null");
85+
}
86+
if (url == null) {
87+
throw new IllegalArgumentException("Target URL can't be null");
88+
}
89+
return IpdataServiceBuilder.of(url, key, cacheConfig, logger, feignClient).build();
90+
}
91+
92+
}
93+
94+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.ipdata.client.error;
2+
3+
/**
4+
* A generic Ipdata client exception.
5+
*/
6+
public class IpdataException extends Exception {
7+
public IpdataException(String message) {
8+
super(message);
9+
}
10+
11+
public IpdataException(String message, Throwable cause) {
12+
super(message, cause);
13+
}
14+
15+
public IpdataException(Throwable cause) {
16+
super(cause);
17+
}
18+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.ipdata.client.error;
2+
3+
import lombok.Getter;
4+
5+
/**
6+
* An <code>IpdataException</code> specialization that captures errors from the downstream.
7+
*/
8+
public class RemoteIpdataException extends IpdataException {
9+
@Getter
10+
private final int status;
11+
12+
public RemoteIpdataException(String message, Throwable cause, int status) {
13+
super(message, cause);
14+
this.status = status;
15+
}
16+
17+
public RemoteIpdataException(String message, int status) {
18+
super(message);
19+
this.status = status;
20+
}
21+
22+
@Override
23+
public String toString() {
24+
return String.format("%nMessage : '%s'%nHttp Status: '%s'", getMessage(), status);
25+
}
26+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.ipdata.client.model;
2+
3+
import lombok.Getter;
4+
import lombok.ToString;
5+
import lombok.experimental.Accessors;
6+
7+
@ToString @Getter @Accessors(fluent = true)
8+
public class Asn {
9+
private String asn;
10+
private String name;
11+
private String domain;
12+
private String route;
13+
private String isp;
14+
}

0 commit comments

Comments
 (0)