Skip to content

Instantly share code, notes, and snippets.

@agosmou
Created November 18, 2023 02:28
Show Gist options
  • Save agosmou/09f68ca660d4bcefc5da6d29ccc5cf80 to your computer and use it in GitHub Desktop.
Save agosmou/09f68ca660d4bcefc5da6d29ccc5cf80 to your computer and use it in GitHub Desktop.

Connexion vs Traditional Flask API Development

This guide compares how certain tasks are handled with and without the connexion library in Python.

1. API-First Design

With connexion:

  • 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

Without connexion:

  • 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"})

2. Automatic Endpoint Routing

With connexion:

  • 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"}

Without connexion:

  • 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"})

3. Input Validation

With connexion:

  • Input validation is automatically handled based on the OpenAPI spec.
  • If a request does not meet the specified parameters, connexion returns an error.

Without connexion, Option 1: Manual Code Validation:

  • 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

Without connexion, Option 2: Python Type Hinting:

  • 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.

4. Data Serialization/Deserialization

With connexion:

  • Serialization and deserialization are automatically handled based on the OpenAPI spec.

Without connexion:

  • 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment