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;
}
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());
}
}
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();
}
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);
}
}
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())));
}
Aggregations