Skip to content

Commit

Permalink
add rest client
Browse files Browse the repository at this point in the history
  • Loading branch information
oven committed Jan 6, 2017
1 parent d06fd9c commit f5457fb
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 0 deletions.
46 changes: 46 additions & 0 deletions parking-rest-client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<project xmlns="http:https://maven.apache.org/POM/4.0.0" xmlns:xsi="http:https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http:https://maven.apache.org/POM/4.0.0 http:https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>no.bouvet</groupId>
<artifactId>parking-rest-client</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.7</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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() + "]");
}
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<modules>
<module>autoconfig-example</module>
<module>webapp</module>
<module>parking-rest-client</module>
</modules>

<properties>
Expand Down

0 comments on commit f5457fb

Please sign in to comment.