use of okhttp3.Request.Builder in project crnk-framework by crnk-project.
the class OkHttpBraveIntegration method onBuild.
@Override
public void onBuild(OkHttpClient.Builder builder) {
Interceptor interceptor = TracingInterceptor.create(httpTracing);
builder.addNetworkInterceptor(interceptor);
builder.dispatcher(new Dispatcher(httpTracing.tracing().currentTraceContext().executorService(new Dispatcher().executorService())));
}
use of okhttp3.Request.Builder in project crnk-framework by crnk-project.
the class QuerySpecClientTest method testUpdate.
public void testUpdate(boolean pushAlways) {
final List<String> methods = new ArrayList<>();
final List<String> paths = new ArrayList<>();
final Interceptor interceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
methods.add(request.method());
paths.add(request.url().encodedPath());
return chain.proceed(request);
}
};
HttpAdapter httpAdapter = client.getHttpAdapter();
if (httpAdapter instanceof OkHttpAdapter) {
((OkHttpAdapter) httpAdapter).addListener(new OkHttpAdapterListener() {
@Override
public void onBuild(Builder builder) {
builder.addInterceptor(interceptor);
}
});
}
Task task = new Task();
task.setId(1L);
task.setName("test");
taskRepo.create(task);
Task savedTask = taskRepo.findOne(1L, new QuerySpec(Task.class));
Assert.assertNotNull(savedTask);
// perform update
task.setName("updatedName");
taskRepo.save(task);
// check updated
savedTask = taskRepo.findOne(1L, new QuerySpec(Task.class));
Assert.assertNotNull(savedTask);
Assert.assertEquals("updatedName", task.getName());
if (httpAdapter instanceof OkHttpAdapter) {
// check HTTP handling
Assert.assertEquals(4, methods.size());
Assert.assertEquals(4, paths.size());
Assert.assertEquals("POST", methods.get(0));
Assert.assertEquals("GET", methods.get(1));
if (pushAlways) {
Assert.assertEquals("POST", methods.get(2));
Assert.assertEquals("/tasks", paths.get(2));
} else {
Assert.assertEquals("PATCH", methods.get(2));
Assert.assertEquals("/tasks/1", paths.get(2));
}
Assert.assertEquals("GET", methods.get(3));
Assert.assertEquals("/tasks", paths.get(0));
Assert.assertEquals("/tasks/1", paths.get(1));
Assert.assertEquals("/tasks/1", paths.get(3));
}
}
use of okhttp3.Request.Builder in project crnk-framework by crnk-project.
the class CharsetTest method testUTF8isDefault.
public void testUTF8isDefault(boolean okHttp) throws InstantiationException, IllegalAccessException {
requestContentType = null;
responseContentType = null;
if (okHttp) {
OkHttpAdapter adapter = OkHttpAdapter.newInstance();
adapter.addListener(new OkHttpAdapterListener() {
@Override
public void onBuild(OkHttpClient.Builder builder) {
builder.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
requestContentType = chain.request().header(HttpHeaders.HTTP_CONTENT_TYPE);
Response response = chain.proceed(chain.request());
responseContentType = response.header(HttpHeaders.HTTP_CONTENT_TYPE);
return response;
}
});
}
});
client.setHttpAdapter(adapter);
} else {
HttpClientAdapter adapter = HttpClientAdapter.newInstance();
adapter.addListener(new HttpClientAdapterListener() {
@Override
public void onBuild(HttpClientBuilder builder) {
builder.addInterceptorFirst(new HttpRequestInterceptor() {
@Override
public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException {
Header header = httpRequest.getFirstHeader(HttpHeaders.HTTP_CONTENT_TYPE);
requestContentType = header != null ? header.getValue() : null;
}
});
builder.addInterceptorFirst(new HttpResponseInterceptor() {
@Override
public void process(HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException {
Header header = httpResponse.getFirstHeader(HttpHeaders.HTTP_CONTENT_TYPE);
responseContentType = header != null ? header.getValue() : null;
}
});
}
});
client.setHttpAdapter(adapter);
}
ResourceRepositoryV2<Task, Long> testRepo = client.getRepositoryForType(Task.class);
Task entity = new Task();
entity.setId(1L);
entity.setName("äöüé@¢€");
testRepo.create(entity);
Assert.assertEquals(HttpHeaders.JSONAPI_CONTENT_TYPE_AND_CHARSET, requestContentType);
Assert.assertEquals(HttpHeaders.JSONAPI_CONTENT_TYPE_AND_CHARSET, responseContentType);
Task savedEntity = testRepo.findOne(1L, new QuerySpec(Task.class));
Assert.assertEquals(entity.getName(), savedEntity.getName());
Assert.assertNull(requestContentType);
Assert.assertEquals(HttpHeaders.JSONAPI_CONTENT_TYPE_AND_CHARSET, responseContentType);
}
use of okhttp3.Request.Builder in project butter-android by butterproject.
the class RecommendationContentProvider method openFile.
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
ParcelFileDescriptor[] pipe = null;
String url = uri.getPath();
try {
String decodedUrl = URLDecoder.decode(url.replaceFirst("/", ""), "UTF-8");
pipe = ParcelFileDescriptor.createPipe();
Request request = new Builder().url(new URL(decodedUrl)).build();
Response response = getOkHttpClient().newCall(request).execute();
new TransferThread(response.body().byteStream(), new ParcelFileDescriptor.AutoCloseOutputStream(pipe[1])).start();
} catch (IOException e) {
Log.e(getClass().getSimpleName(), "Exception opening pipe", e);
throw new FileNotFoundException("Could not open pipe for: " + uri.toString());
}
return (pipe[0]);
}
use of okhttp3.Request.Builder in project dhis2-android-sdk by dhis2.
the class FieldsConverterShould method have_correct_retrofit_request_format.
@Test
public void have_correct_retrofit_request_format() throws IOException, InterruptedException {
MockWebServer mockWebServer = new MockWebServer();
mockWebServer.start();
mockWebServer.enqueue(new MockResponse());
Retrofit retrofit = new Retrofit.Builder().baseUrl(mockWebServer.url("/")).addConverterFactory(FieldsConverterFactory.create()).build();
TestService testService = retrofit.create(TestService.class);
testService.test(Fields.<String>builder().fields(Field.<String, String>create("property_one"), Field.<String, String>create("property_two"), NestedField.<String, String>create("nested_property").with(Field.<String, String>create("nested_property_one"))).build()).execute();
RecordedRequest recordedRequest = mockWebServer.takeRequest();
assertThat(recordedRequest.getPath()).isEqualTo("/api/?fields=property_one,property_two,nested_property[nested_property_one]");
mockWebServer.shutdown();
}
Aggregations