Skip to main content

A lightweight general purpose API framework. Designed to make getting started quick and easy.

Project description

NKAPI

A lightweight general-purpose API framework for Python. Designed to make getting started quick and easy, NKAPI is inspired by Flask and Django but keeps things minimal and straightforward.

Version: 0.1.0


Installation

Install NKAPI via pip (from PyPI, if published):

pip install nkapi

Or install directly from source:

git clone https://github.com/nickkipshidze/python-nkapi.git
cd python-nkapi
pip install .

Quick Start

Here’s a minimal example of using NKAPI:

import nkapi

server = nkapi.NKServer(host="127.0.0.1", port=8000)

def root(request):
    return nkapi.NKResponse(
        headers={"Content-Type": "application/json"},
        data={
            "methods": request.methods,
            "path": request.path,
            "query": request.query,
            "headers": request.headers,
            "body": request.body
        }
    )

server.router.register(methods=["GET"], path="/", callback=root)
server.start()

Start the server and visit http://127.0.0.1:8000/ in your browser. You will see a JSON response containing the request details.


Core Components

NKServer

The main server class. Handles routing, request handling, and starting the HTTP server.

Initialization:

server = nkapi.NKServer(host="localhost", port=8000, debug=True)
  • host: The host IP to bind to (default: localhost)
  • port: Port number to listen on (default: 8000)
  • debug: Whether to print debug information to console (default: True)

Start the server:

server.start()

NKRouter

Handles request routing.

Register routes:

server.router.register(methods=["GET"], path="/", callback=some_function)
  • methods: HTTP methods (GET, POST, etc.)
  • path: URL path to match
  • callback: Function that receives an NKRequest object and returns an NKResponse

NKRequest

Represents an HTTP request.

Attributes:

  • method: HTTP method (e.g., GET, POST)
  • path: URL path
  • query: Dictionary of query parameters
  • headers: Dictionary of HTTP headers
  • body: Request body as a string
  • client_address: Tuple of client IP and port

Example:

def handler(request):
    print(request.method)
    print(request.path)
    print(request.query)

NKResponse

Represents an HTTP response.

Initialization:

response = nkapi.NKResponse(
    headers={"Content-Type": "application/json"},
    data={"message": "Hello, World!"},
    status=200
)
  • headers: Dictionary of HTTP headers
  • data: Response content (string or dictionary if Content-Type is application/json)
  • status: HTTP status code (default: 200)

NKAPI automatically sets the Content-Length header and serializes JSON if needed.


NKRequestHandler

Internal class used by NKServer to handle incoming HTTP requests. You typically don’t need to interact with it directly.


Routing Examples

GET request:

server.router.register(["GET"], "/hello", lambda req: nkapi.NKResponse(data="Hello!"))

POST request:

def echo(request):
    return nkapi.NKResponse(data=request.body)

server.router.register(["POST"], "/echo", echo)

404 Handling:

If a route is not found, NKAPI automatically returns a 404 response.

500 Handling:

If an error occurs in a callback, NKAPI automatically returns a 500 response.


Logging

NKAPI logs requests to the console in the following format:

<client_ip> - - [timestamp] "METHOD PATH HTTP/version" STATUS -

License

MIT License – see LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nkapi-0.1.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nkapi-0.1.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file nkapi-0.1.0.tar.gz.

File metadata

  • Download URL: nkapi-0.1.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for nkapi-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0528430f7db4c43c99d4e8c6ae50c7a2deab9200f425e410870c8b7a27659494
MD5 83bb54b9fd172e5c768922e5a1241b57
BLAKE2b-256 75f8cec4552efa87afc10693f90c3d6544b99ee898a6230e6bd9671a391e919d

See more details on using hashes here.

File details

Details for the file nkapi-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: nkapi-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for nkapi-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d42703206ae1768ceb217cc9109dd581146b7a5f9a62e4c52ec38ed3a7f1559e
MD5 9190a186fedbafb206f431ed2b0f87d2
BLAKE2b-256 94896e02106d4676bf0e9e5bff87f941743ab69ce0adaeb6109228c4adb28ce3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page