#37 Added eBay Data Source + Docs

This commit is contained in:
Marvin Scham
2023-05-31 13:47:29 +02:00
parent 2f1bd4407c
commit 98d0ee1a8e
5 changed files with 121 additions and 6 deletions

View File

@@ -1,5 +1,21 @@
# Changelog # 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 ## 0.1.0
### Added ### Added

View File

@@ -1,3 +1,5 @@
# Amazon Credentials # Amazon Credentials
AMAZON_API_URL= AMAZON_API_URL=
AMAZON_API_KEY= AMAZON_API_KEY=
EBAY_API_URL=
EBAY_API_KEY=

View File

@@ -6,6 +6,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import de.rwu.easydrop.api.client.AmazonProductDataSource; import de.rwu.easydrop.api.client.AmazonProductDataSource;
import de.rwu.easydrop.api.client.EbayItemDataSource;
import de.rwu.easydrop.util.Config; import de.rwu.easydrop.util.Config;
/** /**
@@ -34,14 +35,22 @@ public final class Main {
public static void main(final String[] args) throws ConfigurationException { public static void main(final String[] args) throws ConfigurationException {
Config config = Config.getInstance(); Config config = Config.getInstance();
config.loadConfig(); config.loadConfig();
String amznBaseUrl = config.getProperty("AMAZON_API_URL"); String amznBaseUrl = config.getProperty("AMAZON_API_URL");
String amznApiKey = config.getProperty("AMAZON_API_KEY"); 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); AmazonProductDataSource amznSrc = new AmazonProductDataSource(amznBaseUrl, amznApiKey);
EbayItemDataSource ebaySrc = new EbayItemDataSource(ebayBaseUrl, ebayApiKey);
try { try {
testProduct = amznSrc.getProductDTOById("B096Y2TYKV").toString(); amznTestProduct = amznSrc.getProductDTOById("B096Y2TYKV").toString();
LOGGER.info(testProduct); LOGGER.info(amznTestProduct);
ebayTestProduct = ebaySrc.getProductDTOById("Gigabyte GeForce RTX 3060").toString();
LOGGER.info(ebayTestProduct);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
LOGGER.error("Something went wrong :(", e); LOGGER.error("Something went wrong :(", e);
} }

View File

@@ -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;
}
}

View File

@@ -13,7 +13,7 @@ import org.junit.jupiter.api.Test;
class ConfigImplTest { class ConfigImplTest {
private Config config; 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_KEY = "API_KEY";
private final static String TESTDATA_VAL = "keyIsHere"; private final static String TESTDATA_VAL = "keyIsHere";
@@ -74,7 +74,7 @@ class ConfigImplTest {
@Test @Test
void testLoadConfigSuccessfully() { void testLoadConfigSuccessfully() {
try { try {
config.setConfigLocation("testResources/testdata.properties"); config.setConfigLocation("src/test/resources/testdata.properties");
config.loadConfig(); config.loadConfig();
assertEquals(TESTDATA_VAL, config.getProperty(TESTDATA_KEY)); assertEquals(TESTDATA_VAL, config.getProperty(TESTDATA_KEY));
} catch (ConfigurationException e) { } catch (ConfigurationException e) {