use of com.reprezen.kaizen.oasparser.model3.MediaType in project android-library by nextcloud.
the class SetUserInfoRemoteOperation method run.
@Override
public RemoteOperationResult<Boolean> run(NextcloudClient client) {
RemoteOperationResult<Boolean> result;
PutMethod method = null;
try {
// request body
MediaType json = MediaType.parse("application/json; charset=utf-8");
RequestBody requestBody = RequestBody.create(json, "{\"key\": \"" + field.getFieldName() + "\"" + ", \"value\": \"" + value + "\"}");
// remote request
method = new PutMethod(client.getBaseUri() + OCS_ROUTE_PATH + client.getUserId(), true, requestBody);
int status = client.execute(method);
if (status == HttpStatus.SC_OK) {
result = new RemoteOperationResult<>(true, method);
} else {
result = new RemoteOperationResult<>(false, method);
String response = method.getResponseBodyAsString();
Log_OC.e(TAG, "Failed response while setting user information");
Log_OC.e(TAG, "*** status code: " + status + "; response: " + response);
}
} catch (Exception e) {
result = new RemoteOperationResult<>(e);
Log_OC.e(TAG, "Exception while setting OC user information", e);
} finally {
if (method != null)
method.releaseConnection();
}
return result;
}
use of com.reprezen.kaizen.oasparser.model3.MediaType in project cordova-plugin-background-upload by spoonconsulting.
the class UploadTask method createRequest.
/**
* Create the OkHttp request that will be used, already filled with input data.
*
* @return A ready to use OkHttp request
* @throws FileNotFoundException If the file to upload can't be found
*/
@NonNull
private Request createRequest() throws FileNotFoundException {
final String filepath = getInputData().getString(KEY_INPUT_FILEPATH);
assert filepath != null;
final String fileKey = getInputData().getString(KEY_INPUT_FILE_KEY);
assert fileKey != null;
// Build URL
HttpUrl url = Objects.requireNonNull(HttpUrl.parse(getInputData().getString(KEY_INPUT_URL))).newBuilder().build();
// Build file reader
String extension = MimeTypeMap.getFileExtensionFromUrl(filepath);
MediaType mediaType = MediaType.parse(MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension));
File file = new File(filepath);
ProgressRequestBody fileRequestBody = new ProgressRequestBody(mediaType, file.length(), new FileInputStream(file), this::handleProgress);
// Build body
final MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();
// With the parameters
final int parametersCount = getInputData().getInt(KEY_INPUT_PARAMETERS_COUNT, 0);
if (parametersCount > 0) {
final String[] parameterNames = getInputData().getStringArray(KEY_INPUT_PARAMETERS_NAMES);
assert parameterNames != null;
for (int i = 0; i < parametersCount; i++) {
final String key = parameterNames[i];
final Object value = getInputData().getKeyValueMap().get(KEY_INPUT_PARAMETER_VALUE_PREFIX + i);
bodyBuilder.addFormDataPart(key, value.toString());
}
}
bodyBuilder.addFormDataPart(fileKey, filepath, fileRequestBody);
// Start build request
String method = getInputData().getString(KEY_INPUT_HTTP_METHOD);
if (method == null) {
method = "POST";
}
Request.Builder requestBuilder = new Request.Builder().url(url).method(method.toUpperCase(), bodyBuilder.build());
// Write headers
final int headersCount = getInputData().getInt(KEY_INPUT_HEADERS_COUNT, 0);
final String[] headerNames = getInputData().getStringArray(KEY_INPUT_HEADERS_NAMES);
assert headerNames != null;
for (int i = 0; i < headersCount; i++) {
final String key = headerNames[i];
final Object value = getInputData().getKeyValueMap().get(KEY_INPUT_HEADER_VALUE_PREFIX + i);
requestBuilder.addHeader(key, value.toString());
}
// Ok
return requestBuilder.build();
}
use of com.reprezen.kaizen.oasparser.model3.MediaType in project OkHttpRestAssuredExamples by mfaisalkhatri.
the class TestPatchRequests method testPatchWithOkHttp.
/**
* Executing Put Request using OkHttp
*
* @since Mar 8, 2020
* @param id
* @param name
* @param job
* @throws IOException
*/
@Test(dataProvider = "patchData", groups = "PatchTests")
public void testPatchWithOkHttp(final int id, final String name, final String job) throws IOException {
final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
final PostData postData = new PostData(name, job);
final OkHttpClient client = new OkHttpClient();
final JSONObject json = new JSONObject(postData);
final RequestBody requestBody = RequestBody.create(json.toString(), JSON);
final Request request = new Request.Builder().url(URL + "/api/users" + id).addHeader("Content-Type", "application/json;charset=utf-8").patch(requestBody).build();
final Response response = client.newCall(request).execute();
final int statusCode = response.code();
this.log.info(statusCode);
final String responseBody = response.body().string();
this.log.info(responseBody);
final JSONObject jsonResponse = new JSONObject(responseBody);
assertEquals(statusCode, 200);
assertThat(jsonResponse.getString("name"), equalTo(name));
assertThat(jsonResponse.getString("job"), equalTo(job));
}
use of com.reprezen.kaizen.oasparser.model3.MediaType in project OkHttpRestAssuredExamples by mfaisalkhatri.
the class TestPostRequest method testPostWithOkHttp.
/**
* @since Mar 7, 2020
* @param name
* @param job
* @throws IOException
*/
@Test(dataProvider = "postData", groups = "PostTests")
public void testPostWithOkHttp(final String name, final String job) throws IOException {
final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
final PostData postData = new PostData(name, job);
final OkHttpClient client = new OkHttpClient();
final JSONObject json = new JSONObject(postData);
final RequestBody requestBody = RequestBody.create(json.toString(), JSON);
final Request request = new Request.Builder().url(URL + "/api/users").addHeader("Content-Type", "application/json;charset=utf-8").post(requestBody).build();
final Response response = client.newCall(request).execute();
final int statusCode = response.code();
this.log.info(statusCode);
final String responseBody = response.body().string();
this.log.info(responseBody);
final JSONObject jsonResponse = new JSONObject(responseBody);
assertEquals(statusCode, 201);
assertThat(jsonResponse.getString("name"), equalTo(name));
assertThat(jsonResponse.getString("job"), equalTo(job));
assertThat(jsonResponse.getString("id"), notNullValue());
}
use of com.reprezen.kaizen.oasparser.model3.MediaType in project OkHttpRestAssuredExamples by mfaisalkhatri.
the class TestPutRequests method testPutWithOkHttp.
/**
* Executing Put Request using OkHttp
*
* @since Mar 8, 2020
* @param id
* @param name
* @param job
* @throws IOException
*/
@Test(dataProvider = "putData", groups = "PutTests")
public void testPutWithOkHttp(final int id, final String name, final String job) throws IOException {
final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
final PostData postData = new PostData(name, job);
final OkHttpClient client = new OkHttpClient();
final JSONObject json = new JSONObject(postData);
final RequestBody requestBody = RequestBody.create(json.toString(), JSON);
final Request request = new Request.Builder().url(URL + "/api/users" + id).addHeader("Content-Type", "application/json;charset=utf-8").put(requestBody).build();
final Response response = client.newCall(request).execute();
final int statusCode = response.code();
this.log.info(statusCode);
final String responseBody = response.body().string();
this.log.info(responseBody);
final JSONObject jsonResponse = new JSONObject(responseBody);
assertEquals(statusCode, 200);
assertThat(jsonResponse.getString("name"), equalTo(name));
assertThat(jsonResponse.getString("job"), equalTo(job));
}
Aggregations