Search in sources :

Example 41 with RequestBody

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

the class InnerInlongManagerClient method createSink.

public String createSink(SinkRequest sinkRequest) {
    String path = HTTP_PATH + "/sink/save";
    final String sink = GsonUtil.toJson(sinkRequest);
    final RequestBody sinkBody = RequestBody.create(MediaType.parse("application/json"), sink);
    final String url = formatUrl(path);
    Request request = new Request.Builder().url(url).method("POST", sinkBody).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 sink 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 42 with RequestBody

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

the class InnerInlongManagerClient method updateSource.

public Pair<Boolean, String> updateSource(SourceRequest sourceRequest) {
    final String path = HTTP_PATH + "/source/update";
    final String url = formatUrl(path);
    final String storage = GsonUtil.toJson(sourceRequest);
    final RequestBody storageBody = RequestBody.create(MediaType.parse("application/json"), storage);
    Request request = new Request.Builder().method("POST", storageBody).url(url).build();
    Call call = httpClient.newCall(request);
    try {
        Response response = call.execute();
        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);
        if (responseBody.getData() != null) {
            return Pair.of(Boolean.valueOf(responseBody.getData().toString()), responseBody.getErrMsg());
        } else {
            return Pair.of(false, responseBody.getErrMsg());
        }
    } catch (Exception e) {
        throw new RuntimeException(String.format("Inlong source update failed with ex:%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 43 with RequestBody

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

the class InnerInlongManagerClient method listGroups.

public PageInfo<InlongGroupListResponse> listGroups(String keyword, int status, int pageNum, int pageSize) {
    if (pageNum <= 0) {
        pageNum = 1;
    }
    JSONObject groupQuery = new JSONObject();
    groupQuery.put("keyWord", pageNum);
    groupQuery.put("status", status);
    groupQuery.put("pageNum", pageNum);
    groupQuery.put("pageSize", pageSize);
    String operationData = GsonUtil.toJson(groupQuery);
    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), operationData);
    String path = HTTP_PATH + "/group/list";
    final String url = formatUrl(path);
    Request request = new Request.Builder().get().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);
        if (responseBody.getErrMsg() != null) {
            if (responseBody.getErrMsg().contains("Inlong group does not exist")) {
                return null;
            } else {
                throw new RuntimeException(responseBody.getErrMsg());
            }
        } else {
            return InlongParser.parseGroupList(responseBody);
        }
    } catch (Exception e) {
        throw new RuntimeException(String.format("Inlong group get 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)

Example 44 with RequestBody

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

the class HttpTestControllerTest method testUpload.

@Test
public void testUpload() throws IOException {
    File uploadFile = File.createTempFile(String.valueOf(System.currentTimeMillis()), "-uploadFile.txt");
    try (FileOutputStream fos = new FileOutputStream(uploadFile)) {
        fos.write("testContent".getBytes(StandardCharsets.UTF_8));
    }
    RequestBody requestBody = new MultipartBody.Builder().setType(MediaType.parse(MULTIPART_FORM_DATA_VALUE)).addFormDataPart("file", uploadFile.getName(), RequestBody.create(MediaType.parse(APPLICATION_OCTET_STREAM_VALUE), uploadFile)).build();
    String ret = HttpHelper.INSTANCE.postGateway("/http/test/upload", requestBody, String.class);
    assertEquals(ret, "OK");
}
Also used : MultipartBody(okhttp3.MultipartBody) FileOutputStream(java.io.FileOutputStream) File(java.io.File) RequestBody(okhttp3.RequestBody) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.shenyu.integratedtest.common.AbstractTest)

Example 45 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project msgraph-sdk-java-core by microsoftgraph.

the class CoreHttpProviderTests method getHttpRequestWithTextPlainBodyDoesNotSerializeAsJson.

@Test
void getHttpRequestWithTextPlainBodyDoesNotSerializeAsJson() throws IOException {
    final IHttpRequest absRequest = mock(IHttpRequest.class);
    when(absRequest.getRequestUrl()).thenReturn(new URL("https://graph.microsoft.com/v1.0/me"));
    when(absRequest.getHttpMethod()).thenReturn(HttpMethod.POST);
    final ISerializer serializer = mock(ISerializer.class);
    final ILogger logger = mock(ILogger.class);
    mProvider = new CoreHttpProvider(serializer, logger, new OkHttpClient.Builder().build());
    // GIVEN: A "text/plain" request body
    final HeaderOption option = new HeaderOption("Content-Type", "text/plain");
    when(absRequest.getHeaders()).thenReturn(Arrays.asList(option));
    final String expectedBody = "Plain String Body";
    // WHEN: getHttpRequest is called
    final Request request = mProvider.getHttpRequest(absRequest, String.class, expectedBody);
    // THEN: The serializer must not be called
    verify(serializer, never()).serializeObject(Mockito.any());
    // AND: We expect the request body to contain the plain String, not serialized as Json
    final Buffer buffer = new Buffer();
    final RequestBody requestBody = request.body();
    assertNotNull(requestBody);
    requestBody.writeTo(buffer);
    final String actualRequestBody = buffer.readUtf8();
    assertEquals(expectedBody, actualRequestBody);
}
Also used : Buffer(okio.Buffer) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) ILogger(com.microsoft.graph.logger.ILogger) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) URL(java.net.URL) HeaderOption(com.microsoft.graph.options.HeaderOption) ISerializer(com.microsoft.graph.serializer.ISerializer) RequestBody(okhttp3.RequestBody) Test(org.junit.jupiter.api.Test)

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