This Python script parses all YAML files in a given directory and generates a summary of the API paths and HTTP methods used in those files. It supports all standard HTTP methods (GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS, TRACE) and provides a count for each method used across all files.
- Python 3.x
pyyaml
library
You can install the required dependencies by running:
pip install -r requirements.txt
To run the script, use the following command:
python yamlAPISpecMerge.py <directory>
Replace <directory>
with the path to the directory containing your YAML API specification files.
/example/api/resource/{id}
- PUT: 1
- GET: 1
/example/api/another-resource/{id}/update
- PUT: 1
Total: PUT: 2 GET: 1
The script will print the summary of paths and methods, as well as the total counts for each method.
- The script imports the necessary modules and defines a list of standard HTTP methods.
- The
parse_yaml_files
function takes a directory path as an argument. - Inside the function, two
defaultdict
objects are initialized:path_summary
andmethod_counts
. Thepath_summary
dictionary will store the method counts for each path, and themethod_counts
dictionary will store the total count for each method. - The code iterates over all files in the directory with a
.yaml
extension. - For each YAML file, it uses
pyyaml.safe_load_all
to load all documents in the file. - For each document, it checks if the 'paths' key exists. If it does, it iterates over the paths and methods.
- For each method, it converts it to uppercase and checks if it's in the
HTTP_METHODS
list. - If the method is valid, it increments the count in
path_summary
for that path and method, and also increments the total count inmethod_counts
. - After processing all files, the code prints the summary by iterating over
path_summary
andHTTP_METHODS
. - Finally, it prints the total counts for each method.