diff --git a/CHANGELOG.md b/CHANGELOG.md index 010c0fd..bb12c81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,21 @@ # Changelog +## 0.2.0-SNAPSHOT + +### Added + +- EbayItemDataSource (#37) + +### Changed + +- AbstractDataSource between DataSource interface and implementations to minimize code duplication + - Affects AmazonProductDataSource and related tests (~ #39) +- Testing resources moved to more intuitive location + +### Fixed + +- Logback configuration is now linked correctly + ## 0.1.0 ### Added diff --git a/config/demo.config.properties b/config/demo.config.properties index fbb6e44..d3e4d63 100644 --- a/config/demo.config.properties +++ b/config/demo.config.properties @@ -1,3 +1,5 @@ # Amazon Credentials AMAZON_API_URL= -AMAZON_API_KEY= \ No newline at end of file +AMAZON_API_KEY= +EBAY_API_URL= +EBAY_API_KEY= \ No newline at end of file diff --git a/src/main/java/de/rwu/easydrop/Main.java b/src/main/java/de/rwu/easydrop/Main.java index af1aeea..e776989 100644 --- a/src/main/java/de/rwu/easydrop/Main.java +++ b/src/main/java/de/rwu/easydrop/Main.java @@ -6,6 +6,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import de.rwu.easydrop.api.client.AmazonProductDataSource; +import de.rwu.easydrop.api.client.EbayItemDataSource; import de.rwu.easydrop.util.Config; /** @@ -34,14 +35,22 @@ public final class Main { public static void main(final String[] args) throws ConfigurationException { Config config = Config.getInstance(); config.loadConfig(); + String amznBaseUrl = config.getProperty("AMAZON_API_URL"); String amznApiKey = config.getProperty("AMAZON_API_KEY"); - String testProduct = null; + String ebayBaseUrl = config.getProperty("EBAY_API_URL"); + String ebayApiKey = config.getProperty("EBAY_API_KEY"); + + String amznTestProduct = null; + String ebayTestProduct = null; AmazonProductDataSource amznSrc = new AmazonProductDataSource(amznBaseUrl, amznApiKey); + EbayItemDataSource ebaySrc = new EbayItemDataSource(ebayBaseUrl, ebayApiKey); try { - testProduct = amznSrc.getProductDTOById("B096Y2TYKV").toString(); - LOGGER.info(testProduct); + amznTestProduct = amznSrc.getProductDTOById("B096Y2TYKV").toString(); + LOGGER.info(amznTestProduct); + ebayTestProduct = ebaySrc.getProductDTOById("Gigabyte GeForce RTX 3060").toString(); + LOGGER.info(ebayTestProduct); } catch (IllegalArgumentException e) { LOGGER.error("Something went wrong :(", e); } diff --git a/src/main/java/de/rwu/easydrop/api/client/EbayItemDataSource.java b/src/main/java/de/rwu/easydrop/api/client/EbayItemDataSource.java new file mode 100644 index 0000000..ee998dc --- /dev/null +++ b/src/main/java/de/rwu/easydrop/api/client/EbayItemDataSource.java @@ -0,0 +1,88 @@ +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; + +/** + * Interface to an eBay data source. + * + * @since 0.2.0 + */ +public final class EbayItemDataSource extends AbstractDataSource { + /** + * Name of this data source. + */ + private static final String DATA_ORIGIN = "eBay"; + /** + * Base URL to the eBay data source. + */ + private String baseUrl; + /** + * Credential key to authorize access. + */ + private String apiKey; + + /** + * Sets up instance with Base URL and API Key. + * + * @param newBaseUrl + * @param newApiKey + */ + public EbayItemDataSource(final String newBaseUrl, final String newApiKey) { + this.baseUrl = newBaseUrl; + this.apiKey = newApiKey; + } + + /** + * @param searchQuery Exact product name or other valid identifier. + */ + @Override + public URL createApiUrl(final String searchQuery) throws MalformedURLException { + return new URL(baseUrl + + "/buy/browse/v1/item_summary/search?q=" + + searchQuery + + "&limit=1&offset=0"); + } + + /** + * Enriches a ProductDTO with API-gathered data. + * + * @param product Unfinished ProductDTO + * @param json Product data + * @return Finished ProductDTO + */ + public ProductDTO buildProductDTO(final ProductDTO product, final String json) { + String root = "$.itemSummaries[0]."; + ReadContext ctx = JsonPath.parse(json); + + try { + product.setDataOrigin(DATA_ORIGIN); + product.setAvailable( + ctx.read(root + "shippingOptions[0].guaranteedDelivery", boolean.class)); + product.setCurrentPrice(ctx.read(root + "price.value", double.class)); + product.setDeliveryPrice( + ctx.read(root + "shippingOptions[0].shippingCost.value", double.class)); + product.setMerchant(ctx.read(root + "seller.username", String.class)); + } catch (PathNotFoundException e) { + // Pass, allow incomplete ProductDTO to pass for later validation + } + + return product; + } + + @Override + protected String getDataOrigin() { + return DATA_ORIGIN; + } + + @Override + protected String getApiKey() { + return this.apiKey; + } +} diff --git a/src/test/java/de/rwu/easydrop/util/ConfigImplTest.java b/src/test/java/de/rwu/easydrop/util/ConfigImplTest.java index db7de79..0c5041e 100644 --- a/src/test/java/de/rwu/easydrop/util/ConfigImplTest.java +++ b/src/test/java/de/rwu/easydrop/util/ConfigImplTest.java @@ -13,7 +13,7 @@ import org.junit.jupiter.api.Test; class ConfigImplTest { private Config config; - private final static String TESTDATA_PATH = "testResources/testdata.properties"; + private final static String TESTDATA_PATH = "src/test/resources/testdata.properties"; private final static String TESTDATA_KEY = "API_KEY"; private final static String TESTDATA_VAL = "keyIsHere"; @@ -74,7 +74,7 @@ class ConfigImplTest { @Test void testLoadConfigSuccessfully() { try { - config.setConfigLocation("testResources/testdata.properties"); + config.setConfigLocation("src/test/resources/testdata.properties"); config.loadConfig(); assertEquals(TESTDATA_VAL, config.getProperty(TESTDATA_KEY)); } catch (ConfigurationException e) {