Package: especifico
EspecíficoEspecífico is a hard fork ofConnexion,
the framework that automagically handles HTTP requests based onOpenAPI Specification(formerly known as Swagger Spec) of your API described inYAML format.
Específico allows you to write an OpenAPI specification, then maps the endpoints to your Python functions; this makes it
unique, as many tools generate the specification based on your Python code. You can describe your
REST API in as much detail as you want; then Específico guarantees that it will work as you
specified.Específico tries to:simplify the development processconfirm expectations about what your API will look likeimprove the securityEspecífico featuresValidates requests and endpoint parameters automatically, based on
your specification.Provides a Swagger UI so that the users of your API can
have live documentation and even call your API's endpoints
through itHandles OAuth2 and API Key token-based authenticationSupports API versioningSupports automatic serialization of payloads. If your
specification defines that an endpoint returns JSON, Específico will
automatically serialize the return value for you and set the right
content type in the HTTP header.Why EspecíficoWith Específico,you write the spec first.
Específico then calls your Python
code, handling the mapping from the specification to the code. This
encourages you to write the specification so that all of your
developers can understand what your API does, even before you write a
single line of code.If multiple teams depend on your APIs, you can use Específico to easily send them the documentation of your API.
This guarantees that your API will follow the specification that you wrote.
This is a different process from that offered by frameworks such asHug,
which generates a specificationafteryou've written the code.
Some disadvantages of generating specifications based on code is that they often end up
lacking details or mix your documentation with the code logic of your application.Compared to the original project Específico, Específico keeps aiohttp support and includes many
opinionated patches from the fork's maintainer.How to UsePrerequisitesPython 3.8+Installing ItIn your command line, type:pip install especificoRunning ItPlace your API YAML inside a folder in the root
path of your application (e.gswagger/). Then run:importespecificoapp=especifico.App(__name__,specification_dir='swagger/')app.add_api('my_api.yaml')app.run(port=8080)See theConnexion Pet Store Example Applicationfor a sample
specification.Now you're able to run and use Específico!DetailsOAuth 2 Authentication and AuthorizationEspecífico supports one of the three OAuth 2 handling methods. (See
"TODO" below.) With Específico, the API security definitionmustinclude a 'x-tokenInfoUrl' or 'x-tokenInfoFunc (or setTOKENINFO_URLorTOKENINFO_FUNCenv var respectively). 'x-tokenInfoUrl' must contain an
URL to validate and get thetoken informationand 'x-tokenInfoFunc must
contain a reference to a function used to obtain the token info. When both 'x-tokenInfoUrl'
and 'x-tokenInfoFunc' are used, Específico will prioritize the function method. Específico expects to
receive the OAuth token in theAuthorizationheader field in the
format described inRFC 6750section 2.1. This aspect
represents a significant difference from the usual OAuth flow.Dynamic Rendering of Your SpecificationEspecífico usesJinja2to allow specification parameterization through theargumentsparameter. You can define specification arguments for the application either globally (via theespecifico.Appconstructor) or for each specific API (via theespecifico.App#add_apimethod):app=especifico.App(__name__,specification_dir='swagger/',arguments={'global':'global_value'})app.add_api('my_api.yaml',arguments={'api_local':'local_value'})app.run(port=8080)When a value is provided both globally and on the API, the API value will take precedence.Endpoint Routing to Your Python ViewsEspecífico uses theoperationIdfrom eachOperation Objectto identify which Python function should handle each URL.Explicit Routing:paths:/hello_world:post:operationId:myapp.api.hello_worldIf you provide this path in your specification POST requests tohttp://MYHOST/hello_world, it will be handled by the functionhello_worldin themyapp.apimodule. Optionally, you can includex-swagger-router-controller(orx-openapi-router-controller) in your
operation definition, makingoperationIdrelative:paths:/hello_world:post:x-swagger-router-controller:myapp.apioperationId:hello_worldKeep in mind that Específico follows howHTTP methods work in Flask_ and therefore HEAD requests will be handled by theoperationIdspecified under GET in the specification. If both methods are supported,especifico.request.methodcan be used to determine which request was made.Automatic RoutingTo customize this behavior, Específico can use alternativeResolvers--for example,RestyResolver. TheRestyResolverwill compose anoperationIdbased on the path and HTTP method of
the endpoints in your specification:fromespecifico.resolverimportRestyResolverapp=especifico.App(__name__)app.add_api('swagger.yaml',resolver=RestyResolver('api'))paths:/:get:# Implied operationId: api.get/foo:get:# Implied operationId: api.foo.searchpost:# Implied operationId: api.foo.post'/foo/{id}':get:# Implied operationId: api.foo.getput:# Implied operationId: api.foo.putcopy:# Implied operationId: api.foo.copydelete:# Implied operationId: api.foo.deleteRestyResolverwill give precedence to anyoperationIdencountered in the specification. It will also respectx-router-controller. You can import and extendespecifico.resolver.Resolverto implement your ownoperationId(and function) resolution algorithm.Automatic Parameter HandlingEspecífico automatically maps the parameters defined in your endpoint specification to arguments of your Python views as named parameters, and, whenever possible, with value casting. Simply define the endpoint's parameters with the same names as your views arguments.As an example, say you have an endpoint specified as:paths:/foo:get:operationId:api.foo_getparameters:-name:messagedescription:Some message.in:querytype:stringrequired:trueAnd the view function:# api.py filedeffoo_get(message):# do somethingreturn'You send the message:{}'.format(message),200In this example, Específico automatically recognizes that your view
function expects an argument namedmessageand assigns the value
of the endpoint parametermessageto your view function.In the OpenAPI 3.x.x spec, the requestBody does not have a name.
By default it will be passed in as 'body'. You can optionally
provide the x-body-name parameter in your requestBody
(or legacy position within the requestBody schema)
to override the name of the parameter that will be passed to your
handler function./pathpost:requestBody:x-body-name:bodycontent:application/json:schema:# legacy location here should be ignored because the preferred location for x-body-name is at the requestBody level abovex-body-name:this_should_be_ignored$ref:'#/components/schemas/someComponent'When you define a parameter at your endpoint asnotrequired, and
this argument does not have default value in your Python view, you will get
a "missing positional argument" exception whenever you call this endpoint
WITHOUT the parameter. Provide a default value for a named argument or use**kwargsdict.Type castingWhenever possible, Específico will try to parse your argument values and
do type casting to related Python native values. The current
available type castings are:OpenAPI TypePython TypeintegerintstringstrnumberfloatbooleanboolarraylistnullNoneobjectdictIf you use thearraytype In the Swagger definition, you can define thecollectionFormatso that it won't be recognized. Específico currently
supports collection formats "pipes" and "csv". The default format is "csv".Específico is opinionated about how the URI is parsed forarraytypes.
The default behavior for query parameters that have been defined multiple
times is to use the right-most value. For example, if you provide a URI with
the the query string?letters=a,b,c&letters=d,e,f, especifico will setletters = ['d', 'e', 'f'].You can override this behavior by specifying the URI parser in the app or
api options.fromespecifico.decorators.uri_parsingimportAlwaysMultiURIParseroptions={'uri_parser_class':AlwaysMultiURIParser}app=especifico.App(__name__,specification_dir='swagger/',options=options)You can implement your own URI parsing behavior by inheriting fromespecifico.decorators.uri_parsing.AbstractURIParser.There are a handful of URI parsers included with connection.ParserDescriptionOpenAPIURIParserdefault: OpenAPI 3.0This parser adheres to the OpenAPI 3.x.x spec, and uses thestyleparameter. Query parameters are parsed from left to right, so if a query parameter is defined twice, then the right-most definition will take precedence. For example, if you provided a URI with the query string?letters=a,b,c&letters=d,e,f, andstyle: simple, then especifico will setletters = ['d', 'e', 'f']. For additional information seeOpenAPI 3.0 Style Values`.Swagger2URIParserdefault: OpenAPI 2.0This parser adheres to the Swagger 2.0 spec, and will only join together multiple instance of the same query parameter if thecollectionFormatis set tomulti. Query parameters are parsed from left to right, so if a query parameter is defined twice, then the right-most definition wins. For example, if you provided a URI with the query string?letters=a,b,c&letters=d,e,f, andcollectionFormat: csv, then especifico will setletters = ['d', 'e', 'f'].FirstValueURIParserThis parser behaves like the Swagger2URIParser, except that it prefers the first defined value. For example, if you provided a URI with the query string?letters=a,b,c&letters=d,e,fandcollectionFormat: csvthen especifico will setletters = ['a', 'b', 'c'].AlwaysMultiURIParserThis parser is backwards compatible with Específico 1.x. It joins together multiple instances of the same query parameter.Parameter validationEspecífico can apply strict parameter validation for query and form data
parameters.  When this is enabled, requests that include parameters not defined
in the swagger spec return a 400 error.  You can enable it when adding the API
to your application:app.add_api('my_apy.yaml',strict_validation=True)API Versioning and basePathSetting a base path is useful for versioned APIs. An example of
a base path would be the1.0inhttp://MYHOST/1.0/hello_world.If you are using OpenAPI 3.x.x, you set your base URL path in the
servers block of the specification. You can either specify a full
URL, or just a relative path.servers:-url:https://MYHOST/1.0description:full url example-url:/1.0description:relative path examplepaths:...If you are using OpenAPI 2.0, you can define abasePathon the top level
of your OpenAPI 2.0 specification.basePath:/1.0paths:...If you don't want to include the base path in your specification, you
can provide it when adding the API to your application:app.add_api('my_api.yaml',base_path='/1.0')Swagger JSONEspecífico makes the OpenAPI/Swagger specification in JSON format
available from eitherswagger.json(for OpenAPI 2.0) oropenapi.json(for OpenAPI 3.x.x) at the base path of the API.
For example, if your base path was1.0, then your spec would be
available at/1.0/openapi.json.You can disable serving the spec JSON at the application level:options={"serve_spec":False}app=especifico.App(__name__,specification_dir='openapi/',options=options)app.add_api('my_api.yaml')You can also disable it at the API level:options={"serve_spec":False}app=especifico.App(__name__,specification_dir='openapi/')app.add_api('my_api.yaml',options=options)HTTPS SupportWhen specifying HTTPS as the scheme in the API YAML file, all the URIs
in the served Swagger UI are HTTPS endpoints. The problem: The default
server that runs is a "normal" HTTP server. This means that the
Swagger UI cannot be used to play with the API. What is the correct
way to start a HTTPS server when using Específico?One way,described by Flask, looks like this:fromOpenSSLimportSSLcontext=SSL.Context(SSL.SSLv23_METHOD)context.use_privatekey_file('yourserver.key')context.use_certificate_file('yourserver.crt')app.run(host='127.0.0.1',port='12344',debug=False/True,ssl_context=context)However, Específico doesn't provide an ssl_context parameter. This is
because Flask doesn't, either--but it uses**kwargsto send the
parameters to the underlyingwerkzeugserver.The Swagger UI ConsoleThe Swagger UI for an API is available through pip extras.
You can install it withpip install especifico[swagger-ui].
It will be served up at{base_path}/ui/wherebase_pathis the
base path of the API.You can disable the Swagger UI at the application level:app=especifico.App(__name__,specification_dir='openapi/',options={"swagger_ui":False})app.add_api('my_api.yaml')You can also disable it at the API level:app=especifico.App(__name__,specification_dir='openapi/')app.add_api('my_api.yaml',options={"swagger_ui":False})If necessary, you can explicitly specify the path to the directory with
swagger-ui to not use the especifico[swagger-ui] distro.
In order to do this, you should specify the following option:options={'swagger_path':'/path/to/swagger_ui/'}app=especifico.App(__name__,specification_dir='openapi/',options=options)If you wish to provide your own swagger-ui distro, note that especifico
expects a jinja2 file calledswagger_ui/index.j2in order to load the
correctswagger.jsonby default. Yourindex.j2file can use theopenapi_spec_urljinja variable for this purpose:constui=SwaggerUIBundle({url:"{{ openapi_spec_url }}"})Additionally, if you wish to use swagger-ui-3.x.x, it is also provided by
installing especifico[swagger-ui], and can be enabled like this:fromswagger_ui_bundleimportswagger_ui_3_pathoptions={'swagger_path':swagger_ui_3_path}app=especifico.App(__name__,specification_dir='swagger/',options=options)Server BackendBy default Específico uses theFlaskserver. For asynchronous
applications, you can also useTornadoas the HTTP server. To do
this, set your server totornado:importespecificoapp=especifico.App(__name__,specification_dir='swagger/')app.run(server='tornado',port=8080)You can use the Flask WSGI app with any WSGI container, e.g.using Flask with uWSGI(this is common):app=especifico.App(__name__,specification_dir='swagger/')application=app.app# expose global WSGI application objectYou can use theaiohttpframework as server backend as well:importespecificoapp=especifico.AioHttpApp(__name__,specification_dir='swagger/')app.run(port=8080)Also check aiohttp handlerexamples.Set up and run the installation code:$sudopip3installuwsgi
$uwsgi--http:8080-wapp-p16# use 16 worker processesSee theuWSGI documentationfor more information.DocumentationAdditional information is available atEspecífico's Documentation Page.ChangesA full changelog is maintained on theGitHub releases page.ContributionsWe welcome your ideas, issues, and pull requests. Just follow the
usual/standard GitHub practices.You can find out more about how Específico works and where to apply your changes by having a look
at ourARCHITECTURE.rst.Unless you explicitly state otherwise in advance, any non trivial
contribution intentionally submitted for inclusion in this project by you
to the steward of this repository (Zalando SE, Berlin) shall be under the
terms and conditions of Apache License 2.0 written below, without any
additional copyright information, terms or conditions.If you'd like to become a more consistent contributor to Específico, we'd love your help working on
these we have a list ofissues where we are looking for contributions.ThanksWe'd like to thank all of Connexion's contributors for working on this
project, and to Swagger/OpenAPI for their support.LicenseCopyright 2015 Zalando SE, 2022 Athenian SASLicensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0.Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.