List of usage examples for com.squareup.okhttp RequestBody create
public static RequestBody create(final MediaType contentType, final File file)
From source file:com.onaio.steps.helper.UploadFileTask.java
License:Apache License
@Override protected Boolean doInBackground(File... files) { if (!TextUtils.isEmpty(KeyValueStoreFactory.instance(activity).getString(ENDPOINT_URL))) { try {// w ww .j a v a 2s.c om OkHttpClient client = new OkHttpClient(); final MediaType MEDIA_TYPE = MediaType.parse("text/csv"); RequestBody requestBody = new MultipartBuilder().type(MultipartBuilder.FORM) .addFormDataPart("file", files[0].getName(), RequestBody.create(MEDIA_TYPE, files[0])) .build(); Request request = new Request.Builder() .url(KeyValueStoreFactory.instance(activity).getString(ENDPOINT_URL)).post(requestBody) .build(); client.newCall(request).execute(); new CustomNotification().notify(activity, R.string.export_complete, R.string.export_complete_message); return true; } catch (IOException e) { new Logger().log(e, "Export failed."); new CustomNotification().notify(activity, R.string.error_title, R.string.export_failed); } } else { new CustomNotification().notify(activity, R.string.error_title, R.string.export_failed); } return false; }
From source file:com.oneops.crawler.OneOpsFacade.java
License:Apache License
public int forceDeploy(Environment env, Platform platform, String userName) throws IOException, OneOpsException { Map<String, Platform> platformMap = env.getPlatforms(); if (platformMap == null || platformMap.size() == 0) { return 400; }//from ww w . ja v a2 s . c om StringBuilder excludePlatforms = new StringBuilder(); if (platform != null) { for (String platformName : platformMap.keySet()) { if (!platformName.equals(platform.getName())) { if (excludePlatforms.length() > 0) excludePlatforms.append(","); excludePlatforms.append(platformMap.get(platformName).getId()); } } } HashMap<String, String> params = new HashMap<>(); params.put("exclude", excludePlatforms.toString()); log.info("deploying environment id: " + env.getId()); RequestBody body = RequestBody.create(JSON, gson.toJson(params)); String url = transistorBaseUrl + "/transistor/rest/environments/" + env.getId() + "/deployments/deploy"; Request request = new Request.Builder().url(url).addHeader("X-Cms-User", userName) .addHeader("Content-Type", "application/json").post(body).build(); Response response = client.newCall(request).execute(); String responseBody = response.body().string(); int responseCode = response.code(); log.info("OO response body: " + responseBody + ", code: " + responseCode); if (responseCode >= 300) { throw new OneOpsException("Error while doing deployment. Response from OneOps: " + responseBody + " ResponseCode : " + responseCode); } return responseCode; }
From source file:com.oneops.crawler.OneOpsFacade.java
License:Apache License
public int disablePlatform(Platform platform, String userName) throws IOException, OneOpsException { log.info("disabling platform id: " + platform.getId()); String url = transistorBaseUrl + "/transistor/rest/platforms/" + platform.getId() + "/disable"; RequestBody body = RequestBody.create(JSON, ""); Request request = new Request.Builder().url(url).header("X-Cms-User", userName) .addHeader("Content-Type", "application/json").put(body).build(); Response response = client.newCall(request).execute(); String responseBody = response.body().string(); Map<String, Double> release = gson.fromJson(responseBody, Map.class); log.info("Response from OneOps for disable-platform api: " + release); int responseCode = response.code(); if (responseCode >= 300) { throw new OneOpsException("Could not disable platform, response from OneOps: " + responseBody + ", response code: " + responseCode); }/*from w w w .jav a 2 s . c om*/ return responseCode; }
From source file:com.oneops.crawler.OneOpsFacade.java
License:Apache License
public int sendNotification(NotificationMessage msg) throws IOException, OneOpsException { String url = antennaBaseUrl + "/antenna/rest/notify/"; log.info(url + " sending notification: " + gson.toJson(msg)); RequestBody body = RequestBody.create(JSON, gson.toJson(msg)); Request request = new Request.Builder().url(url).addHeader("Content-Type", "application/json").post(body) .build();/*from w w w . ja v a 2s . c o m*/ Response response = client.newCall(request).execute(); String responseBody = response.body().string(); int responseCode = response.code(); if (responseCode >= 300) { throw new OneOpsException("Error while sending notification. Response from OneOps: " + responseBody + " ResponseCode : " + responseCode); } log.info("OO response body from antenna notify request: " + responseBody + ", code: " + responseCode); return responseCode; }
From source file:com.oneops.crawler.OneOpsFacade.java
License:Apache License
public Deployment scaleDown(long platformId, int scaleDownByNumber, int minComputesInEachCloud, String userId) throws IOException, OneOpsException { HashMap<String, String> params = new HashMap<>(); params.put("scaleDownBy", String.valueOf(scaleDownByNumber)); params.put("minComputesInEachCloud", String.valueOf(minComputesInEachCloud)); RequestBody body = RequestBody.create(JSON, gson.toJson(params)); String url = transistorBaseUrl + "/transistor/rest/platforms/" + +platformId + "/deployments/scaledown"; log.info("scaling down platform id: {} , url: {}", platformId, url); Request request = new Request.Builder().url(url).addHeader("X-Cms-User", userId) .addHeader("Content-Type", "application/json").post(body).build(); Response response = client.newCall(request).execute(); String responseBody = response.body().string(); int responseCode = response.code(); log.info("OO response body: {}, code: {}", responseBody, responseCode); if (responseCode >= 300) { throw new OneOpsException("Error while scaling down platform: " + platformId + ". Response from OneOps: " + responseBody + " ResponseCode : " + responseCode); }/* w ww . j ava 2 s . c o m*/ if (!StringUtils.isEmpty(responseBody)) { return gson.fromJson(responseBody, Deployment.class); } return null; }
From source file:com.oneops.crawler.OneOpsFacade.java
License:Apache License
public int disableVerify(Environment env, String userName) throws IOException, OneOpsException { String envCiJson = getCiJson(env.getId()); envCiJson = envCiJson.replace("\"verify\":\"default\"", "\"verify\":\"false\""); envCiJson = envCiJson.replace("\"verify\":\"true\"", "\"verify\":\"false\""); RequestBody body = RequestBody.create(JSON, envCiJson); String url = adapterBaseUrl + "/adapter/rest/cm/simple/cis/" + env.getId(); Request request = new Request.Builder().url(url).addHeader("X-Cms-User", userName) .addHeader("Content-Type", "application/json").put(body).build(); Response response = client.newCall(request).execute(); String responseBody = response.body().string(); int responseCode = response.code(); log.info("OO response body: " + responseBody + ", code: " + responseCode); if (!response.isSuccessful()) { throw new OneOpsException("Error while disabling verify. Response from OneOps: " + responseBody + " ResponseCode : " + responseCode); }/*from w w w . jav a 2s. co m*/ return responseCode; }
From source file:com.oneops.crawler.ThanosClient.java
License:Apache License
public void updateStatus(String path, String status) throws IOException { HashMap payload = new HashMap(); payload.put("nspath", path); payload.put("status", status); RequestBody body = RequestBody.create(JSON, gson.toJson(payload)); Request request = new Request.Builder().url(baseUrl).addHeader("Authorization", "Bearer " + getAuthToken()) .addHeader("Content-Type", "application/json").post(body).build(); Response response = client.newCall(request).execute(); String responseBody = response.body().string(); int responseCode = response.code(); log.info("Thanos response body: " + responseBody + ", code: " + responseCode); if (responseCode >= 300) { throw new RuntimeException("Error while updating status on thanos. Response: " + responseBody + " ResponseCode : " + responseCode); }/*from ww w. j a v a 2 s.c om*/ }
From source file:com.oracle.bdcs.bdm.client.ApiClient.java
License:Apache License
/** * Build HTTP call with the given options. * * @param path The sub-path of the HTTP URL * @param method The request method, one of "GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH" and "DELETE" * @param queryParams The query parameters * @param body The request body object/* www . ja v a2 s.c o m*/ * @param headerParams The header parameters * @param formParams The form parameters * @param authNames The authentications to apply * @param progressRequestListener Progress request listener * @return The HTTP call * @throws ApiException If fail to serialize the request body object */ public Call buildCall(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, Object> formParams, String[] authNames, ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException { String[] auths = { ApiKeyAuth.class.getName() }; updateParamsForAuth(auths, queryParams, headerParams); final String url = buildUrl(path, queryParams); final Request.Builder reqBuilder = new Request.Builder().url(url); processHeaderParams(headerParams, reqBuilder); String contentType = (String) headerParams.get("Content-Type"); // ensuring a default content type if (contentType == null) { contentType = "application/json"; } RequestBody reqBody; if (!HttpMethod.permitsRequestBody(method)) { reqBody = null; } else if ("application/x-www-form-urlencoded".equals(contentType)) { reqBody = buildRequestBodyFormEncoding(formParams); } else if ("multipart/form-data".equals(contentType)) { reqBody = buildRequestBodyMultipart(formParams); } else if (body == null) { if ("DELETE".equals(method)) { // allow calling DELETE without sending a request body reqBody = null; } else { // use an empty request body (for POST, PUT and PATCH) reqBody = RequestBody.create(MediaType.parse(contentType), ""); } } else { reqBody = serialize(body, contentType); } Request request = null; if (progressRequestListener != null && reqBody != null) { ProgressRequestBody progressRequestBody = new ProgressRequestBody(reqBody, progressRequestListener); request = reqBuilder.method(method, progressRequestBody).build(); } else { request = reqBuilder.method(method, reqBody).build(); } return httpClient.newCall(request); }
From source file:com.phattn.vnexpressnews.io.OkHttpStack.java
License:Open Source License
@SuppressWarnings("deprecation") private static void setConnectionParametersForRequest(com.squareup.okhttp.Request.Builder builder, Request<?> request) throws IOException, AuthFailureError { switch (request.getMethod()) { case Request.Method.DEPRECATED_GET_OR_POST: // Ensure backwards compatibility. Volley assumes a request with a null body is a GET byte[] postBody = request.getPostBody(); if (postBody != null) { builder.post(RequestBody.create(MediaType.parse(request.getPostBodyContentType()), postBody)); }//from ww w . j av a 2 s . co m break; case Request.Method.GET: builder.get(); break; case Request.Method.DELETE: builder.delete(); break; case Request.Method.POST: builder.post(createRequestBody(request)); break; case Request.Method.HEAD: builder.head(); break; case Request.Method.OPTIONS: builder.method("OPTIONS", null); break; case Request.Method.TRACE: builder.method("TRACE", null); break; case Request.Method.PATCH: builder.patch(createRequestBody(request)); break; default: throw new IllegalStateException("Unknown method type."); } }
From source file:com.phattn.vnexpressnews.io.OkHttpStack.java
License:Open Source License
private static RequestBody createRequestBody(Request request) throws AuthFailureError { final byte[] body = request.getBody(); if (body == null) { return null; }//from w ww . j a va 2 s.c o m return RequestBody.create(MediaType.parse(request.getBodyContentType()), body); }