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.1.tar.gz (5.8 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.1-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: netpyx-1.0.1.tar.gz
  • Upload date:
  • Size: 5.8 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.1.tar.gz
Algorithm Hash digest
SHA256 9ebaaf745818c43079df8c69d53bfb6beb6350ead8a6f100cba76ff0566410eb
MD5 45e14a062abcf4004198623093b87e02
BLAKE2b-256 a065413d2c0ab1969a3bdfb1a68d57cadefc5d6377464034fd8869183c102a1f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: netpyx-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.6 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2b0758f400cece135712286c284d1034da421a2f5de7e957fb00c13354cc6a37
MD5 aded9c4d9476b8350eeeca2ec7a46db8
BLAKE2b-256 8d568e211f57ec4e7693deb41898c2f73516677ab69085af9f2c18556ae451e4

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