Пример #1
0
 private void parseCache(Request request, T object, String string, String mimeType)
     throws UnsupportedEncodingException {
   com.squareup.okhttp.CacheControl cacheControl = request.cacheControl();
   if (cacheControl != null) {
     if (!cacheControl.noCache() && !cacheControl.noStore()) {
       if (object instanceof EAResult) {
         EAResult kResult = (EAResult) object;
         if (kResult != null && kResult.isSuccess()) {
           long now = System.currentTimeMillis();
           long maxAge = cacheControl.maxAgeSeconds();
           long softExpire = now + maxAge * 1000;
           System.out.println("缓存时长:" + (softExpire - now) / 1000 + "秒");
           Cache.Entry entry = new Cache.Entry();
           entry.softTtl = softExpire;
           entry.ttl = entry.softTtl;
           // entry.serverDate = serverDate;
           // entry.responseHeaders = headers;
           entry.mimeType = mimeType;
           System.out.println("request.cacheControl()==" + request.cacheControl());
           entry.data = string.getBytes(UTF8);
           cache.put(request.urlString(), entry);
         }
       }
     }
   }
 }
Пример #2
0
 @Test
 public void put() {
   Request request = fromArgs("-X", "PUT", "http://example.com").createRequest();
   assertEquals("PUT", request.method());
   assertEquals("http://example.com", request.urlString());
   assertNull(request.body());
 }
Пример #3
0
 @Test
 public void userAgent() {
   Request request = fromArgs("-A", "foo", "http://example.com").createRequest();
   assertEquals("GET", request.method());
   assertEquals("http://example.com", request.urlString());
   assertEquals("foo", request.header("User-Agent"));
   assertNull(request.body());
 }
Пример #4
0
 @Test
 public void referer() {
   Request request = fromArgs("-e", "foo", "http://example.com").createRequest();
   assertEquals("GET", request.method());
   assertEquals("http://example.com", request.urlString());
   assertEquals("foo", request.header("Referer"));
   assertNull(request.body());
 }
Пример #5
0
 @Test
 public void dataPut() {
   Request request = fromArgs("-d", "foo", "-X", "PUT", "http://example.com").createRequest();
   Request.Body body = request.body();
   assertEquals("PUT", request.method());
   assertEquals("http://example.com", request.urlString());
   assertEquals("application/x-form-urlencoded; charset=utf-8", body.contentType().toString());
   assertEquals("foo", bodyAsString(body));
 }
  KubernetesClientException requestException(Request request, Exception e) {
    StringBuilder sb = new StringBuilder();
    sb.append("Error executing: ")
        .append(request.method())
        .append(" at: ")
        .append(request.urlString())
        .append(". Cause: ")
        .append(e.getMessage());

    return new KubernetesClientException(sb.toString(), e);
  }
Пример #7
0
 @Test
 public void contentTypeHeader() {
   Request request =
       fromArgs("-d", "foo", "-H", "Content-Type: application/json", "http://example.com")
           .createRequest();
   Request.Body body = request.body();
   assertEquals("POST", request.method());
   assertEquals("http://example.com", request.urlString());
   assertEquals("application/json; charset=utf-8", body.contentType().toString());
   assertEquals("foo", bodyAsString(body));
 }
Пример #8
0
 public JsonElement get(JsonObject object, String memberName) throws IOException {
   JsonElement element = object.get(memberName);
   if (element == null) {
     JsonObject links = object.getAsJsonObject("links");
     if (links != null && links.has(memberName)) {
       Request request =
           new Request.Builder()
               .url(HATCHET_BASE_URL + links.get(memberName).getAsString())
               .build();
       Log.d(TAG, "following link: " + request.urlString());
       Response response = mOkHttpClient.newCall(request).execute();
       if (!response.isSuccessful()) {
         throw new IOException(
             "API request with URL '"
                 + request.urlString()
                 + "' not successful. Code was "
                 + response.code());
       }
       element = GsonHelper.get().fromJson(response.body().charStream(), JsonElement.class);
     }
   }
   return element;
 }
Пример #9
0
 /**
  * @param context
  * @param bundle
  */
 public AbstractBaseOkHttp(@NonNull Context context, @NonNull Bundle bundle) {
   this.context = context;
   this.bundle = bundle;
   printLog = new PrintLog();
   printLog.setRequestTime(
       TimeUtils.getCurrentTimeInString(new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒")));
   request = getRequest();
   printLog.setUrl(request.urlString());
   printLog.setHeaders(
       new StringBuilder()
           .append(CONTENT_TYPE_KEY)
           .append("=")
           .append(request.header(CONTENT_TYPE_KEY))
           .toString());
   execute(getOkHttpClient(), getRequest(), getResponseCallBack());
 }
 private void logRequest(Request request, Response response, String body) throws IOException {
   if (Config.LOGS_ENABLED) {
     String requestString = request.method() + " " + request.urlString();
     if (!request.method().toLowerCase().equals("get")) {
       Buffer buffer = new Buffer();
       request.body().writeTo(buffer);
       requestString += " " + buffer.readUtf8();
     }
     if (response.code() >= 400) {
       LogHelper.d(TAG, "request failed " + requestString);
     } else {
       LogHelper.d(TAG, "request success " + requestString);
     }
     LogHelper.i(TAG, "response = " + response.code() + "  " + body);
   }
 }
 @Override
 public Response intercept(Chain chain) throws IOException {
   Request request = chain.request();
   Optional<String> appAuthHeader = Optional.fromNullable(request.header(X_API_KEY_HEADER));
   if (appAuthHeader.isPresent()) {
     final Request.Builder newRequest = request.newBuilder();
     final String today = getTodayDate();
     newRequest.addHeader(DATE_HEADER, today);
     Signature signature =
         getSigner()
             .sign(
                 request.method(),
                 request.urlString(),
                 Collections.singletonMap(DATE_HEADER, today));
     newRequest.addHeader(AUTHORIZATION_HEADER, signature.toString());
     request = newRequest.build();
   }
   return chain.proceed(request);
 }
  KubernetesClientException requestFailure(Request request, Status status) {
    StringBuilder sb = new StringBuilder();
    sb.append("Failure executing: ")
        .append(request.method())
        .append(" at: ")
        .append(request.urlString())
        .append(". Received status: ")
        .append(status)
        .append(".");

    if (status.getMessage() != null && !status.getMessage().isEmpty()) {
      sb.append(" Message: ").append(status.getMessage()).append(".");
    }

    if (status.getReason() != null && !status.getReason().isEmpty()) {
      sb.append(" Reason: ").append(status.getReason()).append(".");
    }

    return new KubernetesClientException(sb.toString(), status.getCode(), status);
  }
 @Override
 public String getRequestUrl() {
   return request.urlString();
 }
Пример #14
0
 @Override
 public String url() {
   return mRequest.urlString();
 }