dart_swagger_to_api_client 1.1.1
dart_swagger_to_api_client: ^1.1.1 copied to clipboard
Generate type-safe HTTP API clients from OpenAPI/Swagger specs for Dart and Flutter.
dart_swagger_to_api_client #
Generate type-safe HTTP API clients from OpenAPI/Swagger specifications
dart_swagger_to_api_client is a code generator that creates fully type-safe, production-ready HTTP clients for Dart and Flutter applications. It works seamlessly with dart_swagger_to_models to generate a complete stack: models + API client.
β¨ Features #
- π― Type-safe API calls β Strongly typed methods generated from OpenAPI specs
- π Multiple HTTP adapters β Support for
http,dio, and custom adapters - π‘οΈ Middleware system β Logging, retries, rate limiting, circuit breakers, and more
- π Flexible authentication β API keys, bearer tokens, environment variables
- π Environment profiles β Easy switching between dev/staging/prod
- π¦ Model integration β Automatic integration with
dart_swagger_to_models - β‘ Watch mode β Auto-regenerate on spec changes
- π CI/CD ready β Templates for GitHub Actions and GitLab CI
- π State management β Examples for Riverpod and BLoC
π Quick Start #
Installation #
Add to your pubspec.yaml:
dev_dependencies:
dart_swagger_to_models: ^0.9.0
dart_swagger_to_api_client: ^1.0.0
Basic Usage #
- Generate models (using
dart_swagger_to_models):
dart run dart_swagger_to_models:dart_swagger_to_models \
--input swagger/api.yaml \
--output-dir lib/models \
--style json_serializable
- Generate API client:
dart run dart_swagger_to_api_client:dart_swagger_to_api_client \
--input swagger/api.yaml \
--output-dir lib/api_client
- Use in your code:
import 'package:my_app/api_client/api_client.dart';
import 'package:my_app/models/user.dart';
final config = ApiClientConfig(
baseUrl: Uri.parse('https://api.example.com'),
auth: AuthConfig(
bearerToken: 'your-token-here',
),
);
final client = ApiClient(config);
try {
// Type-safe API call
final List<User> users = await client.defaultApi.getUsers();
print('Users: $users');
} finally {
await client.close();
}
π Documentation #
- Usage Guide β Complete usage documentation
- Developer Guide β Contributing and development
- Context for AI β Quick context restoration for AI assistants
- Roadmap β Development roadmap (Russian)
π― Key Concepts #
HTTP Adapters #
Choose your HTTP implementation:
// Default: package:http
final config = ApiClientConfig(
baseUrl: Uri.parse('https://api.example.com'),
);
// Dio adapter
import 'package:dio/dio.dart';
final dio = Dio();
final adapter = DioHttpClientAdapter(dio: dio);
final config = ApiClientConfig(
baseUrl: Uri.parse('https://api.example.com'),
httpClientAdapter: adapter,
);
// Custom adapter
class MyCustomAdapter implements HttpClientAdapter {
@override
Future<HttpResponse> send(HttpRequest request) async {
// Your implementation
}
}
Middleware #
Add powerful middleware to your client:
final config = ApiClientConfig(
baseUrl: Uri.parse('https://api.example.com'),
requestInterceptors: [
RateLimitInterceptor(maxRequests: 100, window: Duration(minutes: 1)),
LoggingInterceptor.console(),
],
responseInterceptors: [
RetryInterceptor(maxRetries: 3),
CircuitBreakerInterceptor(failureThreshold: 5),
],
);
Environment Profiles #
Configure different environments:
# dart_swagger_to_api_client.yaml
client:
baseUrl: https://api.example.com
environments:
dev:
baseUrl: https://dev-api.example.com
prod:
baseUrl: https://api.example.com
auth:
bearerTokenEnv: PROD_BEARER_TOKEN
dart run dart_swagger_to_api_client:dart_swagger_to_api_client \
--input swagger/api.yaml \
--output-dir lib/api_client \
--config dart_swagger_to_api_client.yaml \
--env prod
π Examples #
See the example/ directory for complete examples:
complete_example.dartβ Full end-to-end exampleauth_example.dartβ Authentication methodserror_handling_example.dartβ Error handling and retriesmiddleware_example.dartβ Middleware usagecircuit_breaker_example.dartβ Circuit breaker patterntransformer_example.dartβ Request/response transformationsriverpod_integration_example.dartβ Riverpod integrationbloc_integration_example.dartβ BLoC integration
π οΈ CLI Options #
dart run dart_swagger_to_api_client:dart_swagger_to_api_client \
--input swagger/api.yaml \
--output-dir lib/api_client \
--config dart_swagger_to_api_client.yaml \
--env prod \
--watch \
--verbose
Options:
--input/-iβ OpenAPI/Swagger spec path (required)--output-dirβ Output directory (required)--config/-cβ Configuration file path--envβ Environment profile name--watch/-wβ Watch mode for auto-regeneration--verbose/-vβ Verbose output--quiet/-qβ Quiet mode (errors only)--help/-hβ Show help
π Watch Mode #
Automatically regenerate on spec changes:
dart run dart_swagger_to_api_client:dart_swagger_to_api_client \
--input swagger/api.yaml \
--output-dir lib/api_client \
--watch
π€ CI/CD Integration #
Ready-to-use templates for automatic regeneration:
- GitHub Actions β
.github/workflows/regenerate-client.yml - GitLab CI β
.gitlab-ci.yml
See ci/README.md for setup instructions.
π¨ State Management Integration #
Examples for popular state management solutions:
- Riverpod β
example/riverpod_integration_example.dart - BLoC β
example/bloc_integration_example.dart
π Requirements #
- Dart SDK:
^3.11.0 dart_swagger_to_models(for model generation)
π€ Contributing #
Contributions are welcome! Please see DEVELOPERS.md for guidelines.
π License #
MIT License β see LICENSE file for details.
π Related Projects #
dart_swagger_to_modelsβ Generate Dart models from OpenAPI specs
π Support #
- Issues: GitHub Issues
- Documentation: See
doc/directory
Made with β€οΈ for the Dart/Flutter community
