From f5457fb38f109f21d77916da44927b1a72effece Mon Sep 17 00:00:00 2001 From: Ove Gram Nipen Date: Fri, 6 Jan 2017 20:49:35 +0100 Subject: [PATCH] add rest client --- parking-rest-client/pom.xml | 46 +++++++++++++++++ .../java/no/bouvet/parking/CarParkStatus.java | 27 ++++++++++ .../java/no/bouvet/parking/ParkingClient.java | 49 +++++++++++++++++++ .../no/bouvet/parking/ParkingClientTest.java | 18 +++++++ pom.xml | 1 + 5 files changed, 141 insertions(+) create mode 100644 parking-rest-client/pom.xml create mode 100644 parking-rest-client/src/main/java/no/bouvet/parking/CarParkStatus.java create mode 100644 parking-rest-client/src/main/java/no/bouvet/parking/ParkingClient.java create mode 100644 parking-rest-client/src/test/java/no/bouvet/parking/ParkingClientTest.java diff --git a/parking-rest-client/pom.xml b/parking-rest-client/pom.xml new file mode 100644 index 0000000..4cd93f4 --- /dev/null +++ b/parking-rest-client/pom.xml @@ -0,0 +1,46 @@ + + 4.0.0 + + no.bouvet + parking-rest-client + 1.0-SNAPSHOT + jar + + + UTF-8 + + + + + org.apache.httpcomponents + httpclient + 4.5.2 + + + junit + junit + 4.12 + test + + + com.google.code.gson + gson + 2.7 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + + diff --git a/parking-rest-client/src/main/java/no/bouvet/parking/CarParkStatus.java b/parking-rest-client/src/main/java/no/bouvet/parking/CarParkStatus.java new file mode 100644 index 0000000..4562bb1 --- /dev/null +++ b/parking-rest-client/src/main/java/no/bouvet/parking/CarParkStatus.java @@ -0,0 +1,27 @@ +package no.bouvet.parking; + +import java.util.Date; + +public class CarParkStatus { + private final String name; + private final int vacantSpaces; + private final Date timestamp; + + public CarParkStatus(String name, int vacantSpaces, Date timestamp) { + this.name = name; + this.vacantSpaces = vacantSpaces; + this.timestamp = timestamp; + } + + public String getName() { + return name; + } + + public int getVacantSpaces() { + return vacantSpaces; + } + + public Date getTimestamp() { + return timestamp; + } +} diff --git a/parking-rest-client/src/main/java/no/bouvet/parking/ParkingClient.java b/parking-rest-client/src/main/java/no/bouvet/parking/ParkingClient.java new file mode 100644 index 0000000..589be7e --- /dev/null +++ b/parking-rest-client/src/main/java/no/bouvet/parking/ParkingClient.java @@ -0,0 +1,49 @@ +package no.bouvet.parking; + +import com.google.gson.Gson; +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; + +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URI; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; + +public class ParkingClient { + + private URI endpoint; + private Gson gson = new Gson(); + private final static DateFormat df = new SimpleDateFormat("HH:mm"); + + public CarParkStatus[] getData() throws IOException { + HttpGet get = new HttpGet(endpoint); + try ( + CloseableHttpClient httpClient = HttpClients.createDefault(); + CloseableHttpResponse response = httpClient.execute(get); + ) { + HttpEntity entity = response.getEntity(); + Parkeringshusstatus[] deserialized = gson.fromJson(new InputStreamReader(entity.getContent()), Parkeringshusstatus[].class); + CarParkStatus[] result = new CarParkStatus[deserialized.length]; + for (int i = 0; i < deserialized.length; i++) { + result[i] = new CarParkStatus(deserialized[i].navn, deserialized[i].ledigePlasser, df.parse(deserialized[i].oppdatert)); + } + return result; + } catch (ParseException e) { + throw new RuntimeException(e); + } + } + + public void setEndpoint(URI endpoint) { + this.endpoint = endpoint; + } + + static class Parkeringshusstatus { + public String navn, oppdatert; + public int ledigePlasser; + } +} diff --git a/parking-rest-client/src/test/java/no/bouvet/parking/ParkingClientTest.java b/parking-rest-client/src/test/java/no/bouvet/parking/ParkingClientTest.java new file mode 100644 index 0000000..63156db --- /dev/null +++ b/parking-rest-client/src/test/java/no/bouvet/parking/ParkingClientTest.java @@ -0,0 +1,18 @@ +package no.bouvet.parking; + +import org.junit.Test; + +import java.net.URI; + +public class ParkingClientTest { + + @Test + public void name() throws Exception { + ParkingClient client = new ParkingClient(); + client.setEndpoint(URI.create("https://www.bergen.kommune.no/wsproxy/parkering.json")); + CarParkStatus[] result = client.getData(); + for (CarParkStatus status : result) { + System.out.println(status.getName() + ": " + status.getVacantSpaces() + " [" + status.getTimestamp() + "]"); + } + } +} diff --git a/pom.xml b/pom.xml index ff62179..3706b4e 100644 --- a/pom.xml +++ b/pom.xml @@ -11,6 +11,7 @@ autoconfig-example webapp + parking-rest-client