-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
27 changes: 27 additions & 0 deletions
27
parking-rest-client/src/main/java/no/bouvet/parking/CarParkStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
parking-rest-client/src/main/java/no/bouvet/parking/ParkingClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
parking-rest-client/src/test/java/no/bouvet/parking/ParkingClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() + "]"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters