@Test
 public void interceptorDoesAddUserAgent() throws Exception {
   Request request = new Request.Builder().url("https://test.desk.com").build();
   assertFalse(doesHaveUserAgent(request));
   OkHttpClient.Builder builder = new OkHttpClient.Builder();
   builder.interceptors().add(userAgentInterceptor);
   Call call = builder.build().newCall(request);
   Response response = call.execute();
   assertTrue(doesHaveUserAgent(response.request()));
   assertEquals(TEST_USER_AGENT, response.request().header(HEADER_USER_AGENT));
 }
  @Override
  public synchronized Request authenticate(Route route, final Response response)
      throws IOException {
    logger.warn(response.toString());

    final AuthResponse currentAuth = loginPrefs.getCurrentAuth();
    if (null == currentAuth || null == currentAuth.refresh_token) {
      return null;
    }

    String errorCode = getErrorCode(response.peekBody(200).string());

    if (errorCode != null) {
      switch (errorCode) {
        case TOKEN_EXPIRED_ERROR_MESSAGE:
          final AuthResponse refreshedAuth;
          try {
            refreshedAuth = refreshAccessToken(currentAuth);
          } catch (HttpResponseStatusException e) {
            return null;
          }
          return response
              .request()
              .newBuilder()
              .header("Authorization", refreshedAuth.token_type + " " + refreshedAuth.access_token)
              .build();
        case TOKEN_NONEXISTENT_ERROR_MESSAGE:
        case TOKEN_INVALID_GRANT_ERROR_MESSAGE:
          // Retry request with the current access_token if the original access_token used in
          // request does not match the current access_token. This case can occur when
          // asynchronous calls are made and are attempting to refresh the access_token where
          // one call succeeds but the other fails. https://github.com/edx/edx-app-android/pull/834
          if (!response
              .request()
              .headers()
              .get("Authorization")
              .split(" ")[1]
              .equals(currentAuth.access_token)) {
            return response
                .request()
                .newBuilder()
                .header("Authorization", currentAuth.token_type + " " + currentAuth.access_token)
                .build();
          }
      }
    }
    return null;
  }
  @Override
  public Response intercept(Chain chain) throws IOException {

    Request request = chain.request();

    long t1 = System.nanoTime();
    L.i(
        TAG,
        "Sending request="
            + request.url()
            + "   connection="
            + chain.connection()
            + "    head="
            + request.headers()
            + " request=");

    Response response = chain.proceed(request);
    long t2 = System.nanoTime();
    L.i(
        TAG,
        "Received response="
            + response.request().url()
            + "   connect time="
            + ((t2 - t1) / 1e6d)
            + "    head="
            + response.headers()
            + "    tostring"
            + response.toString());

    return response;
  }
Exemplo n.º 4
0
  @Override
  public Response intercept(Interceptor.Chain chain) throws IOException {
    Request request = chain.request();

    long t1 = System.nanoTime();
    Log.v(
        "CS_SDK",
        String.format(
            "Sending request %s on %s%n%s", request.url(), chain.connection(), request.headers()));

    Response response = chain.proceed(request);

    long t2 = System.nanoTime();
    Log.v(
        "CS_SDK",
        String.format(
            "Received response for %s in %.1fms%n%s",
            response.request().url(), (t2 - t1) / 1e6d, response.headers()));

    return response;
  }
 @Override
 public String getUri() {
   return response == null ? null : response.request().url().toString();
 }