The global HTTP client in our Flutter application is constructed using the Dio library. It serves as a centralized system for managing HTTP requests and responses, streamlining network communication throughout the app. Below is an in-depth look at its functionality and extensibility.

Core Features

The HttpClient class is responsible for setting up and configuring instances of the Dio client. It defines common HTTP methods such as get, post, put, and delete, providing a unified interface for network operations. Its key features include:
  • Automatic Token Injection: The client integrates an interceptor that automatically appends the JWT (JSON Web Token) to the headers of every outgoing request. This ensures that all requests are authenticated without requiring manual header management.
  • Error Handling and Token Refresh: The client includes interceptors for response handling. If a response returns an UNAUTHORIZED status, it attempts to refresh the token using HttpAuthClient. If the token refresh is successful, the original request is retried with the new token.
  • Retry Mechanism: A separate Dio instance, _retryDio, is used for retrying requests after a token refresh. This helps to avoid infinite loops in the case where a request continuously fails.

Extending the Global HTTP Client

The global HTTP client is designed for easy extensibility. For instance, if you need to add additional headers to every request, you can modify the interceptor responsible for token injection. Here’s how you can extend it:
onRequest: (RequestOptions options, RequestInterceptorHandler handler) async {
  _currentToken = await StorageHelper.getToken();
  _currentResource = await StorageHelper.getResource();

  options.headers['Authorization'] = 'Bearer $_currentToken';
  options.headers['gymo_resource'] = _currentResource;
  // Add any additional headers here
  options.headers["Custom-Header"] = "Custom Value";

  return handler.next(options);
},
In this example, a custom header is added in the onRequest interceptor. This ensures that the additional header is included in every HTTP request made through the client.

Automatic Token Handling

The global HTTP client in Flutter provides a smooth experience in managing authentication tokens:
  • Automatic Token Injection: Every HTTP request made through the client automatically includes the access token in the authorization header. This is handled by the onRequest> interceptor, ensuring that all outgoing requests are authenticated without additional effort from the developer.
  • Automatic Token Refresh: The client is adept at handling UNAUTHORIZED responses. If a request fails due to an expired token, the client uses HttpAuthClient to obtain a fresh token and retries the original request. This process is transparent to the rest of the application.
By utilizing this global HTTP client, developers can offload the repetitive tasks of token management. This allows them to focus on implementing the core business logic of their application, resulting in more robust and maintainable code.