#39 Rewrote JSON accessing
This commit is contained in:
@@ -7,8 +7,9 @@ import java.net.HttpURLConnection;
|
|||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
import org.json.JSONException;
|
import com.jayway.jsonpath.JsonPath;
|
||||||
import org.json.JSONObject;
|
import com.jayway.jsonpath.PathNotFoundException;
|
||||||
|
import com.jayway.jsonpath.ReadContext;
|
||||||
|
|
||||||
import de.rwu.easydrop.api.dto.ProductDTO;
|
import de.rwu.easydrop.api.dto.ProductDTO;
|
||||||
|
|
||||||
@@ -77,8 +78,7 @@ public final class AmazonProductDataSource implements DataSource {
|
|||||||
}
|
}
|
||||||
reader.close();
|
reader.close();
|
||||||
|
|
||||||
JSONObject offer = new JSONObject(response.toString()).getJSONObject("featuredOffer");
|
buildProductDTO(product, response.toString());
|
||||||
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");
|
||||||
}
|
}
|
||||||
@@ -90,29 +90,22 @@ public final class AmazonProductDataSource implements DataSource {
|
|||||||
* Enriches a ProductDTO with API-gathered data.
|
* Enriches a ProductDTO with API-gathered data.
|
||||||
*
|
*
|
||||||
* @param product Unfinished ProductDTO
|
* @param product Unfinished ProductDTO
|
||||||
* @param offer Product data
|
* @param json Product data
|
||||||
* @return Finished ProductDTO
|
* @return Finished ProductDTO
|
||||||
*/
|
*/
|
||||||
private ProductDTO buildProductDTO(final ProductDTO product, final JSONObject offer) {
|
public ProductDTO buildProductDTO(final ProductDTO product, final String json) {
|
||||||
|
String root = "$.featuredOffer.";
|
||||||
|
ReadContext ctx = JsonPath.parse(json);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
product.setDataOrigin(DATA_ORIGIN);
|
product.setDataOrigin(DATA_ORIGIN);
|
||||||
product.setAvailable(offer
|
product.setAvailable(
|
||||||
.getString("availability")
|
ctx.read(root + "availability", String.class).equals("available"));
|
||||||
.equals("available"));
|
product.setCurrentPrice(ctx.read(root + "price.value.amount", double.class));
|
||||||
product.setCurrentPrice(offer
|
product.setDeliveryPrice(
|
||||||
.getJSONObject("price")
|
ctx.read(root + "shippingOptions[0].shippingCost.value.amount", double.class));
|
||||||
.getJSONObject("value")
|
product.setMerchant(ctx.read(root + "merchant.name", String.class));
|
||||||
.getDouble("amount"));
|
} catch (PathNotFoundException e) {
|
||||||
product.setDeliveryPrice(offer
|
|
||||||
.getJSONArray("shippingOptions")
|
|
||||||
.getJSONObject(0)
|
|
||||||
.getJSONObject("shippingCost")
|
|
||||||
.getJSONObject("value")
|
|
||||||
.getDouble("amount"));
|
|
||||||
product.setMerchant(offer
|
|
||||||
.getJSONObject("merchant")
|
|
||||||
.getString("name"));
|
|
||||||
} catch (JSONException e) {
|
|
||||||
// Pass, allow incomplete ProductDTO to pass for later validation
|
// Pass, allow incomplete ProductDTO to pass for later validation
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user