Added a discover adapter and changed the domain object a bit

This commit is contained in:
reynisson 2021-12-05 22:41:52 +01:00
parent 0b89e88905
commit ec26b84dc9
3 changed files with 91 additions and 4 deletions

View File

@ -0,0 +1,74 @@
package ch.unisg.tapas.auctionhouse.adapter.out.web;
import ch.unisg.tapas.auctionhouse.application.port.out.AuctionHouseDiscoveryPort;
import ch.unisg.tapas.auctionhouse.domain.AuctionHouseDiscoveryInformation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class AuctionHouseDiscoveryHttpAdapter implements AuctionHouseDiscoveryPort {
private static final Logger LOGGER = LogManager.getLogger(AuctionHouseDiscoveryHttpAdapter.class);
public List<AuctionHouseDiscoveryInformation> fetchAuctionHouseInformation(URI auctionHouseURI){
try{
var client = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder()
.uri(auctionHouseURI)
.GET()
.build();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
LOGGER.info(response.body());
var responseBody = new JSONObject(response.body());
var arrayOfInformation = responseBody.getJSONArray("auctionHouseInfo");
var returnList = new LinkedList<AuctionHouseDiscoveryInformation>();
for(int i = 0; i < arrayOfInformation.length(); i++)
{
var informationJSON = arrayOfInformation.getJSONObject(i);
var information = new AuctionHouseDiscoveryInformation(
new AuctionHouseDiscoveryInformation.AuctionHouseUri(URI.create(informationJSON.getString("auctionHouseURI"))),
new AuctionHouseDiscoveryInformation.WebSubUri(URI.create(informationJSON.getString("webSubURI"))),
new AuctionHouseDiscoveryInformation.TaskTypes(getTaskTypes(informationJSON.getJSONArray("taskTypes"))),
new AuctionHouseDiscoveryInformation.TimeStamp(Timestamp.valueOf(informationJSON.getString("timeStamp"))),
new AuctionHouseDiscoveryInformation.GroupName(informationJSON.getString("groupName"))
);
returnList.add(information);
}
return returnList;
} catch (IOException e) {
e.printStackTrace();
return Collections.<AuctionHouseDiscoveryInformation>emptyList();
} catch (InterruptedException e) {
e.printStackTrace();
return Collections.<AuctionHouseDiscoveryInformation>emptyList();
}
}
private List<String> getTaskTypes(JSONArray arrayOfTypes){
var listOfTypes = new LinkedList<String>();
for(int i = 0; i < arrayOfTypes.length(); i++)
{
listOfTypes.add(arrayOfTypes.getString(i));
}
return listOfTypes;
}
}

View File

@ -0,0 +1,10 @@
package ch.unisg.tapas.auctionhouse.application.port.out;
import ch.unisg.tapas.auctionhouse.domain.AuctionHouseDiscoveryInformation;
import java.net.URI;
import java.util.List;
public interface AuctionHouseDiscoveryPort {
List<AuctionHouseDiscoveryInformation> fetchAuctionHouseInformation(URI auctionHouseURI);
}

View File

@ -3,7 +3,10 @@ package ch.unisg.tapas.auctionhouse.domain;
import lombok.Getter;
import lombok.Value;
import java.net.URI;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
//TODO: change to array type
public class AuctionHouseDiscoveryInformation {
@ -37,22 +40,22 @@ public class AuctionHouseDiscoveryInformation {
@Value
public static class AuctionHouseUri {
private String value;
private URI value;
}
@Value
public static class WebSubUri {
private String value;
private URI value;
}
@Value
public static class TaskTypes {
private String value;
private List<String> value;
}
@Value
public static class TimeStamp {
private String value;
private Timestamp value;
}
@Value