use of com.reprezen.kaizen.oasparser.model3.RequestBody in project msgraph-sdk-java-core by microsoftgraph.
the class BaseClientTests method getStringFromRequestBody.
private String getStringFromRequestBody(Request request) {
try {
try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
final BufferedSink buffer = Okio.buffer(Okio.sink(out));
final RequestBody body = request.body();
if (body != null)
body.writeTo(buffer);
try (final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())) {
return CoreHttpProvider.streamToString(in);
}
}
} catch (Exception ex) {
ex.printStackTrace();
return "";
}
}
use of com.reprezen.kaizen.oasparser.model3.RequestBody in project staf by simpleworks-gmbh.
the class HttpClient method buildRequestBody.
/**
* @brief method that builds the request body for an HttpRequest
* @param {@code HttpRequest} request
* @retrun {@code RequestBody} request body, of the respecting request, null if
* method does not support request bodies
*/
private static RequestBody buildRequestBody(final HttpRequest request) {
if (request == null) {
throw new IllegalArgumentException("request can't be null.");
}
if (!request.getMethod().hasRequestBody()) {
return null;
}
final RequestBody result;
final ContentTypeEnum contenttype = request.getContentType();
switch(contenttype) {
case JSON:
result = RequestBody.create(MediaType.parse(request.getContentType().getValue()), request.getBody());
break;
case FORM_URLENCODED:
final Builder formBodyBuilder = new FormBody.Builder();
for (FormParameter parameter : UtilsCollection.toList(request.getFormParameters())) {
formBodyBuilder.add(parameter.getName(), parameter.getValue());
}
result = formBodyBuilder.build();
break;
case MULTIPART_FORM_DATA:
final okhttp3.MultipartBody.Builder multipartBodyBuilder = new okhttp3.MultipartBody.Builder().setType(MultipartBody.FORM);
for (final de.simpleworks.staf.commons.api.MultipartFormDataParameter multiPart : request.getMultipartFormDataParameters()) {
final String name = multiPart.getName();
final String value = multiPart.getValue();
multipartBodyBuilder.addFormDataPart(name, value);
}
final MultipartFormFileParameter multipartFormFileParameter = request.getMultipartFormFileParameter();
if (multipartFormFileParameter != null) {
multipartBodyBuilder.addFormDataPart("file", multipartFormFileParameter.getName(), RequestBody.create(MediaType.parse(multipartFormFileParameter.getMimeType()), new File(multipartFormFileParameter.getFile())));
}
result = multipartBodyBuilder.build();
break;
default:
if (HttpClient.logger.isDebugEnabled()) {
HttpClient.logger.debug(String.format("Content Type '%s' is not implemented yet.", contenttype.getValue()));
HttpClient.logger.debug("The Content Type was not defined, will return an empty request body.");
}
result = RequestBody.create(null, Convert.EMPTY_STRING);
}
return result;
}
use of com.reprezen.kaizen.oasparser.model3.RequestBody in project msgraph-sdk-java-core by microsoftgraph.
the class RedirectHandlerTest method testGetRedirectForPostMethod.
@Test
public void testGetRedirectForPostMethod() {
RedirectHandler redirectHandler = new RedirectHandler();
RequestBody body = RequestBody.create("", MediaType.parse("application/json"));
Request httppost = new Request.Builder().url(testurl).post(body).build();
Response response = new Response.Builder().protocol(Protocol.HTTP_1_1).code(HttpURLConnection.HTTP_MOVED_TEMP).message("Moved Temporarily").addHeader("location", testmeurl).request(httppost).build();
try {
Request request = redirectHandler.getRedirect(httppost, response);
assertTrue(request != null);
final String method = request.method();
assertTrue(method.equalsIgnoreCase("POST"));
} catch (ProtocolException e) {
e.printStackTrace();
fail("Redirect handler testGetRedirectForPostMethod failure");
}
}
use of com.reprezen.kaizen.oasparser.model3.RequestBody in project java-sdk by smartcar.
the class Vehicle method stopCharge.
/**
* Send request to the /charge endpoint to stop charging a vehicle
*
* @return a response indicating success
* @throws SmartcarException if the request is unsuccessful
*/
public ActionResponse stopCharge() throws SmartcarException {
JsonObject json = Json.createObjectBuilder().add("action", "STOP").build();
RequestBody body = RequestBody.create(ApiClient.JSON, json.toString());
return this.call("charge", "POST", body, ActionResponse.class);
}
use of com.reprezen.kaizen.oasparser.model3.RequestBody in project java-sdk by smartcar.
the class Vehicle method batch.
/**
* Send request to the /batch endpoint
*
* @param paths the paths of endpoints to send requests to (ex. "/odometer", "/location", ...)
* @return the BatchResponse object containing the response from all the requested endpoints
* @throws SmartcarException if the request is unsuccessful
*/
public BatchResponse batch(String[] paths) throws SmartcarException {
JsonArrayBuilder endpoints = Json.createArrayBuilder();
for (String path : paths) {
endpoints.add(Json.createObjectBuilder().add("path", path).build());
}
JsonArray requests = endpoints.build();
JsonObject json = Json.createObjectBuilder().add("requests", requests).build();
ApiClient.gson.registerTypeAdapter(BatchResponse.class, new BatchDeserializer());
RequestBody body = RequestBody.create(ApiClient.JSON, json.toString());
BatchResponse response = this.call("batch", "POST", body, BatchResponse.class);
BatchResponse batchResponse = response;
batchResponse.setRequestId(response.getMeta().getRequestId());
return batchResponse;
}
Aggregations