Commit 7cd3cc68 by Patryk Czarnik

Waluty REST

parent 0c0e1871
package com.example.demo.waluty;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URL;
import java.time.LocalDate;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
@RestController
@RequestMapping("/waluty.rest")
public class WalutyRest {
@GetMapping
public TabelaWalut wyswietlWaluty() {
TabelaWalut tabela = pobierzTabele(null);
return tabela;
}
@GetMapping("/{data}")
public TabelaWalut wyswietlWaluty(@PathVariable String data) {
TabelaWalut tabela = pobierzTabele(data);
return tabela;
}
private TabelaWalut pobierzTabele(String data) {
String adres = "https://api.nbp.pl/api/exchangerates/tables/a";
if(data != null) {
adres += "/" + data;
}
adres += "?format=xml";
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = null;
URL url = new URL(adres);
try (InputStream in = url.openStream()) {
doc = db.parse(in);
}
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
// String numer = xpath.evaluate(
// "/ArrayOfExchangeRatesTable/ExchangeRatesTable/No", doc);
String nazwaTabeli = xpath.evaluate("//Table", doc);
String numerTabeli = xpath.evaluate("//No", doc);
LocalDate dataTabeli = LocalDate.parse(xpath.evaluate("//EffectiveDate", doc));
TabelaWalut tabela = new TabelaWalut(nazwaTabeli, numerTabeli, dataTabeli);
NodeList rates = (NodeList) xpath.evaluate("//Rate", doc, XPathConstants.NODESET);
final int n = rates.getLength();
for (int i = 0; i < n; i++) {
Node rate = rates.item(i);
String kod = xpath.evaluate("Code", rate);
String nazwa = xpath.evaluate("Currency", rate);
BigDecimal kurs = new BigDecimal(xpath.evaluate("Mid", rate));
Waluta waluta = new Waluta(kod, nazwa, kurs);
tabela.dodajWalute(waluta);
}
return tabela;
} catch (ParserConfigurationException | SAXException | XPathExpressionException | IOException e) {
e.printStackTrace();
return null;
}
}
}
......@@ -38,6 +38,8 @@
<ul>
<li><a th:href="@{/kalkulator}">Kalkulator</a></li>
<li><a th:href="@{/waluty}">Kursy walut</a></li>
<li><a th:href="@{/waluty.rest}">Waluty REST</a> - bieżące</li>
<li><a th:href="@{/waluty.rest/2008-08-08}">Waluty REST</a> - archiwalne</li>
</ul>
</body>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment