Git Product home page Git Product logo

remote-http's Introduction

简易版HTTP远程调用

  1. 定义一个接口
@RemoteApi("https://console.tim.qq.com/v4/sns")
public interface ImRemoteMethod {

    @RemoteMethod("/friend_update?usersig=xxx&identifier=admin&sdkappid=88888888&random=99999999&contenttype=json")
    Map get(@Param("random") int random);


    @RemoteMethod(value = "/friend_update?usersig=xxx&identifier=admin&sdkappid=88888888&random=99999999&contenttype=json",
                method = RequestMethod.POST, dataType = RequestDataType.APPLICATION_FORM_URLENCODED)
    Map post(@RequestBody Map map, @Param("test2") String test2);
}
  1. 调用该方法
public class RemoteMethodInvokeTest {

    @Test
    public void generate() {
        ImRemoteMethod imRemoteMethod = RemoteApiManager.generateInstance(ImRemoteMethod.class);
        Map response = imRemoteMethod.get(111);
        System.out.println(JsonUtils.format(response));

        response = imRemoteMethod.post(Collections.singletonMap("test", "test2"), "hello");
        System.out.println(JsonUtils.format(response));
    }
}
  1. 输出结果
{"ActionStatus":"FAIL","ErrorCode":70032,"ErrorInfo":""}
{"ActionStatus":"FAIL","ErrorCode":70032,"ErrorInfo":""}
  1. 通过注解设置Header
@RemoteMethod(value = "https://console.tim.qq.com/v4/sns/friend_update?usersig=xxx&identifier=admin&sdkappid=88888888&random=99999999&contenttype=json", method = RequestMethod.POST)
@Header(name = "test", value = "test2")
Map post(@RequestBody Map map, @Param("test2") String test2);
  • 通过Parameter Annotation 的形式添加请求头
@RemoteMethod(value = "https://console.tim.qq.com/v4/sns/friend_update?usersig=xxx&identifier=admin&sdkappid=88888888&random=99999999&contenttype=json", method = RequestMethod.POST)
@Header(name = "test", value = "test2")
Map post(@RequestBody Map map, @Param("test2") String test2, @Header(name = "test") int headerValue);
  1. 通过提供对应的Handler设置参数
public class PreHanderTest implements RemotePreHandler {

    @Override
    public void handler(RequestHandler requestHandler) {
        requestHandler.addHeader(Long.toString(System.currentTimeMillis()), "testHeader");
    }

}
@RemoteMethod(value = "https://console.tim.qq.com/v4/sns/friend_update?usersig=xxx&identifier=admin&sdkappid=88888888&random=99999999&contenttype=json", method = RequestMethod.POST)
@PreHandler(PreHanderTest.class)
Map post(@RequestBody Map map, @Param("test2") String test2);
  1. 通过Manager对象设置请求头
RemoteApiManager remoteApiManager = RemoteApiManager.getInstance();
remoteApiManager.addHeader("globalTestHeader", "test12324");

ImRemoteMethod imRemoteMethod = remoteApiManager.generate(ImRemoteMethod.class);

retrofit

TODO

  • 全局容器, 处理请求认证之类的Header, 不需要handler每次去设置.
  • 接口定义对应的Domain注解, 方法中对应uri即刻
  • JSON 改成对应的 resolve
  • 参数里面加上Header注解, Map post(@RequestBody Map map, @Param("test2") String test2, @Header(name="xxx") String xxx);

remote-http's People

Contributors

alanviast avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.