pylaag-openapi 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pylaag_openapi-0.1.0/.gitignore +56 -0
- pylaag_openapi-0.1.0/LICENSE +21 -0
- pylaag_openapi-0.1.0/PKG-INFO +332 -0
- pylaag_openapi-0.1.0/README.md +304 -0
- pylaag_openapi-0.1.0/pyproject.toml +56 -0
- pylaag_openapi-0.1.0/src/pylaag_openapi/__init__.py +22 -0
- pylaag_openapi-0.1.0/src/pylaag_openapi/codegen.py +367 -0
- pylaag_openapi-0.1.0/src/pylaag_openapi/components.py +126 -0
- pylaag_openapi-0.1.0/src/pylaag_openapi/curl.py +115 -0
- pylaag_openapi-0.1.0/src/pylaag_openapi/document.py +153 -0
- pylaag_openapi-0.1.0/src/pylaag_openapi/paths.py +113 -0
- pylaag_openapi-0.1.0/src/pylaag_openapi/py.typed +0 -0
- pylaag_openapi-0.1.0/src/pylaag_openapi/samples.py +227 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
*.so
|
|
6
|
+
.Python
|
|
7
|
+
build/
|
|
8
|
+
develop-eggs/
|
|
9
|
+
dist/
|
|
10
|
+
downloads/
|
|
11
|
+
eggs/
|
|
12
|
+
.eggs/
|
|
13
|
+
lib/
|
|
14
|
+
lib64/
|
|
15
|
+
parts/
|
|
16
|
+
sdist/
|
|
17
|
+
var/
|
|
18
|
+
wheels/
|
|
19
|
+
*.egg-info/
|
|
20
|
+
.installed.cfg
|
|
21
|
+
*.egg
|
|
22
|
+
|
|
23
|
+
# Virtual environments
|
|
24
|
+
venv/
|
|
25
|
+
ENV/
|
|
26
|
+
env/
|
|
27
|
+
.venv
|
|
28
|
+
|
|
29
|
+
# uv
|
|
30
|
+
.uv/
|
|
31
|
+
|
|
32
|
+
# Testing
|
|
33
|
+
.pytest_cache/
|
|
34
|
+
.coverage
|
|
35
|
+
htmlcov/
|
|
36
|
+
.hypothesis/
|
|
37
|
+
|
|
38
|
+
# Type checking
|
|
39
|
+
.mypy_cache/
|
|
40
|
+
.dmypy.json
|
|
41
|
+
dmypy.json
|
|
42
|
+
|
|
43
|
+
# IDEs
|
|
44
|
+
.vscode/
|
|
45
|
+
.idea/
|
|
46
|
+
*.swp
|
|
47
|
+
*.swo
|
|
48
|
+
*~
|
|
49
|
+
|
|
50
|
+
# OS
|
|
51
|
+
.DS_Store
|
|
52
|
+
Thumbs.db
|
|
53
|
+
|
|
54
|
+
# Build artifacts
|
|
55
|
+
*.whl
|
|
56
|
+
*.tar.gz
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 B Schwarz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pylaag-openapi
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: OpenAPI/Swagger document manipulation for Python
|
|
5
|
+
Project-URL: Homepage, https://github.com/laag/laag-python
|
|
6
|
+
Project-URL: Repository, https://github.com/laag/laag-python
|
|
7
|
+
Project-URL: Documentation, https://github.com/laag/laag-python#readme
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/laag/laag-python/issues
|
|
9
|
+
Author: Laag Contributors
|
|
10
|
+
License: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: api,api-design,api-documentation,code-generation,openapi,rest,specification,swagger
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Python: >=3.10
|
|
25
|
+
Requires-Dist: pylaag-core>=0.1.0
|
|
26
|
+
Requires-Dist: pyyaml>=6.0.1
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# pylaag-openapi
|
|
30
|
+
|
|
31
|
+
OpenAPI/Swagger document manipulation for Python.
|
|
32
|
+
|
|
33
|
+
## Overview
|
|
34
|
+
|
|
35
|
+
`pylaag-openapi` provides comprehensive support for working with OpenAPI 3.0+ specifications, including:
|
|
36
|
+
|
|
37
|
+
- Document parsing and serialization (JSON and YAML)
|
|
38
|
+
- Path and operation management
|
|
39
|
+
- Component management (schemas, responses, parameters, etc.)
|
|
40
|
+
- Sample data generation from schemas
|
|
41
|
+
- Client code generation (Python, JavaScript, TypeScript)
|
|
42
|
+
- Curl command generation
|
|
43
|
+
|
|
44
|
+
## Installation
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
pip install pylaag-openapi
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
This will automatically install `pylaag-core` and `pyyaml` as dependencies.
|
|
51
|
+
|
|
52
|
+
## Quick Start
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from pylaag_openapi import OpenAPIDocument
|
|
56
|
+
|
|
57
|
+
# Parse an OpenAPI document
|
|
58
|
+
doc = OpenAPIDocument.from_yaml(yaml_content)
|
|
59
|
+
doc.validate()
|
|
60
|
+
|
|
61
|
+
# Access document properties
|
|
62
|
+
print(doc.info['title'])
|
|
63
|
+
print(doc.openapi_version)
|
|
64
|
+
print(doc.paths)
|
|
65
|
+
|
|
66
|
+
# Serialize back to YAML or JSON
|
|
67
|
+
yaml_output = doc.to_yaml()
|
|
68
|
+
json_output = doc.to_json()
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Features
|
|
72
|
+
|
|
73
|
+
### Document Management
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
from pylaag_openapi import OpenAPIDocument
|
|
77
|
+
|
|
78
|
+
# Create a new document
|
|
79
|
+
doc = OpenAPIDocument()
|
|
80
|
+
|
|
81
|
+
# Parse from JSON
|
|
82
|
+
doc = OpenAPIDocument.from_json(json_string)
|
|
83
|
+
|
|
84
|
+
# Parse from YAML
|
|
85
|
+
doc = OpenAPIDocument.from_yaml(yaml_string)
|
|
86
|
+
|
|
87
|
+
# Validate document
|
|
88
|
+
doc.validate() # Raises ValidationError if invalid
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
### Path and Operation Management
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
from pylaag_openapi import OpenAPIDocument, PathManager
|
|
96
|
+
|
|
97
|
+
doc = OpenAPIDocument()
|
|
98
|
+
path_mgr = PathManager(doc)
|
|
99
|
+
|
|
100
|
+
# Add a path
|
|
101
|
+
path_mgr.add_path('/users', {})
|
|
102
|
+
|
|
103
|
+
# Add an operation
|
|
104
|
+
path_mgr.add_operation('/users', 'get', {
|
|
105
|
+
'summary': 'List users',
|
|
106
|
+
'responses': {
|
|
107
|
+
'200': {
|
|
108
|
+
'description': 'Success',
|
|
109
|
+
'content': {
|
|
110
|
+
'application/json': {
|
|
111
|
+
'schema': {
|
|
112
|
+
'type': 'array',
|
|
113
|
+
'items': {'$ref': '#/components/schemas/User'}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
# Get an operation
|
|
122
|
+
operation = path_mgr.get_operation('/users', 'get')
|
|
123
|
+
|
|
124
|
+
# Remove an operation
|
|
125
|
+
path_mgr.remove_operation('/users', 'get')
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Component Management
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
from pylaag_openapi import OpenAPIDocument, ComponentManager
|
|
132
|
+
|
|
133
|
+
doc = OpenAPIDocument()
|
|
134
|
+
comp_mgr = ComponentManager(doc)
|
|
135
|
+
|
|
136
|
+
# Add a schema component
|
|
137
|
+
comp_mgr.add_component('schemas', 'User', {
|
|
138
|
+
'type': 'object',
|
|
139
|
+
'properties': {
|
|
140
|
+
'id': {'type': 'integer'},
|
|
141
|
+
'name': {'type': 'string'},
|
|
142
|
+
'email': {'type': 'string', 'format': 'email'}
|
|
143
|
+
},
|
|
144
|
+
'required': ['id', 'name', 'email']
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
# Get a component
|
|
148
|
+
user_schema = comp_mgr.get_component('schemas', 'User')
|
|
149
|
+
|
|
150
|
+
# Resolve a reference
|
|
151
|
+
resolved = comp_mgr.resolve_reference('#/components/schemas/User')
|
|
152
|
+
|
|
153
|
+
# Remove a component
|
|
154
|
+
comp_mgr.remove_component('schemas', 'User')
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
### Sample Generation
|
|
159
|
+
|
|
160
|
+
```python
|
|
161
|
+
from pylaag_openapi import OpenAPIDocument, SampleGenerator
|
|
162
|
+
|
|
163
|
+
doc = OpenAPIDocument.from_yaml(yaml_content)
|
|
164
|
+
sample_gen = SampleGenerator(doc)
|
|
165
|
+
|
|
166
|
+
# Generate sample from a schema
|
|
167
|
+
schema = {
|
|
168
|
+
'type': 'object',
|
|
169
|
+
'properties': {
|
|
170
|
+
'name': {'type': 'string'},
|
|
171
|
+
'age': {'type': 'integer', 'minimum': 0, 'maximum': 120},
|
|
172
|
+
'email': {'type': 'string', 'format': 'email'}
|
|
173
|
+
},
|
|
174
|
+
'required': ['name', 'email']
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
sample = sample_gen.generate_from_schema(schema)
|
|
178
|
+
# Example output: {'name': 'abc', 'age': 42, 'email': 'user@example.com'}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### Code Generation
|
|
182
|
+
|
|
183
|
+
```python
|
|
184
|
+
from pylaag_openapi import OpenAPIDocument, CodeGenerator
|
|
185
|
+
|
|
186
|
+
doc = OpenAPIDocument.from_yaml(yaml_content)
|
|
187
|
+
code_gen = CodeGenerator(doc)
|
|
188
|
+
|
|
189
|
+
# Generate Python client
|
|
190
|
+
python_code = code_gen.generate_client('python')
|
|
191
|
+
|
|
192
|
+
# Generate JavaScript client
|
|
193
|
+
js_code = code_gen.generate_client('javascript')
|
|
194
|
+
|
|
195
|
+
# Generate TypeScript client
|
|
196
|
+
ts_code = code_gen.generate_client('typescript')
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### Curl Command Generation
|
|
200
|
+
|
|
201
|
+
```python
|
|
202
|
+
from pylaag_openapi import OpenAPIDocument, CurlGenerator
|
|
203
|
+
|
|
204
|
+
doc = OpenAPIDocument.from_yaml(yaml_content)
|
|
205
|
+
curl_gen = CurlGenerator(doc)
|
|
206
|
+
|
|
207
|
+
# Generate curl command for an operation
|
|
208
|
+
curl_cmd = curl_gen.generate_curl(
|
|
209
|
+
path='/users',
|
|
210
|
+
method='post',
|
|
211
|
+
base_url='https://api.example.com',
|
|
212
|
+
include_sample_body=True
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
print(curl_cmd)
|
|
216
|
+
# Output: curl -X POST -H 'Content-Type: application/json' -d '...' 'https://api.example.com/users'
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
### Extension Properties
|
|
221
|
+
|
|
222
|
+
```python
|
|
223
|
+
from pylaag_openapi import OpenAPIDocument
|
|
224
|
+
|
|
225
|
+
doc = OpenAPIDocument()
|
|
226
|
+
|
|
227
|
+
# Set extension property
|
|
228
|
+
doc.set_extension('x-api-id', 'my-api-123')
|
|
229
|
+
|
|
230
|
+
# Get extension property
|
|
231
|
+
api_id = doc.get_extension('x-api-id')
|
|
232
|
+
|
|
233
|
+
# Remove extension property
|
|
234
|
+
doc.remove_extension('x-api-id')
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## API Reference
|
|
238
|
+
|
|
239
|
+
### Classes
|
|
240
|
+
|
|
241
|
+
#### OpenAPIDocument
|
|
242
|
+
|
|
243
|
+
Main class for working with OpenAPI documents.
|
|
244
|
+
|
|
245
|
+
**Class Methods:**
|
|
246
|
+
- `from_json(json_str: str) -> OpenAPIDocument`: Parse from JSON string
|
|
247
|
+
- `from_yaml(yaml_str: str) -> OpenAPIDocument`: Parse from YAML string
|
|
248
|
+
|
|
249
|
+
**Methods:**
|
|
250
|
+
- `validate() -> None`: Validate the document structure
|
|
251
|
+
- `to_json(indent: int = 2) -> str`: Serialize to JSON string
|
|
252
|
+
- `to_yaml() -> str`: Serialize to YAML string
|
|
253
|
+
|
|
254
|
+
**Properties:**
|
|
255
|
+
- `openapi_version: str`: The OpenAPI version
|
|
256
|
+
- `info: Dict[str, Any]`: The info object
|
|
257
|
+
- `paths: Dict[str, Any]`: The paths object
|
|
258
|
+
|
|
259
|
+
#### PathManager
|
|
260
|
+
|
|
261
|
+
Manages paths and operations in an OpenAPI document.
|
|
262
|
+
|
|
263
|
+
**Methods:**
|
|
264
|
+
- `add_path(path: str, path_item: Optional[Dict] = None) -> None`
|
|
265
|
+
- `remove_path(path: str) -> bool`
|
|
266
|
+
- `get_path(path: str) -> Optional[Dict]`
|
|
267
|
+
- `add_operation(path: str, method: HttpMethod, operation: Dict) -> None`
|
|
268
|
+
- `remove_operation(path: str, method: HttpMethod) -> bool`
|
|
269
|
+
- `get_operation(path: str, method: HttpMethod) -> Optional[Dict]`
|
|
270
|
+
|
|
271
|
+
#### ComponentManager
|
|
272
|
+
|
|
273
|
+
Manages reusable components in an OpenAPI document.
|
|
274
|
+
|
|
275
|
+
**Methods:**
|
|
276
|
+
- `add_component(component_type: ComponentType, name: str, component: Dict) -> None`
|
|
277
|
+
- `remove_component(component_type: ComponentType, name: str) -> bool`
|
|
278
|
+
- `get_component(component_type: ComponentType, name: str) -> Optional[Dict]`
|
|
279
|
+
- `resolve_reference(ref: str) -> Optional[Dict]`
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
#### SampleGenerator
|
|
283
|
+
|
|
284
|
+
Generates sample data from OpenAPI schemas.
|
|
285
|
+
|
|
286
|
+
**Methods:**
|
|
287
|
+
- `generate_from_schema(schema: Dict, depth: int = 0, max_depth: int = 5) -> Any`
|
|
288
|
+
|
|
289
|
+
#### CodeGenerator
|
|
290
|
+
|
|
291
|
+
Generates client code from OpenAPI documents.
|
|
292
|
+
|
|
293
|
+
**Methods:**
|
|
294
|
+
- `generate_client(language: Language) -> str`: Generate client code in specified language
|
|
295
|
+
|
|
296
|
+
**Supported Languages:**
|
|
297
|
+
- `'python'`: Python client with requests library
|
|
298
|
+
- `'javascript'`: JavaScript client with fetch API
|
|
299
|
+
- `'typescript'`: TypeScript client with type annotations
|
|
300
|
+
|
|
301
|
+
#### CurlGenerator
|
|
302
|
+
|
|
303
|
+
Generates curl commands from OpenAPI operations.
|
|
304
|
+
|
|
305
|
+
**Methods:**
|
|
306
|
+
- `generate_curl(path: str, method: str, base_url: str = 'https://api.example.com', include_sample_body: bool = True) -> str`
|
|
307
|
+
|
|
308
|
+
## Requirements
|
|
309
|
+
|
|
310
|
+
- Python 3.10 or higher
|
|
311
|
+
- pylaag-core >= 0.1.0
|
|
312
|
+
- pyyaml >= 6.0.1
|
|
313
|
+
|
|
314
|
+
## License
|
|
315
|
+
|
|
316
|
+
MIT License - see LICENSE file for details
|
|
317
|
+
|
|
318
|
+
## Related Packages
|
|
319
|
+
|
|
320
|
+
- [pylaag-core](https://pypi.org/project/pylaag-core/) - Core utilities
|
|
321
|
+
- [pylaag-raml](https://pypi.org/project/pylaag-raml/) - RAML support
|
|
322
|
+
- [pylaag-smithy](https://pypi.org/project/pylaag-smithy/) - Smithy support
|
|
323
|
+
|
|
324
|
+
## Contributing
|
|
325
|
+
|
|
326
|
+
Contributions are welcome! Please see the main repository for guidelines.
|
|
327
|
+
|
|
328
|
+
## Links
|
|
329
|
+
|
|
330
|
+
- [GitHub Repository](https://github.com/laag/laag-python)
|
|
331
|
+
- [Issue Tracker](https://github.com/laag/laag-python/issues)
|
|
332
|
+
- [Documentation](https://github.com/laag/laag-python#readme)
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
# pylaag-openapi
|
|
2
|
+
|
|
3
|
+
OpenAPI/Swagger document manipulation for Python.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
`pylaag-openapi` provides comprehensive support for working with OpenAPI 3.0+ specifications, including:
|
|
8
|
+
|
|
9
|
+
- Document parsing and serialization (JSON and YAML)
|
|
10
|
+
- Path and operation management
|
|
11
|
+
- Component management (schemas, responses, parameters, etc.)
|
|
12
|
+
- Sample data generation from schemas
|
|
13
|
+
- Client code generation (Python, JavaScript, TypeScript)
|
|
14
|
+
- Curl command generation
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
pip install pylaag-openapi
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
This will automatically install `pylaag-core` and `pyyaml` as dependencies.
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from pylaag_openapi import OpenAPIDocument
|
|
28
|
+
|
|
29
|
+
# Parse an OpenAPI document
|
|
30
|
+
doc = OpenAPIDocument.from_yaml(yaml_content)
|
|
31
|
+
doc.validate()
|
|
32
|
+
|
|
33
|
+
# Access document properties
|
|
34
|
+
print(doc.info['title'])
|
|
35
|
+
print(doc.openapi_version)
|
|
36
|
+
print(doc.paths)
|
|
37
|
+
|
|
38
|
+
# Serialize back to YAML or JSON
|
|
39
|
+
yaml_output = doc.to_yaml()
|
|
40
|
+
json_output = doc.to_json()
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Features
|
|
44
|
+
|
|
45
|
+
### Document Management
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
from pylaag_openapi import OpenAPIDocument
|
|
49
|
+
|
|
50
|
+
# Create a new document
|
|
51
|
+
doc = OpenAPIDocument()
|
|
52
|
+
|
|
53
|
+
# Parse from JSON
|
|
54
|
+
doc = OpenAPIDocument.from_json(json_string)
|
|
55
|
+
|
|
56
|
+
# Parse from YAML
|
|
57
|
+
doc = OpenAPIDocument.from_yaml(yaml_string)
|
|
58
|
+
|
|
59
|
+
# Validate document
|
|
60
|
+
doc.validate() # Raises ValidationError if invalid
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
### Path and Operation Management
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
from pylaag_openapi import OpenAPIDocument, PathManager
|
|
68
|
+
|
|
69
|
+
doc = OpenAPIDocument()
|
|
70
|
+
path_mgr = PathManager(doc)
|
|
71
|
+
|
|
72
|
+
# Add a path
|
|
73
|
+
path_mgr.add_path('/users', {})
|
|
74
|
+
|
|
75
|
+
# Add an operation
|
|
76
|
+
path_mgr.add_operation('/users', 'get', {
|
|
77
|
+
'summary': 'List users',
|
|
78
|
+
'responses': {
|
|
79
|
+
'200': {
|
|
80
|
+
'description': 'Success',
|
|
81
|
+
'content': {
|
|
82
|
+
'application/json': {
|
|
83
|
+
'schema': {
|
|
84
|
+
'type': 'array',
|
|
85
|
+
'items': {'$ref': '#/components/schemas/User'}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
# Get an operation
|
|
94
|
+
operation = path_mgr.get_operation('/users', 'get')
|
|
95
|
+
|
|
96
|
+
# Remove an operation
|
|
97
|
+
path_mgr.remove_operation('/users', 'get')
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Component Management
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
from pylaag_openapi import OpenAPIDocument, ComponentManager
|
|
104
|
+
|
|
105
|
+
doc = OpenAPIDocument()
|
|
106
|
+
comp_mgr = ComponentManager(doc)
|
|
107
|
+
|
|
108
|
+
# Add a schema component
|
|
109
|
+
comp_mgr.add_component('schemas', 'User', {
|
|
110
|
+
'type': 'object',
|
|
111
|
+
'properties': {
|
|
112
|
+
'id': {'type': 'integer'},
|
|
113
|
+
'name': {'type': 'string'},
|
|
114
|
+
'email': {'type': 'string', 'format': 'email'}
|
|
115
|
+
},
|
|
116
|
+
'required': ['id', 'name', 'email']
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
# Get a component
|
|
120
|
+
user_schema = comp_mgr.get_component('schemas', 'User')
|
|
121
|
+
|
|
122
|
+
# Resolve a reference
|
|
123
|
+
resolved = comp_mgr.resolve_reference('#/components/schemas/User')
|
|
124
|
+
|
|
125
|
+
# Remove a component
|
|
126
|
+
comp_mgr.remove_component('schemas', 'User')
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
### Sample Generation
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
from pylaag_openapi import OpenAPIDocument, SampleGenerator
|
|
134
|
+
|
|
135
|
+
doc = OpenAPIDocument.from_yaml(yaml_content)
|
|
136
|
+
sample_gen = SampleGenerator(doc)
|
|
137
|
+
|
|
138
|
+
# Generate sample from a schema
|
|
139
|
+
schema = {
|
|
140
|
+
'type': 'object',
|
|
141
|
+
'properties': {
|
|
142
|
+
'name': {'type': 'string'},
|
|
143
|
+
'age': {'type': 'integer', 'minimum': 0, 'maximum': 120},
|
|
144
|
+
'email': {'type': 'string', 'format': 'email'}
|
|
145
|
+
},
|
|
146
|
+
'required': ['name', 'email']
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
sample = sample_gen.generate_from_schema(schema)
|
|
150
|
+
# Example output: {'name': 'abc', 'age': 42, 'email': 'user@example.com'}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Code Generation
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
from pylaag_openapi import OpenAPIDocument, CodeGenerator
|
|
157
|
+
|
|
158
|
+
doc = OpenAPIDocument.from_yaml(yaml_content)
|
|
159
|
+
code_gen = CodeGenerator(doc)
|
|
160
|
+
|
|
161
|
+
# Generate Python client
|
|
162
|
+
python_code = code_gen.generate_client('python')
|
|
163
|
+
|
|
164
|
+
# Generate JavaScript client
|
|
165
|
+
js_code = code_gen.generate_client('javascript')
|
|
166
|
+
|
|
167
|
+
# Generate TypeScript client
|
|
168
|
+
ts_code = code_gen.generate_client('typescript')
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Curl Command Generation
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
from pylaag_openapi import OpenAPIDocument, CurlGenerator
|
|
175
|
+
|
|
176
|
+
doc = OpenAPIDocument.from_yaml(yaml_content)
|
|
177
|
+
curl_gen = CurlGenerator(doc)
|
|
178
|
+
|
|
179
|
+
# Generate curl command for an operation
|
|
180
|
+
curl_cmd = curl_gen.generate_curl(
|
|
181
|
+
path='/users',
|
|
182
|
+
method='post',
|
|
183
|
+
base_url='https://api.example.com',
|
|
184
|
+
include_sample_body=True
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
print(curl_cmd)
|
|
188
|
+
# Output: curl -X POST -H 'Content-Type: application/json' -d '...' 'https://api.example.com/users'
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
### Extension Properties
|
|
193
|
+
|
|
194
|
+
```python
|
|
195
|
+
from pylaag_openapi import OpenAPIDocument
|
|
196
|
+
|
|
197
|
+
doc = OpenAPIDocument()
|
|
198
|
+
|
|
199
|
+
# Set extension property
|
|
200
|
+
doc.set_extension('x-api-id', 'my-api-123')
|
|
201
|
+
|
|
202
|
+
# Get extension property
|
|
203
|
+
api_id = doc.get_extension('x-api-id')
|
|
204
|
+
|
|
205
|
+
# Remove extension property
|
|
206
|
+
doc.remove_extension('x-api-id')
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
## API Reference
|
|
210
|
+
|
|
211
|
+
### Classes
|
|
212
|
+
|
|
213
|
+
#### OpenAPIDocument
|
|
214
|
+
|
|
215
|
+
Main class for working with OpenAPI documents.
|
|
216
|
+
|
|
217
|
+
**Class Methods:**
|
|
218
|
+
- `from_json(json_str: str) -> OpenAPIDocument`: Parse from JSON string
|
|
219
|
+
- `from_yaml(yaml_str: str) -> OpenAPIDocument`: Parse from YAML string
|
|
220
|
+
|
|
221
|
+
**Methods:**
|
|
222
|
+
- `validate() -> None`: Validate the document structure
|
|
223
|
+
- `to_json(indent: int = 2) -> str`: Serialize to JSON string
|
|
224
|
+
- `to_yaml() -> str`: Serialize to YAML string
|
|
225
|
+
|
|
226
|
+
**Properties:**
|
|
227
|
+
- `openapi_version: str`: The OpenAPI version
|
|
228
|
+
- `info: Dict[str, Any]`: The info object
|
|
229
|
+
- `paths: Dict[str, Any]`: The paths object
|
|
230
|
+
|
|
231
|
+
#### PathManager
|
|
232
|
+
|
|
233
|
+
Manages paths and operations in an OpenAPI document.
|
|
234
|
+
|
|
235
|
+
**Methods:**
|
|
236
|
+
- `add_path(path: str, path_item: Optional[Dict] = None) -> None`
|
|
237
|
+
- `remove_path(path: str) -> bool`
|
|
238
|
+
- `get_path(path: str) -> Optional[Dict]`
|
|
239
|
+
- `add_operation(path: str, method: HttpMethod, operation: Dict) -> None`
|
|
240
|
+
- `remove_operation(path: str, method: HttpMethod) -> bool`
|
|
241
|
+
- `get_operation(path: str, method: HttpMethod) -> Optional[Dict]`
|
|
242
|
+
|
|
243
|
+
#### ComponentManager
|
|
244
|
+
|
|
245
|
+
Manages reusable components in an OpenAPI document.
|
|
246
|
+
|
|
247
|
+
**Methods:**
|
|
248
|
+
- `add_component(component_type: ComponentType, name: str, component: Dict) -> None`
|
|
249
|
+
- `remove_component(component_type: ComponentType, name: str) -> bool`
|
|
250
|
+
- `get_component(component_type: ComponentType, name: str) -> Optional[Dict]`
|
|
251
|
+
- `resolve_reference(ref: str) -> Optional[Dict]`
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
#### SampleGenerator
|
|
255
|
+
|
|
256
|
+
Generates sample data from OpenAPI schemas.
|
|
257
|
+
|
|
258
|
+
**Methods:**
|
|
259
|
+
- `generate_from_schema(schema: Dict, depth: int = 0, max_depth: int = 5) -> Any`
|
|
260
|
+
|
|
261
|
+
#### CodeGenerator
|
|
262
|
+
|
|
263
|
+
Generates client code from OpenAPI documents.
|
|
264
|
+
|
|
265
|
+
**Methods:**
|
|
266
|
+
- `generate_client(language: Language) -> str`: Generate client code in specified language
|
|
267
|
+
|
|
268
|
+
**Supported Languages:**
|
|
269
|
+
- `'python'`: Python client with requests library
|
|
270
|
+
- `'javascript'`: JavaScript client with fetch API
|
|
271
|
+
- `'typescript'`: TypeScript client with type annotations
|
|
272
|
+
|
|
273
|
+
#### CurlGenerator
|
|
274
|
+
|
|
275
|
+
Generates curl commands from OpenAPI operations.
|
|
276
|
+
|
|
277
|
+
**Methods:**
|
|
278
|
+
- `generate_curl(path: str, method: str, base_url: str = 'https://api.example.com', include_sample_body: bool = True) -> str`
|
|
279
|
+
|
|
280
|
+
## Requirements
|
|
281
|
+
|
|
282
|
+
- Python 3.10 or higher
|
|
283
|
+
- pylaag-core >= 0.1.0
|
|
284
|
+
- pyyaml >= 6.0.1
|
|
285
|
+
|
|
286
|
+
## License
|
|
287
|
+
|
|
288
|
+
MIT License - see LICENSE file for details
|
|
289
|
+
|
|
290
|
+
## Related Packages
|
|
291
|
+
|
|
292
|
+
- [pylaag-core](https://pypi.org/project/pylaag-core/) - Core utilities
|
|
293
|
+
- [pylaag-raml](https://pypi.org/project/pylaag-raml/) - RAML support
|
|
294
|
+
- [pylaag-smithy](https://pypi.org/project/pylaag-smithy/) - Smithy support
|
|
295
|
+
|
|
296
|
+
## Contributing
|
|
297
|
+
|
|
298
|
+
Contributions are welcome! Please see the main repository for guidelines.
|
|
299
|
+
|
|
300
|
+
## Links
|
|
301
|
+
|
|
302
|
+
- [GitHub Repository](https://github.com/laag/laag-python)
|
|
303
|
+
- [Issue Tracker](https://github.com/laag/laag-python/issues)
|
|
304
|
+
- [Documentation](https://github.com/laag/laag-python#readme)
|