Search in sources :

Example 6 with ResponseBody

use of okhttp3.ResponseBody in project android-oss by kickstarter.

the class ApiExceptionFactory method tfaFailed.

@NonNull
public static ApiException tfaFailed() {
    final ErrorEnvelope envelope = ErrorEnvelope.builder().ksrCode(ErrorEnvelope.TFA_FAILED).httpCode(400).build();
    final ResponseBody body = ResponseBody.create(null, new Gson().toJson(envelope));
    final retrofit2.Response<Observable<User>> response = retrofit2.Response.error(envelope.httpCode(), body);
    return new ApiException(envelope, response);
}
Also used : ErrorEnvelope(com.kickstarter.services.apiresponses.ErrorEnvelope) Gson(com.google.gson.Gson) Observable(rx.Observable) ResponseBody(okhttp3.ResponseBody) ApiException(com.kickstarter.services.ApiException) NonNull(android.support.annotation.NonNull)

Example 7 with ResponseBody

use of okhttp3.ResponseBody in project android-oss by kickstarter.

the class ApiExceptionFactory method apiError.

@NonNull
public static ApiException apiError(@NonNull final ErrorEnvelope errorEnvelope) {
    final ResponseBody body = ResponseBody.create(null, new Gson().toJson(errorEnvelope));
    final retrofit2.Response<Observable<User>> response = retrofit2.Response.error(errorEnvelope.httpCode(), body);
    return new ApiException(errorEnvelope, response);
}
Also used : Gson(com.google.gson.Gson) Observable(rx.Observable) ResponseBody(okhttp3.ResponseBody) ApiException(com.kickstarter.services.ApiException) NonNull(android.support.annotation.NonNull)

Example 8 with ResponseBody

use of okhttp3.ResponseBody in project okhttputils by hongyangAndroid.

the class LoggerInterceptor method logForResponse.

private Response logForResponse(Response response) {
    try {
        //===>response log
        Log.e(tag, "========response'log=======");
        Response.Builder builder = response.newBuilder();
        Response clone = builder.build();
        Log.e(tag, "url : " + clone.request().url());
        Log.e(tag, "code : " + clone.code());
        Log.e(tag, "protocol : " + clone.protocol());
        if (!TextUtils.isEmpty(clone.message()))
            Log.e(tag, "message : " + clone.message());
        if (showResponse) {
            ResponseBody body = clone.body();
            if (body != null) {
                MediaType mediaType = body.contentType();
                if (mediaType != null) {
                    Log.e(tag, "responseBody's contentType : " + mediaType.toString());
                    if (isText(mediaType)) {
                        String resp = body.string();
                        Log.e(tag, "responseBody's content : " + resp);
                        body = ResponseBody.create(mediaType, resp);
                        return response.newBuilder().body(body).build();
                    } else {
                        Log.e(tag, "responseBody's content : " + " maybe [file part] , too large too print , ignored!");
                    }
                }
            }
        }
        Log.e(tag, "========response'log=======end");
    } catch (Exception e) {
    //            e.printStackTrace();
    }
    return response;
}
Also used : Response(okhttp3.Response) MediaType(okhttp3.MediaType) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody)

Example 9 with ResponseBody

use of okhttp3.ResponseBody in project zipkin by openzipkin.

the class AWSSignatureVersion4 method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    Request input = chain.request();
    Request signed = sign(input);
    Response response = chain.proceed(signed);
    if (response.code() == 403) {
        try (ResponseBody body = response.body()) {
            JsonReader message = enterPath(JsonReader.of(body.source()), "message");
            if (message != null)
                throw new IllegalStateException(message.nextString());
        }
        throw new IllegalStateException(response.toString());
    }
    return response;
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) JsonReader(com.squareup.moshi.JsonReader) ResponseBody(okhttp3.ResponseBody)

Example 10 with ResponseBody

use of okhttp3.ResponseBody in project spring-framework by spring-projects.

the class AbstractMockWebServerTestCase method postRequest.

private MockResponse postRequest(RecordedRequest request, String expectedRequestContent, String location, String contentType, byte[] responseBody) {
    assertEquals(1, request.getHeaders().values("Content-Length").size());
    assertTrue("Invalid request content-length", Integer.parseInt(request.getHeader("Content-Length")) > 0);
    String requestContentType = request.getHeader("Content-Type");
    assertNotNull("No content-type", requestContentType);
    Charset charset = StandardCharsets.ISO_8859_1;
    if (requestContentType.contains("charset=")) {
        String charsetName = requestContentType.split("charset=")[1];
        charset = Charset.forName(charsetName);
    }
    assertEquals("Invalid request body", expectedRequestContent, request.getBody().readString(charset));
    Buffer buf = new Buffer();
    buf.write(responseBody);
    return new MockResponse().setHeader("Location", baseUrl + location).setHeader("Content-Type", contentType).setHeader("Content-Length", responseBody.length).setBody(buf).setResponseCode(201);
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) Charset(java.nio.charset.Charset)

Aggregations

ResponseBody (okhttp3.ResponseBody)584 DateTimeRfc1123 (com.microsoft.rest.DateTimeRfc1123)332 DateTime (org.joda.time.DateTime)332 ServiceCall (com.microsoft.rest.ServiceCall)140 Test (org.junit.Test)123 Request (okhttp3.Request)112 Observable (rx.Observable)97 Response (retrofit2.Response)94 ServiceResponse (com.microsoft.rest.ServiceResponse)92 PagedList (com.microsoft.azure.PagedList)80 ServiceResponseWithHeaders (com.microsoft.rest.ServiceResponseWithHeaders)78 List (java.util.List)64 IOException (java.io.IOException)33 Response (okhttp3.Response)33 Buffer (okio.Buffer)33 RequestBody (okhttp3.RequestBody)31 PageImpl (com.microsoft.azure.batch.protocol.models.PageImpl)26 MockResponse (okhttp3.mockwebserver.MockResponse)24 InputStream (java.io.InputStream)19 MultipartBody (okhttp3.MultipartBody)16