#39 Added basic implementation
This commit is contained in:
@@ -1,5 +1,8 @@
|
|||||||
package de.rwu.easydrop;
|
package de.rwu.easydrop;
|
||||||
|
|
||||||
|
import de.rwu.easydrop.api.client.AmazonProductDataSource;
|
||||||
|
import de.rwu.easydrop.util.ConfigUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Kickoff point for the service.
|
* Kickoff point for the service.
|
||||||
*
|
*
|
||||||
@@ -19,6 +22,11 @@ public final class Main {
|
|||||||
* @param args
|
* @param args
|
||||||
*/
|
*/
|
||||||
public static void main(final String[] args) {
|
public static void main(final String[] args) {
|
||||||
System.out.println("I'm alive!");
|
String amznBaseUrl = ConfigUtil.getConfig("AMAZON_API_URL");
|
||||||
|
String amznApiKey = ConfigUtil.getConfig("AMAZON_API_KEY");
|
||||||
|
|
||||||
|
AmazonProductDataSource amznSrc = new AmazonProductDataSource(amznBaseUrl, amznApiKey);
|
||||||
|
System.out.print(amznSrc.getProductDTOById("B096Y2TYKV").toString());
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,121 @@
|
|||||||
|
package de.rwu.easydrop.api.client;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import de.rwu.easydrop.api.dto.ProductDTO;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface to an Amazon data source.
|
||||||
|
*
|
||||||
|
* @since 0.1.0
|
||||||
|
*/
|
||||||
|
public final class AmazonProductDataSource implements DataSource {
|
||||||
|
/**
|
||||||
|
* Name of this data source.
|
||||||
|
*/
|
||||||
|
private static final String DATA_ORIGIN = "Amazon";
|
||||||
|
/**
|
||||||
|
* Base URL to the Amazon data source.
|
||||||
|
*/
|
||||||
|
private String baseUrl;
|
||||||
|
/**
|
||||||
|
* Credential key to authorize acccess.
|
||||||
|
*/
|
||||||
|
private String apiKey;
|
||||||
|
/**
|
||||||
|
* Product region parameter required for data access.
|
||||||
|
*/
|
||||||
|
private static final String PRODUCT_REGION = "DE";
|
||||||
|
/**
|
||||||
|
* Locale parameter required for data access.
|
||||||
|
*/
|
||||||
|
private static final String LOCALE = "de_DE";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets up instance with Base URL and API Key.
|
||||||
|
*
|
||||||
|
* @param newBaseUrl
|
||||||
|
* @param newApiKey
|
||||||
|
*/
|
||||||
|
public AmazonProductDataSource(final String newBaseUrl, final String newApiKey) {
|
||||||
|
this.baseUrl = newBaseUrl;
|
||||||
|
this.apiKey = newApiKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ProductDTO getProductDTOById(final String productId) {
|
||||||
|
JSONObject offer = null;
|
||||||
|
ProductDTO product = new ProductDTO(productId, DATA_ORIGIN);
|
||||||
|
|
||||||
|
try {
|
||||||
|
URL apiUrl = new URL(
|
||||||
|
baseUrl
|
||||||
|
+ "/products/2020-08-26/products/"
|
||||||
|
+ productId
|
||||||
|
+ "/offers?productRegion="
|
||||||
|
+ PRODUCT_REGION
|
||||||
|
+ "&locale="
|
||||||
|
+ LOCALE);
|
||||||
|
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
|
||||||
|
connection.setRequestMethod("GET");
|
||||||
|
connection.setRequestProperty("Credential", apiKey);
|
||||||
|
|
||||||
|
int responseCode = connection.getResponseCode();
|
||||||
|
BufferedReader reader;
|
||||||
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||||
|
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||||
|
} else {
|
||||||
|
return product;
|
||||||
|
}
|
||||||
|
|
||||||
|
String line;
|
||||||
|
StringBuilder response = new StringBuilder();
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
response.append(line);
|
||||||
|
}
|
||||||
|
reader.close();
|
||||||
|
|
||||||
|
offer = new JSONObject(response.toString());
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (offer == null) {
|
||||||
|
return product;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
offer = offer.getJSONObject("featuredOffer");
|
||||||
|
|
||||||
|
product.setDataOrigin(DATA_ORIGIN);
|
||||||
|
product.setAvailable(offer
|
||||||
|
.getString("availability")
|
||||||
|
.equals("available"));
|
||||||
|
product.setCurrentPrice(offer
|
||||||
|
.getJSONObject("price")
|
||||||
|
.getJSONObject("value")
|
||||||
|
.getDouble("amount"));
|
||||||
|
product.setDeliveryPrice(offer
|
||||||
|
.getJSONArray("shippingOptions")
|
||||||
|
.getJSONObject(0)
|
||||||
|
.getJSONObject("shippingCost")
|
||||||
|
.getJSONObject("value")
|
||||||
|
.getDouble("amount"));
|
||||||
|
product.setMerchant(offer
|
||||||
|
.getJSONObject("merchant")
|
||||||
|
.getString("name"));
|
||||||
|
} catch (JSONException e) {
|
||||||
|
return product;
|
||||||
|
}
|
||||||
|
|
||||||
|
return product;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,12 +12,5 @@ public interface DataSource {
|
|||||||
* @param productId Identifier
|
* @param productId Identifier
|
||||||
* @return ProductDTO
|
* @return ProductDTO
|
||||||
*/
|
*/
|
||||||
ProductDTO getProductById(String productId);
|
ProductDTO getProductDTOById(String productId);
|
||||||
|
|
||||||
/**
|
|
||||||
* Breaks the connection after specified time.
|
|
||||||
*
|
|
||||||
* @param timeoutSeconds Seconds to time out after
|
|
||||||
*/
|
|
||||||
void setConnectionTimeout(int timeoutSeconds);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,146 @@
|
|||||||
package de.rwu.easydrop.api.dto;
|
package de.rwu.easydrop.api.dto;
|
||||||
|
|
||||||
|
import de.rwu.easydrop.util.FormattingUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Product data transfer object.
|
* Product data transfer object.
|
||||||
|
*
|
||||||
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
public class ProductDTO {
|
public class ProductDTO {
|
||||||
|
/**
|
||||||
|
* Data source platform, like "Amazon".
|
||||||
|
*/
|
||||||
|
private String dataOrigin;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the dataOrigin
|
||||||
|
*/
|
||||||
|
public String getDataOrigin() {
|
||||||
|
return dataOrigin;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param newDataOrigin the dataOrigin to set
|
||||||
|
*/
|
||||||
|
public void setDataOrigin(final String newDataOrigin) {
|
||||||
|
this.dataOrigin = newDataOrigin;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Platform internal product identifier.
|
||||||
|
*/
|
||||||
|
private String productId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the productId
|
||||||
|
*/
|
||||||
|
public String getProductId() {
|
||||||
|
return productId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param newProductId the productId to set
|
||||||
|
*/
|
||||||
|
public void setProductId(final String newProductId) {
|
||||||
|
this.productId = newProductId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Current product price per piece in Euro.
|
||||||
|
*/
|
||||||
|
private double currentPrice;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the currentPrice
|
||||||
|
*/
|
||||||
|
public double getCurrentPrice() {
|
||||||
|
return currentPrice;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param newCurrentPrice the currentPrice to set
|
||||||
|
*/
|
||||||
|
public void setCurrentPrice(final double newCurrentPrice) {
|
||||||
|
this.currentPrice = newCurrentPrice;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Name of mercant offering the product on the platform.
|
||||||
|
*/
|
||||||
|
private String merchant;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the merchant
|
||||||
|
*/
|
||||||
|
public String getMerchant() {
|
||||||
|
return merchant;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param newMerchant the merchant to set
|
||||||
|
*/
|
||||||
|
public void setMerchant(final String newMerchant) {
|
||||||
|
this.merchant = newMerchant;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Additional Cost for delivery in Euro.
|
||||||
|
*/
|
||||||
|
private double deliveryPrice;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the deliveryPrice
|
||||||
|
*/
|
||||||
|
public double getDeliveryPrice() {
|
||||||
|
return deliveryPrice;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param newDeliveryPrice the deliveryPrice to set
|
||||||
|
*/
|
||||||
|
public void setDeliveryPrice(final double newDeliveryPrice) {
|
||||||
|
this.deliveryPrice = newDeliveryPrice;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether the product can be purchased at this point.
|
||||||
|
*/
|
||||||
|
private boolean available;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the available
|
||||||
|
*/
|
||||||
|
public boolean isAvailable() {
|
||||||
|
return available;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param newAvailable the available to set
|
||||||
|
*/
|
||||||
|
public void setAvailable(final boolean newAvailable) {
|
||||||
|
this.available = newAvailable;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates ProductDTO instance.
|
||||||
|
*
|
||||||
|
* @param newProductId Interal Product indetifier
|
||||||
|
* @param newDataOrigin Data Origin
|
||||||
|
*/
|
||||||
|
public ProductDTO(final String newProductId, final String newDataOrigin) {
|
||||||
|
this.productId = newProductId;
|
||||||
|
this.dataOrigin = newDataOrigin;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final String toString() {
|
||||||
|
return "ProductDTO{"
|
||||||
|
+ productId + " from "
|
||||||
|
+ merchant + " ("
|
||||||
|
+ dataOrigin + ")"
|
||||||
|
+ " at "
|
||||||
|
+ FormattingUtil.formatEuro(currentPrice) + " (available: "
|
||||||
|
+ (available ? "yes" : "no") + ")}";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,39 @@
|
|||||||
package de.rwu.easydrop.util;
|
package de.rwu.easydrop.util;
|
||||||
|
|
||||||
public class FormattingUtil {
|
import java.text.NumberFormat;
|
||||||
|
import java.util.Currency;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helps format a bunch of things.
|
||||||
|
*
|
||||||
|
* @since 0.1.0
|
||||||
|
*/
|
||||||
|
public final class FormattingUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Private constructor to prevent unwanted instantiation.
|
||||||
|
*
|
||||||
|
* @throws UnsupportedOperationException always
|
||||||
|
*/
|
||||||
|
private FormattingUtil() throws UnsupportedOperationException {
|
||||||
|
throw new UnsupportedOperationException("This is a utility class, don't instantiate it.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formats a price to the German Euro format.
|
||||||
|
*
|
||||||
|
* @param amount price
|
||||||
|
* @return formatted price
|
||||||
|
*/
|
||||||
|
public static String formatEuro(final double amount) {
|
||||||
|
// Set up environment
|
||||||
|
Locale locale = Locale.GERMANY;
|
||||||
|
Currency currency = Currency.getInstance("EUR");
|
||||||
|
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
|
||||||
|
|
||||||
|
numberFormat.setCurrency(currency);
|
||||||
|
|
||||||
|
return numberFormat.format(amount);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user