Amazon and eBay Buy/Sell APIs

This commit is contained in:
Marvin Scham
2023-06-22 16:31:54 +00:00
parent a86ed4c5e0
commit 0be5a5ad40
21 changed files with 980 additions and 7 deletions

View File

@@ -42,7 +42,7 @@ public abstract class AbstractDataSource implements DataSource {
protected abstract ProductDTO buildProductDTO(ProductDTO product, String json);
/**
* Overridable standard implementation.
* Pulls product data from a remote data source.
*/
@Override
public ProductDTO getProductDTOById(final String productIdentifier)

View File

@@ -0,0 +1,81 @@
package de.rwu.easydrop.api.client;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import de.rwu.easydrop.api.dto.ProductDTO;
import de.rwu.easydrop.exception.DataWriterException;
import de.rwu.easydrop.util.FormattingUtil;
/**
* Writes data to a remote API.
*
* @since 0.3.0
*/
public abstract class AbstractDataWriter {
/**
* Returns the data source's API key.
*
* @return Data source API key
*/
protected abstract String getApiKey();
/**
* Returns the target API's name.
*
* @return Data target API name
*/
protected abstract String getDataTarget();
/**
* Creates an URL object to connect to the API with.
*
* @param productIdentifier Product identifier
* @return URL object
* @throws MalformedURLException
*/
protected abstract URL createApiUrl(String productIdentifier) throws MalformedURLException;
/**
* Sends a put request to the API.
*
* @param dto Product data transfer object
* @param apiType API Type, like Purchase or Sales
*/
protected void sendPutRequest(final ProductDTO dto, final String apiType) {
String apiKey = getApiKey();
String dataTarget = getDataTarget();
if (!dataTarget.equals(dto.getDataOrigin())) {
throw new DataWriterException(
"Product data source and target " + apiType + " API are incompatible.");
}
try {
String urlReadyIdentifier = FormattingUtil.urlEncode(dto.getProductId());
URL apiUrl = createApiUrl(urlReadyIdentifier);
HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
connection.setRequestMethod("PUT");
connection.setRequestProperty("Credential", apiKey);
int responseCode = connection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
throw new DataWriterException(
dataTarget
+ " "
+ apiType
+ " API responded with error code "
+ responseCode);
}
} catch (IOException e) {
throw new DataWriterException(
"Couldn't fulfill "
+ dataTarget
+ " API request");
}
}
}

View File

@@ -0,0 +1,19 @@
package de.rwu.easydrop.api.client;
import de.rwu.easydrop.api.dto.ProductDTO;
/**
* Abstract construct to provide access to a purchase API.
*
* @since 0.3.0
*/
public abstract class AbstractPurchaser extends AbstractDataWriter {
/**
* Sends a buy request to the Amazon API.
*
* @param dto
*/
public void purchaseProduct(final ProductDTO dto) {
sendPutRequest(dto, "Purchase");
}
}

View File

@@ -0,0 +1,19 @@
package de.rwu.easydrop.api.client;
import de.rwu.easydrop.api.dto.ProductDTO;
/**
* Abstract construct to provide access to a sales API.
*
* @since 0.3.0
*/
public abstract class AbstractSeller extends AbstractDataWriter {
/**
* Sends a sell request to the Amazon API.
*
* @param dto
*/
public void sellProduct(final ProductDTO dto) {
sendPutRequest(dto, "Sales");
}
}

View File

@@ -0,0 +1,67 @@
package de.rwu.easydrop.api.client;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Sends a buy request to the Amazon API.
*
* @since 0.3.0
*/
public final class AmazonPurchaser extends AbstractPurchaser {
/**
* Name of this data source.
*/
private static final String DATA_TARGET = "Amazon";
/**
* Base URL to the Amazon Purchase API.
*/
private String baseUrl;
/**
* Access credential.
*/
private String apiKey;
/**
* Product region parameter required for data writes.
*/
private static final String PRODUCT_REGION = "DE";
/**
* Locale parameter required for data writes.
*/
private static final String LOCALE = "de_DE";
/**
* Sets up instance with Base URL and API Key.
*
* @param newBaseUrl
* @param newApiKey
*/
public AmazonPurchaser(final String newBaseUrl, final String newApiKey) {
this.baseUrl = newBaseUrl;
this.apiKey = newApiKey;
}
/**
* @param productId ASIN
*/
@Override
protected URL createApiUrl(final String productId) throws MalformedURLException {
return new URL(baseUrl
+ "/products/2020-08-26/products/"
+ productId
+ "/buy?productRegion="
+ PRODUCT_REGION
+ "&locale="
+ LOCALE);
}
@Override
protected String getApiKey() {
return this.apiKey;
}
@Override
protected String getDataTarget() {
return DATA_TARGET;
}
}

View File

@@ -0,0 +1,67 @@
package de.rwu.easydrop.api.client;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Sends a sell request to the Amazon API.
*
* @since 0.3.0
*/
public final class AmazonSeller extends AbstractSeller {
/**
* Name of this data source.
*/
private static final String DATA_TARGET = "Amazon";
/**
* Base URL to the Amazon Purchase API.
*/
private String baseUrl;
/**
* Access credential.
*/
private String apiKey;
/**
* Product region parameter required for data writes.
*/
private static final String PRODUCT_REGION = "DE";
/**
* Locale parameter required for data writes.
*/
private static final String LOCALE = "de_DE";
/**
* Sets up instance with Base URL and API Key.
*
* @param newBaseUrl
* @param newApiKey
*/
public AmazonSeller(final String newBaseUrl, final String newApiKey) {
this.baseUrl = newBaseUrl;
this.apiKey = newApiKey;
}
/**
* @param productId ASIN
*/
@Override
protected URL createApiUrl(final String productId) throws MalformedURLException {
return new URL(baseUrl
+ "/products/2020-08-26/products/"
+ productId
+ "/sell?productRegion="
+ PRODUCT_REGION
+ "&locale="
+ LOCALE);
}
@Override
protected String getApiKey() {
return apiKey;
}
@Override
protected String getDataTarget() {
return DATA_TARGET;
}
}

View File

@@ -0,0 +1,52 @@
package de.rwu.easydrop.api.client;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Sends a buy request to the eBay API.
*
* @since 0.3.0
*/
public final class EbayPurchaser extends AbstractPurchaser {
/**
* Name of this data source.
*/
private static final String DATA_TARGET = "eBay";
/**
* Base URL to the eBay Purchase API.
*/
private String baseUrl;
/**
* Access credential.
*/
private String apiKey;
/**
* Sets up instance with Base URL and API Key.
*
* @param newBaseUrl
* @param newApiKey
*/
public EbayPurchaser(final String newBaseUrl, final String newApiKey) {
this.baseUrl = newBaseUrl;
this.apiKey = newApiKey;
}
@Override
protected URL createApiUrl(final String searchQuery) throws MalformedURLException {
return new URL(baseUrl
+ "/buy/"
+ searchQuery);
}
@Override
protected String getApiKey() {
return apiKey;
}
@Override
protected String getDataTarget() {
return DATA_TARGET;
}
}

View File

@@ -0,0 +1,52 @@
package de.rwu.easydrop.api.client;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Sends a sell request to the eBay API.
*
* @since 0.3.0
*/
public final class EbaySeller extends AbstractSeller {
/**
* Name of this data source.
*/
private static final String DATA_TARGET = "eBay";
/**
* Base URL to the eBay Purchase API.
*/
private String baseUrl;
/**
* Access credential.
*/
private String apiKey;
/**
* Sets up instance with Base URL and API Key.
*
* @param newBaseUrl
* @param newApiKey
*/
public EbaySeller(final String newBaseUrl, final String newApiKey) {
this.baseUrl = newBaseUrl;
this.apiKey = newApiKey;
}
@Override
protected URL createApiUrl(final String searchQuery) throws MalformedURLException {
return new URL(baseUrl
+ "/sell/"
+ searchQuery);
}
@Override
protected String getApiKey() {
return this.apiKey;
}
@Override
protected String getDataTarget() {
return DATA_TARGET;
}
}

View File

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