Search in sources :

Example 36 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project spring-framework-5.2.9.RELEASE by somepeopleHavingDream.

the class OkHttp3ClientHttpRequestFactory method buildRequest.

static Request buildRequest(HttpHeaders headers, byte[] content, URI uri, HttpMethod method) throws MalformedURLException {
    okhttp3.MediaType contentType = getContentType(headers);
    RequestBody body = (content.length > 0 || okhttp3.internal.http.HttpMethod.requiresRequestBody(method.name()) ? RequestBody.create(contentType, content) : null);
    Request.Builder builder = new Request.Builder().url(uri.toURL()).method(method.name(), body);
    headers.forEach((headerName, headerValues) -> {
        for (String headerValue : headerValues) {
            builder.addHeader(headerName, headerValue);
        }
    });
    return builder.build();
}
Also used : Request(okhttp3.Request) RequestBody(okhttp3.RequestBody)

Example 37 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project AnkiChinaAndroid by ankichinateam.

the class AnkiChinaSyncer method uploadLocalMediaFileInfo.

/**
 * 上传媒体文件信息给服务端,服务端比对完后下发文件的同步结果
 */
private void uploadLocalMediaFileInfo(String token) {
    // 获取本地media文件夹的目录
    updateDialogProgress(SYNCING_MEDIA, "", 1);
    File mediaDir = new File(Media.getCollectionMediaPath(CollectionHelper.getInstance().getColSafe(AnkiDroidApp.getInstance()).getPath()));
    // if (!mediaDir.exists()) {
    // return;
    // }
    File[] files = mediaDir.listFiles();
    // if (files.length == 0) {
    // return;
    // }
    String[] localFiles = new String[files.length];
    for (int i = 0; i < files.length; i++) {
        localFiles[i] = files[i].getName();
    }
    JSONArray filesJson = new JSONArray(localFiles);
    // Timber.i("local file list:%s", filesJson);
    RequestBody formBody = new FormBody.Builder().add("file_list", filesJson.toString()).build();
    updateDialogProgress(SYNCING_MEDIA, "", 5);
    if (mCancel) {
        return;
    }
    OKHttpUtil.post(Consts.ANKI_CHINA_BASE + Consts.API_VERSION + "napi/sync/postFileInfo", formBody, token, "", new OKHttpUtil.MyCallBack() {

        @Override
        public void onFailure(Call call, IOException e) {
            updateDialogMessage(SYNCING_ERROR, ERROR_NETWORK);
        }

        @Override
        public void onResponse(Call call, String token, Object arg1, Response response) throws IOException {
            if (response.isSuccessful()) {
                Timber.e("upload media file info succeed!");
                try {
                    final JSONObject object = new JSONObject(response.body().string());
                    Timber.e("fetch media sync info from server:%s", object.toString());
                    if (object.getInt("status_code") != 0) {
                        updateDialogMessage(SYNCING_ERROR, object.getString("message"));
                        return;
                    }
                    final JSONObject item = object.getJSONObject("data");
                    updateDialogProgress(SYNCING_MEDIA, "", 10);
                    handleMediaSync(item, token);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                Timber.e("upload media file info error, code %d", response.code());
            }
        }
    });
}
Also used : Call(okhttp3.Call) OKHttpUtil(com.ichi2.utils.OKHttpUtil) JSONArray(com.ichi2.utils.JSONArray) IOException(java.io.IOException) SuppressLint(android.annotation.SuppressLint) FileNotFoundException(java.io.FileNotFoundException) SQLiteConstraintException(android.database.sqlite.SQLiteConstraintException) IOException(java.io.IOException) Response(okhttp3.Response) JSONObject(com.ichi2.utils.JSONObject) JSONObject(com.ichi2.utils.JSONObject) ZipFile(java.util.zip.ZipFile) File(java.io.File) RequestBody(okhttp3.RequestBody)

Example 38 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project incubator-inlong by apache.

the class InnerInlongManagerClient method operateInlongGroup.

public boolean operateInlongGroup(String groupId, InlongGroupState status) {
    String path = HTTP_PATH;
    if (status == InlongGroupState.STOPPED) {
        path += "/group/suspendProcess/";
    } else if (status == InlongGroupState.STARTED) {
        path += "/group/restartProcess/";
    } else {
        throw new IllegalArgumentException(String.format("Unsupported state: %s", status));
    }
    path += groupId;
    final String url = formatUrl(path);
    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), "");
    Request request = new Request.Builder().url(url).method("POST", requestBody).build();
    Call call = httpClient.newCall(request);
    try {
        Response response = call.execute();
        assert response.body() != null;
        String body = response.body().string();
        AssertUtil.isTrue(response.isSuccessful(), String.format("Inlong request failed: %s", body));
        org.apache.inlong.manager.common.beans.Response responseBody = InlongParser.parseResponse(body);
        String errMsg = responseBody.getErrMsg();
        return errMsg == null || !errMsg.contains("current status was not allowed");
    } catch (Exception e) {
        throw new RuntimeException(String.format("Inlong group operate: %s failed with ex: %s", status, e.getMessage()), e);
    }
}
Also used : Call(okhttp3.Call) SinkRequest(org.apache.inlong.manager.common.pojo.sink.SinkRequest) Request(okhttp3.Request) InlongStreamApproveRequest(org.apache.inlong.manager.common.pojo.stream.InlongStreamApproveRequest) InlongGroupRequest(org.apache.inlong.manager.common.pojo.group.InlongGroupRequest) SourceRequest(org.apache.inlong.manager.common.pojo.source.SourceRequest) InlongGroupApproveRequest(org.apache.inlong.manager.common.pojo.group.InlongGroupApproveRequest) Response(okhttp3.Response) InlongStreamConfigLogListResponse(org.apache.inlong.manager.common.pojo.stream.InlongStreamConfigLogListResponse) SinkListResponse(org.apache.inlong.manager.common.pojo.sink.SinkListResponse) SourceListResponse(org.apache.inlong.manager.common.pojo.source.SourceListResponse) InlongGroupListResponse(org.apache.inlong.manager.common.pojo.group.InlongGroupListResponse) InlongGroupResponse(org.apache.inlong.manager.common.pojo.group.InlongGroupResponse) FullStreamResponse(org.apache.inlong.manager.common.pojo.stream.FullStreamResponse) RequestBody(okhttp3.RequestBody)

Example 39 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project incubator-inlong by apache.

the class InnerInlongManagerClient method createStreamInfo.

public String createStreamInfo(InlongStreamInfo streamInfo) {
    String path = HTTP_PATH + "/stream/save";
    final String stream = GsonUtil.toJson(streamInfo);
    final RequestBody streamBody = RequestBody.create(MediaType.parse("application/json"), stream);
    final String url = formatUrl(path);
    Request request = new Request.Builder().url(url).method("POST", streamBody).build();
    Call call = httpClient.newCall(request);
    try {
        Response response = call.execute();
        assert response.body() != null;
        String body = response.body().string();
        AssertUtil.isTrue(response.isSuccessful(), String.format("Inlong request failed: %s", body));
        org.apache.inlong.manager.common.beans.Response responseBody = InlongParser.parseResponse(body);
        AssertUtil.isTrue(responseBody.getErrMsg() == null, String.format("Inlong request failed: %s", responseBody.getErrMsg()));
        return responseBody.getData().toString();
    } catch (Exception e) {
        throw new RuntimeException(String.format("Inlong stream save failed: %s", e.getMessage()), e);
    }
}
Also used : Response(okhttp3.Response) InlongStreamConfigLogListResponse(org.apache.inlong.manager.common.pojo.stream.InlongStreamConfigLogListResponse) SinkListResponse(org.apache.inlong.manager.common.pojo.sink.SinkListResponse) SourceListResponse(org.apache.inlong.manager.common.pojo.source.SourceListResponse) InlongGroupListResponse(org.apache.inlong.manager.common.pojo.group.InlongGroupListResponse) InlongGroupResponse(org.apache.inlong.manager.common.pojo.group.InlongGroupResponse) FullStreamResponse(org.apache.inlong.manager.common.pojo.stream.FullStreamResponse) Call(okhttp3.Call) SinkRequest(org.apache.inlong.manager.common.pojo.sink.SinkRequest) Request(okhttp3.Request) InlongStreamApproveRequest(org.apache.inlong.manager.common.pojo.stream.InlongStreamApproveRequest) InlongGroupRequest(org.apache.inlong.manager.common.pojo.group.InlongGroupRequest) SourceRequest(org.apache.inlong.manager.common.pojo.source.SourceRequest) InlongGroupApproveRequest(org.apache.inlong.manager.common.pojo.group.InlongGroupApproveRequest) RequestBody(okhttp3.RequestBody)

Example 40 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project incubator-inlong by apache.

the class InnerInlongManagerClient method startInlongGroup.

public WorkflowResult startInlongGroup(int taskId, Pair<InlongGroupApproveRequest, List<InlongStreamApproveRequest>> initMsg) {
    JSONObject workflowTaskOperation = new JSONObject();
    workflowTaskOperation.put("transferTo", Lists.newArrayList());
    workflowTaskOperation.put("remark", "approved by system");
    JSONObject inlongGroupApproveForm = new JSONObject();
    inlongGroupApproveForm.put("groupApproveInfo", initMsg.getKey());
    inlongGroupApproveForm.put("streamApproveInfoList", initMsg.getValue());
    inlongGroupApproveForm.put("formName", "InlongGroupApproveForm");
    workflowTaskOperation.put("form", inlongGroupApproveForm);
    String operationData = GsonUtil.toJson(workflowTaskOperation);
    final String path = HTTP_PATH + "/workflow/approve/" + taskId;
    final String url = formatUrl(path);
    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), operationData);
    Request request = new Request.Builder().url(url).method("POST", requestBody).build();
    Call call = httpClient.newCall(request);
    try {
        Response response = call.execute();
        assert response.body() != null;
        String body = response.body().string();
        AssertUtil.isTrue(response.isSuccessful(), String.format("Inlong request failed: %s", body));
        org.apache.inlong.manager.common.beans.Response responseBody = InlongParser.parseResponse(body);
        AssertUtil.isTrue(responseBody.getErrMsg() == null, String.format("Inlong request failed: %s", responseBody.getErrMsg()));
        return InlongParser.parseWorkflowResult(responseBody);
    } catch (Exception e) {
        throw new RuntimeException(String.format("Inlong group start failed: %s", e.getMessage()), e);
    }
}
Also used : Call(okhttp3.Call) SinkRequest(org.apache.inlong.manager.common.pojo.sink.SinkRequest) Request(okhttp3.Request) InlongStreamApproveRequest(org.apache.inlong.manager.common.pojo.stream.InlongStreamApproveRequest) InlongGroupRequest(org.apache.inlong.manager.common.pojo.group.InlongGroupRequest) SourceRequest(org.apache.inlong.manager.common.pojo.source.SourceRequest) InlongGroupApproveRequest(org.apache.inlong.manager.common.pojo.group.InlongGroupApproveRequest) Response(okhttp3.Response) InlongStreamConfigLogListResponse(org.apache.inlong.manager.common.pojo.stream.InlongStreamConfigLogListResponse) SinkListResponse(org.apache.inlong.manager.common.pojo.sink.SinkListResponse) SourceListResponse(org.apache.inlong.manager.common.pojo.source.SourceListResponse) InlongGroupListResponse(org.apache.inlong.manager.common.pojo.group.InlongGroupListResponse) InlongGroupResponse(org.apache.inlong.manager.common.pojo.group.InlongGroupResponse) FullStreamResponse(org.apache.inlong.manager.common.pojo.stream.FullStreamResponse) JSONObject(com.alibaba.fastjson.JSONObject) RequestBody(okhttp3.RequestBody)

Aggregations

RequestBody (okhttp3.RequestBody)1358 Request (okhttp3.Request)785 Response (okhttp3.Response)598 IOException (java.io.IOException)420 Test (org.junit.Test)235 OkHttpClient (okhttp3.OkHttpClient)216 MultipartBody (okhttp3.MultipartBody)213 MediaType (okhttp3.MediaType)204 Call (okhttp3.Call)198 JSONObject (org.json.JSONObject)183 ResponseBody (okhttp3.ResponseBody)177 Callback (okhttp3.Callback)115 FormBody (okhttp3.FormBody)106 Buffer (okio.Buffer)98 File (java.io.File)92 Map (java.util.Map)90 JsonObject (io.vertx.core.json.JsonObject)89 Headers (okhttp3.Headers)88 HashMap (java.util.HashMap)83 HttpUrl (okhttp3.HttpUrl)80