Search in sources :

Example 86 with Request

use of okhttp3.Request in project muzei by romannurik.

the class DownloadArtworkTask method openUri.

private InputStream openUri(Context context, Uri uri) throws IOException {
    if (uri == null) {
        throw new IllegalArgumentException("Uri cannot be empty");
    }
    String scheme = uri.getScheme();
    if (scheme == null) {
        throw new IOException("Uri had no scheme");
    }
    InputStream in = null;
    if ("content".equals(scheme)) {
        try {
            in = context.getContentResolver().openInputStream(uri);
        } catch (SecurityException e) {
            throw new FileNotFoundException("No access to " + uri + ": " + e.toString());
        }
    } else if ("file".equals(scheme)) {
        List<String> segments = uri.getPathSegments();
        if (segments != null && segments.size() > 1 && "android_asset".equals(segments.get(0))) {
            AssetManager assetManager = context.getAssets();
            StringBuilder assetPath = new StringBuilder();
            for (int i = 1; i < segments.size(); i++) {
                if (i > 1) {
                    assetPath.append("/");
                }
                assetPath.append(segments.get(i));
            }
            in = assetManager.open(assetPath.toString());
        } else {
            in = new FileInputStream(new File(uri.getPath()));
        }
    } else if ("http".equals(scheme) || "https".equals(scheme)) {
        OkHttpClient client = OkHttpClientFactory.getNewOkHttpsSafeClient();
        Request request;
        request = new Request.Builder().url(new URL(uri.toString())).build();
        Response response = client.newCall(request).execute();
        int responseCode = response.code();
        if (!(responseCode >= 200 && responseCode < 300)) {
            throw new IOException("HTTP error response " + responseCode);
        }
        in = response.body().byteStream();
    }
    if (in == null) {
        throw new FileNotFoundException("Null input stream for URI: " + uri);
    }
    return in;
}
Also used : AssetManager(android.content.res.AssetManager) OkHttpClient(okhttp3.OkHttpClient) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) Request(okhttp3.Request) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) URL(java.net.URL) Response(okhttp3.Response) List(java.util.List) File(java.io.File)

Example 87 with Request

use of okhttp3.Request in project muzei by romannurik.

the class FiveHundredPxExampleArtSource method onTryUpdate.

@Override
protected void onTryUpdate(@UpdateReason int reason) throws RetryException {
    String currentToken = (getCurrentArtwork() != null) ? getCurrentArtwork().getToken() : null;
    OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {

        @Override
        public Response intercept(final Chain chain) throws IOException {
            Request request = chain.request();
            HttpUrl url = request.url().newBuilder().addQueryParameter("consumer_key", Config.CONSUMER_KEY).build();
            request = request.newBuilder().url(url).build();
            return chain.proceed(request);
        }
    }).build();
    Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.500px.com/").client(okHttpClient).addConverterFactory(GsonConverterFactory.create()).build();
    FiveHundredPxService service = retrofit.create(FiveHundredPxService.class);
    PhotosResponse response;
    try {
        response = service.getPopularPhotos().execute().body();
    } catch (IOException e) {
        Log.w(TAG, "Error reading 500px response", e);
        throw new RetryException();
    }
    if (response == null || response.photos == null) {
        throw new RetryException();
    }
    if (response.photos.size() == 0) {
        Log.w(TAG, "No photos returned from API.");
        scheduleUpdate(System.currentTimeMillis() + ROTATE_TIME_MILLIS);
        return;
    }
    Random random = new Random();
    Photo photo;
    String token;
    while (true) {
        photo = response.photos.get(random.nextInt(response.photos.size()));
        token = Integer.toString(photo.id);
        if (response.photos.size() <= 1 || !TextUtils.equals(token, currentToken)) {
            break;
        }
    }
    publishArtwork(new Artwork.Builder().title(photo.name).byline(photo.user.fullname).imageUri(Uri.parse(photo.image_url)).token(token).viewIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://500px.com/photo/" + photo.id))).build());
    scheduleUpdate(System.currentTimeMillis() + ROTATE_TIME_MILLIS);
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Artwork(com.google.android.apps.muzei.api.Artwork) Request(okhttp3.Request) Photo(com.example.muzei.examplesource500px.FiveHundredPxService.Photo) Intent(android.content.Intent) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) Retrofit(retrofit2.Retrofit) Random(java.util.Random) PhotosResponse(com.example.muzei.examplesource500px.FiveHundredPxService.PhotosResponse) Interceptor(okhttp3.Interceptor)

Example 88 with Request

use of okhttp3.Request in project zipkin by openzipkin.

the class ZipkinDispatcherTest method toQueryRequest.

@Test
public void toQueryRequest() {
    HttpUrl url = baseUrl.newBuilder().addQueryParameter("serviceName", "zipkin-server").addQueryParameter("spanName", "get").addQueryParameter("limit", "1000").build();
    QueryRequest request = ZipkinDispatcher.toQueryRequest(url);
    assertThat(request.serviceName).isEqualTo("zipkin-server");
    assertThat(request.spanName).isEqualTo("get");
    assertThat(request.limit).isEqualTo(1000);
}
Also used : QueryRequest(zipkin.storage.QueryRequest) HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

Example 89 with Request

use of okhttp3.Request in project zipkin by openzipkin.

the class ZipkinDispatcherTest method toQueryRequest_parseAnnotations.

@Test
public void toQueryRequest_parseAnnotations() {
    HttpUrl url = baseUrl.newBuilder().addQueryParameter("serviceName", "zipkin-server").addQueryParameter("annotationQuery", "error and finagle.timeout").build();
    QueryRequest request = ZipkinDispatcher.toQueryRequest(url);
    assertThat(request.annotations).containsExactly(Constants.ERROR, "finagle.timeout");
}
Also used : QueryRequest(zipkin.storage.QueryRequest) HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

Example 90 with Request

use of okhttp3.Request in project okhttp by square.

the class OkApacheClientTest method putEmptyEntity.

@Test
public void putEmptyEntity() throws Exception {
    server.enqueue(new MockResponse());
    final HttpPut put = new HttpPut(server.url("/").url().toURI());
    client.execute(put);
    RecordedRequest request = server.takeRequest();
    assertEquals(0, request.getBodySize());
    assertNotNull(request.getBody());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Aggregations

Request (okhttp3.Request)1552 Response (okhttp3.Response)1090 Test (org.junit.Test)948 IOException (java.io.IOException)624 MockResponse (okhttp3.mockwebserver.MockResponse)560 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)556 OkHttpClient (okhttp3.OkHttpClient)343 RequestBody (okhttp3.RequestBody)281 Call (okhttp3.Call)255 ResponseBody (okhttp3.ResponseBody)252 HttpUrl (okhttp3.HttpUrl)186 Test (org.junit.jupiter.api.Test)158 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)142 Buffer (okio.Buffer)138 List (java.util.List)114 Callback (okhttp3.Callback)114 File (java.io.File)105 URI (java.net.URI)101 InputStream (java.io.InputStream)99 JSONObject (org.json.JSONObject)96