File tree Expand file tree Collapse file tree 7 files changed +67
-10
lines changed
Expand file tree Collapse file tree 7 files changed +67
-10
lines changed Original file line number Diff line number Diff line change @@ -36,6 +36,7 @@ pip install swagger-coverage
3636``` dotenv
3737API_DOCS_TYPE="swagger" # Note: "openapi" is default type of API docs
3838API_DOCS_VERSION="2.0" # Note: "3.0.0" is default version of API docs
39+ API_DOCS_FORMAT="yaml" # Note: "json" is default format of API docs and output files
3940```
4041
4142### 3. Add the session-scoped fixture
@@ -79,6 +80,23 @@ def setup_swagger_coverage():
7980 reporter2.generate_report()
8081```
8182
83+ #### YAML format is also supported:
84+
85+ ``` python
86+ import pytest
87+ from swagger_coverage_py.reporter import CoverageReporter
88+
89+
90+ @pytest.fixture (scope = " session" , autouse = True )
91+ def setup_swagger_coverage ():
92+ reporter = CoverageReporter(api_name = " petstore" , host = " https://petstore.swagger.io" )
93+ reporter.cleanup_input_files()
94+ reporter.setup(" /v2/swagger.yaml" )
95+
96+ yield
97+ reporter.generate_report()
98+ ```
99+
82100> #### Steps and Parameters:
83101> ` api_name ` - Define the name of the API. This name will be used to find a configuration file.<br >
84102>   ;  ;  ;  ; For APIs in this example the files must
Original file line number Diff line number Diff line change 11requests >= 2.25.1
22Faker >= 6.0.0
3- setuptools >= 53.0.0
3+ setuptools >= 53.0.0
4+ PyYAML ~= 6.0
5+ python-dotenv ~= 0.16.0
6+ rootpath ~= 0.1.1
Original file line number Diff line number Diff line change 1- from setuptools import setup , find_packages
1+ from setuptools import find_packages , setup
22
33with open ("README.md" , "r" ) as fh :
44 long_description = fh .read ()
55
66setup (
77 name = "swagger-coverage" ,
8- version = "2.1.3 " ,
8+ version = "2.2.0 " ,
99 author = "Jamal Zeinalov" ,
1010 author_email = "jamal.zeynalov@gmail.com" ,
1111 description = 'Python adapter for "swagger-coverage" tool' ,
Original file line number Diff line number Diff line change 11import os
22
3+ import rootpath
4+ from dotenv import load_dotenv
5+
6+ load_dotenv (f"{ rootpath .detect ()} /.env" )
7+
38API_DOCS_TYPE = os .environ .get ("API_DOCS_TYPE" , "openapi" ) # "swagger"
49API_DOCS_VERSION = os .environ .get ("API_DOCS_VERSION" , "3.0.0" ) # "2.0"
10+ API_DOCS_FORMAT = os .environ .get ("API_DOCS_FORMAT" , "json" ) # "yaml"
Original file line number Diff line number Diff line change 11import json
22
3- from swagger_coverage_py .configs import API_DOCS_TYPE
3+ import requests
4+ import yaml
45
6+ from swagger_coverage_py .configs import API_DOCS_FORMAT , API_DOCS_TYPE
57
6- def write_api_doc_to_file (file_path : str , api_doc_data : dict ):
8+
9+ def __write_api_doc_to_json (file_path : str , api_doc_data : requests .Response ):
10+ api_doc_data = api_doc_data .json ()
711 if API_DOCS_TYPE == "swagger" and not api_doc_data .get ("swagger" , None ):
812 api_doc_data ["swagger" ] = "2.0"
913
1014 with open (file_path , "w+" ) as file :
1115 file .write (json .dumps (api_doc_data ))
16+
17+
18+ def __write_api_doc_to_yaml (file_path : str , api_doc_data : requests .Response ):
19+ data = yaml .safe_load (str (api_doc_data .text ))
20+
21+ if API_DOCS_TYPE == "swagger" and not data .get ("swagger" , None ):
22+ data ["swagger" ] = "2.0"
23+
24+ with open (file_path , "w+" ) as file :
25+ file .write (yaml .safe_dump (data , indent = 4 , sort_keys = False ))
26+
27+
28+ def write_api_doc_to_file (file_path : str , api_doc_data : requests .Response ):
29+ if API_DOCS_FORMAT == "json" :
30+ __write_api_doc_to_json (file_path , api_doc_data )
31+ else :
32+ __write_api_doc_to_yaml (file_path , api_doc_data )
Original file line number Diff line number Diff line change 66
77import requests
88
9+ from swagger_coverage_py .configs import API_DOCS_FORMAT
910from swagger_coverage_py .docs_writers .api_doc_writer import write_api_doc_to_file
1011
1112
1213class CoverageReporter :
1314 def __init__ (self , api_name : str , host : str ):
1415 self .host = host
15- self .swagger_doc_file = f"swagger-doc-{ api_name } .json "
16+ self .swagger_doc_file = f"swagger-doc-{ api_name } .{ API_DOCS_FORMAT } "
1617 self .output_dir = self .__get_output_dir ()
1718 self .ignore_requests = []
1819 self .swagger_coverage_config = f"swagger-coverage-config-{ api_name } .json"
@@ -40,9 +41,8 @@ def setup(
4041 f"{ response .status_code } { response .request .url } "
4142 f"{ response .content } \n { response .content } "
4243 )
43- swagger_json_data = response .json ()
4444
45- write_api_doc_to_file (self .swagger_doc_file , swagger_json_data )
45+ write_api_doc_to_file (self .swagger_doc_file , response )
4646
4747 def generate_report (self ):
4848 inner_location = "swagger-coverage-commandline/bin/swagger-coverage-commandline"
Original file line number Diff line number Diff line change 33import platform
44import re
55
6+ import yaml
67from faker import Faker
78from requests import Response
89
10+ from swagger_coverage_py .configs import API_DOCS_FORMAT
911from swagger_coverage_py .uri import URI
1012
1113
@@ -64,11 +66,18 @@ def write_schema(self):
6466 ).replace (":" , "_" )
6567 path_ = f"swagger-coverage-output/{ self .__get_output_subdir ()} "
6668 file_path = f"{ path_ } /{ file_name } " .split ("?" )[0 ]
67- file_path = f"{ file_path } ({ rnd } ).json "
69+ file_path = f"{ file_path } ({ rnd } ).{ API_DOCS_FORMAT } "
6870
6971 try :
7072 with open (file_path , "w+" ) as file :
71- file .write (json .dumps (schema_dict , indent = 4 ))
73+ if API_DOCS_FORMAT == "yaml" :
74+ file .write (yaml .safe_dump (schema_dict , indent = 4 , sort_keys = False ))
75+ elif API_DOCS_FORMAT == "json" :
76+ file .write (json .dumps (schema_dict , indent = 4 ))
77+ else :
78+ raise Exception (
79+ f"Unexpected docs format: { API_DOCS_FORMAT } . Valid formats: json, yaml"
80+ )
7281
7382 except FileNotFoundError as e :
7483 system_ = platform .system ()
You can’t perform that action at this time.
0 commit comments