HTTP Client

In this section, you will information on how to configure and use the HTTP client in Beagle Flutter.

Introduction

It defines how the services requests are configured, to use it, you need to create a class that implements a HttpClient abstract class. Beagle Flutter provides a default implementation called DefaultHttpClient. Here is the contract:

abstract class HttpClient {
  Future<Response> sendRequest(BeagleRequest req);
}

In the sendRequest method, you can create the rules for your network layer. You can add headers to your requests, define method request, body response, data response, run cryptography, etc.

Now, let’s see in details what is the parameter and return of this method:

BeagleRequest

It’s the class for configuring HTTP requests.

AttributeTypeRequiredDefinition
urlStringDefines the endpoint that returns the screen or component you wish to display.
methodBeagleHttpMethodIt is an enum class that defines which HTTP operation you wish to do. It works as a HTTP request method and it is set as get by default.
headersMap<String, String>It is used when you need to send data via an HTTP header.
bodyStringIt is set default as null and it just needs to be implemented when you need to send a HTTP messages as body data.

BeagleHttpMethod

Http method to indicate the desired action to be performed for a given resource. It is an enum and the values are:

ValueDefinition
getThe get method requests a representation of a specific resource. Requests using the method get must return only data.
postThe post method is used to submit an entity to a specific resource, frenquetly causing a change in the resource state or colateral effects on the server.
putThe put method replaces all the current representation of the target resources with the data of the request.
deleteThe delete method removes a specific resource.
headThe head method request an answer the same way the get method does, however without a response body.
patchThe patch method is used to apply partial modifications in a resource.

Response

It’s used to return data made by the request.

AttributeTypeRequiredDefinition
statusintReturns the response code returned by the remote HTTP server.
bodyStringResponse body returned from request.
headersMap<String, String>It is used when you need to send data via an HTTP header.
bodyBytesUint8ListReturns the response message returned by the remote HTTP server.

It has a method toJson which returns a String with the status, body and headers encoded.

Create a custom HTTP client

To create your own HTTP client, you only need to implement the sendRequest method, here is the default implementation:

@override
Future<Response> sendRequest(BeagleRequest req) async {
  final uri = Uri.parse(req.url);
  final handlers = {
    BeagleHttpMethod.get: () => 
        http.get(uri, headers: req.headers),
    BeagleHttpMethod.post: () =>
        http.post(uri, headers: req.headers, body: req.body),
    BeagleHttpMethod.put: () =>
        http.put(uri, headers: req.headers, body: req.body),
    BeagleHttpMethod.patch: () =>
        http.patch(uri, headers: req.headers, body: req.body),
    BeagleHttpMethod.delete: () => 
        http.delete(uri, headers: req.headers),
  };
  final response = await handlers[req.method]();
  return Response(
    response.statusCode,
    response.body,
    response.headers,
    response.bodyBytes,
  );
}

Use a custom HTTP client

To use your client, pass it in the Beagle initialization method BeagleSdk.init just like the example below:

BeagleSdk.init(
  httpClient: YourClient(),
);