Class HttpClientModule

  • All Implemented Interfaces:
    io.vertx.core.Verticle

    public abstract class HttpClientModule
    extends VertxModule
    Module that exposes a set of functions to send different requests to a server. It's created from a HttpClientOptions instance. It's just another verticle that needs to be deployed. You can create as many as you want, with different configurations:
     
          HttpClientOptions server1Options = new HttpClientOptions();
          HttpClientOptions server2Options = new HttpClientOptions();
    
          HttpClientModule  httpServer1Module = new HttpClientModule(server1Options);
          HttpClientModule  httpServer2Module = new HttpClientModule(server2Options);
    
          vertx.deployVerticle(httpServer1Module);
          vertx.deployVerticle(httpServer2Module);
     
     
    Once deployed, you can use the defined functions get, post, put, delete and so on. You can create new verticles and expose them as functions of this module implementing the methods deployRequests() and defineRequests()
    See Also:
    deployRequests(), defineRequests()
    • Field Detail

      • get

        public final λ<GetMessage,​jsonvalues.JsObj> get
        represents a GET request. It takes as input a GetMessage instance and returns a response in a JsObj. The class Resp contains all the lenses and functions to get info from the response and manipulate it
      • post

        public final λ<PostMessage,​jsonvalues.JsObj> post
        represents a POST request. It takes as input a PostMessage instance and returns a response in a JsObj. The class Resp contains all the lenses and functions to get info from the response and manipulate it
      • put

        public final λ<PutMessage,​jsonvalues.JsObj> put
        represents a PUT request. It takes as input a PutMessage instance and returns a response in a JsObj. The class Resp contains all the lenses and functions to get info from the response and manipulate it
      • delete

        public final λ<DeleteMessage,​jsonvalues.JsObj> delete
        represents a DELETE request. It takes as input a DeleteMessage instance and returns a response in a JsObj. The class Resp contains all the lenses and functions to get info from the response and manipulate it
      • head

        public final λ<HeadMessage,​jsonvalues.JsObj> head
        represents a HEAD request. It takes as input a HeadMessage instance and returns a response in a JsObj. The class Resp contains all the lenses and functions to get info from the response and manipulate it
      • options

        public final λ<OptionsMessage,​jsonvalues.JsObj> options
        represents a OPTIONS request. It takes as input a OptionsMessage instance and returns a response in a JsObj. The class Resp contains all the lenses and functions to get info from the response and manipulate it
      • patch

        public final λ<PatchMessage,​jsonvalues.JsObj> patch
        represents a PATCH request. It takes as input a PatchMessage instance and returns a response in a JsObj. The class Resp contains all the lenses and functions to get info from the response and manipulate it
      • trace

        public final λ<TraceMessage,​jsonvalues.JsObj> trace
        represents a TRACE request. It takes as input a TraceMessage instance and returns a response in a JsObj. The class Resp contains all the lenses and functions to get info from the response and manipulate it
      • connect

        public final λ<ConnectMessage,​jsonvalues.JsObj> connect
        represents a CONNECT request. It takes as input a ConnectMessage instance and returns a response in a JsObj. The class Resp contains all the lenses and functions to get info from the response and manipulate it
    • Constructor Detail

      • HttpClientModule

        public HttpClientModule​(io.vertx.core.http.HttpClientOptions options)
    • Method Detail

      • define

        protected void define()
        Description copied from class: VertxModule
        override this method to initialize the instance fields of this class that represent the functions exposed by this module. The VerticleRef instances associated to the Verticles deployed in the VertxModule.deploy() method can be gotten from their addresses with the method VertxModule.getDeployedVerticle(String). A VerticleRef it's just a wrapper around a Verticle to interact with it. You can establish a dialogue with the Verticle using the method VerticleRef.ask(), which returns a function, or just send it messages without waiting for the response with the method VerticleRef.tell(), which returns a consumer. You may be interested in deploying Verticles on the fly to get a greater level of parallelism with the functions Deployer.spawn(Function) or Deployer.spawn(λ).
        
        
             private final String ADD_ONE_ADDRESS = "addOne";
             private final String PRINT_ADDRESS = "print";
        
             public static Function<Integer, Val<Integer>> triple;
             public static Function<Integer, Val<Integer>> addOne;
             public static Consumer<Integer> printNumber;
        
        
             protected void define(){
             addOne = this.<Integer, Integer>getDeployedVerticle(ADD_ONE_ADDRESS).ask();
        
             printNumber = this.<Integer, Void>getDeployedVerticle(PRINT_ADDRESS).tell();
        
             //spawn returns a function that creates a verticle every time it's called. After the value is returned, the
             //the verticle is undeployed
             triple = deployer.spawnFn(i -> i * 3);
             }
             
             
        Specified by:
        define in class VertxModule
      • defineRequests

        protected abstract void defineRequests()
        method to initialize from the deployed verticles in deployRequests() the functions that will be exposed by this module. You can create new functions spawning verticles as well with the method Deployer.spawn(λ) like in the following example:
             
        
             public λ<JsObj, JsObj> post_client;
             public λ<JsObj, JsObj> get_client;
             public λ<JsObj, JsObj> search;
        
             protected void defineRequests(){
        
                 // "create_client" was deployed in deployRequests method
                 this.post_client = this.<JsObj, JsObj>getDeployedVerticle("create_client").ask();
        
                // "get_client" was deployed in deployRequests method
                this.get_client = this.<String, JsObj>getDeployedVerticle("get_client").ask();
        
                // define a λ that will spawn a new verticle every call
                this.search = deployer.spawnλ(term -> get.apply(new GetMessage().host("www.google.com")
                                                                                .uri("/search?q=" + term)
                                                               )
                                             );
             }
        
             
             
        See Also:
        deployRequests()
      • deployRequests

        protected abstract void deployRequests()
        method to deploy new verticles. You can just use the provided functions get, post, put etc exposed by the module and leave this method implementation empty. On the other hand you may be interested in creating new ones out of them. For example, to deploy two verticles listening on the addresses post_client and search:
        
             protected void deployRequests(){
        
                  λ<JsObj, JsObj> post_client = body -> post.apply(new PostMessage(body.serialize()));
        
                  this.deployFn("post_client",
                                 post_customer
                               );
        
                  λ<String, JsObj> get_client = id -> get.apply(new GetMessage().uri("/" + id));
        
                  this.deployFn("get_client",
                                 get_client
                                );
             }
             
             
        See Also:
        defineRequests()