HttpRestClient class

The HttpRestClient class acts as the central hub, coordinating the flow of requests and responses, and allowing for extensibility and customization at various stages through request and response converters, middlewares, and the RequestExecutor class.

When a request is made through the HttpRestClient, the following steps occur:

Request Conversion: The request object is passed through the request converter, which transforms it into the appropriate format for sending over the network. This ensures compatibility with the API endpoint and handles any necessary data conversions.

Request Middlewares: The converted request then goes through a chain of request middlewares. These middlewares allow you to inject custom logic before the request is sent. Examples of request middleware functionalities include authentication, adding headers, or modifying the request payload.

Request Execution: The processed request is passed to the RequestExecutor class, which handles the actual execution of the HTTP request. The RequestExecutor interacts with the network layer, communicates with the API endpoint, and receives the raw response.

Response Middlewares: The response received from the RequestExecutor is then passed through a chain of response middlewares. These middlewares enable you to manipulate and process the response before it is returned to the caller. Common use cases for response middlewares include parsing response data, error handling, or logging.

Response Conversion: After going through the response middlewares, the response is passed to the response converter. The response converter transforms the raw response into a structured format that aligns with your application's needs. This conversion step ensures that the response is in a format that can be easily consumed and understood by your code.

Result Return: Finally, the converted response is returned as the result of the original request made through the HttpRestClient. The caller receives the processed response, which can be further processed or used to update the application's state.

To create an instance of HttpRestClient use the HttpRestClient.builder method and provide a RequestExecutor's implementation, the "HttpRest" library ships with a default implementation DefaultRequestExecutor, you can use it or create your own implementation of RequestExecutor. After calling the HttpRestClient.builder provide request/response converters and middlewares.

Here is an example on how to build a HttpRestClient

final HttpRestClient client =
      HttpRestClient.builder(DefaultRequestExecutor(Client()))
          .addRequestConverter(MapToJsonRequestConverter())
          .addResponseConverter(JsonToMapResponseConverter())
          .addResponseMiddleware(ResponseLogger())
          .addRequestMiddleware(RequestLogger())
          .build();

Library ships with the most required request/response converters such as JSON to Map JsonToMapResponseConverter and Map to JSON MapToJsonRequestConverter converters for more check the HttpRest_converter. There are default logging middlewares for requests and responses which ships with the library, see RequestLogger and ResponseLogger.

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

execute(HttpRestRequest request) Future<HttpRestResponse>
Use this method to execute requests, Receives a single argument of HttpRestRequest, which represents the requests parameters, and returns HttpRestResponse as a result. When you are calling the HttpRestClient.execute method the ordering of the request pipeline is the following,
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

builder(RequestExecutor requestExecutor) HttpRestClientBuilder
This method is the entry point to start create a HttpRestClient, RequestExecutor should be provided as a parameter, which mainly executes the requests, DefaultRequestExecutor can be used for that which handles most of the cases for regular and multipart requests. Returns HttpRestClientBuilder which provide a methods to add middlewares and request/response converters to HttpRestClient instance.