This guide compares how certain tasks are handled with and without the connexion
library in Python.
- Define your API in a YAML or JSON file using the OpenAPI Specification.
- The
connexion
app reads this file and sets up the API.
# openapi.yaml
paths:
/users/{userId}:
get:
summary: Get a user by ID
parameters:
- name: userId
in: path
required: true
schema:
type: integer
responses:
'200':
description: Successful response
content:
application/json:
schema:
$ref: '#/components/schemas/User'
components:
schemas:
User:
type: object
properties:
id:
type: integer
name:
type: string
- Define routes and handlers directly in Python code, typically using Flask.
- The API's structure is embedded in the code.
# app.py (using Flask)
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/users/<int:userId>', methods=['GET'])
def get_user(userId):
# Implementation to fetch and return user
return jsonify({"id": userId, "name": "John Doe"})
- Routes are automatically set up based on the OpenAPI specification file.
- Each path in the specification is linked to a Python function.
import connexion
app = connexion.App(__name__)
app.add_api('openapi.yaml')
def get_user(userId):
return {"id": userId, "name": "John Doe"}
- Each route is manually defined in the code.
# app.py (using Flask)
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/users/<int:userId>', methods=['GET']) # manually defined URI
def get_user(userId):
# Implementation to fetch and return user
return jsonify({"id": userId, "name": "John Doe"})
- Input validation is automatically handled based on the OpenAPI spec.
- If a request does not meet the specified parameters,
connexion
returns an error.
- Manually validate inputs in your function.
- This approach involves explicitly checking the types and values of the inputs.
@app.route('/users/<int:userId>', methods=['GET'])
def get_user(userId):
if type(userId) is not int:
return "Invalid input", 400
# Further processing
- Utilize Python's type hinting to indicate expected data types.
- Type hints do not enforce type checking at runtime but are useful for static analysis.
- For runtime validation, manual checks or additional libraries like
pydantic
can be employed.
from flask import Flask, jsonify, request
from typing import Any, Dict
app = Flask(__name__)
@app.route('/users/<int:userId>', methods=['GET'])
def get_user(userId: int) -> Dict[str, Any]:
# Type hinting suggests that userId should be an integer
# Additional runtime validation can be performed if necessary
return jsonify({"id": userId, "name": "John Doe"})
Note: While type hints improve code readability and help with development tools, they do not replace runtime validation for ensuring data integrity.
- Serialization and deserialization are automatically handled based on the OpenAPI spec.
- Manually serialize/deserialize data.
@app.route('/users/<int:userId>', methods=['GET'])
def get_user(userId):
return jsonify({"id": userId, "name": "John Doe"})
This guide is intended to provide a basic comparison and is not exhaustive.