Untitled
unknown
plain_text
8 months ago
5.8 kB
6
Indexable
ipvsresponse.java:
// Copyright 2019-2020, Charter Communications, All rights reserved.
package sth.ipvs.rest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import sth.http.ServiceResponse;
import sth.http.client.Request;
import sth.http.client.Response;
import sth.http.common.HttpStatus;
import sth.json.JSON;
import sth.xml.XML;
/**
* A wrapper around the Request/Response dialog between HTTP client and server. For specific
* use cases further methods could be added (for example to deserialize proprietary formats).
*/
public class IPVSResponse implements ServiceResponse {
private Request request;
private Response response;
protected Logger log = LoggerFactory.getLogger(getClass());
public IPVSResponse(Request request, Response response) {
this.request = request;
this.response = response;
}
/**
* Get the {@link Request} object that was executed to create the {@link Response}
*/
public Request request() {
return request;
}
/**
* Get the underlying {@link Response} object
*/
public Response response() {
return response;
}
/**
* True if the response status code is in the success range (200-299)
*/
public boolean isSuccess() {
return response.status().isSuccess();
}
/**
* Assert that the response status code was in the success range (200-299)
*/
public IPVSResponse assertSuccess() {
Assert.assertTrue(isSuccess(), "request to " + request.uri() + " failed:\n" + response);
return this;
}
/**
* True if the response status code is in the error range (>=400)
*/
public boolean isError() {
return response.status().isError();
}
/**
* Parse the response body to a JSON object
*/
public JSON json() {
return response.as(JSON.class);
}
/**
* Parse the response body to an XML object
*/
public XML xml() {
return response.as(XML.class);
}
/**
* Get the response body as a String
*/
public String responseString() {
return response.as(String.class);
}
/**
* Get HTTP status from response
*/
public HttpStatus status() {
return response.status();
}
/**
* Get the HTTP status code of the response
*/
public int statusCode() {
return response.status().code();
}
/**
* Get the request URI that was executed
*/
public String uri() {
return request.uri().toString();
}
/**
* Get a response header
*/
public String header(String headerName) {
return response.header(headerName);
}
@Override
public String toString() {
String contentType = response.header("content-type");
if (contentType.contains("json")) {
return json().toString(3);
} else if (contentType.contains("xml")) {
return xml().toString(3);
} else {
return responseString();
}
}
/**
* getError(JSON json, String errorField)
* If there are errors in a json body
* return the error msg portion of the body.
*
* @param json
* @param errorField name to check for errors.
* @return errorStr
*/
private String getError(JSON json, String errorField) {
String errorStr = "";
JSON errorMsgs = null;
log.debug("getError(): errorField={}, json={}", errorField, json);
if (json != null) {
if (json.hasField(errorField)) {
errorMsgs = json.get(errorField);
if (errorMsgs.size() > 0) {
errorStr = errorMsgs.toString();
log.debug("getError(): Found errorField={}, json={}", errorField, json);
}
}
}
return errorStr;
}
/**
* getErrorMsg(JSON json)
* Return the error or errors or errorMessages or ? msg portion of the body.
*
* @param json
* @return errorStr
*/
public String getErrorMsg(JSON json) {
String errorStr = "";
errorStr = getError(json, "error");
if (errorStr == "") {
errorStr = getError(json, "errors");
} else if (json.toString().contains("errorMessages")) {
errorStr = json.toString();
}
return errorStr;
}
}
systemhealthcontroller.java:
package sth.ipvs.restservice;
import sth.http.client.RequestExecutor;
import sth.http.client.RequestFactory;
import sth.login.model.AuthToken;
import sth.ipvs.rest.BaseRestController;
import javax.inject.Inject;
import java.util.Optional;
public class SystemHealthController extends BaseRestController {
private static String SERVICE_VERSION;
private final String VERSION_ID = "product_version";
private final String PATH_BASE = "";
private final String ENV_PROD = "PROD";
private static final String SYSTEM_HEALTH = "system_health";
@Override
public String baseUrl() {
return props.baseUrl() + PATH_BASE;
}
@Inject
public void postInitCustom(RequestExecutor executor) {
if (props.testEnvironment().equalsIgnoreCase(ENV_PROD)) {
log.info("No Auth for " + ENV_PROD + " environment");
request = RequestFactory.noAuth(baseUrl()).executor(executor)
.timeout(props.requestTimeout());
}
else {
request = RequestFactory.basic(baseUrl(), props.username(), props.password()).executor(executor)
.timeout(props.requestTimeout());
}
}
/**
* Url Format: system_health
* <p>
* Available Methods: GET
*/
public SystemHealthController systemHealth () {
request.reset();
request.path(SYSTEM_HEALTH);
return this;
}
}
Editor is loading...
Leave a Comment