Introduction to Pycnic
Pycnic is a minimalist and high-performance web framework specifically designed for building JSON-based APIs in Python. Its lightweight nature and focus on speed make it a top choice for developers looking to quickly create robust APIs with minimal overhead. In this post, we’ll explore some of the features and functionality Pycnic offers, along with numerous API examples and a complete application example.
Getting Started
The first step to using Pycnic is installing it. You can easily install it via pip:
pip install pycnic
Creating Your First API
Creating an API with Pycnic is straightforward. Here’s a simple example of how to create a “Hello, World!” API:
from pycnic.core import WSGI, Handler class HelloWorldHandler(Handler): def get(self): return {"message": "Hello, World!"} class MyAPI(WSGI): routes = [("/hello", HelloWorldHandler)] # Now, run this with WSGI-compatible servers like Gunicorn: # gunicorn -w 4 my_api:MyAPI
Route Parameters
You can define dynamic routes with route parameters to handle flexible paths:
from pycnic.core import WSGI, Handler class GreetingHandler(Handler): def get(self, name): return {"greeting": f"Hello, {name}!"} class MyAPI(WSGI): routes = [("/greet/", GreetingHandler)]
Handling POST Requests
Pycnic provides simple methods for handling POST requests and extracting data from them:
from pycnic.core import WSGI, Handler class DataHandler(Handler): def post(self): data = self.request.data # Extract JSON body from the request return {"received": data} class MyAPI(WSGI): routes = [("/data", DataHandler)]
Using Query Parameters
You can access query parameters from the request object easily:
from pycnic.core import WSGI, Handler class QueryHandler(Handler): def get(self): param = self.request.args.get("param", "default") return {"param": param} class MyAPI(WSGI): routes = [("/query", QueryHandler)]
Error Handling
Custom error handling is an important feature when building APIs. Pycnic allows you to define error responses:
from pycnic.core import WSGI, Handler from pycnic.errors import HTTP_400 class ErrorHandler(Handler): def post(self): data = self.request.data if not data: raise HTTP_400("Missing data") return {"data": data} class MyAPI(WSGI): routes = [("/error-handler", ErrorHandler)]
Middleware Support
Add middleware to preprocess requests or manipulate responses globally:
from pycnic.core import WSGI, Handler class CustomMiddleware: def process_request(self, handler, request): print("Middleware pre-processing request") def process_response(self, handler, response): print("Middleware post-processing response") return response class HelloWorldHandler(Handler): def get(self): return {"message": "Hello, Middleware!"} class MyAPI(WSGI): routes = [("/hello", HelloWorldHandler)] middleware = [CustomMiddleware()]
App Example with Multiple APIs
Below is an example of a complete app using various demonstrated APIs:
from pycnic.core import WSGI, Handler from pycnic.errors import HTTP_400 class HelloWorldHandler(Handler): def get(self): return {"message": "Hello, World!"} class GreetingHandler(Handler): def get(self, name): return {"greeting": f"Hello, {name}!"} class DataHandler(Handler): def post(self): data = self.request.data if not data: raise HTTP_400("No data provided") return {"received": data} class QueryHandler(Handler): def get(self): param = self.request.args.get("param", "default") return {"param": param} class MyAPI(WSGI): routes = [ ("/hello", HelloWorldHandler), ("/greet/", GreetingHandler), ("/data", DataHandler), ("/query", QueryHandler), ] # Run this app via Gunicorn or any WSGI server
With Pycnic, you can build fast and efficient APIs while keeping your codebase clean and maintainable. Its lightweight design is perfect for microservices and lean application architectures.
Conclusion
Pycnic stands out as a lightweight framework crafted solely for API development. Its simplicity, performance, and ease of use make it a favorite among developers seeking a Python-based solution for lightweight RESTful services. Whether you’re building a simple microservice or a complex API backend, Pycnic provides the tools to get the job done efficiently.