Split monolithic function
This commit is contained in:
@@ -4,6 +4,7 @@ import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import org.json.JSONException;
|
||||
@@ -55,14 +56,7 @@ public final class AmazonProductDataSource implements DataSource {
|
||||
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);
|
||||
URL apiUrl = createApiUrl(productId);
|
||||
|
||||
HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
|
||||
connection.setRequestMethod("GET");
|
||||
@@ -74,7 +68,7 @@ public final class AmazonProductDataSource implements DataSource {
|
||||
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
} else {
|
||||
throw new IllegalArgumentException(
|
||||
"Amazon API responded with error code " + responseCode);
|
||||
"Nothing found: Amazon API responded with error code " + responseCode);
|
||||
}
|
||||
|
||||
String line;
|
||||
@@ -82,13 +76,25 @@ public final class AmazonProductDataSource implements DataSource {
|
||||
response.append(line);
|
||||
}
|
||||
reader.close();
|
||||
|
||||
JSONObject offer = new JSONObject(response.toString()).getJSONObject("featuredOffer");
|
||||
buildProductDTO(product, offer);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("Couldn't fulfill Amazon API request");
|
||||
}
|
||||
|
||||
try {
|
||||
JSONObject offer = new JSONObject(response.toString()).getJSONObject("featuredOffer");
|
||||
return product;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enriches a ProductDTO with API-gathered data.
|
||||
*
|
||||
* @param product Unfinished ProductDTO
|
||||
* @param offer Product data
|
||||
* @return Finished ProductDTO
|
||||
*/
|
||||
private ProductDTO buildProductDTO(final ProductDTO product, final JSONObject offer) {
|
||||
try {
|
||||
product.setDataOrigin(DATA_ORIGIN);
|
||||
product.setAvailable(offer
|
||||
.getString("availability")
|
||||
@@ -112,4 +118,15 @@ public final class AmazonProductDataSource implements DataSource {
|
||||
|
||||
return product;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URL createApiUrl(final String productId) throws MalformedURLException {
|
||||
return new URL(baseUrl
|
||||
+ "/products/2020-08-26/products/"
|
||||
+ productId
|
||||
+ "/offers?productRegion="
|
||||
+ PRODUCT_REGION
|
||||
+ "&locale="
|
||||
+ LOCALE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package de.rwu.easydrop.api.client;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import de.rwu.easydrop.api.dto.ProductDTO;
|
||||
|
||||
/**
|
||||
@@ -9,8 +12,17 @@ public interface DataSource {
|
||||
/**
|
||||
* Retrieves product info from the data source.
|
||||
*
|
||||
* @param productId Identifier
|
||||
* @param productId ASIN
|
||||
* @return ProductDTO
|
||||
*/
|
||||
ProductDTO getProductDTOById(String productId);
|
||||
|
||||
/**
|
||||
* Creates an URL object to connect to the API with.
|
||||
*
|
||||
* @param productId ASIN
|
||||
* @return URL object
|
||||
* @throws MalformedURLException
|
||||
*/
|
||||
URL createApiUrl(String productId) throws MalformedURLException;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user