Skip to content

Commit

Permalink
Merge pull request ipinfo#47 from ipinfo/awais/resources_inline
Browse files Browse the repository at this point in the history
Inlined the countries data
  • Loading branch information
UmanShahzad authored Oct 7, 2023
2 parents 10bf68e + 4c6a5c6 commit 93c8b9f
Show file tree
Hide file tree
Showing 9 changed files with 1,048 additions and 1,138 deletions.
32 changes: 0 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,6 @@ public class Main {
This library provides a system to lookup country names through ISO2 country
codes.

By default, this translation exists for English (United States). If you wish to
provide a different language mapping, just use the following system in the
builder:

```java
import io.ipinfo.api.IPinfo;
import io.ipinfo.api.errors.RateLimitedException;
Expand All @@ -153,7 +149,6 @@ public class Main {
public static void main(String... args) {
IPinfo ipInfo = new IPinfo.Builder()
.setToken("YOUR TOKEN")
.setCountryFile(new File("path/to/file.json"))
.build();

try {
Expand All @@ -171,18 +166,11 @@ public class Main {
}
```

This file must follow the same layout as seen [here](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/en_US.json)

More language files can be found [here](https://country.io/data)

#### EU Country Lookup

This library provides a system to lookup if a country is a member of the European Union (EU) through
ISO2 country codes.

By default, [here](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/eu.json) is the file containing all the EU members.
If you wish to provide a different file, just use the following system in the builder:

```java
import io.ipinfo.api.IPinfo;
import io.ipinfo.api.errors.RateLimitedException;
Expand All @@ -192,7 +180,6 @@ public class Main {
public static void main(String... args) {
IPinfo ipInfo = new IPinfo.Builder()
.setToken("YOUR TOKEN")
.setEUCountryFile(new File("path/to/file.json"))
.build();

try {
Expand All @@ -211,14 +198,6 @@ public class Main {

This library provides a system to lookup if a country is a member of the European Union (EU), emoji and unicode of the country's flag, code and symbol of the country's currency, and public link to the country's flag image as an SVG and continent code and name through ISO2 country codes.

Following are the file that are loaded by default:
- [eu.json](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/eu.json)
- [flags.json](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/flags.json)
- [currency.json](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/currency.json)
- [continent.json](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/continent.json)

If you wish to provide a different file, just use the following system in the builder:

```java
import io.ipinfo.api.IPinfo;
import io.ipinfo.api.errors.RateLimitedException;
Expand All @@ -228,10 +207,6 @@ public class Main {
public static void main(String... args) {
IPinfo ipInfo = new IPinfo.Builder()
.setToken("YOUR TOKEN")
.setEUCountryFile(new File("path/to/file.json"))
.setCountryFlagFile(new File("path/to/file.json"))
.setCountryCurrencyFile(new File("path/to/file.json"))
.setContinentFile(new File("path/to/file.json"))
.build();

try {
Expand All @@ -258,12 +233,6 @@ public class Main {
}
```

The files must follow the same layout as seen at:
- [eu.json](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/eu.json)
- [flags.json](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/flags.json)
- [currency.json](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/currency.json)
- [continent.json](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/continent.json)

#### Location Information

This library provides an easy way to get the latitude and longitude of an IP Address:
Expand All @@ -277,7 +246,6 @@ public class Main {
public static void main(String... args) {
IPinfo ipInfo = new IPinfo.Builder()
.setToken("YOUR TOKEN")
.setCountryFile(new File("path/to/file.json"))
.build();

try {
Expand Down
8 changes: 0 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,6 @@
<outputDirectory>target/classes</outputDirectory>

<finalName>${project.name}-${project.version}</finalName>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<includes>
<include>*.json</include>
</includes>
</resource>
</resources>

<plugins>
<plugin>
Expand Down
70 changes: 1 addition & 69 deletions src/main/java/io/ipinfo/api/IPinfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
import io.ipinfo.api.context.Context;
import io.ipinfo.api.errors.RateLimitedException;
import io.ipinfo.api.model.ASNResponse;
import io.ipinfo.api.model.Continent;
import io.ipinfo.api.model.CountryCurrency;
import io.ipinfo.api.model.CountryFlag;
import io.ipinfo.api.model.IPResponse;
import io.ipinfo.api.model.MapResponse;
import io.ipinfo.api.request.ASNRequest;
Expand All @@ -19,15 +16,11 @@
import okhttp3.*;

import javax.annotation.ParametersAreNonnullByDefault;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -361,16 +354,6 @@ public static String cacheKey(String k) {
}

public static class Builder {
private File countryFile =
new File(this.getClass().getClassLoader().getResource("en_US.json").getFile());
private File euCountryFile =
new File(this.getClass().getClassLoader().getResource("eu.json").getFile());
private File countryFlagFile =
new File(this.getClass().getClassLoader().getResource("flags.json").getFile());
private File countryCurrencyFile =
new File(this.getClass().getClassLoader().getResource("currency.json").getFile());
private File continentFile =
new File(this.getClass().getClassLoader().getResource("continent.json").getFile());
private OkHttpClient client = new OkHttpClient.Builder().build();
private String token = "";
private Cache cache = new SimpleCache(Duration.ofDays(1));
Expand All @@ -385,64 +368,13 @@ public Builder setToken(String token) {
return this;
}

public Builder setCountryFile(File file) {
this.countryFile = file;
return this;
}

public Builder setEUCountryFile(File file) {
this.euCountryFile = file;
return this;
}

public Builder setCountryFlagFile(File file) {
this.countryFlagFile = file;
return this;
}

public Builder setCountryCurrencyFile(File file) {
this.countryCurrencyFile = file;
return this;
}

public Builder setContinentFile(File file) {
this.continentFile = file;
return this;
}

public Builder setCache(Cache cache) {
this.cache = cache;
return this;
}

public IPinfo build() {
Type type = new TypeToken<Map<String, String>>(){}.getType();
Type euCountriesType = new TypeToken<List<String>>(){}.getType();
Type countriesFlagsType = new TypeToken<Map<String, CountryFlag>>(){}.getType();
Type countriesCurrencyType = new TypeToken<Map<String, CountryCurrency>>(){}.getType();
Type continentType = new TypeToken<Map<String, Continent>>(){}.getType();
Gson gson = new Gson();
Map<String, String> map;
Map<String, CountryFlag> countriesFlags;
Map<String, CountryCurrency> countriesCurrencies;
Map<String, Continent> continents;
List<String> euList;

try {
map = Collections.unmodifiableMap(gson.fromJson(new FileReader(countryFile), type));
countriesFlags = Collections.unmodifiableMap(gson.fromJson(new FileReader(countryFlagFile), countriesFlagsType));
countriesCurrencies = Collections.unmodifiableMap(gson.fromJson(new FileReader(countryCurrencyFile), countriesCurrencyType));
continents = Collections.unmodifiableMap(gson.fromJson(new FileReader(continentFile), continentType));
euList = Collections.unmodifiableList(gson.fromJson(new FileReader(euCountryFile), euCountriesType));
} catch (Exception e) {
map = Collections.unmodifiableMap(new HashMap<>());
countriesFlags = Collections.unmodifiableMap(new HashMap<>());
countriesCurrencies = Collections.unmodifiableMap(new HashMap<>());
continents = Collections.unmodifiableMap(new HashMap<>());
euList = Collections.unmodifiableList(new ArrayList<>());
}

return new IPinfo(client, new Context(map, euList, countriesFlags, countriesCurrencies, continents), token, cache);
return new IPinfo(client, new Context(), token, cache);
}
}

Expand Down
Loading

0 comments on commit 93c8b9f

Please sign in to comment.