Split monolithic function

This commit is contained in:
Marvin Scham
2023-05-23 08:35:51 +02:00
parent 120f4a0ee4
commit 4674f4e826
2 changed files with 41 additions and 12 deletions

View File

@@ -4,6 +4,7 @@ import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import org.json.JSONException; import org.json.JSONException;
@@ -55,14 +56,7 @@ public final class AmazonProductDataSource implements DataSource {
ProductDTO product = new ProductDTO(productId, DATA_ORIGIN); ProductDTO product = new ProductDTO(productId, DATA_ORIGIN);
try { try {
URL apiUrl = new URL( URL apiUrl = createApiUrl(productId);
baseUrl
+ "/products/2020-08-26/products/"
+ productId
+ "/offers?productRegion="
+ PRODUCT_REGION
+ "&locale="
+ LOCALE);
HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection(); HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
connection.setRequestMethod("GET"); connection.setRequestMethod("GET");
@@ -74,7 +68,7 @@ public final class AmazonProductDataSource implements DataSource {
reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
} else { } else {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Amazon API responded with error code " + responseCode); "Nothing found: Amazon API responded with error code " + responseCode);
} }
String line; String line;
@@ -82,13 +76,25 @@ public final class AmazonProductDataSource implements DataSource {
response.append(line); response.append(line);
} }
reader.close(); reader.close();
JSONObject offer = new JSONObject(response.toString()).getJSONObject("featuredOffer");
buildProductDTO(product, offer);
} catch (IOException e) { } catch (IOException e) {
throw new IllegalArgumentException("Couldn't fulfill Amazon API request"); throw new IllegalArgumentException("Couldn't fulfill Amazon API request");
} }
try { return product;
JSONObject offer = new JSONObject(response.toString()).getJSONObject("featuredOffer"); }
/**
* 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.setDataOrigin(DATA_ORIGIN);
product.setAvailable(offer product.setAvailable(offer
.getString("availability") .getString("availability")
@@ -112,4 +118,15 @@ public final class AmazonProductDataSource implements DataSource {
return product; 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);
}
} }

View File

@@ -1,5 +1,8 @@
package de.rwu.easydrop.api.client; package de.rwu.easydrop.api.client;
import java.net.MalformedURLException;
import java.net.URL;
import de.rwu.easydrop.api.dto.ProductDTO; import de.rwu.easydrop.api.dto.ProductDTO;
/** /**
@@ -9,8 +12,17 @@ public interface DataSource {
/** /**
* Retrieves product info from the data source. * Retrieves product info from the data source.
* *
* @param productId Identifier * @param productId ASIN
* @return ProductDTO * @return ProductDTO
*/ */
ProductDTO getProductDTOById(String productId); 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;
} }