All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ifengxue.http.contract.HttpOperations Maven / Gradle / Ivy

/*
 * Copyright 2019 https://www.ifengxue.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ifengxue.http.contract;

import com.ifengxue.http.annotation.BodyType;
import com.ifengxue.http.annotation.HttpMethod;
import com.ifengxue.http.annotation.ResponseType;
import java.lang.reflect.Type;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * http operations,通过该接口定义的方法可以很容易的发起http请求
 */
public interface HttpOperations {

  // get

  /**
   * 自定义执行get请求并获取json编码的实体
   *
   * @param url 请求地址。如请求地址为https://example.com/foo/bar,则url应为/foo/bar
   * @param type 返回实体类型
   * @param httpHeaders 请求header
   * @param uriVariables 查询参数
   * @return 由type指定的返回值
   */
  default  T getForJsonEntity(@Nonnull String url, @Nonnull Type type, @Nullable Map httpHeaders,
      @Nullable Map uriVariables) {
    return exchange(url, HttpMethod.GET, BodyType.X_WWW_FORM_URLENCODED, ResponseType.APPLICATION_JSON, type,
        httpHeaders, uriVariables, null);
  }

  /**
   * 自定义执行get请求并获取实体
   *
   * @param url 请求地址。如请求地址为https://example.com/foo/bar,则url应为/foo/bar
   * @param bodyType 请求body类型
   * @param responseType response body类型
   * @param type 返回实体类型
   * @param httpHeaders 请求header
   * @param uriVariables 查询参数
   * @return 由type指定的返回值
   */
  default  T getForEntity(@Nonnull String url, @Nonnull BodyType bodyType, @Nonnull ResponseType responseType,
      @Nonnull Type type, @Nullable Map httpHeaders,
      @Nullable Map uriVariables) {
    return exchange(url, HttpMethod.GET, bodyType, responseType, type, httpHeaders, uriVariables, null);
  }

  /**
   * 自定义执行get请求并获取json编码的实体
   *
   * @param url 请求地址。如请求地址为https://example.com/foo/bar,则url应为/foo/bar
   * @param responseEntityClass 返回实体类
   * @param httpHeaders 请求header
   * @param uriVariables 查询参数
   * @return 由type指定的返回值
   */
  default  T getForJsonEntity(@Nonnull String url, @Nonnull Class responseEntityClass,
      @Nullable Map httpHeaders,
      @Nullable Map uriVariables) {
    return exchange(url, HttpMethod.GET, BodyType.X_WWW_FORM_URLENCODED, ResponseType.APPLICATION_JSON,
        responseEntityClass,
        httpHeaders, uriVariables, null);
  }

  /**
   * 自定义执行get请求并获取实体
   *
   * @param url 请求地址。如请求地址为https://example.com/foo/bar,则url应为/foo/bar
   * @param bodyType 请求body类型
   * @param responseType response body类型
   * @param responseEntityClass 返回实体类
   * @param httpHeaders 请求header
   * @param uriVariables 查询参数
   * @return 由responseEntityClass指定的返回值
   */
  default  T getForEntity(@Nonnull String url, @Nonnull BodyType bodyType, @Nonnull ResponseType responseType,
      @Nonnull Class responseEntityClass, @Nullable Map httpHeaders,
      @Nullable Map uriVariables) {
    return exchange(url, HttpMethod.GET, bodyType, responseType, (Type) responseEntityClass, httpHeaders, uriVariables,
        null);
  }

  /**
   * 自定义执行get请求并获取实体
   *
   * @param url 请求地址。如请求地址为https://example.com/foo/bar,则url应为/foo/bar
   * @param bodyType 请求body类型
   * @param responseType response body类型
   * @param typeReference 返回实体类型
   * @param httpHeaders 请求header
   * @param uriVariables 查询参数
   * @return 由typeReference指定的返回值
   */
  default  T getForEntity(@Nonnull String url, @Nonnull BodyType bodyType, @Nonnull ResponseType responseType,
      @Nonnull TypeReference typeReference, @Nullable Map httpHeaders,
      @Nullable Map uriVariables) {
    return exchange(url, HttpMethod.GET, bodyType, responseType, typeReference.getType(), httpHeaders, uriVariables,
        null);
  }

  // post

  /**
   * 自定义执行post请求并获取json实体
   *
   * @param url 请求地址。如请求地址为https://example.com/foo/bar,则url应为/foo/bar
   * @param type 返回实体类型
   * @param httpHeaders 请求header
   * @param uriVariables 查询参数
   * @return 由type指定的返回值
   */
  default  T postForJsonEntity(@Nonnull String url, @Nonnull Type type, @Nullable Map httpHeaders,
      @Nullable Map uriVariables, @Nullable Object requestBody) {
    return exchange(url, HttpMethod.POST, BodyType.APPLICATION_JSON, ResponseType.APPLICATION_JSON, type, httpHeaders,
        uriVariables, requestBody);
  }

  /**
   * 自定义执行post请求并获取实体
   *
   * @param url 请求地址。如请求地址为https://example.com/foo/bar,则url应为/foo/bar
   * @param bodyType 请求body类型
   * @param responseType response body类型
   * @param type 返回实体类型
   * @param httpHeaders 请求header
   * @param uriVariables 查询参数
   * @return 由type指定的返回值
   */
  default  T postForEntity(@Nonnull String url, @Nonnull BodyType bodyType, @Nonnull ResponseType responseType,
      @Nonnull Type type, @Nullable Map httpHeaders,
      @Nullable Map uriVariables, @Nullable Object requestBody) {
    return exchange(url, HttpMethod.POST, bodyType, responseType, type, httpHeaders, uriVariables, requestBody);
  }

  /**
   * 自定义执行post请求并获取json实体
   *
   * @param url 请求地址。如请求地址为https://example.com/foo/bar,则url应为/foo/bar
   * @param responseEntityClass 返回实体类
   * @param httpHeaders 请求header
   * @param uriVariables 查询参数
   * @return 由type指定的返回值
   */
  default  T postForJsonEntity(@Nonnull String url, @Nonnull Class responseEntityClass,
      @Nullable Map httpHeaders,
      @Nullable Map uriVariables, @Nullable Object requestBody) {
    return exchange(url, HttpMethod.POST, BodyType.APPLICATION_JSON, ResponseType.APPLICATION_JSON, responseEntityClass,
        httpHeaders, uriVariables, requestBody);
  }

  /**
   * 自定义执行post请求并获取实体
   *
   * @param url 请求地址。如请求地址为https://example.com/foo/bar,则url应为/foo/bar
   * @param bodyType 请求body类型
   * @param responseType response body类型
   * @param responseEntityClass 返回实体类
   * @param httpHeaders 请求header
   * @param uriVariables 查询参数
   * @return 由responseEntityClass指定的返回值
   */
  default  T postForEntity(@Nonnull String url, @Nonnull BodyType bodyType, @Nonnull ResponseType responseType,
      @Nonnull Class responseEntityClass, @Nullable Map httpHeaders,
      @Nullable Map uriVariables, @Nullable Object requestBody) {
    return exchange(url, HttpMethod.POST, bodyType, responseType, (Type) responseEntityClass, httpHeaders, uriVariables,
        requestBody);
  }

  /**
   * 自定义执行post请求并获取实体
   *
   * @param url 请求地址。如请求地址为https://example.com/foo/bar,则url应为/foo/bar
   * @param bodyType 请求body类型
   * @param responseType response body类型
   * @param typeReference 返回实体类型
   * @param httpHeaders 请求header
   * @param uriVariables 查询参数
   * @return 由typeReference指定的返回值
   */
  default  T postForEntity(@Nonnull String url, @Nonnull BodyType bodyType, @Nonnull ResponseType responseType,
      @Nonnull TypeReference typeReference, @Nullable Map httpHeaders,
      @Nullable Map uriVariables, @Nullable Object requestBody) {
    return exchange(url, HttpMethod.POST, bodyType, responseType, typeReference.getType(), httpHeaders, uriVariables,
        requestBody);
  }

  // exchange

  /**
   * 自定义执行http请求
   *
   * @param url 请求地址。如请求地址为https://example.com/foo/bar,则url应为/foo/bar
   * @param method 请求方法。如GET, POST等
   * @param bodyType 请求body类型
   * @param responseType response body类型
   * @param responseEntityClass 返回实体类
   * @param httpHeaders 请求header
   * @param uriVariables 查询参数
   * @param requestBody 请求body
   * @return 由responseEntityClass指定的返回值
   */
  default  T exchange(@Nonnull String url, @Nonnull HttpMethod method, @Nonnull BodyType bodyType,
      @Nonnull ResponseType responseType, @Nonnull Class responseEntityClass,
      @Nullable Map httpHeaders, @Nullable Map uriVariables,
      @Nullable Object requestBody) {
    return exchange(url, method, bodyType, responseType, (Type) responseEntityClass,
        httpHeaders, uriVariables, requestBody);
  }

  /**
   * 自定义执行http请求
   *
   * @param url 请求地址。如请求地址为https://example.com/foo/bar,则url应为/foo/bar
   * @param method 请求方法。如GET, POST等
   * @param bodyType 请求body类型
   * @param responseType response body类型
   * @param typeReference 返回实体类型
   * @param httpHeaders 请求header
   * @param uriVariables 查询参数
   * @param requestBody 请求body
   * @return 由typeReference指定的返回值
   */
  default  T exchange(@Nonnull String url, @Nonnull HttpMethod method, @Nonnull BodyType bodyType,
      @Nonnull ResponseType responseType, @Nonnull TypeReference typeReference,
      @Nullable Map httpHeaders, @Nullable Map uriVariables,
      @Nullable Object requestBody) {
    return exchange(url, method, bodyType, responseType, typeReference.getType(), httpHeaders, uriVariables,
        requestBody);
  }

  /**
   * 自定义执行http请求
   *
   * @param url 请求地址。如请求地址为https://example.com/foo/bar,则url应为/foo/bar
   * @param method 请求方法。如GET, POST等
   * @param bodyType 请求body类型
   * @param responseType response body类型
   * @param responseEntityType 返回实体类型 {@link TypeReference}
   * @param httpHeaders 请求header
   * @param uriVariables 查询参数
   * @param requestBody 请求body
   * @return 由responseEntityType指定的返回值
   */
   T exchange(@Nonnull String url, @Nonnull HttpMethod method, @Nonnull BodyType bodyType,
      @Nonnull ResponseType responseType, @Nonnull Type responseEntityType, @Nullable Map httpHeaders,
      @Nullable Map uriVariables, @Nullable Object requestBody);

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy