Skip to content

Commit 512d6cf

Browse files
de-shMec-iS
authored andcommitted
Document sample use-cases (#18)
* Add sample usecases of the library to README * Add outputs to samples * Correct README
1 parent 7d86061 commit 512d6cf

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

README.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Currently the library consists of openapi_parser module which helps hydrus parse
55

66
To install the library:
77

8-
```
8+
```bash
99
pip install git+https://github.com/HTTP-APIs/hydra-openapi-parser.git#egg=hydra_openapi_parser
1010
```
1111

@@ -19,3 +19,58 @@ from hydra_openapi_parser import openapi_parser
1919
```
2020

2121
Porting out from hydrus the hydraspecs directory
22+
23+
## Sample use-cases of openapi_parser module
24+
25+
Once the OpenAPI YAML document is assigned into the variable `doc` as a Python dictionary, you can do the following:
26+
- Parse the OpenAPI doc into a HydraDoc
27+
```python3
28+
parsed_dict = openapi_parser.parse(doc)
29+
```
30+
- Generate an empty object
31+
```python3
32+
object = openapi_parser.generate_empty_object()
33+
```
34+
- Test if an endpoint is valid
35+
```python3
36+
path = 'A/B/{id}/C/D'
37+
openapi_parser.valid_endpoint(path)
38+
# False
39+
path = 'A/B/{id}'
40+
openapi_parser.valid_endpoint(path)
41+
# Collection
42+
path = 'A/B/C'
43+
openapi_parser.valid_endpoint(path)
44+
# True
45+
```
46+
- Extract class name from the path
47+
```python3
48+
path = "A/B/C/Pet"
49+
path_list = path.split('/')
50+
openapi_parser.get_class_name(path_list)
51+
# Pet
52+
```
53+
- Fetch data from location
54+
```python3
55+
path = '#/definitions/Order'
56+
path_list = path.split('/')
57+
data = openapi_parser.get_data_at_location(path_list, doc)
58+
# data is of type dict
59+
```
60+
- Remove variables from path
61+
```python3
62+
path = "A/B/C/{id}"
63+
output = openapi_parser.sanitise_path(path)
64+
```
65+
- Identify if an object is a collection
66+
```python3
67+
schema_block = {
68+
'type': 'array',
69+
'items': {
70+
'$ref': '#/definitions/Pet'
71+
}
72+
}
73+
method = "/Pet"
74+
openapi_parser.check_collection(schema_block, method)
75+
# True
76+
```

0 commit comments

Comments
 (0)