|
| 1 | +import 'dart:convert'; |
| 2 | +import '../services/services.dart'; |
| 3 | +import '../../models/models.dart'; |
| 4 | + |
| 5 | +class DocumentationFeature { |
| 6 | + final DashBotService _service; |
| 7 | + |
| 8 | + DocumentationFeature(this._service); |
| 9 | + |
| 10 | + Future<String> generateApiDocumentation({ |
| 11 | + required RequestModel? requestModel, |
| 12 | + required dynamic responseModel, |
| 13 | + }) async { |
| 14 | + if (requestModel == null || responseModel == null) { |
| 15 | + return "No recent API requests found."; |
| 16 | + } |
| 17 | + |
| 18 | + final method = requestModel.httpRequestModel?.method |
| 19 | + .toString() |
| 20 | + .split('.') |
| 21 | + .last |
| 22 | + .toUpperCase() ?? |
| 23 | + "GET"; |
| 24 | + final endpoint = requestModel.httpRequestModel?.url ?? "Unknown Endpoint"; |
| 25 | + final headers = requestModel.httpRequestModel?.enabledHeadersMap ?? {}; |
| 26 | + final parameters = requestModel.httpRequestModel?.enabledParamsMap ?? {}; |
| 27 | + final body = requestModel.httpRequestModel?.body; |
| 28 | + final rawResponse = responseModel.body; |
| 29 | + final responseBody = |
| 30 | + rawResponse is String ? rawResponse : jsonEncode(rawResponse); |
| 31 | + final statusCode = responseModel.statusCode ?? 0; |
| 32 | + |
| 33 | + final prompt = """ |
| 34 | +API DOCUMENTATION GENERATION |
| 35 | +
|
| 36 | +**API Details:** |
| 37 | +- Endpoint: $endpoint |
| 38 | +- Method: $method |
| 39 | +- Status Code: $statusCode |
| 40 | +
|
| 41 | +**Request Components:** |
| 42 | +- Headers: ${headers.isNotEmpty ? jsonEncode(headers) : "None"} |
| 43 | +- Query Parameters: ${parameters.isNotEmpty ? jsonEncode(parameters) : "None"} |
| 44 | +- Request Body: ${body != null && body.isNotEmpty ? body : "None"} |
| 45 | +
|
| 46 | +**Response Example:** |
| 47 | +``` |
| 48 | +$responseBody |
| 49 | +``` |
| 50 | +
|
| 51 | +**Documentation Instructions:** |
| 52 | +Create comprehensive API documentation that includes: |
| 53 | +
|
| 54 | +1. **Overview**: A clear, concise description of what this API endpoint does |
| 55 | +2. **Authentication**: Required authentication method based on headers |
| 56 | +3. **Request Details**: All required and optional parameters with descriptions |
| 57 | +4. **Response Structure**: Breakdown of response fields and their meanings |
| 58 | +5. **Error Handling**: Possible error codes and troubleshooting |
| 59 | +6. **Example Usage**: A complete code example showing how to call this API |
| 60 | +
|
| 61 | +Format in clean markdown with proper sections and code blocks where appropriate. |
| 62 | +"""; |
| 63 | + |
| 64 | + return _service.generateResponse(prompt); |
| 65 | + } |
| 66 | +} |
0 commit comments