use of okhttp3.Request in project Lightning-Browser by anthonycr.
the class BaseSuggestionsModel method downloadSuggestionsForQuery.
/**
* This method downloads the search suggestions for the specific query.
* NOTE: This is a blocking operation, do not getResults on the UI thread.
*
* @param query the query to get suggestions for
* @return the cache file containing the suggestions
*/
@Nullable
private InputStream downloadSuggestionsForQuery(@NonNull String query, @NonNull String language) {
String queryUrl = createQueryUrl(query, language);
try {
URL url = new URL(queryUrl);
// OkHttp automatically gzips requests
Request suggestionsRequest = new Request.Builder().url(url).addHeader("Accept-Charset", mEncoding).cacheControl(mCacheControl).build();
Response suggestionsResponse = mHttpClient.newCall(suggestionsRequest).execute();
ResponseBody responseBody = suggestionsResponse.body();
return responseBody != null ? responseBody.byteStream() : null;
} catch (IOException exception) {
Log.e(TAG, "Problem getting search suggestions", exception);
}
return null;
}
use of okhttp3.Request in project ignite by apache.
the class RestExecutor method sendRequest.
/** */
private RestResult sendRequest(boolean demo, String path, Map<String, Object> params, String mtd, Map<String, Object> headers, String body) throws IOException {
if (demo && AgentClusterDemo.getDemoUrl() == null) {
try {
AgentClusterDemo.tryStart().await();
} catch (InterruptedException ignore) {
throw new IllegalStateException("Failed to execute request because of embedded node for demo mode is not started yet.");
}
}
String url = demo ? AgentClusterDemo.getDemoUrl() : nodeUrl;
HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
if (path != null)
urlBuilder.addPathSegment(path);
final Request.Builder reqBuilder = new Request.Builder();
if (headers != null) {
for (Map.Entry<String, Object> entry : headers.entrySet()) if (entry.getValue() != null)
reqBuilder.addHeader(entry.getKey(), entry.getValue().toString());
}
if ("GET".equalsIgnoreCase(mtd)) {
if (params != null) {
for (Map.Entry<String, Object> entry : params.entrySet()) {
if (entry.getValue() != null)
urlBuilder.addQueryParameter(entry.getKey(), entry.getValue().toString());
}
}
} else if ("POST".equalsIgnoreCase(mtd)) {
if (body != null) {
MediaType contentType = MediaType.parse("text/plain");
reqBuilder.post(RequestBody.create(contentType, body));
} else {
FormBody.Builder formBody = new FormBody.Builder();
if (params != null) {
for (Map.Entry<String, Object> entry : params.entrySet()) {
if (entry.getValue() != null)
formBody.add(entry.getKey(), entry.getValue().toString());
}
}
reqBuilder.post(formBody.build());
}
} else
throw new IllegalArgumentException("Unknown HTTP-method: " + mtd);
reqBuilder.url(urlBuilder.build());
try (Response resp = httpClient.newCall(reqBuilder.build()).execute()) {
String content = resp.body().string();
if (resp.isSuccessful()) {
JsonNode node = mapper.readTree(content);
int status = node.get("successStatus").asInt();
switch(status) {
case STATUS_SUCCESS:
return RestResult.success(node.get("response").toString());
default:
return RestResult.fail(status, node.get("error").asText());
}
}
if (resp.code() == 401)
return RestResult.fail(STATUS_AUTH_FAILED, "Failed to authenticate in grid. Please check agent\'s login and password or node port.");
return RestResult.fail(STATUS_FAILED, "Failed connect to node and execute REST command.");
} catch (ConnectException ignore) {
throw new ConnectException("Failed connect to node and execute REST command [url=" + urlBuilder + "]");
}
}
use of okhttp3.Request in project spring-framework by spring-projects.
the class AbstractMockWebServerTestCase method getRequest.
private MockResponse getRequest(RecordedRequest request, byte[] body, String contentType) {
if (request.getMethod().equals("OPTIONS")) {
return new MockResponse().setResponseCode(200).setHeader("Allow", "GET, OPTIONS, HEAD, TRACE");
}
Buffer buf = new Buffer();
buf.write(body);
MockResponse response = new MockResponse().setHeader("Content-Length", body.length).setBody(buf).setResponseCode(200);
if (contentType != null) {
response = response.setHeader("Content-Type", contentType);
}
return response;
}
use of okhttp3.Request in project spring-framework by spring-projects.
the class AbstractMockWebServerTestCase method putRequest.
private MockResponse putRequest(RecordedRequest request, String expectedRequestContent) {
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));
return new MockResponse().setResponseCode(202);
}
use of okhttp3.Request 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);
}
Aggregations