#37 Added eBay Data Source + Docs
This commit is contained in:
16
CHANGELOG.md
16
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
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
# Amazon Credentials
|
||||
AMAZON_API_URL=
|
||||
AMAZON_API_KEY=
|
||||
EBAY_API_URL=
|
||||
EBAY_API_KEY=
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user