Skip to content

Commit

Permalink
Cast in the parseApiResult instead of the caller
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael DeCosta committed May 17, 2018
1 parent 25dc0e1 commit 5467c88
Showing 1 changed file with 10 additions and 6 deletions.
Expand Up @@ -133,22 +133,22 @@ public CreateDeploymentResult createDeploymentEvent(
@Override
public ExecutionResult getExecutionResults(String eventId) throws IOException, MablSystemError {
final String url = restApiBaseUrl + String.format(DEPLOYMENT_RESULT_ENDPOINT_TEMPLATE, eventId);
return (ExecutionResult) parseApiResult(httpClient.execute(buildGetRequest(url)), ExecutionResult.class);
return parseApiResult(httpClient.execute(buildGetRequest(url)), ExecutionResult.class);
}

public GetApiKeyResult getApiKeyResult(String formApiKey) throws IOException, MablSystemError {
final String url = restApiBaseUrl + String.format(GET_ORGANIZATION_ENDPOINT_TEMPLATE, formApiKey);
return (GetApiKeyResult) parseApiResult(httpClient.execute(buildGetRequest(url)), GetApiKeyResult.class);
return parseApiResult(httpClient.execute(buildGetRequest(url)), GetApiKeyResult.class);
}

public GetApplicationsResult getApplicationsResult(String organizationId) throws IOException, MablSystemError {
final String url = restApiBaseUrl + String.format(GET_APPLICATIONS_ENDPOINT_TEMPLATE, organizationId);
return (GetApplicationsResult) parseApiResult(httpClient.execute(buildGetRequest(url)), GetApplicationsResult.class);
return parseApiResult(httpClient.execute(buildGetRequest(url)), GetApplicationsResult.class);
}

public GetEnvironmentsResult getEnvironmentsResult(String organizationId) throws IOException, MablSystemError {
final String url = restApiBaseUrl + String.format(GET_ENVIRONMENTS_ENDPOINT_TEMPLATE, organizationId);
return (GetEnvironmentsResult) parseApiResult(httpClient.execute(buildGetRequest(url)), GetEnvironmentsResult.class);
return parseApiResult(httpClient.execute(buildGetRequest(url)), GetEnvironmentsResult.class);
}

private HttpGet buildGetRequest(String url) throws MablSystemError {
Expand All @@ -161,13 +161,17 @@ private HttpGet buildGetRequest(String url) throws MablSystemError {
}
}

private ApiResult parseApiResult(final HttpResponse response, Class resultClass) throws IOException, MablSystemError {
private <ApiResult> ApiResult parseApiResult(
final HttpResponse response,
Class<ApiResult> resultClass
) throws IOException, MablSystemError {

final int statusCode = response.getStatusLine().getStatusCode();

switch (statusCode) {
case SC_OK: // fall through case
case SC_CREATED:
return objectMapper.reader(resultClass).readValue(response.getEntity().getContent());
return resultClass.cast(objectMapper.reader(resultClass).readValue(response.getEntity().getContent()));
case SC_NOT_FOUND:
return null;
default:
Expand Down

0 comments on commit 5467c88

Please sign in to comment.