Abrufen der PRodukte der AmazonAPI

This commit is contained in:
Shan Ruhhammer
2023-06-18 18:26:17 +02:00
parent 36de6ce7bb
commit 82a0569e9d
7 changed files with 140 additions and 51 deletions

View File

@@ -1,5 +1,6 @@
package de.rwu.easydrop;
import java.net.MalformedURLException;
import java.util.List;
import javax.naming.ConfigurationException;
@@ -11,6 +12,7 @@ import org.sqlite.SQLiteDataSource;
import de.rwu.easydrop.api.client.DataSourceFactory;
import de.rwu.easydrop.data.connector.AbstractProductPersistence;
import de.rwu.easydrop.data.connector.SQLiteConnector;
import de.rwu.easydrop.model.Product;
import de.rwu.easydrop.model.ProductCatalogue;
import de.rwu.easydrop.service.retriever.CatalogueRetriever;
import de.rwu.easydrop.service.retriever.ProductRetriever;
@@ -58,5 +60,15 @@ public final class Main {
String pCatStr = pCat.toString();
LOGGER.info(pCatStr);
}
for (ProductCatalogue pCat : pCats) {
List<Product> products = pCat.getProducts();
for (Product product : products) {
String productId = product.getProductId();
double price = product.getCurrentPrice();
LOGGER.info("Product ID: " + productId + ", Price: " + price);
}
}
}
}

View File

@@ -0,0 +1,61 @@
package de.rwu.easydrop;
import java.net.MalformedURLException;
import java.util.List;
import javax.naming.ConfigurationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sqlite.SQLiteDataSource;
import de.rwu.easydrop.api.client.DataSourceFactory;
import de.rwu.easydrop.data.connector.AbstractProductPersistence;
import de.rwu.easydrop.data.connector.SQLiteConnector;
import de.rwu.easydrop.model.Product;
import de.rwu.easydrop.model.ProductCatalogue;
import de.rwu.easydrop.service.retriever.CatalogueRetriever;
import de.rwu.easydrop.service.retriever.ProductRetriever;
import de.rwu.easydrop.service.writer.CatalogueWriter;
import de.rwu.easydrop.util.Config;
import de.rwu.easydrop.util.ProductsConfig;
/**
* Diese Klasse PriceFetcher enthält die Methode getProductPrice,
* die den Preis eines Produkts von den jeweiligen API's abruft.
*/
public final class PriceFetcher {
private static final Logger LOGGER = LoggerFactory.getLogger(PriceFetcher.class);
public static void getProductPrice(final String[] args) throws ConfigurationException{
Config config = Config.getInstance();
ProductsConfig pConfig = ProductsConfig.getInstance();
DataSourceFactory dataSourceFactory = new DataSourceFactory(config);
ProductRetriever retriever = new ProductRetriever(dataSourceFactory);
CatalogueRetriever catRetriever = new CatalogueRetriever(pConfig, retriever);
AbstractProductPersistence db = new SQLiteConnector(new SQLiteDataSource());
CatalogueWriter catWriter = new CatalogueWriter(db);
catRetriever.loadCatalogues();
List<ProductCatalogue> pCats = catRetriever.getProductCatalogues();
catWriter.writeCatalogues(pCats);
for (ProductCatalogue pCat : pCats) {
String pCatStr = pCat.toString();
LOGGER.info(pCatStr);
}
for (ProductCatalogue pCat : pCats) {
List<Product> products = pCat.getProducts();
for (Product product : products) {
String productId = product.getProductId();
double price = product.getCurrentPrice();
LOGGER.info("Product ID: " + productId + ", Price: " + price);
}
}
}
}

View File

@@ -0,0 +1,5 @@
package de.rwu.easydrop.api;
public @interface BeforeEach {
}

View File

@@ -1,51 +0,0 @@
package de.rwu.easydrop.api.client;
import java.net.MalformedURLException;
import java.net.URL;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.ReadContext;
import de.rwu.easydrop.api.dto.ProductDTO;
/**
* Diese Klasse AmazonPriceFetcher enthält die Methode getProductPrice,
* die den Preis eines Produkts von der Amazon API abruft.
* Die Methode verwendet die Base URL, den API-Schlüssel und die Produkt-ID,
* um die API-URL zu erstellen und die Daten von der API abzurufen.
* Anschließend wird der Preis aus der JSON-Antwort extrahiert und zurückgegeben.
*/
public final class AmazonPriceFetcher {
/**
* URL zur Amazon API.
*/
private String baseUrl;
/**
* API-Schlüssel zur Autorisierung.
*/
private String apiKey;
/**
* Produktregion für den Datenzugriff.
*/
private static final String PRODUCT_REGION = "DE";
/**
* Sprachparameter für den Datenzugriff.
*/
private static final String LOCALE = "de_DE";
/**
* Initialisiert die Klasse mit der Base URL und dem API-Schlüssel.
*
* @param newBaseUrl Die Base URL zur Amazon API.
* @param newApiKey Der API-Schlüssel zur Autorisierung.
*/
public AmazonPriceFetcher(final String newBaseUrl, final String newApiKey) {
this.baseUrl = newBaseUrl;
this.apiKey = newApiKey;
}
}

View File

@@ -0,0 +1,5 @@
package de.rwu.easydrop.api;
public @interface Test {
}

View File

@@ -24,6 +24,7 @@ public class ProductCatalogue {
* Product collection.
*/
private List<Product> products;
public float getProducts;
/**
* Creates new Product Catalogue.

View File

@@ -0,0 +1,56 @@
package de.rwu.easydrop.api;
import java.net.MalformedURLException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class AmazonPriceFetcherTest {
private static final String BASE_URL = "https://api.amazon.com";
private static final String API_KEY = "your_api_key";
private static final String PRODUCT_ID = "12345";
private AmazonPriceFetcher priceFetcher;
@BeforeEach
public void setup() {
priceFetcher = new AmazonPriceFetcher(BASE_URL, API_KEY);
}
@Test
public void testGetProductPrice_ValidProductId_ReturnsPrice() throws MalformedURLException {
// Arrange
double expectedPrice = -1.0;
// Act
double actualPrice = priceFetcher.getProductPrice(PRODUCT_ID);
// Assert
Assertions.assertEquals(expectedPrice, actualPrice);
}
@Test
public void testGetProductPrice_InvalidProductId_ReturnsNegativePrice() throws MalformedURLException {
// Arrange
double expectedPrice = -1.0;
// Act
double actualPrice = priceFetcher.getProductPrice("invalid_product_id");
// Assert
Assertions.assertEquals(expectedPrice, actualPrice);
}
@Test
public void testGetProductPrice_ExceptionThrown_ReturnsNegativePrice() throws MalformedURLException {
// Arrange
double expectedPrice = -1.0;
// Act
double actualPrice = priceFetcher.getProductPrice("exception_thrown");
// Assert
Assertions.assertEquals(expectedPrice, actualPrice);
}
}