From b048d12e95d1781eb67200b1d8d7a1a59d3aa07c Mon Sep 17 00:00:00 2001 From: Piotr Baranowski Date: Wed, 10 Jun 2020 12:59:15 +0200 Subject: [PATCH] =?UTF-8?q?dorobi=C4=87=20p=C4=99tl=C4=99=20pytaj=C4=85c?= =?UTF-8?q?=C4=85=20o=20waluty?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ZJJX-Projekt2.iml | 2 ++ pom.xml | 36 ++++++++++++++++++++ src/main/java/ApiException.java | 9 +++++ src/main/java/ConnectionFactory.java | 5 +++ src/main/java/EcbApi.java | 29 ++++++++++++++++ src/main/java/ExchangeApi.java | 5 +++ src/main/java/HttpConnection.java | 51 ++++++++++++++++++++++++++++ src/main/java/Main.java | 6 ++++ src/main/java/RatesProcessing.java | 35 +++++++++++++++++++ 9 files changed, 178 insertions(+) create mode 100644 ZJJX-Projekt2.iml create mode 100644 pom.xml create mode 100644 src/main/java/ApiException.java create mode 100644 src/main/java/ConnectionFactory.java create mode 100644 src/main/java/EcbApi.java create mode 100644 src/main/java/ExchangeApi.java create mode 100644 src/main/java/HttpConnection.java create mode 100644 src/main/java/Main.java create mode 100644 src/main/java/RatesProcessing.java diff --git a/ZJJX-Projekt2.iml b/ZJJX-Projekt2.iml new file mode 100644 index 0000000..4098198 --- /dev/null +++ b/ZJJX-Projekt2.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..eff7dc9 --- /dev/null +++ b/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + + ZJJX-Projekt2 + ZJJX-Projekt2 + 1.0-SNAPSHOT + + + javax.json + javax.json-api + 1.1.4 + + + org.glassfish + javax.json + 1.1 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + \ No newline at end of file diff --git a/src/main/java/ApiException.java b/src/main/java/ApiException.java new file mode 100644 index 0000000..5986469 --- /dev/null +++ b/src/main/java/ApiException.java @@ -0,0 +1,9 @@ +public class ApiException extends RuntimeException { + public ApiException(Throwable cause) { + super(cause); + } + + public ApiException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/main/java/ConnectionFactory.java b/src/main/java/ConnectionFactory.java new file mode 100644 index 0000000..e22e264 --- /dev/null +++ b/src/main/java/ConnectionFactory.java @@ -0,0 +1,5 @@ +public class ConnectionFactory { + public HttpConnection build(String url) { + return new HttpConnection(url); + } +} \ No newline at end of file diff --git a/src/main/java/EcbApi.java b/src/main/java/EcbApi.java new file mode 100644 index 0000000..6d6c339 --- /dev/null +++ b/src/main/java/EcbApi.java @@ -0,0 +1,29 @@ +import javax.json.Json; +import javax.json.JsonObject; +import java.math.BigDecimal; + +public class EcbApi implements ExchangeApi { + + private static final String API_URL_TEMPLATE = "https://api.exchangeratesapi.io/latest"; + + private final ConnectionFactory connectionFactory; + private final JsonObject json; + + public EcbApi() { + this(new ConnectionFactory(), Json.createObjectBuilder().build()); + } + + public EcbApi(ConnectionFactory connectionFactory, JsonObject json) { + this.connectionFactory = connectionFactory; + this.json = json; + } + + @Override + public BigDecimal exchangeRate(String currencyCode) { + try (HttpConnection connection = connectionFactory.build(API_URL_TEMPLATE)) { + String response = connection.response(); + RatesProcessing rates = new RatesProcessing(response, currencyCode); + return rates.getRate(currencyCode); + } + } +} \ No newline at end of file diff --git a/src/main/java/ExchangeApi.java b/src/main/java/ExchangeApi.java new file mode 100644 index 0000000..f9a8799 --- /dev/null +++ b/src/main/java/ExchangeApi.java @@ -0,0 +1,5 @@ +import java.math.BigDecimal; + +public interface ExchangeApi { + BigDecimal exchangeRate(String currencyCode); +} \ No newline at end of file diff --git a/src/main/java/HttpConnection.java b/src/main/java/HttpConnection.java new file mode 100644 index 0000000..79a3160 --- /dev/null +++ b/src/main/java/HttpConnection.java @@ -0,0 +1,51 @@ +import java.io.BufferedReader; +import java.io.Closeable; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + +public class HttpConnection implements Closeable { + + private final HttpURLConnection connection; + + HttpConnection(String url) { + try { + connection = (HttpURLConnection) new URL(url).openConnection(); + } catch (IOException e) { + throw new ApiException(e); + } + } + + String response() { + validateResponse(); + StringBuilder response = new StringBuilder(); + try (BufferedReader responseReader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { + String line; + while ((line = responseReader.readLine()) != null) { + response.append(line); + } + } catch (IOException e) { + throw new ApiException(e); + } + return response.toString(); + } + + @Override + public void close() { + connection.disconnect(); + } + + void validateResponse() { + try { + int responseCode = connection.getResponseCode(); + if (responseCode != 200) { + String responseMessage = connection.getResponseMessage(); + throw new ApiException(String.format("Something went wrong! [%d] %s", responseCode, responseMessage)); + } + } catch (IOException e) { + throw new ApiException(e); + } + } + +} \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000..0c770d7 --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,6 @@ +public class Main { + public static void main(String[] args) { + EcbApi getRates = new EcbApi(); + System.out.println(getRates.exchangeRate("PLN")); + } +} diff --git a/src/main/java/RatesProcessing.java b/src/main/java/RatesProcessing.java new file mode 100644 index 0000000..6f0b55d --- /dev/null +++ b/src/main/java/RatesProcessing.java @@ -0,0 +1,35 @@ +import javax.json.Json; +import javax.json.stream.JsonParser; +import java.io.StringReader; +import java.math.BigDecimal; +import java.util.HashMap; +import java.util.Map; + +public class RatesProcessing { + private Map rates; + + public RatesProcessing(String result, String currencyCode) { + this.rates = new HashMap<>(); + + final JsonParser parser = Json.createParser(new StringReader(result)); + String key = null; + BigDecimal value = null; + while (parser.hasNext()) { + final JsonParser.Event event = parser.next(); + switch (event) { + case KEY_NAME: + key = parser.getString(); + break; + case VALUE_NUMBER: + value = parser.getBigDecimal(); + break; + } + rates.put(key, value); + } + parser.close(); + } + + public BigDecimal getRate(String currencyCode) { + return rates.get(currencyCode); + } +}