Skip to main content

A simple HTTP client wrapper for requests

Project description

netpy

netpy is a lightweight and easy-to-use HTTP client wrapper built on top of the popular requests library. It simplifies making HTTP requests with support for session management, headers, cookies, and both Basic and Bearer authentication.


Features

  • Supports GET, POST, PUT, DELETE HTTP methods.
  • Manage common headers and cookies automatically.
  • Handles Bearer and Basic Authentication easily.
  • Supports a base URL to simplify request endpoints.
  • Tracks cookies from server responses and allows easy access.
  • Includes context manager support for automatic resource cleanup.
  • Detailed logging for request lifecycle (can be adjusted).

Installation

You can install netpy via pip (once published on PyPI):

pip install netpy

Or install it locally by cloning the repository and running:

pip install .

Make sure you have Python 3.7 or above and the requests library installed.


Usage

Here is a quick example of how to use the HttpClient class from netpy:

from netpy import HttpClient

# Initialize the client with a base URL
client = HttpClient(base_url="https://api.example.com/")

# Set a custom User-Agent header
client.set_user_agent("netpy-client/1.0")

# Set Basic Authentication credentials
client.set_auth(("username", "password"))

# Or set Bearer token authentication
# client.set_auth_bearer("your_bearer_token")

# Make a GET request to a relative endpoint
response = client.get("/data/items", params={"limit": 10})

if response:
    print("Status code:", response.status_code)
    print("Response JSON:", response.json())

# Access current cookies stored in the session
cookies = client.get_cookies()
print("Cookies:", cookies)

# Always close the session when done
client.close_session()

API Reference

Class: HttpClient

  • __init__(base_url: str = "")
    Initialize the HTTP client with an optional base URL.

  • set_headers(headers: Dict[str, str])
    Set or update global HTTP headers.

  • set_user_agent(user_agent: str)
    Set a custom User-Agent header.

  • set_cookies(cookies: Dict[str, str])
    Manually set or update cookies.

  • set_auth(auth: tuple[str, str])
    Set Basic Authentication credentials (username, password).

  • set_auth_bearer(token: str)
    Set Bearer token authentication.

  • get(endpoint: str, params: Optional[Dict[str, Any]] = None, **kwargs) -> Optional[requests.Response]
    Make a GET request to the specified endpoint.

  • post(endpoint: str, data: Optional[Dict[str, Any]] = None, json: Optional[Dict[str, Any]] = None, **kwargs) -> Optional[requests.Response]
    Make a POST request.

  • put(endpoint: str, data: Optional[Dict[str, Any]] = None, json: Optional[Dict[str, Any]] = None, **kwargs) -> Optional[requests.Response]
    Make a PUT request.

  • delete(endpoint: str, **kwargs) -> Optional[requests.Response]
    Make a DELETE request.

  • get_cookies() -> Dict[str, str]
    Retrieve the current cookies stored in the session.

  • close_session()
    Close the HTTP session and release resources.


Contributing

Contributions, issues, and feature requests are welcome! Feel free to check the issues page.


License

This project is licensed under the MIT License. See the LICENSE file for details.


Contact

Created by José Luis (Pepe).
Feel free to reach out for questions or suggestions.


Happy coding! 🚀

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

netpyx-1.0.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

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

netpyx-1.0.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file netpyx-1.0.0.tar.gz.

File metadata

  • Download URL: netpyx-1.0.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for netpyx-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7b5885f965a8bd237fc311157cf87a65ec4dcf6453b64d637067ee4058f4758f
MD5 c01e6824ff1e0c2b360bcecc06b72b76
BLAKE2b-256 c328d484ecb49c5d2025584f10758aae6aa50db93aed488954ebc5bc0f6a84dd

See more details on using hashes here.

File details

Details for the file netpyx-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: netpyx-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for netpyx-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ea65772a38f3b8e191d77481ee0fe270973c66b68cabf97266dec4f02ab1103a
MD5 432bd3dcb181db3e00df154cefe11cbe
BLAKE2b-256 9e69ac4211f8d7f293435fa7e8045f36a80ac0b85feb5901837f42a517099826

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