Skip to content

Commit

Permalink
Merge pull request #27 from folio-org/improve-one
Browse files Browse the repository at this point in the history
Improved API calls - timeout setting and try/catch
  • Loading branch information
mis306lu authored Aug 25, 2020
2 parents ffdd67a + ba2e1db commit 05c505f
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 91 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/folio/ncip/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,9 @@ public class Constants {
public static final String ITEM_URL = "/inventory/items";
public static final String REQUEST_URL = "/circulation/requests";
public static final String ADDRESS_TYPES = "/addresstypes";


public static final String DEFAULT_TIMEOUT = "3000";
public static final String SERVICE_MGR_TIMEOUT = "service_manager_timeout_ms";

}
55 changes: 37 additions & 18 deletions src/main/java/org/folio/ncip/FolioNcipHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Properties;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
Expand Down Expand Up @@ -391,25 +392,43 @@ public String getConfigValue(String code, MultiMap okapiHeaders) {
public String callApiGet(String uriString, MultiMap okapiHeaders)
throws Exception {
CloseableHttpClient client = HttpClients.custom().build();
final String timeoutString = System.getProperty(Constants.SERVICE_MGR_TIMEOUT,Constants.DEFAULT_TIMEOUT);
int timeout = Integer.parseInt(timeoutString);
logger.info("Using timeout: " + timeout);
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(timeout)
.setSocketTimeout(timeout)
.build();
HttpUriRequest request = RequestBuilder.get().setUri(uriString)
.setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT))
.setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN) // do i need version here?
.setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL))
.setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN)).build();

HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
int responseCode = response.getStatusLine().getStatusCode();

logger.info("GET:");
logger.info(uriString);
logger.info(responseCode);
logger.info(responseString);

if (responseCode > 399) {
String responseBody = processErrorResponse(responseString);
throw new Exception(responseBody);
.setConfig(config)
.setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT))
.setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN) // do i need version here?
.setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL))
.setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN)).build();
String responseString = "";
try {
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
responseString = EntityUtils.toString(entity, "UTF-8");
int responseCode = response.getStatusLine().getStatusCode();

logger.info("GET:");
logger.info(uriString);
logger.info(responseCode);
logger.info(responseString);

if (responseCode > 399) {
String responseBody = processErrorResponse(responseString);
throw new Exception(responseBody);
}
}
catch(Exception e) {
logger.fatal("callApiGet failed");
logger.fatal(uriString);
throw e;
}
finally {
client.close();
}

return responseString;
Expand Down
171 changes: 126 additions & 45 deletions src/main/java/org/folio/ncip/FolioRemoteServiceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
Expand Down Expand Up @@ -101,16 +102,38 @@ long millisElapsedSince(long t0) {

public String callApiGet(String uriString) throws Exception, IOException, InterruptedException {
CloseableHttpClient client = HttpClients.custom().build();
final String timeoutString = System.getProperty(Constants.SERVICE_MGR_TIMEOUT,Constants.DEFAULT_TIMEOUT);
int timeout = Integer.parseInt(timeoutString);
logger.info("Using timeout: " + timeout);
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(timeout)
.setSocketTimeout(timeout)
.build();
HttpUriRequest request = RequestBuilder.get().setUri(uriString)
.setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT))
.setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN) // do i need version here?
.setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL))
.setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN)).build();

HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
int responseCode = response.getStatusLine().getStatusCode();
.setConfig(config)
.setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT))
.setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN) // do i need version here?
.setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL))
.setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN)).build();

HttpResponse response = null;
HttpEntity entity = null;
String responseString = null;
int responseCode = 0;
try {
response = client.execute(request);
entity = response.getEntity();
responseString = EntityUtils.toString(entity, "UTF-8");
responseCode = response.getStatusLine().getStatusCode();
}
catch(Exception e) {
logger.fatal("getApiGet failed");
logger.fatal(uriString);
throw e;
}
finally {
client.close();
}

logger.info("GET:");
logger.info(uriString);
Expand All @@ -119,7 +142,7 @@ public String callApiGet(String uriString) throws Exception, IOException, Interr

if (responseCode > 399) {
String responseBody = processErrorResponse(responseString);
throw new Exception(responseString);
throw new Exception(responseBody);
}

return responseString;
Expand All @@ -128,21 +151,46 @@ public String callApiGet(String uriString) throws Exception, IOException, Interr

public String callApiPost(String uriString, JsonObject body)
throws Exception, IOException, InterruptedException {
final String timeoutString = System.getProperty(Constants.SERVICE_MGR_TIMEOUT,Constants.DEFAULT_TIMEOUT);
int timeout = Integer.parseInt(timeoutString);
logger.info("Using timeout: " + timeout);
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(timeout)
.setSocketTimeout(timeout)
.build();
CloseableHttpClient client = HttpClients.custom().build();
HttpUriRequest request = RequestBuilder.post()
.setUri(uriString)
.setEntity(new StringEntity(body.toString()))
.setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT))
.setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN).setVersion(HttpVersion.HTTP_1_1)
.setHeader(Constants.CONTENT_TYPE_TEXT, Constants.CONTENT_JSON)
.setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL))
.setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN))
.build();

HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
int responseCode = response.getStatusLine().getStatusCode();
.setConfig(config)
.setUri(uriString)
.setEntity(new StringEntity(body.toString()))
.setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT))
.setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN).setVersion(HttpVersion.HTTP_1_1)
.setHeader(Constants.CONTENT_TYPE_TEXT, Constants.CONTENT_JSON)
.setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL))
.setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN))
.build();

HttpResponse response = null;
HttpEntity entity = null;
String responseString = null;
int responseCode = 0;
try {
response = client.execute(request);
entity = response.getEntity();
responseString = EntityUtils.toString(entity, "UTF-8");
responseCode = response.getStatusLine().getStatusCode();
}
catch(Exception e) {
String responseBody = e.getLocalizedMessage();
logger.fatal("callApiPost failed");
logger.fatal(uriString);
logger.fatal(body);
logger.fatal(e.getMessage());
throw e;
}
finally {
client.close();
}

logger.info("POST:");
logger.info(body.toString());
Expand All @@ -161,19 +209,42 @@ public String callApiPost(String uriString, JsonObject body)

public HttpResponse callApiDelete(String uriString) throws Exception, IOException, InterruptedException {

final String timeoutString = System.getProperty(Constants.SERVICE_MGR_TIMEOUT,Constants.DEFAULT_TIMEOUT);
int timeout = Integer.parseInt(timeoutString);
logger.info("Using timeout: " + timeout);
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(timeout)
.setSocketTimeout(timeout)
.build();
CloseableHttpClient client = HttpClients.custom().build();
HttpUriRequest request = RequestBuilder.delete() // set timeout?
.setUri(uriString)
.setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT))
.setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN) // do i need version here?
.setHeader(Constants.CONTENT_TYPE_TEXT, Constants.CONTENT_JSON)
.setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL))
.setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN))
.build();

HttpResponse response = client.execute(request);
int responseCode = response.getStatusLine().getStatusCode();

.setUri(uriString)
.setConfig(config)
.setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT))
.setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN) // do i need version here?
.setHeader(Constants.CONTENT_TYPE_TEXT, Constants.CONTENT_JSON)
.setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL))
.setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN))
.build();

HttpResponse response = null;
HttpEntity entity = null;
String responseString = null;
int responseCode = 0;
try {
response = client.execute(request);
responseCode = response.getStatusLine().getStatusCode();
}
catch(Exception e) {
logger.fatal("callApiDelete failed");
logger.fatal(uriString);
logger.fatal(e.getMessage());
throw e;
}
finally {
client.close();
}

logger.info("DELETE:");
logger.info(uriString);
logger.info(responseCode);
Expand Down Expand Up @@ -495,10 +566,10 @@ public JsonObject gatherPatronData(JsonObject user, String userId) throws Except
final long LONG_DELAY_MS = 10000;

List<String> apiCallsNeeded = Arrays.asList(
baseUrl + "/circulation/loans?query=(userId=" + userId + "+and+status=open)&limit=700",
baseUrl + "/accounts?query=(userId==" + userId + ")&limit=700", baseUrl + "/groups/" + groupId,
baseUrl + "/manualblocks?query=(userId=" + userId + ")&limit=100",
baseUrl + "/service-points-users?query=(userId==" + userId + ")&limit=700");
baseUrl + "/circulation/loans?query=(userId=" + userId + "+and+status=open)&limit=700",
baseUrl + "/accounts?query=(userId==" + userId + ")&limit=700", baseUrl + "/groups/" + groupId,
baseUrl + "/manualblocks?query=(userId=" + userId + ")&limit=100",
baseUrl + "/service-points-users?query=(userId==" + userId + ")&limit=700");

ExecutorService executor = Executors.newFixedThreadPool(6);
CompletionService<String> cs = new ExecutorCompletionService<>(executor);
Expand Down Expand Up @@ -571,6 +642,7 @@ private void initProperties(String requesterAgencyId, String baseUrl) throws Exc

logger.info("=======> initializing properties");
JSONParser parser = new JSONParser();
CloseableHttpClient client = HttpClients.custom().build();
try {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(Constants.INIT_PROP_FILE);
JSONObject obj = (JSONObject) parser.parse(new InputStreamReader(inputStream));
Expand All @@ -582,8 +654,6 @@ private void initProperties(String requesterAgencyId, String baseUrl) throws Exc
int responseCode = 0;
JsonObject jsonObject = null;

CloseableHttpClient client = HttpClients.custom().build();

while (i.hasNext()) {
JSONObject setting = (JSONObject) i.next();
String lookup = (String) setting.get("lookup");
Expand All @@ -607,12 +677,21 @@ private void initProperties(String requesterAgencyId, String baseUrl) throws Exc
//IS SURROUNDED BY QUOTES
if (lookupValue.contains("/")) lookupValue = '"' + lookupValue + '"';
url = url.replace("{lookup}", URLEncoder.encode(lookupValue));

final String timeoutString = System.getProperty(Constants.SERVICE_MGR_TIMEOUT,Constants.DEFAULT_TIMEOUT);
int timeout = Integer.parseInt(timeoutString);
logger.info("Using timeout: " + timeout);
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(timeout)
.setSocketTimeout(timeout)
.build();

HttpUriRequest request = RequestBuilder.get().setUri(baseUrl + url.trim())
.setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT))
.setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN) // do i need version here?
.setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL))
.setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN)).build();
.setConfig(config)
.setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT))
.setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN) // do i need version here?
.setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL))
.setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN)).build();

response = client.execute(request);
HttpEntity entity = response.getEntity();
Expand All @@ -632,7 +711,6 @@ private void initProperties(String requesterAgencyId, String baseUrl) throws Exc
jsonObject.getJsonArray(returnArray).getJsonObject(0).getString(identifier));

}
client.close();
// IF WE MADE IT THIS FAR, ALL OF THE PROPERTIES HAVE BEEN INITIALIZED
ncipProperties.setProperty(requesterAgencyId + Constants.INITIALIZED_PROPERTY, "true");

Expand All @@ -643,6 +721,9 @@ private void initProperties(String requesterAgencyId, String baseUrl) throws Exc
"Initializing NCIP properties failed. Are you sure you have NCIP properties set for this AgencyId: "
+ requesterAgencyId + ". DETAILS: " + e.getLocalizedMessage());
}
finally {
client.close();
}

}

Expand Down
Loading

0 comments on commit 05c505f

Please sign in to comment.