#37 Added eBay data src, updated context + tests

This commit is contained in:
Marvin Scham
2023-06-06 02:11:50 +02:00
parent 39176b5dc0
commit af8993cb0f
12 changed files with 325 additions and 14 deletions

View File

@@ -8,6 +8,7 @@ import java.net.MalformedURLException;
import java.net.URL;
import de.rwu.easydrop.api.dto.ProductDTO;
import de.rwu.easydrop.exception.DataSourceException;
import de.rwu.easydrop.util.FormattingUtil;
/**
@@ -38,7 +39,7 @@ public abstract class AbstractDataSource implements DataSource {
* @param json Product data
* @return Finished ProductDTO
*/
public abstract ProductDTO buildProductDTO(ProductDTO product, String json);
protected abstract ProductDTO buildProductDTO(ProductDTO product, String json);
/**
* Overridable standard implementation.
@@ -64,7 +65,7 @@ public abstract class AbstractDataSource implements DataSource {
if (responseCode == HttpURLConnection.HTTP_OK) {
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
} else {
throw new IllegalArgumentException(
throw new DataSourceException(
"Nothing found: "
+ dataOrigin
+ " API responded with error code "
@@ -79,7 +80,7 @@ public abstract class AbstractDataSource implements DataSource {
buildProductDTO(product, response.toString());
} catch (IOException e) {
throw new IllegalArgumentException(
throw new DataSourceException(
"Couldn't fulfill "
+ dataOrigin
+ " API request");

View File

@@ -48,7 +48,7 @@ public final class AmazonProductDataSource extends AbstractDataSource {
}
@Override
public ProductDTO buildProductDTO(final ProductDTO product, final String json) {
protected ProductDTO buildProductDTO(final ProductDTO product, final String json) {
String root = "$.featuredOffer.";
ReadContext ctx = JsonPath.parse(json);
@@ -71,7 +71,7 @@ public final class AmazonProductDataSource extends AbstractDataSource {
* @param productId ASIN
*/
@Override
public URL createApiUrl(final String productId) throws MalformedURLException {
protected URL createApiUrl(final String productId) throws MalformedURLException {
return new URL(baseUrl
+ "/products/2020-08-26/products/"
+ productId

View File

@@ -0,0 +1,55 @@
package de.rwu.easydrop.api.client;
import javax.naming.ConfigurationException;
import de.rwu.easydrop.util.Config;
/**
* Factory for Data Sources.
*
* @since 0.2.0
*/
public class DataSourceFactory {
/**
* The data source config.
*/
private Config config;
/**
* @param newConfig the config to set
*/
public void setConfig(final Config newConfig) throws ConfigurationException {
this.config = newConfig;
this.config.loadConfig();
}
/**
* @param newConfig
*/
public DataSourceFactory(final Config newConfig) throws ConfigurationException {
this.setConfig(newConfig);
}
/**
* Creates an Amazon Product Data Source.
*
* @return AmazonProductDataSource
*/
public AmazonProductDataSource createAmazonProductDataSource() {
String apiUrl = config.getProperty("AMAZON_API_URL");
String apiKey = config.getProperty("AMAZON_API_KEY");
return new AmazonProductDataSource(apiUrl, apiKey);
}
/**
* Creates an eBay Item Data Source.
*
* @return EbayItemDataSource
*/
public EbayItemDataSource createEbayItemDataSource() {
String apiUrl = config.getProperty("EBAY_API_URL");
String apiKey = config.getProperty("EBAY_API_KEY");
return new EbayItemDataSource(apiUrl, apiKey);
}
}

View File

@@ -43,7 +43,7 @@ public final class EbayItemDataSource extends AbstractDataSource {
* @param searchQuery Exact product name or other valid identifier.
*/
@Override
public URL createApiUrl(final String searchQuery) throws MalformedURLException {
protected URL createApiUrl(final String searchQuery) throws MalformedURLException {
return new URL(baseUrl
+ "/buy/browse/v1/item_summary/search?q="
+ searchQuery
@@ -57,7 +57,7 @@ public final class EbayItemDataSource extends AbstractDataSource {
* @param json Product data
* @return Finished ProductDTO
*/
public ProductDTO buildProductDTO(final ProductDTO product, final String json) {
protected ProductDTO buildProductDTO(final ProductDTO product, final String json) {
String root = "$.itemSummaries[0].";
ReadContext ctx = JsonPath.parse(json);

View File

@@ -0,0 +1,27 @@
package de.rwu.easydrop.exception;
/**
* Exception that signifies the data of a Product are invalid.
*
* @since 0.2.0
*/
public class DataSourceException extends RuntimeException {
/**
* Throws an exception that signifies the data of a Product are invalid.
*
* @param message
*/
public DataSourceException(final String message) {
super(message);
}
/**
* Throws an exception that signifies the data of a Product are invalid.
*
* @param message
* @param cause
*/
public DataSourceException(final String message, final Throwable cause) {
super(message, cause);
}
}

View File

@@ -0,0 +1,27 @@
package de.rwu.easydrop.exception;
/**
* Exception that signifies the data of a Product are invalid.
*
* @since 0.2.0
*/
public class InvalidProductException extends RuntimeException {
/**
* Throws an exception that signifies the data of a Product are invalid.
*
* @param message
*/
public InvalidProductException(final String message) {
super(message);
}
/**
* Throws an exception that signifies the data of a Product are invalid.
*
* @param message
* @param cause
*/
public InvalidProductException(final String message, final Throwable cause) {
super(message, cause);
}
}

View File

@@ -0,0 +1,6 @@
/**
* Contains EasyDrop-related custom exceptions.
*
* @since 0.2.0
*/
package de.rwu.easydrop.exception;