Skip to main content

ZeroHTTP

ZeroHTTP is a lightweight HTTP client library for Python that provides various authentication methods and OAuth2 flows for Microsoft Entra (Azure AD).

Features

  • Simple HTTP requests (GET, POST)
  • Support for Basic and Bearer Authentication
  • OAuth2 integration for Microsoft Entra ID
  • Interactive authentication flows
  • Client certificate authentication
  • Local callback servers for OAuth2
  • JSON and form data support
  • Minimal dependencies

Installation

pip install zerohttp

Or for development:

git clone https://github.com/SecNex/zerohttp.git
cd zerohttp
pip install -e .

Quick Start

Basic HTTP Requests

from zerohttp import HTTPClient

# Create client
client = HTTPClient(base_url="https://api.example.com")

# GET request with parameters
response = client.get("/users", params=[("page", "1"), ("limit", "10")])
data = response.json()

# POST request with JSON data
response = client.post("/users", data={
    "name": "John Doe",
    "email": "john@example.com"
})
result = response.json()

Basic Authentication

from zerohttp import HTTPClient
from zerohttp.auth import BasicAuthentication

# Create Basic Authentication
auth = BasicAuthentication("username", "password")

# Client with authentication
client = HTTPClient(auth=auth, base_url="https://api.example.com")

# Request will automatically include Authorization header
response = client.get("/protected-endpoint")

Bearer Token Authentication

from zerohttp import HTTPClient
from zerohttp.auth import BearerAuthentication

# Create Bearer Token
auth = BearerAuthentication("your-access-token-here")

# Client with Bearer Authentication
client = HTTPClient(auth=auth, base_url="https://api.example.com")

response = client.get("/api/data")

Microsoft Entra Integration

Interactive User Authentication

from zerohttp.providers import Entra
from zerohttp import HTTPClient

# Entra Provider for interactive user authentication
entra = Entra(
    tenant="your-tenant-id",
    client=("client-id", "client-secret"),
    scope="https://graph.microsoft.com/.default"
)

# Perform authentication (opens browser)
auth = entra.authenticate()

# Create client with authenticated token
client = HTTPClient(auth=auth)

# Make requests to Microsoft Graph API
response = client.get("https://graph.microsoft.com/v1.0/me")
user_data = response.json()

Application Authentication (Client Credentials)

from zerohttp.providers import EntraApp
from zerohttp import HTTPClient

# Entra App for Client Credentials Flow
app = EntraApp(
    tenant="your-tenant-id",
    client=("client-id", "client-secret"),
    scope="https://graph.microsoft.com/.default"
)

# Authentication without user interaction
auth = app.authenticate()

# Client for application requests
client = HTTPClient(auth=auth)

response = client.get("https://graph.microsoft.com/v1.0/users")

Advanced Usage

Custom Headers

from zerohttp import HTTPClient

client = HTTPClient(base_url="https://api.example.com")

# Set individual header
client.set_header("User-Agent", "MyApp/1.0")

# Set multiple headers at once
client.set_headers({
    "Content-Type": "application/json",
    "Accept": "application/json",
    "X-API-Key": "your-api-key"
})

URL Parameter Handling

from zerohttp import HTTPClient

client = HTTPClient(base_url="https://api.example.com")

# GET with complex parameters
response = client.get("/search", params=[
    ("q", "python"),
    ("sort", "stars"),
    ("order", "desc"),
    ("per_page", "10")
])

URL Generation

from zerohttp import HTTPClient

client = HTTPClient(base_url="https://api.example.com")

# Generate URL without making request
url = client.link("/users", params=[("page", "2"), ("limit", "20")])
# Result: "https://api.example.com/users?page=2&limit=20"

Browser Integration

from zerohttp import HTTPClient

client = HTTPClient(base_url="https://api.example.com")

# Open link in browser
client.webbrowser("/login", params=[
    ("redirect_uri", "http://localhost:8000/callback"),
    ("response_type", "code")
])

API Reference

HTTPClient

Main class for HTTP requests.

Constructor:

HTTPClient(auth=None, proxy="", base_url="")

Methods:

  • get(url="", params=[]) - Send GET request
  • post(url="", data={}, params=[]) - Send POST request
  • set_header(key, value) - Set individual header
  • set_headers(headers) - Set multiple headers
  • get_header(key) - Get header value
  • get_headers() - Get all headers
  • link(url="", params=[]) - Generate URL
  • webbrowser(url="", params=[]) - Open URL in browser

Response

Wrapper for HTTP responses.

Methods:

  • json() - Parse response as JSON dictionary
  • text - Response text as string

Authentication Classes

BasicAuthentication

BasicAuthentication(username: str, password: str)

BearerAuthentication

BearerAuthentication(token: str)

Entra Provider

Entra (Interactive User Authentication)

Entra(
    tenant: str,
    client: tuple,  # (client_id, client_secret)
    scope: str,
    token_url: str = "https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token",
    authorization_url: str = "https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize",
    open: bool = True
)

EntraApp (Application Authentication)

EntraApp(
    tenant: str,
    client: tuple,  # (client_id, client_secret)
    scope: str = "https://graph.microsoft.com/.default",
    token_url: str = ""
)

Error Handling

ZeroHTTP propagates HTTP errors and prints response contexts:

from zerohttp import HTTPClient
import urllib.error

client = HTTPClient(base_url="https://api.example.com")

try:
    response = client.get("/nonexistent-endpoint")
except urllib.error.HTTPError as e:
    print(f"HTTP Error: {e.code}")
    # Response is automatically printed
except Exception as e:
    print(f"Other error: {e}")

OAuth2 Flow Details

The interactive OAuth2 flow works as follows:

  1. Local Server: An HTTP server is started on localhost:8000
  2. Browser Authentication: User is redirected to Microsoft login page
  3. Callback: After successful login, user is redirected to local server
  4. Token Exchange: Authorization code is exchanged for access token
  5. Server Shutdown: Local server is automatically shutdown

Development

Project Structure

zerohttp/
├── src/zerohttp/
│   ├── __init__.py
│   ├── __main__.py
│   ├── http.py              # HTTPClient and Response
│   ├── auth/                # Authentication classes
│   │   ├── __init__.py
│   │   ├── basic.py
│   │   ├── bearer.py
│   │   └── interactive.py
│   └── providers/           # Provider implementations
│       ├── __init__.py
│       └── entra.py
├── pyproject.toml
├── requirements.txt
└── README.md

Build and Installation

# Build package
python -m build

# Install in development mode
pip install -e .

# Install dependencies
pip install -r requirements.txt

License

This project is licensed under the MIT License - see LICENSE for details.

Contributing

Contributions are welcome! Please open an issue or pull request.

Support

For questions and issues, please create a GitHub Issue.

Release files for zerohttp 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for zerohttp 0.0.1
File Size Uploaded
zerohttp-0.0.1.tar.gz 10.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for zerohttp 0.0.1
File Interpreter ABI Platform
zerohttp-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 20.7 kB

Release files / zerohttp-0.0.1.tar.gz

Download URL zerohttp-0.0.1.tar.gz
Size 10.4 kB
Tags Source
SHA-256 checksum
How to use checksums
2b993bfc87849a4176e389f899de1b4accdb2dbd8abf4442e84c68196272e27a
BLAKE2b-256 checksum
How to use checksums
d34b089b381c7e02404f9217cdc0a1f9d3abba371073fdca8c93b22d621f1645
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / zerohttp-0.0.1-py3-none-any.whl

Download URL zerohttp-0.0.1-py3-none-any.whl
Size 10.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2cf0a0ddcd69b4e0be0eeac98af57c98d6489cde304275318d0a4b1cd12e40de
BLAKE2b-256 checksum
How to use checksums
c29582f77fe55f9c5798b2e06310b333c015bb79f391710c365c29605d4e30eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page