A Dart package providing a robust REST API service implementation, including HTTP request handling, CRUD operations, and file management.
- Dependency injection-based service registration
- Supports both Dio and Http clients with flexible switching
- HTTP methods: GET, POST, PUT, PATCH, DELETE
- Response handling with error management
- File upload and deletion support
- Configurable base URL and API key
- Abstract classes and solid-principled design for extensibility
Add the following dependency in your pubspec.yaml file:
dependencies:
rest_api_impl: latest_versionYou can register services using either Dio or Http clients:
void main() {
registerRestApiDataSourceServiceGetItDI(
clientType: RestApiClientType.dio, // or http
baseUrl: "https://example.com/api",
apiKey: "your_api_key_here",
);
}Or define static values in constants better security:
void main() {
registerRestApiDataSourceServiceGetItDI(
clientType: RestApiClientType.dio, // or http
baseUrl: RestApiConfigConst.BASE_URL,
apiKey: RestApiConfigConst.API_KEY,
);
}You can also pass custom configurations:
registerRestApiDataSourceServiceGetItDI(
clientType: RestApiClientType.dio,
config: DefaultRestApiConfig(
baseUrl: "https://example.com/api",
apiKey: "your_api_key_here",
),
);final restApiService = sl<IRestApiCrudService>();
// Create Data
restApiService.addData(
endPoint: 'user/create',
data: {'name': 'John Doe', 'email': 'johndoe@example.com'},
);
// Get Data
restApiService.getData(endPoint: 'user/1');final imageService = sl<IImageServiceRestApiDataSource>();
final file = File('path/to/image.png');
imageService.uploadFile(
file: file,
endPoint: 'upload/image',
);Use the DefaultRestApiConfig to configure base URL and API key:
final config = DefaultRestApiConfig(
baseUrl: "https://example.com/api",
apiKey: "your_api_key_here",
);Or define static values in constants:
class RestApiConfigConst {
static const BASE_URL = "https://example.com/api";
static const API_KEY = "your_api_key_here";
}Contributions are welcome! Please submit issues or pull requests on GitHub.
This project is licensed under the MIT License.