本文整理汇总了Java中com.mashape.unirest.request.HttpRequestWithBody.asJson方法的典型用法代码示例。如果您正苦于以下问题:Java HttpRequestWithBody.asJson方法的具体用法?Java HttpRequestWithBody.asJson怎么用?Java HttpRequestWithBody.asJson使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.mashape.unirest.request.HttpRequestWithBody
的用法示例。
在下文中一共展示了HttpRequestWithBody.asJson方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: notifyEvent
import com.mashape.unirest.request.HttpRequestWithBody; //导入方法依赖的package包/类
public EventResult notifyEvent(Incident incident) throws NotifyEventException {
try {
HttpRequestWithBody request = Unirest.post(eventApi)
.header("Accept", "application/json");
request.body(incident);
HttpResponse<JsonNode> jsonResponse = request.asJson();
log.debug(IOUtils.toString(jsonResponse.getRawBody()));
switch(jsonResponse.getStatus()) {
case HttpStatus.SC_OK:
case HttpStatus.SC_CREATED:
case HttpStatus.SC_ACCEPTED:
return EventResult.successEvent(JsonUtils.getPropertyValue(jsonResponse, "status"), JsonUtils.getPropertyValue(jsonResponse, "message"), JsonUtils.getPropertyValue(jsonResponse, "dedup_key"));
case HttpStatus.SC_BAD_REQUEST:
return EventResult.errorEvent(JsonUtils.getPropertyValue(jsonResponse, "status"), JsonUtils.getPropertyValue(jsonResponse, "message"), JsonUtils.getArrayValue(jsonResponse, "errors"));
default:
return EventResult.errorEvent(String.valueOf(jsonResponse.getStatus()), "", IOUtils.toString(jsonResponse.getRawBody()));
}
} catch (UnirestException | IOException e) {
throw new NotifyEventException(e);
}
}
示例2: executePostQuery
import com.mashape.unirest.request.HttpRequestWithBody; //导入方法依赖的package包/类
/**
* Test the {@link FreesoundClient#executeQuery(Query)} method, to ensure it correctly constructs and submits an
* HTTP POST request, and processes the results.
*
* @param mockUnirest Mock version of the {@link Unirest} library
* @param mockPostRequest Mock {@link HttpRequestWithBody} representing a POST call
* @param mockHttpResponse Mock {@link HttpResponse}
* @param mockResultsMapper Mock {@link SoundMapper}
*
* @throws Exception Any exceptions thrown in test
*/
@SuppressWarnings("static-access")
@Test
public void executePostQuery(
@Mocked final Unirest mockUnirest,
@Mocked final HttpRequestWithBody mockPostRequest,
@Mocked final HttpResponse<JsonNode> mockHttpResponse,
@Mocked final SoundMapper mockResultsMapper) throws Exception {
final Sound sound = new Sound();
new Expectations() {
{
mockUnirest.post(FreesoundClient.API_ENDPOINT + TEST_PATH); result = mockPostRequest;
mockPostRequest.header("Authorization", "Token " + CLIENT_SECRET);
mockPostRequest.routeParam(ROUTE_ELEMENT, ROUTE_ELEMENT_VALUE);
mockPostRequest.fields(with(new Delegate<HashMap<String, Object>>() {
@SuppressWarnings("unused")
void checkRequestParameters(final Map<String, Object> queryParameters) {
assertNotNull(queryParameters);
assertTrue(queryParameters.size() == 1);
assertEquals(QUERY_PARAMETER_VALUE, queryParameters.get(QUERY_PARAMETER));
}
}));
mockPostRequest.asJson(); result = mockHttpResponse;
mockHttpResponse.getStatus(); result = 200;
mockResultsMapper.map(mockHttpResponse.getBody().getObject()); result = sound;
}
};
final JSONResponseQuery<Sound> query = new TestJSONResponseQuery(HTTPRequestMethod.POST, mockResultsMapper);
final Response<Sound> response = freesoundClient.executeQuery(query);
assertSame(sound, response.getResults());
}
示例3: refreshAccessToken
import com.mashape.unirest.request.HttpRequestWithBody; //导入方法依赖的package包/类
/**
* Test that requests to renew an OAuth2 bearer token are correctly constructed and passed to the appropriate
* endpoint.
*
* @param mockUnirest Mock version of the {@link Unirest} library
* @param mockTokenRequest Mock {@link HttpRequestWithBody} representing a POST call to token endpoint
* @param mockHttpResponse Mock {@link HttpResponse}
*
* @throws Exception Any exceptions thrown in test
*/
@SuppressWarnings("static-access")
@Test
public void refreshAccessToken(
@Mocked final Unirest mockUnirest,
@Mocked final HttpRequestWithBody mockTokenRequest,
@Mocked final HttpResponse<JsonNode> mockHttpResponse) throws Exception {
new Expectations() {
{
mockUnirest.post(FreesoundClient.API_ENDPOINT + AccessTokenQuery.OAUTH_TOKEN_ENDPOINT_PATH);
result = mockTokenRequest;
mockTokenRequest.fields(with(new Delegate<HashMap<String, Object>>() {
@SuppressWarnings("unused")
void checkRequestParameters(final Map<String, Object> queryParameters) {
assertEquals(CLIENT_ID, queryParameters.get(AccessTokenQuery.CLIENT_ID_PARAMETER_NAME));
assertEquals(CLIENT_SECRET, queryParameters.get(AccessTokenQuery.CLIENT_SECRET_PARAMETER_NAME));
assertEquals(
RefreshOAuth2AccessTokenRequest.GRANT_TYPE,
queryParameters.get(AccessTokenQuery.GRANT_TYPE_PARAMETER_NAME));
assertEquals(
OAUTH_REFRESH_TOKEN,
queryParameters.get(RefreshOAuth2AccessTokenRequest.CODE_PARAMETER_NAME));
}
}));
mockTokenRequest.asJson(); result = mockHttpResponse;
mockHttpResponse.getStatus(); result = 200;
mockHttpResponse.getBody().getObject(); result = OAUTH_TOKEN_DETAILS_JSON;
}
};
final Response<AccessTokenDetails> response = freesoundClient.refreshAccessToken(OAUTH_REFRESH_TOKEN);
final AccessTokenDetails accessTokenDetails = response.getResults();
assertEquals(OAUTH_ACCESS_TOKEN, accessTokenDetails.getAccessToken());
assertEquals(OAUTH_REFRESH_TOKEN, accessTokenDetails.getRefreshToken());
assertEquals(OAUTH_SCOPE, accessTokenDetails.getScope());
assertEquals(OAUTH_TOKEN_EXPIRES_IN, accessTokenDetails.getExpiresIn());
}
示例4: requestAccessToken
import com.mashape.unirest.request.HttpRequestWithBody; //导入方法依赖的package包/类
/**
* Test that requests to redeem an authorisation code for an OAuth2 bearer token are correctly constructed and
* passed to the appropriate endpoint.
*
* @param mockUnirest Mock version of the {@link Unirest} library
* @param mockTokenRequest Mock {@link HttpRequestWithBody} representing a POST call to token endpoint
* @param mockHttpResponse Mock {@link HttpResponse}
*
* @throws Exception Any exceptions thrown in test
*/
@SuppressWarnings("static-access")
@Test
public void requestAccessToken(
@Mocked final Unirest mockUnirest,
@Mocked final HttpRequestWithBody mockTokenRequest,
@Mocked final HttpResponse<JsonNode> mockHttpResponse) throws Exception {
new Expectations() {
{
mockUnirest.post(FreesoundClient.API_ENDPOINT + AccessTokenQuery.OAUTH_TOKEN_ENDPOINT_PATH);
result = mockTokenRequest;
mockTokenRequest.fields(with(new Delegate<HashMap<String, Object>>() {
@SuppressWarnings("unused")
void checkRequestParameters(final Map<String, Object> queryParameters) {
assertEquals(CLIENT_ID, queryParameters.get(AccessTokenQuery.CLIENT_ID_PARAMETER_NAME));
assertEquals(CLIENT_SECRET, queryParameters.get(AccessTokenQuery.CLIENT_SECRET_PARAMETER_NAME));
assertEquals(
OAuth2AccessTokenRequest.GRANT_TYPE,
queryParameters.get(AccessTokenQuery.GRANT_TYPE_PARAMETER_NAME));
assertEquals(
OAUTH_AUTHORISATION_CODE,
queryParameters.get(OAuth2AccessTokenRequest.CODE_PARAMETER_NAME));
}
}));
mockTokenRequest.asJson(); result = mockHttpResponse;
mockHttpResponse.getStatus(); result = 200;
mockHttpResponse.getBody().getObject(); result = OAUTH_TOKEN_DETAILS_JSON;
}
};
final Response<AccessTokenDetails> response =
freesoundClient.redeemAuthorisationCodeForAccessToken(OAUTH_AUTHORISATION_CODE);
final AccessTokenDetails accessTokenDetails = response.getResults();
assertEquals(OAUTH_ACCESS_TOKEN, accessTokenDetails.getAccessToken());
assertEquals(OAUTH_REFRESH_TOKEN, accessTokenDetails.getRefreshToken());
assertEquals(OAUTH_SCOPE, accessTokenDetails.getScope());
assertEquals(OAUTH_TOKEN_EXPIRES_IN, accessTokenDetails.getExpiresIn());
}
示例5: makePostRequest
import com.mashape.unirest.request.HttpRequestWithBody; //导入方法依赖的package包/类
/**
* Method which uses OKHTTP to send a POST request to the specified URL saved
* within the APIMethod class
* @throws UnirestException
*/
@SuppressWarnings("unchecked")
private <T> T makePostRequest(ApiMethod method) throws RobinhoodApiException {
HttpRequestWithBody request = Unirest.post(method.getBaseUrl());
//Append each of the headers for the method
Iterator<HttpHeaderParameter> headerIterator = method.getHttpHeaderParameters().iterator();
while(headerIterator.hasNext()) {
HttpHeaderParameter currentHeader = headerIterator.next();
request.header(currentHeader.getKey(), currentHeader.getValue());
}
try {
//Append the request body
request.body(method.getUrlParametersAsPostBody());
//Make the request
HttpResponse<JsonNode> jsonResponse = request.asJson();
//Parse the response with Gson
Gson gson = new Gson();
String responseJsonString = jsonResponse.getBody().toString();
//If the response type for this is VOID (Meaning we are not expecting a response) do not
//try to use Gson
if(method.getReturnType() == Void.TYPE)
return (T) Void.TYPE;
T data = gson.fromJson(responseJsonString, method.getReturnType());
return data;
} catch (UnirestException ex) {
System.err.println("[RobinhoodApi] Failed to communicate with Robinhood servers, request failed");
}
throw new RobinhoodApiException();
}