List of usage examples for com.squareup.okhttp Request urlString
public String urlString()
From source file:com.navercorp.pinpoint.plugin.okhttp.v2.OkHttpClientRequestAdaptor.java
License:Apache License
@Override public String getUrl(Request request) { return request.urlString(); }
From source file:com.parse.ParseOkHttpClient.java
License:Open Source License
private ParseHttpRequest getParseHttpRequest(Request okHttpRequest) { ParseHttpRequest.Builder parseRequestBuilder = new ParseHttpRequest.Builder(); // Set method switch (okHttpRequest.method()) { case OKHTTP_GET: parseRequestBuilder.setMethod(ParseHttpRequest.Method.GET); break;/*from w w w . ja v a2 s.c o m*/ case OKHTTP_DELETE: parseRequestBuilder.setMethod(ParseHttpRequest.Method.DELETE); break; case OKHTTP_POST: parseRequestBuilder.setMethod(ParseHttpRequest.Method.POST); break; case OKHTTP_PUT: parseRequestBuilder.setMethod(ParseHttpRequest.Method.PUT); break; default: // This should never happen throw new IllegalArgumentException("Invalid http method " + okHttpRequest.method()); } // Set url parseRequestBuilder.setUrl(okHttpRequest.urlString()); // Set Header for (Map.Entry<String, List<String>> entry : okHttpRequest.headers().toMultimap().entrySet()) { parseRequestBuilder.addHeader(entry.getKey(), entry.getValue().get(0)); } // Set Body ParseOkHttpRequestBody okHttpBody = (ParseOkHttpRequestBody) okHttpRequest.body(); if (okHttpBody != null) { parseRequestBuilder.setBody(okHttpBody.getParseHttpBody()); } return parseRequestBuilder.build(); }
From source file:com.parse.ParseOkHttpClientTest.java
License:Open Source License
@Test public void testGetOkHttpRequest() throws IOException { Map<String, String> headers = new HashMap<>(); String headerName = "name"; String headerValue = "value"; headers.put(headerName, headerValue); String url = "http://www.parse.com/"; String content = "test"; int contentLength = content.length(); String contentType = "application/json"; ParseHttpRequest parseRequest = new ParseHttpRequest.Builder().setUrl(url) .setMethod(ParseHttpRequest.Method.POST).setBody(new ParseByteArrayHttpBody(content, contentType)) .setHeaders(headers).build(); ParseOkHttpClient parseClient = new ParseOkHttpClient(10000, null); Request okHttpRequest = parseClient.getRequest(parseRequest); // Verify method assertEquals(ParseHttpRequest.Method.POST.toString(), okHttpRequest.method()); // Verify URL assertEquals(url, okHttpRequest.urlString()); // Verify Headers assertEquals(1, okHttpRequest.headers(headerName).size()); assertEquals(headerValue, okHttpRequest.headers(headerName).get(0)); // Verify Body RequestBody okHttpBody = okHttpRequest.body(); assertEquals(contentLength, okHttpBody.contentLength()); assertEquals(contentType, okHttpBody.contentType().toString()); // Can not read parseRequest body to compare since it has been read during // creating okHttpRequest Buffer buffer = new Buffer(); okHttpBody.writeTo(buffer);/*from ww w. j a v a2 s .c om*/ ByteArrayOutputStream output = new ByteArrayOutputStream(); buffer.copyTo(output); assertArrayEquals(content.getBytes(), output.toByteArray()); }
From source file:com.shopify.buy.data.MockResponder.java
License:Open Source License
@Override public Response intercept(Interceptor.Chain chain) throws IOException { Request request = chain.request(); String key = currentTestName.get() + '_' + Integer.toString(currentTestRequestIndex.getAndIncrement()); JsonElement element = data.get(key); JsonObject responseJson = element.getAsJsonObject(); int code = responseJson.get(KEY_CODE).getAsInt(); String message = responseJson.get(KEY_MESSAGE).getAsString(); String body = responseJson.get(KEY_BODY).getAsString(); if (code >= 400) { retrofit.client.Response retrofitResponse = new retrofit.client.Response(request.urlString(), code, "reason", Collections.EMPTY_LIST, new TypedString(body)); throw RetrofitError.httpError(request.urlString(), retrofitResponse, null, null); }//from ww w.j av a2 s . c o m MediaType contentType = MediaType.parse("application/json; charset=utf-8"); return new Response.Builder().protocol(Protocol.HTTP_1_1).code(code).request(request) .body(ResponseBody.create(contentType, body)).message(message).addHeader("key", "value").build(); }
From source file:com.shopify.buy.dataprovider.BuyClient.java
License:Open Source License
/** * Post a credit card to Shopify's card server and associate it with a Checkout * * @param card the {@link CreditCard} to associate * @param checkout the {@link Checkout} to associate the card with * @param callback the {@link Callback} that will be used to indicate the response from the asynchronous network operation, not null */// w ww.j av a 2 s . c o m public void storeCreditCard(final CreditCard card, final Checkout checkout, final Callback<Checkout> callback) { if (card == null) { throw new NullPointerException("card cannot be null"); } if (checkout == null) { throw new NullPointerException("checkout cannot be null"); } new Thread(new Runnable() { @Override public void run() { PaymentSessionCheckoutWrapper dataWrapper = new PaymentSessionCheckoutWrapper(); PaymentSessionCheckout data = new PaymentSessionCheckout(); data.setToken(checkout.getToken()); data.setCreditCard(card); data.setBillingAddress(checkout.getBillingAddress()); dataWrapper.setCheckout(data); RequestBody body = RequestBody.create(jsonMediateType, new Gson().toJson(dataWrapper)); Request request = new Request.Builder().url(checkout.getPaymentUrl()).post(body) .addHeader("Authorization", "Basic " + Base64.encodeToString(apiKey.getBytes(), Base64.NO_WRAP)) .addHeader("Content-Type", "application/json").addHeader("Accept", "application/json") .build(); try { com.squareup.okhttp.Response httpResponse = httpClient.newCall(request).execute(); String paymentSessionId = parsePaymentSessionResponse(httpResponse); checkout.setPaymentSessionId(paymentSessionId); Response retrofitResponse = new Response(request.urlString(), httpResponse.code(), httpResponse.message(), Collections.<Header>emptyList(), null); callback.success(checkout, retrofitResponse); } catch (IOException e) { e.printStackTrace(); callback.failure(RetrofitError.unexpectedError(request.urlString(), e)); } } }).start(); }
From source file:com.tomtom.camera.api.BanditOkHttpClient.java
License:Apache License
private boolean isDownloadRequest(@NonNull Request request) { return request.urlString().contains("/contents"); }
From source file:com.xing.api.CallSpecTest.java
License:Apache License
@Test public void builderAcceptsPathParams() throws Exception { CallSpec.Builder builder = builder(HttpMethod.GET, "/{param1}/{param2}", false).responseAs(Object.class) .pathParam("param1", "test1").pathParam("param2", "test2"); builder.build();//from w ww. j a va2 s .c om Request request = builder.request(); assertThat(request.method()).isEqualTo(HttpMethod.GET.method()); assertThat(request.urlString()).isEqualTo(httpUrl + "test1/test2"); assertThat(request.body()).isNull(); }
From source file:com.xing.api.CallSpecTest.java
License:Apache License
@Test public void builderAcceptsPathParamsAsVarList() throws Exception { CallSpec.Builder builder = builder(HttpMethod.GET, "/{params}", false).responseAs(Object.class) .pathParam("params", "one", "two", "three"); builder.build();/*from ww w .j av a2s . co m*/ Request request = builder.request(); assertThat(request.method()).isEqualTo(HttpMethod.GET.method()); assertThat(request.urlString()).isEqualTo(httpUrl + "one,two,three"); assertThat(request.body()).isNull(); }
From source file:com.xing.api.CallSpecTest.java
License:Apache License
@Test public void builderAcceptsPathParamsAsList() throws Exception { List<String> params = new ArrayList<>(3); params.add("one"); params.add("two"); params.add("three"); CallSpec.Builder builder = builder(HttpMethod.GET, "/{params}", false).responseAs(Object.class) .pathParam("params", params); builder.build();/*from w w w .j a v a 2s . co m*/ Request request = builder.request(); assertThat(request.method()).isEqualTo(HttpMethod.GET.method()); assertThat(request.urlString()).isEqualTo(httpUrl + "one,two,three"); assertThat(request.body()).isNull(); }
From source file:com.xing.api.CallSpecTest.java
License:Apache License
@Test public void builderAttachesQueryParams() throws Exception { CallSpec.Builder builder = builder(HttpMethod.GET, "", false).responseAs(Object.class).queryParam("q", "test1"); // Build the CallSpec so that we don't test this behaviour twice. builder.build().queryParam("w", "test2"); Request request = builder.request(); assertThat(request.method()).isEqualTo(HttpMethod.GET.method()); assertThat(request.urlString()).isEqualTo(httpUrl + "?q=test1&w=test2"); assertThat(request.body()).isNull(); }