fromService static method

Future<ApiDefinition?> fromService(
  1. dynamic service, {
  2. required String baseUrl,
  3. String? title,
  4. String? description,
})

Extracts API definitions from a Retrofit service instance.

This method analyzes the runtime type and attempts to extract API definition information from the service class.

service - An instance of a Retrofit service baseUrl - The base URL for the API (required since we can't always extract it) title - Optional title for the API service

Example:

final apiService = ApiService(dio);
final definition = await RetrofitInspector.fromService(
  apiService,
  baseUrl: 'https://api.example.com',
  title: 'My API',
);

Implementation

static Future<ApiDefinition?> fromService(
  dynamic service, {
  required String baseUrl,
  String? title,
  String? description,
}) async {
  try {
    final serviceType = service.runtimeType;
    final serviceName = serviceType.toString();

    // For now, return a basic definition with the service info
    // In a real implementation, we would analyze the service class
    return ApiDefinition(
      title: title ?? serviceName,
      description: description ?? 'Auto-generated from $serviceName',
      version: '1.0.0',
      services: [
        ApiService(
          name: serviceName,
          baseUrl: baseUrl,
          description: description,
          endpoints: [], // Will be populated by analysis
        ),
      ],
    );
  } catch (e) {
    // Error inspecting service: $e
    debugPrint('Error inspecting service: $e');
    return null;
  }
}