specd
Project description
specd
Command-line interface (CLI) for making Swagger 2.0 projects easier to maintain by breaking the paths and definitions out into discrete folders and files in OpenAPI Specification 2.0 format
Installing
Install and update using pip
$ pip install specd --upgrade
specd Components
-
Structure
. ├── definitions │ ├── Foo.yaml │ └── Bar.yaml └── paths ├── foo │ └── {fooId} │ ├── get.yaml │ └── post.yaml ├── etc ... └── etc ...
Paths
The paths and definitions of the specification file get broken out into separate directories. The `paths` directory contains OpenAPI specs for every method that was defined in the original specification file. In this example, the swagger definition for the API's `GET` method that would be found at example URL `hostname.io/foo/{fooId}/`, lives in `paths/foo/{fooId}/get.yaml`Definitions
The definitions of the specd are used as models to populate response fields, request body fields, and really anything else in the Swagger UI that requires a model of an object -
yaml and json Spec Files
All specification files are in the OpenAPI Specification 2.0 format. Below are examples of a
path,definition,.yamlspecification files that correspond with the example directory structure above.get.yaml- pathdescription: Returns a single pet operationId: get_foo_by_fooId produces: - application/json parameters: - description: ID of pet to return format: int32 in: path name: fooId required: true type: integer responses: '200': description: successful operation schema: $ref: '#/definitions/Foo' #Reference to the Foo model in definitions that is returned on response '400': description: Invalid bar supplied '404': description: foo object not found tags: - foo
Foo.yaml- definitionproperties: bar: $ref: "#/definitions/Bar" fooId: format: int32 type: integer data_entries: items: type: string type: array name: type: string title: Foo type: object
Core Features
-
Converting a Specification File to a specd:
convertconverttakes a swagger specification file as input and an output directory as arguments, and creates a specd directory with the followingCommand Options
Command Options Description Default Values -f, --formatspecify the format of the files in the output specd yamljsonoryamlExample
To get the specification file that defines the Swagger Petstore UI at https://petstore.swagger.io/, perform a wget to download the specification.json file, and then perform a convert on it
$ wget "http://petstore.swagger.io/v2/swagger.json" $ specd convert ./swagger.json ~/petstore/
By specifying the output directory to be
~/petstore/, specd will automatically create this directory if it does not already exist, and create aspecsdirectory within it that contains aspecd.yamlfile, apathsdirectory, and adefinitionsdirectory.. ├── petstore │ └── specs │ ├── definitions [not opening dir to save space] │ │ ├── ApiResponse.yaml │ │ ├── Category.yaml │ │ ├── Order.yaml │ │ ├── Pet.yaml │ │ ├── Tag.yaml │ │ └── User.yaml │ ├── paths │ │ ├── pet │ │ │ └── ... [omitting subdirs to save space] │ │ ├── store │ │ │ └── ... [omitting subdirs to save space] │ │ └── user │ │ └── ... [omitting subdirs to save space] │ └── specd.yaml └── swagger.json
-
Generating a Specification File:
generategenerateis the inverse of theconvertcommand. It takes a specd directory and an output file as arguments, and generates a swagger specification file from the path and definition files of the specd directoryCommand Options
Command Options Description Default Values -c, --casespecify if operation names in specification file
should be converted tosnake_caseorcamelCasesnakesnakeorcamelExample
Continuing off of the example from the
convertcommand, we can create a new specification file for the Swagger Petstore API in yaml format based off of our specd directory$ specd generate ~/petstore/specs ~/new_generated_spec.yaml $ cd ~ $ ls petstore/ swagger.json new_generated_spec.yaml
-
Running Swagger:
swaggerswaggerstarts a flask app for Swagger UI, allowing you to view and test your API. This command must be run out of your specd directory in order to function properly.Command Options
Command Options Description Default Values/Type -h,
--hostspecify the host name of the server you
wish to hit with your swagger app.
IfNoneis given, then specd retrieves a
hostname from thespecd.yamlfile within the specd directoryNoneAny str-n,--namespecify name of API NoneAny str-t, --targetspecify the target API endpoints that you wish to be displayed NoneComma separated list of strExample
$ cd ~/petstore/specs $ specd swagger --name="swagger_petstore" * Serving Flask app "specd.app" (lazy loading) * Environment: swagger * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
By simply doing a
CTRL+clickon the URL you receive from the command line, you can access and test your swagger specd.
Additional Utility Commands
-
Comparing Specifications:
diffdifftakes two swagger specification files as arguments, and displays path and definition differences between the twoExample
$ specd diff ~/swagger.json ~/new_generated_spec.yaml
-
List Definitions and Paths:
lslscan be run inside of a specd directory in order to display all definitions and paths for that specExample
$ cd ~/petstore/specs $ specd ls Definitions: ApiResponse Category Order Pet Tag User Paths: /pet/findByStatus: get /pet/findByTags: get /pet/{petId}/uploadImage: post /pet/{petId}: delete, get, post /pet: post, put /store/inventory: get /store/order/{orderId}: delete, get /store/order: post /user/createWithArray: post /user/createWithList: post /user/login: get /user/logout: get /user/{username}: delete, get, put /user: post
-
Linting Path and Definition Files:
lintWhen a specification swagger file is first broken out into definition and paths directories using
convert, the file may have contained fields and types that are not registered with bravado. Although this does not directly affect the API spec's ability to function, logging can become very cluttered, as whenever bravado encounters an unregistered field, it throws a warning message.Running the
lintcommand takes a path to a specd directory as its only argument. When it is executed,lintwill recursively traverse every single path and definition .json/.yaml file in the specd directory and remove any lines that will cause bravado to throw warnings.Example
$ specd lint ~/petstore/specs
-
Validate Specd Directory:
validatevalidatetakes no arguments, and will verify if your current working directory is a valid specd directory$ cd ~/petstore/specs $ specd > Successfully validated.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file specd-0.8.3.tar.gz.
File metadata
- Download URL: specd-0.8.3.tar.gz
- Upload date:
- Size: 55.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa7817d76530c8a08e7edf921943f0e13e0634ae5a8701e1bc16dd45fcef0f63
|
|
| MD5 |
512af7130fb4f8fa2d400e536380aed7
|
|
| BLAKE2b-256 |
b0b5c0064c9baa22d524aa2584a695f1fded491dd7f3c7f8f77eb5fb14a93091
|
File details
Details for the file specd-0.8.3-py2.py3-none-any.whl.
File metadata
- Download URL: specd-0.8.3-py2.py3-none-any.whl
- Upload date:
- Size: 20.5 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6d93beff5800a87501c229676a3ee120b89b244347530253c252d5017b5777f2
|
|
| MD5 |
e14e52a292bfe17dbd3a222b4d1683c9
|
|
| BLAKE2b-256 |
b6361b3821733eb8611e9e07577234a5e4db6b3ace4cedf1c2c70fcb8a350a7a
|