diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..81a66a0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +/target/ +/.settings/ +/.classpath +/.project +/build/ +/.idea/ +*.iml + diff --git a/README.md b/README.md new file mode 100644 index 0000000..d19783a --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +## GeoapifyAPi + +https://apidocs.geoapify.com/docs/ + +Instantiate + +```java +GeoapifyApi api =new GeoapifyApi(apiKey); +``` + +Postal Code Lookip + +```java +api.postalCodeLookup(postalCode, countryCode); +``` diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..21e92e7 --- /dev/null +++ b/pom.xml @@ -0,0 +1,45 @@ + + 4.0.0 + + io.ljunggren + geoapify-api + 0.0.1-SNAPSHOT + jar + + geoapify-api + http://maven.apache.org + + + UTF-8 + 1.8 + 1.8 + + + + + org.apache.httpcomponents + httpclient + 4.5.8 + + + org.projectlombok + lombok + 1.18.22 + provided + + + io.ljunggren + json-utils + 0.0.1-SNAPSHOT + + + junit + junit + 4.13.2 + test + + + + diff --git a/src/main/java/io/ljunggren/geoapify/api/GeoapifyApi.java b/src/main/java/io/ljunggren/geoapify/api/GeoapifyApi.java new file mode 100644 index 0000000..0e4996f --- /dev/null +++ b/src/main/java/io/ljunggren/geoapify/api/GeoapifyApi.java @@ -0,0 +1,18 @@ +package io.ljunggren.geoapify.api; + +import io.ljunggren.geoapify.api.model.response.Response; +import io.ljunggren.geoapify.api.service.PostalCodeService; + +public class GeoapifyApi { + + private PostalCodeService postalCodeService; + + public GeoapifyApi(String apiKey) { + this.postalCodeService = new PostalCodeService(apiKey); + } + + public Response postalCodeLookup(String postalCode, String countryCode) throws Exception { + return postalCodeService.lookup(postalCode, countryCode); + } + +} diff --git a/src/main/java/io/ljunggren/geoapify/api/model/response/Feature.java b/src/main/java/io/ljunggren/geoapify/api/model/response/Feature.java new file mode 100644 index 0000000..085be25 --- /dev/null +++ b/src/main/java/io/ljunggren/geoapify/api/model/response/Feature.java @@ -0,0 +1,17 @@ +package io.ljunggren.geoapify.api.model.response; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import lombok.Data; + +@Data +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class Feature { + + private String type; + private Properties properties; + +} diff --git a/src/main/java/io/ljunggren/geoapify/api/model/response/Properties.java b/src/main/java/io/ljunggren/geoapify/api/model/response/Properties.java new file mode 100644 index 0000000..d3f8d6c --- /dev/null +++ b/src/main/java/io/ljunggren/geoapify/api/model/response/Properties.java @@ -0,0 +1,31 @@ +package io.ljunggren.geoapify.api.model.response; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import lombok.Data; + +@Data +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class Properties { + + @JsonProperty("postcode") + private String postalCode; + @JsonProperty("lat") + private Float latitude; + @JsonProperty("lon") + private Float longitude; + private String country; + @JsonProperty("country_code") + private String countryCode; + private String state; + @JsonProperty("state_code") + private String stateCode; + private String county; + private String city; + private Timezone timezone; + +} diff --git a/src/main/java/io/ljunggren/geoapify/api/model/response/Response.java b/src/main/java/io/ljunggren/geoapify/api/model/response/Response.java new file mode 100644 index 0000000..017ae2b --- /dev/null +++ b/src/main/java/io/ljunggren/geoapify/api/model/response/Response.java @@ -0,0 +1,19 @@ +package io.ljunggren.geoapify.api.model.response; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import lombok.Data; + +@Data +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class Response { + + private String type; + private List features; + +} diff --git a/src/main/java/io/ljunggren/geoapify/api/model/response/Timezone.java b/src/main/java/io/ljunggren/geoapify/api/model/response/Timezone.java new file mode 100644 index 0000000..2da4f74 --- /dev/null +++ b/src/main/java/io/ljunggren/geoapify/api/model/response/Timezone.java @@ -0,0 +1,25 @@ +package io.ljunggren.geoapify.api.model.response; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import lombok.Data; + +@Data +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class Timezone { + + private String name; + @JsonProperty("offset_STD") + private String offsetSTD; + @JsonProperty("offset_DST") + private String offsetDST; + @JsonProperty("abbreviation_STD") + private String abbreviatedSTD; + @JsonProperty("abbreviation_DST") + private String abbreviatedDST; + +} diff --git a/src/main/java/io/ljunggren/geoapify/api/service/BaseService.java b/src/main/java/io/ljunggren/geoapify/api/service/BaseService.java new file mode 100644 index 0000000..d57be3b --- /dev/null +++ b/src/main/java/io/ljunggren/geoapify/api/service/BaseService.java @@ -0,0 +1,53 @@ +package io.ljunggren.geoapify.api.service; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; + +import org.apache.http.HttpResponse; +import org.apache.http.client.config.CookieSpecs; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; + +import com.fasterxml.jackson.core.JsonProcessingException; + +import io.ljunggren.json.utils.JsonUtils; + +public class BaseService { + + protected String apiKey; + protected String domain = "https://api.geoapify.com"; + + protected BaseService(String apiKey) { + this.apiKey = apiKey; + } + + protected CloseableHttpClient getHttpClient() { + return HttpClients.custom() + .setDefaultRequestConfig(RequestConfig.custom() + .setCookieSpec(CookieSpecs.STANDARD).build()) + .build(); + } + + protected static String getResult(HttpResponse response) throws UnsupportedOperationException, IOException { + BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8)); + StringBuilder result = new StringBuilder(); + String line = new String(); + while ((line = rd.readLine()) != null) { + result.append(line); + } + return result.toString(); + } + + protected T parse(String json, Class clazz) throws Exception { + try { + return JsonUtils.jsonToObject(json, clazz); + } catch (JsonProcessingException e) { + throw new Exception("Unable to parse response"); + } + } + + +} diff --git a/src/main/java/io/ljunggren/geoapify/api/service/PostalCodeService.java b/src/main/java/io/ljunggren/geoapify/api/service/PostalCodeService.java new file mode 100644 index 0000000..266854a --- /dev/null +++ b/src/main/java/io/ljunggren/geoapify/api/service/PostalCodeService.java @@ -0,0 +1,51 @@ +package io.ljunggren.geoapify.api.service; + +import java.net.URI; +import java.util.Arrays; +import java.util.List; + +import org.apache.http.NameValuePair; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.message.BasicNameValuePair; + +import io.ljunggren.geoapify.api.model.response.Response; + +public class PostalCodeService extends BaseService { + + private final String URI = domain + "/v1/postcode/search"; + + public PostalCodeService(String apiKey) { + super(apiKey); + } + + public Response lookup(String postalCode, String countryCode) throws Exception { + return lookup(postalCode, countryCode, getHttpClient()); + } + + public Response lookup(String postalCode, String countryCode, CloseableHttpClient httpClient) throws Exception { + List parameters = generateParameters(postalCode, countryCode == null ? null : countryCode.toLowerCase()); + URI uri = new URIBuilder(URI) + .setParameters(parameters) + .build(); + HttpGet get = new HttpGet(uri.toString()); + try { + CloseableHttpResponse response = httpClient.execute(get); + String json = getResult(response); + return parse(json, Response.class); + } finally { + httpClient.close(); + } + } + + private List generateParameters(String postalCode, String countryCode) { + return Arrays.asList(new BasicNameValuePair[] { + new BasicNameValuePair("apiKey", apiKey), + new BasicNameValuePair("postcode", postalCode), + new BasicNameValuePair("countrycode", countryCode) + }); + } + +} diff --git a/src/test/java/io/ljunggren/geoapify/api/AppTest.java b/src/test/java/io/ljunggren/geoapify/api/AppTest.java new file mode 100644 index 0000000..20ac4fb --- /dev/null +++ b/src/test/java/io/ljunggren/geoapify/api/AppTest.java @@ -0,0 +1,38 @@ +package io.ljunggren.geoapify.api; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +}