youzan/httpfetch

Name: httpfetch

Owner: ??

Description: ?http?????????????????????????http??

Forked from: wumoyu850921/httpfetch

Created: 2017-09-11 04:35:51.0

Updated: 2018-05-24 02:11:09.0

Pushed: 2018-03-01 15:33:54.0

Homepage:

Size: 341

Language: Java

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

?http????????

??

?????java??http??????????HttpClient?????HttpUrlConnention? ??????????????http???

    HttpClient client = new HttpClient();
    client.getHostConfiguration().setProxy("127.0.0.1", 8888);
    client.getHostConfiguration().setHost("bl.ocks.org", 80, "http");
    GetMethod getMethod = new GetMethod("/mbostock/raw/4090846/us-congress-113.json");
    client.executeMethod(getMethod);
    //??????????
    System.out.println(getMethod.getStatusLine().getStatusCode());
    if(getMethod.getStatusLine().getStatusCode() == 200){
        //??????
        String response = new String(getMethod.getResponseBodyAsString().getBytes("8859_1"));

        //???????
        System.out.println(response);
    }
    getMethod.releaseConnection();

?????????????????????MyBatis????????????????http?????????????????????????????

???HttpFetch?????http?????????

??
clone https://github.com/youzan/httpfetch.git
QuickStart

https://github.com/youzan/httpfetch/wiki/QuickStart

??
??
??
Maven
endency>
<groupId>com.github.youzan</groupId>
<artifactId>http-fetch</artifactId>
<version>1.1.6</version>
pendency>
?spring??

1.??http-api.xml?????

l version="1.0" encoding="UTF-8"?>
ting>

<!-- ????? -->
<chains>
</chains>

<!-- ????? -->
<argumentResolvers>
</argumentResolvers>

<!-- ????? -->
<resultConvertors>
</resultConvertors>

<!-- api?url???? -->
<aliases>
    <alias key="mbostockApi.getUsCongress" value="https://bl.ocks.org/mbostock/raw/4090846/us-congress-113.json" />
</aliases>

tting>

2.??MbostockApi????

age com.github.nezha.httpfetch.mbostock.api;

rt com.github.nezha.httpfetch.HttpApi;
rt com.github.nezha.httpfetch.Header;
rt com.github.nezha.httpfetch.mbostock.vo.UsCongressResponseVo;


reated by daiqiang on 17/3/14.

ic interface MbostockApi {

@HttpApi(timeout = 1000, headers = {@Header(key="user-agent", value = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")})
UsCongressResponseVo getUsCongress();


3.??????

    SourceReader xmlReader = new XmlReader(Arrays.asList("httpapi.xml"));

    HttpApiConfiguration configuration = new HttpApiConfiguration();
    configuration.setSourceReaders(Arrays.asList(xmlReader));
    configuration.init();

    HttpApiService service = new HttpApiService(configuration);
    service.init();

    MbostockApi mbostockApi = service.getOrCreateService(MbostockApi.class);

    UsCongressResponseVo responseVo = mbostockApi.getUsCongress();
    System.out.println("type=="+responseVo.getType());
    System.out.println("arcs->size=="+responseVo.getArcs().size());
    System.out.println("objects->districts->bbox->size=="+responseVo.getObjects().getDistricts().getBbox().size());
    System.out.println("objects->districts->type=="+responseVo.getObjects().getDistricts().getType());
    System.out.println("objects->districts->geometries->size=="+responseVo.getObjects().getDistricts().getGeometries().size());
    System.out.println("transform->scale=="+responseVo.getTransform().getScale());
    System.out.println("transform->translate=="+responseVo.getTransform().getTranslate());

?????spring?????

spring?????

1.??application-httpapi.xml???

l version="1.0" encoding="UTF-8"?>
ns xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
    ">

<bean id="springReader" class="com.github.nezha.httpfetch.spring.SpringReader" >
    <!-- ????? -->
    <property name="chains" >
        <list>
            <bean class="com.github.nezha.httpfetch.bookworm.chains.BookWormTokenChain" />
        </list>
    </property>
    <!-- ????? -->
    <property name="parameterResolvers">
        <list>
        </list>
    </property>
    <!-- ????? -->
    <property name="convertors">
        <list>
        </list>
    </property>
    <!-- api?url???? -->
    <property name="urlAlias">
        <map>
            <entry key="mbostockApi.getUsCongress" value="${mock.host}/mbostock/raw/4090846/us-congress-113.json" />
        </map>
    </property>
</bean>

<bean id="httpApiConfiguration" class="com.github.nezha.httpfetch.HttpApiConfiguration" init-method="init">
    <property name="sourceReaders">
        <list>
            <ref bean="springReader" />
        </list>
    </property>
</bean>

<bean id="httpApiService" class="com.github.nezha.httpfetch.HttpApiService" init-method="init">
    <constructor-arg index="0" ref="httpApiConfiguration" />
</bean>

<!-- http api???? -->
<bean class="com.github.nezha.httpfetch.spring.HttpApiScannerConfigurer">
    <property name="basePackage" value="com.github.nezha.httpfetch.bookworm.api,com.github.nezha.httpfetch.mbostock.api,com.github.nezha.httpfetch.youzan.api" />
</bean>

ans>

2.??MbostockApi????

age com.github.nezha.httpfetch.mbostock.api;

rt com.github.nezha.httpfetch.HttpApi;
rt com.github.nezha.httpfetch.Header;
rt com.github.nezha.httpfetch.mbostock.vo.UsCongressResponseVo;


reated by daiqiang on 17/3/14.

ic interface MbostockApi {

@HttpApi(timeout = 1000, headers = {@Header(key="user-agent", value = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")})
UsCongressResponseVo getUsCongress();


3.??????

ic class MbostockApiTest extends BaseTest {

@Autowired
private MbostockApi mbostockApi;

@Test
public void test(){
    UsCongressResponseVo responseVo = mbostockApi.getUsCongress();
    System.out.println("type=="+responseVo.getType());
    System.out.println("arcs->size=="+responseVo.getArcs().size());
    System.out.println("objects->districts->bbox->size=="+responseVo.getObjects().getDistricts().getBbox().size());
    System.out.println("objects->districts->type=="+responseVo.getObjects().getDistricts().getType());
    System.out.println("objects->districts->geometries->size=="+responseVo.getObjects().getDistricts().getGeometries().size());
    System.out.println("transform->scale=="+responseVo.getTransform().getScale());
    System.out.println("transform->translate=="+responseVo.getTransform().getTranslate());
}


URL??

url???????????

1.??xml?????

<aliases>
    <alias key="mbostockApi.getUsCongress" value="https://bl.ocks.org/mbostock/raw/4090846/us-congress-113.json" />
</aliases>

2.???????

age com.github.nezha.httpfetch.bookworm.api;

rt com.github.nezha.httpfetch.Header;
rt com.github.nezha.httpfetch.HttpApi;
rt com.github.nezha.httpfetch.resolver.RequestBody;

rt java.util.Map;


reated by daiqiang on 17/6/16.

ic interface AlarmJobApi {

@HttpApi(method = "POST", headers = @Header(url = "http://alert.s.qima-inc.com/api/v1/alert", key = "Content-type", value = "application/json"), timeout = 2000)
String alert(@RequestBody Map<String, Object> param);


3.?????????

@HttpApi(timeout = 1000, headers = {@Header(key="user-agent", value = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")})
UsCongressResponseVo getUsCongress(@URL String url);

????

public void test_url_param(){
    String url = "https://bl.ocks.org/mbostock/raw/4090846/us-congress-113.json";
    UsCongressResponseVo responseVo = mbostockApi.getUsCongress(url);
    System.out.println("type=="+responseVo.getType());
    System.out.println("arcs->size=="+responseVo.getArcs().size());
    System.out.println("objects->districts->bbox->size=="+responseVo.getObjects().getDistricts().getBbox().size());
    System.out.println("objects->districts->type=="+responseVo.getObjects().getDistricts().getType());
    System.out.println("objects->districts->geometries->size=="+responseVo.getObjects().getDistricts().getGeometries().size());
    System.out.println("transform->scale=="+responseVo.getTransform().getScale());
    System.out.println("transform->translate=="+responseVo.getTransform().getTranslate());
}
????

1.Get????? ??QueryParam?????????????

@HttpApi(timeout = 2000, url = "http://bookworm365.com/uploadImage")
@BookWormApi
UploadFileResponseVo uploadFile(@QueryParam("name") String name,
                                @QueryParam("n_value") String nValue);

2.Post????? ??PostParam?????????????

audit(@PostParam("advertisementId") Integer advertisementId);

3.Form????? ??FormParam?????????????

@HttpApi(timeout = 2000, url = "http://bookworm365.com/uploadImage")
@BookWormApi
UploadFileResponseVo uploadFile(@FormParam("file") File file,
                                @QueryParam("name") String name,
                                @QueryParam("n_value") String nValue);

4.BeanParam????? ???????bean????????????bean????????http????????????BeanParam???

@HttpApi(timeout = 2000, url = "http://bookworm365.com/uploadImage")
@BookWormApi
UploadFileResponseVo uploadFile(@BeanParam @QueryParam UploadFileRequestVo requestVo);
age com.github.nezha.httpfetch.bookworm.vo;

rt com.alibaba.fastjson.annotation.JSONField;
rt java.io.File;

ic class UploadFileRequestVo {
@JSONField(name = "file")
private File file;
private String name;
@JSONField(name="n_value")
private String nValue;
public File getFile() {return file;}
public void setFile(File file) {this.file = file;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public String getnValue() {return nValue;}
public void setnValue(String nValue) {this.nValue = nValue;}

http???????http://bookworm365.com/uploadImage?file=XXX&name=XXX&n_value=XXX

5.RequestBody????? ??????????????????????? ??????????application\json????

@HttpApi(method = "POST",timeout = 2000,headers = {@Header(key = "Content-type", value = "application/json;charset=UTF-8")})
@WechatApi
WechatBaseResponseVo<AddCustomAudiencesResponseVo> add(@RequestBody AddCustomAudiencesRequestVo requestVo);
????

?????????????JSON??? 1.???????????String?int?long??api???????????????

@HttpApi(timeout = 2000, url = "http://bookworm365.com/checkHeader")
@BookWormApi
String checkHeader();

2.JSON?????????json?????????????bean??????????fastjson???????

@HttpApi(timeout = 1000, headers = {@Header(key="user-agent", value = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")})
UsCongressResponseVo getUsCongress();
java
age com.github.nezha.httpfetch.mbostock.vo;

rt com.alibaba.fastjson.annotation.JSONField;
rt java.util.List;

ic class UsCongressResponseVo {
@JSONField(name="type")
private String type;
@JSONField(name="objects")
private ObjectsVo objects;
@JSONField(name="arcs")
private List<List<List<Integer>>> arcs;
@JSONField(name="transform")
private TransformVo transform;
public String getType() {return type;}
public void setType(String type) {this.type = type;}
public ObjectsVo getObjects() {return objects;}
public void setObjects(ObjectsVo objects) {this.objects = objects;}
public List<List<List<Integer>>> getArcs() {return arcs;}
public void setArcs(List<List<List<Integer>>> arcs) {this.arcs = arcs;}
public TransformVo getTransform() {return transform;}
public void setTransform(TransformVo transform) {this.transform = transform;}

java
age com.github.nezha.httpfetch.mbostock.vo;

rt com.alibaba.fastjson.annotation.JSONField;
rt com.github.nezha.httpfetch.BaseTest;

ic class ObjectsVo {
@JSONField(name="districts")
private DistrictsVo districts;
public DistrictsVo getDistricts() {return districts;}
public void setDistricts(DistrictsVo districts) {this.districts = districts;}

????????????

pApi(timeout = 1000, headers = {@Header(key="user-agent", value = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")})
UsCongressResponseVo<TransformVo> getUsCongress();
????

?????1.2.0????????
HttpApi??????retry?retryPolicy?????
retry??????
retryPolicy?????????ConnectFailureRetryPolicy??????????????

???????

??? ??

???????????RetryPolicy??????needRetry???


????? 
 
ic interface RetryPolicy {  

/** 
 * 
 * @param result http???? 
 * @param retryTimes ???? 
 * @param remainRetryTimes  ?????? 
 * @return 
 */  
boolean needRetry(HttpResult result, int retryTimes, int remainRetryTimes);  


ConnectFailureRetryPolicy?

ic class ConnectFailureRetryPolicy implements RetryPolicy {  

private static final Logger LOGGER = LoggerFactory.getLogger(ConnectFailureRetryPolicy.class);  

/** 
 * ?????????? 
 * @param result http???? 
 * @param retryTimes ???? 
 * @param remainRetryTimes  ?????? 
 * @return 
 */  
@Override  
public boolean needRetry(HttpResult result, int retryTimes, int remainRetryTimes) {  
    Exception e = result.getException();  
    if(e instanceof SocketTimeoutException || e instanceof ConnectException){  
        LOGGER.info("????: {}, ????: {} ????: {}", e, retryTimes, remainRetryTimes);  
        return true;  
    }  
    return false;  
}  


????????????????HttpApi?????retryPolicy???????

??????????test?????

????

????? MIT???????????????

??

?????????????????? [issue] ? [PR]???? [http-fetch] ????


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.