|
10 | 10 | ) |
11 | 11 |
|
12 | 12 |
|
13 | | -def _generate_docstring_with_examples( |
14 | | - endpoint: dict, file_info: dict, request_body_type: str | None |
15 | | -) -> str: |
16 | | - """Generate docstring with usage examples for endpoints.""" |
17 | | - base_docs = endpoint.get("description", endpoint.get("summary", "")) |
| 13 | +def _generate_docstring_with_examples(endpoint: dict, file_info: dict) -> str: |
| 14 | + """Reuse the generated endpoint example for multipart method documentation.""" |
| 15 | + docs = endpoint.get("description", endpoint.get("summary", "")) |
| 16 | + example = endpoint.get("x-python", {}).get("example") |
| 17 | + if file_info.get("has_json_body_multipart") and example: |
| 18 | + docs += f"\n\nExamples:\n```python\n{example}\n```" |
18 | 19 | # Preserve literal backslashes when the docs are embedded in Python source. |
19 | | - base_docs = base_docs.replace("\\", "\\\\") |
20 | | - |
21 | | - # Add examples for JSON + multipart endpoints |
22 | | - if file_info.get("has_json_body_multipart", False): |
23 | | - examples = f""" |
24 | | -
|
25 | | -Examples: |
26 | | - Basic usage with file attachments: |
27 | | - |
28 | | - ```python |
29 | | - from pathlib import Path |
30 | | - from kittycad.models.{camel_to_snake(request_body_type) if request_body_type else "text_to_cad_multi_file_iteration_body"} import {request_body_type or "TextToCadMultiFileIterationBody"} |
31 | | - |
32 | | - # Create the request body |
33 | | - body = {request_body_type or "TextToCadMultiFileIterationBody"}( |
34 | | - # Add your parameters here |
35 | | - ) |
36 | | - |
37 | | - # Prepare file attachments |
38 | | - file_attachments = {{ |
39 | | - "main.kcl": Path("path/to/main.kcl"), |
40 | | - "helper.kcl": Path("path/to/helper.kcl"), |
41 | | - }} |
42 | | - |
43 | | - # Make the request |
44 | | - result = client.{camel_to_snake(endpoint.get("operationId", ""))}( |
45 | | - body=body, |
46 | | - file_attachments=file_attachments, |
47 | | - ) |
48 | | - ``` |
49 | | - |
50 | | - Using different file types: |
51 | | - |
52 | | - ```python |
53 | | - from io import BytesIO |
54 | | - |
55 | | - # Mix of file paths and file-like objects |
56 | | - file_attachments = {{ |
57 | | - "main.kcl": Path("main.kcl"), |
58 | | - "config.kcl": BytesIO(b"// KCL configuration"), |
59 | | - "data.json": "path/to/data.json", |
60 | | - }} |
61 | | - |
62 | | - result = client.{camel_to_snake(endpoint.get("operationId", ""))}( |
63 | | - body=body, |
64 | | - file_attachments=file_attachments, |
65 | | - ) |
66 | | - ``` |
67 | | - """ |
68 | | - return base_docs + examples |
69 | | - |
70 | | - return base_docs |
| 20 | + return docs.replace("\\", "\\\\") |
71 | 21 |
|
72 | 22 |
|
73 | 23 | def generate_sync_function(path: str, method: str, endpoint: dict, data: dict) -> str: |
@@ -158,9 +108,7 @@ def generate_sync_function(path: str, method: str, endpoint: dict, data: dict) - |
158 | 108 | "has_request_body": has_body_param, |
159 | 109 | "request_body_type": request_body_type, |
160 | 110 | "args": args, |
161 | | - "docs": _generate_docstring_with_examples( |
162 | | - endpoint, file_info, request_body_type |
163 | | - ), |
| 111 | + "docs": _generate_docstring_with_examples(endpoint, file_info), |
164 | 112 | "file_info": file_info, # Add file operation info to context |
165 | 113 | } |
166 | 114 |
|
@@ -262,9 +210,7 @@ def generate_async_function(path: str, method: str, endpoint: dict, data: dict) |
262 | 210 | "has_request_body": has_body_param, |
263 | 211 | "request_body_type": request_body_type, |
264 | 212 | "args": args, |
265 | | - "docs": _generate_docstring_with_examples( |
266 | | - endpoint, file_info, request_body_type |
267 | | - ), |
| 213 | + "docs": _generate_docstring_with_examples(endpoint, file_info), |
268 | 214 | "file_info": file_info, # Add file operation info to context |
269 | 215 | } |
270 | 216 |
|
|
0 commit comments