#39 Added basic implementation
This commit is contained in:
@@ -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
|
||||
* @return ProductDTO
|
||||
*/
|
||||
ProductDTO getProductById(String productId);
|
||||
|
||||
/**
|
||||
* Breaks the connection after specified time.
|
||||
*
|
||||
* @param timeoutSeconds Seconds to time out after
|
||||
*/
|
||||
void setConnectionTimeout(int timeoutSeconds);
|
||||
ProductDTO getProductDTOById(String productId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user