Search in sources :

Example 1 with HttpUrl

use of com.squareup.okhttp.HttpUrl in project sbt-android by scala-android.

the class DribbbleSearch method search.

@WorkerThread
public static List<Shot> search(String query, @SortOrder String sort, int page) {
    String html = null;
    // e.g https://dribbble.com/search?q=material+design&page=7&per_page=12
    HttpUrl url = new HttpUrl.Builder().scheme("https").host("dribbble.com").addPathSegment("search").addQueryParameter("q", query).addQueryParameter("s", sort).addQueryParameter("page", String.valueOf(page)).addQueryParameter("per_page", "12").build();
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(url).build();
    try {
        Response response = client.newCall(request).execute();
        html = response.body().string();
    } catch (IOException ioe) {
        return null;
    }
    if (html == null)
        return null;
    Elements shotElements = Jsoup.parse(html, HOST).select("li[id^=screenshot]");
    SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM d, yyyy");
    List<Shot> shots = new ArrayList<>(shotElements.size());
    for (Element element : shotElements) {
        Shot shot = parseShot(element, dateFormat);
        if (shot != null) {
            shots.add(shot);
        }
    }
    return shots;
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) Element(org.jsoup.nodes.Element) Request(com.squareup.okhttp.Request) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Elements(org.jsoup.select.Elements) HttpUrl(com.squareup.okhttp.HttpUrl) Response(com.squareup.okhttp.Response) SimpleDateFormat(java.text.SimpleDateFormat) Shot(io.plaidapp.data.api.dribbble.model.Shot) WorkerThread(android.support.annotation.WorkerThread)

Example 2 with HttpUrl

use of com.squareup.okhttp.HttpUrl in project mbed-cloud-sdk-java by ARMmbed.

the class TestApiClientWrapper method testClient.

/**
 * Tests that the HTTP client wrapper works as expected by spawning a mock server and checking received requests.
 */
@Test
public void testClient() {
    try {
        MockWebServer server = new MockWebServer();
        server.enqueue(new MockResponse().setBody("hello, world!"));
        server.start();
        HttpUrl baseUrl = server.url("");
        ConnectionOptions opt = new ConnectionOptions("apikey");
        opt.setHost(baseUrl.toString());
        ApiClientWrapper clientWrapper = new ApiClientWrapper(opt);
        TestApiService testService = clientWrapper.createService(TestApiService.class);
        assertTrue(testService.getEndpointValue().execute().isSuccessful());
        RecordedRequest request = server.takeRequest();
        assertEquals("/" + AN_ENDPOINT_PATH, request.getPath());
        assertNotNull(request.getHeader("Authorization"));
        assertTrue(request.getHeader("Authorization").contains("Bearer"));
        assertNotNull(request.getHeader("User-Agent"));
        assertTrue(request.getHeader("User-Agent").contains(ApiClientWrapper.UserAgent.MBED_CLOUD_SDK_IDENTIFIER));
        server.shutdown();
    } catch (IOException | InterruptedException e) {
        fail(e.getMessage());
    }
}
Also used : RecordedRequest(com.squareup.okhttp.mockwebserver.RecordedRequest) MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) MockWebServer(com.squareup.okhttp.mockwebserver.MockWebServer) IOException(java.io.IOException) HttpUrl(com.squareup.okhttp.HttpUrl) Test(org.junit.Test)

Example 3 with HttpUrl

use of com.squareup.okhttp.HttpUrl in project grpc-java by grpc.

the class OkHttpClientTransport method createHttpProxyRequest.

private Request createHttpProxyRequest(InetSocketAddress address, String proxyUsername, String proxyPassword) {
    HttpUrl tunnelUrl = new HttpUrl.Builder().scheme("https").host(address.getHostName()).port(address.getPort()).build();
    Request.Builder request = new Request.Builder().url(tunnelUrl).header("Host", tunnelUrl.host() + ":" + tunnelUrl.port()).header("User-Agent", userAgent);
    // If we have proxy credentials, set them right away
    if (proxyUsername != null && proxyPassword != null) {
        request.header("Proxy-Authorization", Credentials.basic(proxyUsername, proxyPassword));
    }
    return request.build();
}
Also used : Request(com.squareup.okhttp.Request) HttpUrl(com.squareup.okhttp.HttpUrl)

Example 4 with HttpUrl

use of com.squareup.okhttp.HttpUrl in project pinpoint by naver.

the class RequestBuilderBuildMethodInterceptor method before.

@Override
public void before(Object target, Object[] args) {
    if (isDebug) {
        logger.beforeInterceptor(target, args);
    }
    final Trace trace = traceContext.currentRawTraceObject();
    if (trace == null) {
        return;
    }
    try {
        if (!(target instanceof Request.Builder)) {
            return;
        }
        final Request.Builder builder = ((Request.Builder) target);
        if (!trace.canSampled()) {
            if (isDebug) {
                logger.debug("set Sampling flag=false");
            }
            ((Request.Builder) target).header(Header.HTTP_SAMPLED.toString(), SamplingFlagUtils.SAMPLING_RATE_FALSE);
            return;
        }
        final InterceptorScopeInvocation invocation = interceptorScope.getCurrentInvocation();
        if (invocation == null || invocation.getAttachment() == null || !(invocation.getAttachment() instanceof TraceId)) {
            logger.debug("Invalid interceptor scope invocation. {}", invocation);
            return;
        }
        final TraceId nextId = (TraceId) invocation.getAttachment();
        builder.header(Header.HTTP_TRACE_ID.toString(), nextId.getTransactionId());
        builder.header(Header.HTTP_SPAN_ID.toString(), String.valueOf(nextId.getSpanId()));
        builder.header(Header.HTTP_PARENT_SPAN_ID.toString(), String.valueOf(nextId.getParentSpanId()));
        builder.header(Header.HTTP_FLAGS.toString(), String.valueOf(nextId.getFlags()));
        builder.header(Header.HTTP_PARENT_APPLICATION_NAME.toString(), traceContext.getApplicationName());
        builder.header(Header.HTTP_PARENT_APPLICATION_TYPE.toString(), Short.toString(traceContext.getServerTypeCode()));
        if (target instanceof HttpUrlGetter) {
            final HttpUrl url = ((HttpUrlGetter) target)._$PINPOINT$_getHttpUrl();
            if (url != null) {
                final String endpoint = getDestinationId(url);
                logger.debug("Set HTTP_HOST {}", endpoint);
                builder.header(Header.HTTP_HOST.toString(), endpoint);
            }
        }
    } catch (Throwable t) {
        logger.warn("Failed to BEFORE process. {}", t.getMessage(), t);
    }
}
Also used : InterceptorScopeInvocation(com.navercorp.pinpoint.bootstrap.interceptor.scope.InterceptorScopeInvocation) Request(com.squareup.okhttp.Request) HttpUrl(com.squareup.okhttp.HttpUrl)

Example 5 with HttpUrl

use of com.squareup.okhttp.HttpUrl in project data-transfer-project by google.

the class MicrosoftCalendarExportTest method testExport.

@Test
public void testExport() throws Exception {
    server.enqueue(new MockResponse().setBody(CALENDARS_RESPONSE));
    server.enqueue(new MockResponse().setBody(CALENDAR1_EVENTS_RESPONSE));
    server.enqueue(new MockResponse().setBody(CALENDAR2_EVENTS_RESPONSE));
    server.start();
    HttpUrl baseUrl = server.url("");
    MicrosoftCalendarExporter exporter = new MicrosoftCalendarExporter(baseUrl.toString(), client, mapper, transformerService);
    ExportResult<CalendarContainerResource> resource = exporter.export(UUID.randomUUID(), token);
    CalendarContainerResource calendarResource = resource.getExportedData();
    Assert.assertEquals(2, calendarResource.getCalendars().size());
    Assert.assertFalse(calendarResource.getCalendars().stream().anyMatch(c -> "Calendar1".equals(c.getId()) && "Calendar2".equals(c.getId())));
    Assert.assertEquals(2, calendarResource.getEvents().size());
    Assert.assertFalse(calendarResource.getEvents().stream().anyMatch(e -> "Test Appointment 1".equals(e.getTitle()) && "Test Appointment 2".equals(e.getTitle())));
}
Also used : MicrosoftCalendarExporter(org.dataportabilityproject.transfer.microsoft.calendar.MicrosoftCalendarExporter) MockWebServer(com.squareup.okhttp.mockwebserver.MockWebServer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) ExportResult(org.dataportabilityproject.spi.transfer.provider.ExportResult) TokenAuthData(org.dataportabilityproject.types.transfer.auth.TokenAuthData) Test(org.junit.Test) UUID(java.util.UUID) MicrosoftCalendarExporter(org.dataportabilityproject.transfer.microsoft.calendar.MicrosoftCalendarExporter) HttpUrl(com.squareup.okhttp.HttpUrl) CalendarContainerResource(org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource) TransformerServiceImpl(org.dataportabilityproject.transfer.microsoft.transformer.TransformerServiceImpl) OkHttpClient(okhttp3.OkHttpClient) After(org.junit.After) Assert(org.junit.Assert) Before(org.junit.Before) MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) CalendarContainerResource(org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource) HttpUrl(com.squareup.okhttp.HttpUrl) Test(org.junit.Test)

Aggregations

HttpUrl (com.squareup.okhttp.HttpUrl)8 Request (com.squareup.okhttp.Request)5 MockResponse (com.squareup.okhttp.mockwebserver.MockResponse)3 IOException (java.io.IOException)3 Test (org.junit.Test)3 MockWebServer (com.squareup.okhttp.mockwebserver.MockWebServer)2 RecordedRequest (com.squareup.okhttp.mockwebserver.RecordedRequest)2 CalendarContainerResource (org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource)2 WorkerThread (android.support.annotation.WorkerThread)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 InterceptorScopeInvocation (com.navercorp.pinpoint.bootstrap.interceptor.scope.InterceptorScopeInvocation)1 UserRequestGetter (com.navercorp.pinpoint.plugin.okhttp.v2.UserRequestGetter)1 OkHttpClient (com.squareup.okhttp.OkHttpClient)1 Response (com.squareup.okhttp.Response)1 StatusLine (com.squareup.okhttp.internal.http.StatusLine)1 Shot (io.plaidapp.data.api.dribbble.model.Shot)1 Socket (java.net.Socket)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Collections.singletonList (java.util.Collections.singletonList)1