Skip to content

HTTP Client Plugin

The HTTP plugin allows you to register a global HTTP client that can be used to make HTTP requests. Velvet provides a base class VelvetHttp, this plugin provides a concrete implementation of this class.

Installation

To install the HTTP plugin, run the following command:

bash
flutter pub add http_client_velvet_plugin

Update the main.dart file to include the plugin:

dart
import 'package:http_client_velvet_plugin/http_client_velvet_plugin.dart';  

void main() {
  createVelvetApp()
    ..withPlugins((pluginManager) {  
        pluginManager.addPlugin(HttpClientVelvetPlugin()); 
    })  
    ..run();
}

Add in your .env file the following configuration:

dotenv
HTTP_CLIENT_BASE_URL=

Usage

To use the HTTP client, you can inject it into your class:

dart
void someMethod() {
 container.httpClient.request(/** ... */);
}

Configuring the HTTP Client

The plugin is only responsible for creating the HTTP client and provide an extension to the container to access it.

If you need to configure the HTTP client, you can do so by chaining a withBoot method in the main.dart file or creating another plugin if you want to separate the concerns.

dart
import 'package:http_client_velvet_plugin/http_client_velvet_plugin.dart';  

void main() {
  createVelvetApp()
    ..withPlugins((pluginManager) {
        pluginManager.addPlugin(HttpClientVelvetPlugin());
    })
    ..withBoot(() {  
      container.httpClient.dioInstance.addInterceptor(YourInterceptor());  
    });  
    ..run();
}

or creating a new plugin:

dart
import 'package:http_client_velvet_plugin/http_client_velvet_plugin.dart';
import 'package:velvet_framework/velvet_framework.dart';

class MyHttpClientPlugin extends VelvetPlugin {
  @override
  void boot() {
    container.httpClient.dioInstance.addInterceptor(YourInterceptor());
  }
}

Released under the MIT License.