rest_api_impl
A Dart package providing a robust REST API service implementation, including HTTP request handling, CRUD operations, and file management.
Features
- 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
Installation
Add the following dependency in your pubspec.yaml file:
dependencies:
rest_api_impl: latest_version
Usage
Register Services
You 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",
),
);
Make API Calls
CRUD Operations
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');
File Upload
final imageService = sl<IImageServiceRestApiDataSource>();
final file = File('path/to/image.png');
imageService.uploadFile(
file: file,
endPoint: 'upload/image',
);
Configuration
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";
}
Contributing
Contributions are welcome! Please submit issues or pull requests on GitHub.
License
This project is licensed under the MIT License.
Libraries
- config/dependency_injection/register_rest_api_data_source_service_get_it_di
- constants/rest_api_constants
- data/data_sources/http_patch_method
- data/data_sources/http_post_methods
- data/data_sources/http_put_methods
- data/data_sources/i_data_sources/i_http_methods
- data/data_sources/i_data_sources/i_rest_api_crud_service
- data/data_sources/i_data_sources/i_rest_api_data_sender
- data/data_sources/image_service/i_image_service/i_image_service_rest_api_data_source
- data/data_sources/image_service/image_service_rest_api_data_source_http_impl
- data/data_sources/rest_api_crud_service_http_client_impl
- data/data_sources/rest_api_crud_service_rest_api_data_sender_impl
- data/data_sources/rest_api_data_sender_dio_impl
- data/data_sources/rest_api_data_sender_http_impl
- data/data_sources/rest_api_data_sender_http_methods_impl
- data/enums/method
- data/models/boolean_response_model
- data/models/map_response_model
- data/models/response_model
- domain/entity/boolean_response_entity
- domain/entity/map_response_entity
- domain/entity/response_entity
- enums/rest_api_client_type
- exceptions/exception_handler/rest_api_exception_handler
- exceptions/exceptions_impl/common_exceptions_impl
- extenstions/http_response_extensions
- extenstions/http_status_code_extenstions
- rest_api_impl
- utils/default_rest_api_config
- utils/header_provider_impl
- utils/http_response_data_parser
- utils/http_status_code_handler
- utils/i_utils/i_header_provider
- utils/i_utils/i_parse_response_data
- utils/i_utils/i_response_handler
- utils/i_utils/i_rest_api_config
- utils/i_utils/i_url_generator
- utils/parse_http_response_data
- utils/response_handler_dio_impl
- utils/response_handler_http_impl
- utils/url_generator_rest_api_impl