wreqs


Namewreqs JSON
Version 1.2.1 PyPI version JSON
download
home_pagehttps://github.com/munozarturo/wreqs
SummarySimplified and enhanced request handling.
upload_time2024-07-24 20:39:28
maintainerNone
docs_urlNone
authorArturo Munoz
requires_python>=3.7
licenseNone
keywords http requests wrapper retry timeout
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <div align="center">
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://www.munozarturo.com/assets/wreqs/logo-github-dark.svg">
    <source media="(prefers-color-scheme: light)" srcset="https://www.munozarturo.com/assets/wreqs/logo-github-light.svg">
    <img alt="wreqs" src="https://www.munozarturo.com/assets/wreqs/logo-github-light.svg" width="50%" height="40%">
  </picture>
</div>

<!-- omit from toc -->
# wreqs: wrapped requests

The **wreqs** module is a powerful wrapper around the popular `requests` library, designed to simplify and enhance HTTP request handling in Python. It provides a context manager for making HTTP requests with built-in retry logic, timeout handling, and session management.

<!-- omit from toc -->
## Table of Contents

- [Installation](#installation)
- [Quick Start Guide](#quick-start-guide)
- [Advanced Usage](#advanced-usage)
  - [Making Multiple Requests with the Same Session](#making-multiple-requests-with-the-same-session)
  - [Implementing Custom Retry Logic](#implementing-custom-retry-logic)
  - [Handling Timeouts](#handling-timeouts)
  - [Using Retry Callbacks](#using-retry-callbacks)
  - [Using Proxy Rotation](#using-proxy-rotation)
- [Logging Configuration](#logging-configuration)
  - [Default Logging](#default-logging)
  - [Configuring the Logger](#configuring-the-logger)
  - [Using a Custom Logger](#using-a-custom-logger)
- [Error Handling](#error-handling)
  - [`wreqs` Specific Errors](#wreqs-specific-errors)
    - [RetryRequestError](#retryrequesterror)
  - [Common `requests` Exceptions](#common-requests-exceptions)
  - [Other Exceptions](#other-exceptions)
- [Development and Publishing](#development-and-publishing)
  - [Testing](#testing)
  - [CI/CD](#cicd)
  - [Publishing a New Version](#publishing-a-new-version)

## Installation

To install the `wreqs` module, use pip:

```bash
pip install wreqs
```

## Quick Start Guide

Getting started with the `wreqs` module is simple. Follow these steps to make your first wrapped request:

1. First, install the module:

   ```bash
   pip install wreqs
   ```

2. Import the necessary components:

   ```python
   from wreqs import wreq
   import requests
   ```

3. Create a request object:

   ```python
   req = requests.Request("GET", "https://api.example.com/data")
   ```

4. Use the `wreq` context manager to make the request:

   ```python
   with wreq(req) as response:
       print(response.status_code)
       print(response.json())
   ```

That's it! You've now made a request using `wreqs`. This simple example demonstrates the basic usage, but `wreqs` offers much more functionality, including retry mechanisms, timeout handling, and custom session management.

Here's a slightly more advanced example that includes a retry check:

```python
from wreqs import wreq
import requests

def check_retry(response: requests.Response) -> bool:
    return response.status_code >= 500

req = requests.Request("GET", "https://api.example.com/data")
with wreq(req, max_retries=3, check_retry=check_retry) as response:
    print(response.json())
```

This example will retry the request up to 3 times if it receives a 5xx status code. If all requests fail it will throw a `RequestRetryError` see [Error Handling](#error-handling) for more information on errors.

For more advanced usage and configuration options, please refer to the subsequent sections of this documentation.

## Advanced Usage

The `wreqs` module offers several advanced features to handle complex scenarios and improve your HTTP request workflow.

### Making Multiple Requests with the Same Session

`wreqs` provides a convenient `wreqs_session` context manager that automatically manages session creation, use, and cleanup. This simplifies the process of making multiple requests with the same session.

Here's an example that demonstrates how to use `wreqs_session` for authentication and subsequent data retrieval:

```python
from wreqs import wreq, wreqs_session
from requests import Request

with wreqs_session():
    # authentication request
    auth_req = Request("POST", "https://api.example.com/login", json={
        "username": "user",
        "password": "pass"
    })
    with wreq(auth_req) as auth_response:
        if auth_response.status_code != 200:
            raise Exception("Failed to authenticate.")

    # data request using the same authenticated session
    data_req = Request("GET", "https://api.example.com/protected-data")
    with wreq(data_req) as data_response:
        print(data_response.json())
```

In this example, the `wreqs_session` context manager automatically creates and manages a session for all requests within its block. The first request authenticates the user, and the second request uses the same session to access protected data. The session automatically handles cookies and other state information between requests.

This approach is equivalent to manually creating and managing a session, as shown in the following example:

```python
import requests
from wreqs import wreq

session = requests.Session()

auth_req = requests.Request(...)
with wreq(auth_req, session=session) as auth_response: # session explicitly defined
    ...

data_req = requests.Request(...)
with wreq(data_req, session=session) as data_response: # session explicitly defined
    ...
```

It is still possible to use a different session within a `wreqs_session` context so long as it is explicitly defined.

```python
from wreqs import wreq, wreqs_session
from requests import Request, Session

with wreqs_session():
    auth_req = Request(...)
    with wreq(auth_req) as auth_response: # will use wreqs_session
        ...

    other_session = Session()
    data_req = Request(...)
    with wreq(data_req, session=other_session) as data_response: # will use other_session
        ...
```

### Implementing Custom Retry Logic

The `wreqs` module allows you to implement custom retry logic using the `check_retry` parameter. This function should return `True` if a retry should be attempted, and `False` otherwise.

Here's an example that retries on specific status codes and implements an exponential backoff:

```python
import time
from wreqs import wreq
import requests

def check_retry_with_backoff(response: requests.Response) -> bool:
    if response.status_code in [429, 500, 502, 503, 504]:
        retry_after = int(response.headers.get("Retry-After", 0))
        time.sleep(max(retry_after, 2 ** (response.request.retry_count - 1)))
        return True
    return False

req = requests.Request("GET", "https://api.example.com/data")
with wreq(req, max_retries=5, check_retry=check_retry_with_backoff) as response:
    print(response.json())
```

This example retries on specific status codes and implements an exponential backoff strategy.

### Handling Timeouts

`wreqs` allows you to set timeouts for your requests to prevent them from hanging indefinitely. Here"s how you can use the timeout feature:

```python
from wreqs import wreq
import requests

req = requests.Request("GET", "https://api.example.com/slow-endpoint")

try:
    with wreq(req, timeout=5) as response:
        print(response.json())
except requests.Timeout:
    print("The request timed out after 5 seconds")
```

This example sets a 5-second timeout for the request. If the server doesn't respond within 5 seconds, a `Timeout` exception is raised.

### Using Retry Callbacks

You can use the `retry_callback` parameter to perform actions before each retry attempt. This can be useful for logging, updating progress bars, or implementing more complex backoff strategies.

```python
import time
from wreqs import wreq
import requests

def check_retry(response: requests.Response) -> bool:
    return response.status_code != 200

def retry_callback(response):
    print(f"Retrying request. Previous status code: {response.status_code}")
    time.sleep(2)  # Wait 2 seconds before retrying

req = requests.Request("GET", "https://api.example.com/unstable-endpoint")
with wreq(req, check_retry=check_retry, retry_callback=retry_callback) as res:
    print(res.json())
```

This example prints a message and waits for 2 seconds before each retry attempt.

These advanced usage examples demonstrate the flexibility and power of the `wreqs` module. By leveraging these features, you can create robust and efficient HTTP request handling in your Python applications.

### Using Proxy Rotation

`wreqs` now supports proxy rotation, allowing you to distribute your requests across multiple proxies. This can be useful for avoiding rate limits or accessing region-restricted content. Here's how to use this feature:

```python
from wreqs import wreq
import requests

proxies = [
    "http://proxy1.example.com:8080",
    "http://proxy2.example.com:8080",
    "http://proxy3.example.com:8080",
]

req = requests.Request("GET", "https://api.example.com/data")

with wreq(req, max_retries=3, proxies=proxies) as response:
    print(response.json())
```

In this example, wreqs will rotate through the provided list of proxies for each request or retry attempt. If a request fails, it will automatically use the next proxy in the list for the retry.

Note:

- Proxies should be provided as a list of strings in the format "<http://host:port>" or "<https://host:port>".
- The proxy rotation is done in a round-robin fashion.
- If no proxies are provided, wreqs will use the default network connection.

## Logging Configuration

The `wreqs` module provides flexible logging capabilities to help you track and debug your HTTP requests. You can configure logging at the module level, which will apply to all subsequent uses of `wreq`.

### Default Logging

Out of the box, `wreqs` uses a default logger with minimal configuration:

```python
import wreqs

context = wreqs.wreq(some_request)
```

This will use the default logger, which outputs to the console at the INFO level.

### Configuring the Logger

You can configure the logger using the `configure_logger` function:

```python
import logging
import wreqs

wreqs.configure_logger(
    level=logging.DEBUG,
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
    filename="wreqs.log"
)

# all subsequent calls will use this logger configuration
context1 = wreqs.wreq(some_request)
context2 = wreqs.wreq(another_request)
```

### Using a Custom Logger

For more advanced logging needs, you can create and configure your own logger and set it as the module logger:

```python
import logging
import wreqs

# create and configure a custom logger
custom_logger = logging.getLogger("my_app.wreqs")
custom_logger.setLevel(logging.INFO)

# create handlers, set levels, create formatter, and add handlers to the logger
# ... (configure your custom logger as needed)

# set the custom logger as the module logger
wreqs.configure_logger(custom_logger=custom_logger)

# all subsequent calls will use this custom logger
context = wreqs.wreq(some_request)
```

## Error Handling

The `wreqs` module is designed for simplicity and doesn't include complex error handling mechanisms. The context manager re-throws any errors that occur inside the wrapped request.

### `wreqs` Specific Errors

#### RetryRequestError

Thrown when all retry attempts have failed.

```python
from wreqs import wreq, RetryRequestError
import requests

def check_retry(response):
    return response.status_code >= 500

req = requests.Request("GET", "https://api.example.com/unstable-endpoint")

try:
    with wreq(req, max_retries=3, check_retry=check_retry) as response:
        print(response.json())
except RetryRequestError as e:
    print(f"All retry attempts failed: {e}")
```

### Common `requests` Exceptions

`wreqs` uses the `requests` library internally, so you may encounter these common exceptions:

1. `requests.exceptions.Timeout`: Raised when the request times out.
2. `requests.exceptions.ConnectionError`: Raised when there's a network problem (e.g., DNS failure, refused connection).
3. `requests.exceptions.RequestException`: The base exception class for all `requests` exceptions.

### Other Exceptions

Any other exceptions that can be raised by the code inside the `with wreq(...) as response:` block will be propagated as-is.

Example of handling multiple exception types:

```python
from wreqs import wreq, RetryRequestError
import requests

req = requests.Request("GET", "https://api.example.com/data")

try:
    with wreq(req, timeout=5) as response:
        data = response.json()
        process_data(data)
except DataProcessingError as e: # error thrown by process_data
    ...
```

This approach allows you to handle specific exceptions as needed while keeping error management straightforward.

## Development and Publishing

### Testing

Run tests locally:

```bash
pip install .[dev]
pytest
```

### CI/CD

This project uses GitHub Actions for Continuous Integration and Continuous Deployment:

1. **Pull Request Checks**: Automatically run tests on all pull requests to the main branch.
2. **Automated Publishing**: Triggers package build and publication to PyPI when a new version tag is pushed.

### Publishing a New Version

1. Create a new branch for the version bump:

   ```bash
   git checkout -b bump-vx.y.z
   ```

2. Update the version in `setup.py` following [Semantic Versioning](https://semver.org/).

3. Commit changes:

   ```bash
   git add setup.py
   git commit -m "pack: bump version to x.y.z"
   ```

4. Create and push a new tag on the bump branch:

   ```bash
   git tag vx.y.z
   git push origin bump-vx.y.z --tags
   ```

   Replace `x.y.z` with the new version number.

5. Push the branch and create a pull request:

   ```bash
   git push origin bump-vx.y.z
   ```

   Then create a pull request on GitHub from this branch to main.

6. After the pull request is approved and merged, the tag will be part of the main branch.

7. To publish the new version to PyPI:
   - Go to the "Actions" tab in your GitHub repository
   - Select the "Publish Python distribution to PyPI" workflow
   - Click "Run workflow"
   - Enter the version tag you created (e.g., v1.2.3) and click "Run workflow"

8. The GitHub Action will build, test, and publish the new version to PyPI based on the specified tag.

Note: This process allows you to control exactly when the package is published. You can create and push tags on feature branches without triggering the publish process, and you can choose to publish specific tags at any time using the manual workflow trigger. The tag becomes part of the main branch history when the pull request is merged.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/munozarturo/wreqs",
    "name": "wreqs",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": "http, requests, wrapper, retry, timeout",
    "author": "Arturo Munoz",
    "author_email": "munoz.arturoroman@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/8a/42/79de294605d3d9c8dfc365a93b39f6e3281e1e3e80b0bd21f5f75000624c/wreqs-1.2.1.tar.gz",
    "platform": null,
    "description": "<div align=\"center\">\n  <picture>\n    <source media=\"(prefers-color-scheme: dark)\" srcset=\"https://www.munozarturo.com/assets/wreqs/logo-github-dark.svg\">\n    <source media=\"(prefers-color-scheme: light)\" srcset=\"https://www.munozarturo.com/assets/wreqs/logo-github-light.svg\">\n    <img alt=\"wreqs\" src=\"https://www.munozarturo.com/assets/wreqs/logo-github-light.svg\" width=\"50%\" height=\"40%\">\n  </picture>\n</div>\n\n<!-- omit from toc -->\n# wreqs: wrapped requests\n\nThe **wreqs** module is a powerful wrapper around the popular `requests` library, designed to simplify and enhance HTTP request handling in Python. It provides a context manager for making HTTP requests with built-in retry logic, timeout handling, and session management.\n\n<!-- omit from toc -->\n## Table of Contents\n\n- [Installation](#installation)\n- [Quick Start Guide](#quick-start-guide)\n- [Advanced Usage](#advanced-usage)\n  - [Making Multiple Requests with the Same Session](#making-multiple-requests-with-the-same-session)\n  - [Implementing Custom Retry Logic](#implementing-custom-retry-logic)\n  - [Handling Timeouts](#handling-timeouts)\n  - [Using Retry Callbacks](#using-retry-callbacks)\n  - [Using Proxy Rotation](#using-proxy-rotation)\n- [Logging Configuration](#logging-configuration)\n  - [Default Logging](#default-logging)\n  - [Configuring the Logger](#configuring-the-logger)\n  - [Using a Custom Logger](#using-a-custom-logger)\n- [Error Handling](#error-handling)\n  - [`wreqs` Specific Errors](#wreqs-specific-errors)\n    - [RetryRequestError](#retryrequesterror)\n  - [Common `requests` Exceptions](#common-requests-exceptions)\n  - [Other Exceptions](#other-exceptions)\n- [Development and Publishing](#development-and-publishing)\n  - [Testing](#testing)\n  - [CI/CD](#cicd)\n  - [Publishing a New Version](#publishing-a-new-version)\n\n## Installation\n\nTo install the `wreqs` module, use pip:\n\n```bash\npip install wreqs\n```\n\n## Quick Start Guide\n\nGetting started with the `wreqs` module is simple. Follow these steps to make your first wrapped request:\n\n1. First, install the module:\n\n   ```bash\n   pip install wreqs\n   ```\n\n2. Import the necessary components:\n\n   ```python\n   from wreqs import wreq\n   import requests\n   ```\n\n3. Create a request object:\n\n   ```python\n   req = requests.Request(\"GET\", \"https://api.example.com/data\")\n   ```\n\n4. Use the `wreq` context manager to make the request:\n\n   ```python\n   with wreq(req) as response:\n       print(response.status_code)\n       print(response.json())\n   ```\n\nThat's it! You've now made a request using `wreqs`. This simple example demonstrates the basic usage, but `wreqs` offers much more functionality, including retry mechanisms, timeout handling, and custom session management.\n\nHere's a slightly more advanced example that includes a retry check:\n\n```python\nfrom wreqs import wreq\nimport requests\n\ndef check_retry(response: requests.Response) -> bool:\n    return response.status_code >= 500\n\nreq = requests.Request(\"GET\", \"https://api.example.com/data\")\nwith wreq(req, max_retries=3, check_retry=check_retry) as response:\n    print(response.json())\n```\n\nThis example will retry the request up to 3 times if it receives a 5xx status code. If all requests fail it will throw a `RequestRetryError` see [Error Handling](#error-handling) for more information on errors.\n\nFor more advanced usage and configuration options, please refer to the subsequent sections of this documentation.\n\n## Advanced Usage\n\nThe `wreqs` module offers several advanced features to handle complex scenarios and improve your HTTP request workflow.\n\n### Making Multiple Requests with the Same Session\n\n`wreqs` provides a convenient `wreqs_session` context manager that automatically manages session creation, use, and cleanup. This simplifies the process of making multiple requests with the same session.\n\nHere's an example that demonstrates how to use `wreqs_session` for authentication and subsequent data retrieval:\n\n```python\nfrom wreqs import wreq, wreqs_session\nfrom requests import Request\n\nwith wreqs_session():\n    # authentication request\n    auth_req = Request(\"POST\", \"https://api.example.com/login\", json={\n        \"username\": \"user\",\n        \"password\": \"pass\"\n    })\n    with wreq(auth_req) as auth_response:\n        if auth_response.status_code != 200:\n            raise Exception(\"Failed to authenticate.\")\n\n    # data request using the same authenticated session\n    data_req = Request(\"GET\", \"https://api.example.com/protected-data\")\n    with wreq(data_req) as data_response:\n        print(data_response.json())\n```\n\nIn this example, the `wreqs_session` context manager automatically creates and manages a session for all requests within its block. The first request authenticates the user, and the second request uses the same session to access protected data. The session automatically handles cookies and other state information between requests.\n\nThis approach is equivalent to manually creating and managing a session, as shown in the following example:\n\n```python\nimport requests\nfrom wreqs import wreq\n\nsession = requests.Session()\n\nauth_req = requests.Request(...)\nwith wreq(auth_req, session=session) as auth_response: # session explicitly defined\n    ...\n\ndata_req = requests.Request(...)\nwith wreq(data_req, session=session) as data_response: # session explicitly defined\n    ...\n```\n\nIt is still possible to use a different session within a `wreqs_session` context so long as it is explicitly defined.\n\n```python\nfrom wreqs import wreq, wreqs_session\nfrom requests import Request, Session\n\nwith wreqs_session():\n    auth_req = Request(...)\n    with wreq(auth_req) as auth_response: # will use wreqs_session\n        ...\n\n    other_session = Session()\n    data_req = Request(...)\n    with wreq(data_req, session=other_session) as data_response: # will use other_session\n        ...\n```\n\n### Implementing Custom Retry Logic\n\nThe `wreqs` module allows you to implement custom retry logic using the `check_retry` parameter. This function should return `True` if a retry should be attempted, and `False` otherwise.\n\nHere's an example that retries on specific status codes and implements an exponential backoff:\n\n```python\nimport time\nfrom wreqs import wreq\nimport requests\n\ndef check_retry_with_backoff(response: requests.Response) -> bool:\n    if response.status_code in [429, 500, 502, 503, 504]:\n        retry_after = int(response.headers.get(\"Retry-After\", 0))\n        time.sleep(max(retry_after, 2 ** (response.request.retry_count - 1)))\n        return True\n    return False\n\nreq = requests.Request(\"GET\", \"https://api.example.com/data\")\nwith wreq(req, max_retries=5, check_retry=check_retry_with_backoff) as response:\n    print(response.json())\n```\n\nThis example retries on specific status codes and implements an exponential backoff strategy.\n\n### Handling Timeouts\n\n`wreqs` allows you to set timeouts for your requests to prevent them from hanging indefinitely. Here\"s how you can use the timeout feature:\n\n```python\nfrom wreqs import wreq\nimport requests\n\nreq = requests.Request(\"GET\", \"https://api.example.com/slow-endpoint\")\n\ntry:\n    with wreq(req, timeout=5) as response:\n        print(response.json())\nexcept requests.Timeout:\n    print(\"The request timed out after 5 seconds\")\n```\n\nThis example sets a 5-second timeout for the request. If the server doesn't respond within 5 seconds, a `Timeout` exception is raised.\n\n### Using Retry Callbacks\n\nYou can use the `retry_callback` parameter to perform actions before each retry attempt. This can be useful for logging, updating progress bars, or implementing more complex backoff strategies.\n\n```python\nimport time\nfrom wreqs import wreq\nimport requests\n\ndef check_retry(response: requests.Response) -> bool:\n    return response.status_code != 200\n\ndef retry_callback(response):\n    print(f\"Retrying request. Previous status code: {response.status_code}\")\n    time.sleep(2)  # Wait 2 seconds before retrying\n\nreq = requests.Request(\"GET\", \"https://api.example.com/unstable-endpoint\")\nwith wreq(req, check_retry=check_retry, retry_callback=retry_callback) as res:\n    print(res.json())\n```\n\nThis example prints a message and waits for 2 seconds before each retry attempt.\n\nThese advanced usage examples demonstrate the flexibility and power of the `wreqs` module. By leveraging these features, you can create robust and efficient HTTP request handling in your Python applications.\n\n### Using Proxy Rotation\n\n`wreqs` now supports proxy rotation, allowing you to distribute your requests across multiple proxies. This can be useful for avoiding rate limits or accessing region-restricted content. Here's how to use this feature:\n\n```python\nfrom wreqs import wreq\nimport requests\n\nproxies = [\n    \"http://proxy1.example.com:8080\",\n    \"http://proxy2.example.com:8080\",\n    \"http://proxy3.example.com:8080\",\n]\n\nreq = requests.Request(\"GET\", \"https://api.example.com/data\")\n\nwith wreq(req, max_retries=3, proxies=proxies) as response:\n    print(response.json())\n```\n\nIn this example, wreqs will rotate through the provided list of proxies for each request or retry attempt. If a request fails, it will automatically use the next proxy in the list for the retry.\n\nNote:\n\n- Proxies should be provided as a list of strings in the format \"<http://host:port>\" or \"<https://host:port>\".\n- The proxy rotation is done in a round-robin fashion.\n- If no proxies are provided, wreqs will use the default network connection.\n\n## Logging Configuration\n\nThe `wreqs` module provides flexible logging capabilities to help you track and debug your HTTP requests. You can configure logging at the module level, which will apply to all subsequent uses of `wreq`.\n\n### Default Logging\n\nOut of the box, `wreqs` uses a default logger with minimal configuration:\n\n```python\nimport wreqs\n\ncontext = wreqs.wreq(some_request)\n```\n\nThis will use the default logger, which outputs to the console at the INFO level.\n\n### Configuring the Logger\n\nYou can configure the logger using the `configure_logger` function:\n\n```python\nimport logging\nimport wreqs\n\nwreqs.configure_logger(\n    level=logging.DEBUG,\n    format=\"%(asctime)s - %(name)s - %(levelname)s - %(message)s\",\n    filename=\"wreqs.log\"\n)\n\n# all subsequent calls will use this logger configuration\ncontext1 = wreqs.wreq(some_request)\ncontext2 = wreqs.wreq(another_request)\n```\n\n### Using a Custom Logger\n\nFor more advanced logging needs, you can create and configure your own logger and set it as the module logger:\n\n```python\nimport logging\nimport wreqs\n\n# create and configure a custom logger\ncustom_logger = logging.getLogger(\"my_app.wreqs\")\ncustom_logger.setLevel(logging.INFO)\n\n# create handlers, set levels, create formatter, and add handlers to the logger\n# ... (configure your custom logger as needed)\n\n# set the custom logger as the module logger\nwreqs.configure_logger(custom_logger=custom_logger)\n\n# all subsequent calls will use this custom logger\ncontext = wreqs.wreq(some_request)\n```\n\n## Error Handling\n\nThe `wreqs` module is designed for simplicity and doesn't include complex error handling mechanisms. The context manager re-throws any errors that occur inside the wrapped request.\n\n### `wreqs` Specific Errors\n\n#### RetryRequestError\n\nThrown when all retry attempts have failed.\n\n```python\nfrom wreqs import wreq, RetryRequestError\nimport requests\n\ndef check_retry(response):\n    return response.status_code >= 500\n\nreq = requests.Request(\"GET\", \"https://api.example.com/unstable-endpoint\")\n\ntry:\n    with wreq(req, max_retries=3, check_retry=check_retry) as response:\n        print(response.json())\nexcept RetryRequestError as e:\n    print(f\"All retry attempts failed: {e}\")\n```\n\n### Common `requests` Exceptions\n\n`wreqs` uses the `requests` library internally, so you may encounter these common exceptions:\n\n1. `requests.exceptions.Timeout`: Raised when the request times out.\n2. `requests.exceptions.ConnectionError`: Raised when there's a network problem (e.g., DNS failure, refused connection).\n3. `requests.exceptions.RequestException`: The base exception class for all `requests` exceptions.\n\n### Other Exceptions\n\nAny other exceptions that can be raised by the code inside the `with wreq(...) as response:` block will be propagated as-is.\n\nExample of handling multiple exception types:\n\n```python\nfrom wreqs import wreq, RetryRequestError\nimport requests\n\nreq = requests.Request(\"GET\", \"https://api.example.com/data\")\n\ntry:\n    with wreq(req, timeout=5) as response:\n        data = response.json()\n        process_data(data)\nexcept DataProcessingError as e: # error thrown by process_data\n    ...\n```\n\nThis approach allows you to handle specific exceptions as needed while keeping error management straightforward.\n\n## Development and Publishing\n\n### Testing\n\nRun tests locally:\n\n```bash\npip install .[dev]\npytest\n```\n\n### CI/CD\n\nThis project uses GitHub Actions for Continuous Integration and Continuous Deployment:\n\n1. **Pull Request Checks**: Automatically run tests on all pull requests to the main branch.\n2. **Automated Publishing**: Triggers package build and publication to PyPI when a new version tag is pushed.\n\n### Publishing a New Version\n\n1. Create a new branch for the version bump:\n\n   ```bash\n   git checkout -b bump-vx.y.z\n   ```\n\n2. Update the version in `setup.py` following [Semantic Versioning](https://semver.org/).\n\n3. Commit changes:\n\n   ```bash\n   git add setup.py\n   git commit -m \"pack: bump version to x.y.z\"\n   ```\n\n4. Create and push a new tag on the bump branch:\n\n   ```bash\n   git tag vx.y.z\n   git push origin bump-vx.y.z --tags\n   ```\n\n   Replace `x.y.z` with the new version number.\n\n5. Push the branch and create a pull request:\n\n   ```bash\n   git push origin bump-vx.y.z\n   ```\n\n   Then create a pull request on GitHub from this branch to main.\n\n6. After the pull request is approved and merged, the tag will be part of the main branch.\n\n7. To publish the new version to PyPI:\n   - Go to the \"Actions\" tab in your GitHub repository\n   - Select the \"Publish Python distribution to PyPI\" workflow\n   - Click \"Run workflow\"\n   - Enter the version tag you created (e.g., v1.2.3) and click \"Run workflow\"\n\n8. The GitHub Action will build, test, and publish the new version to PyPI based on the specified tag.\n\nNote: This process allows you to control exactly when the package is published. You can create and push tags on feature branches without triggering the publish process, and you can choose to publish specific tags at any time using the manual workflow trigger. The tag becomes part of the main branch history when the pull request is merged.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Simplified and enhanced request handling.",
    "version": "1.2.1",
    "project_urls": {
        "Bug Reports": "https://github.com/munozarturo/wreqs/issues",
        "Homepage": "https://github.com/munozarturo/wreqs",
        "Source": "https://github.com/munozarturo/wreqs/"
    },
    "split_keywords": [
        "http",
        " requests",
        " wrapper",
        " retry",
        " timeout"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4ec204aa782eaeb355135e93427cd25657f638377eecc6f90a2bae82e0f56893",
                "md5": "c82f3d762b94179ac6d5eb6ed6d48090",
                "sha256": "285771f56ff12c743bc03c5fa8cf3268df525ee6d248b8f9d9278030c1d5b228"
            },
            "downloads": -1,
            "filename": "wreqs-1.2.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c82f3d762b94179ac6d5eb6ed6d48090",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 14073,
            "upload_time": "2024-07-24T20:39:27",
            "upload_time_iso_8601": "2024-07-24T20:39:27.261247Z",
            "url": "https://files.pythonhosted.org/packages/4e/c2/04aa782eaeb355135e93427cd25657f638377eecc6f90a2bae82e0f56893/wreqs-1.2.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8a4279de294605d3d9c8dfc365a93b39f6e3281e1e3e80b0bd21f5f75000624c",
                "md5": "9c56c82e3e4922463db5ad2c52514ee4",
                "sha256": "5ae9be394780577d948bc31c72c19e7f112d7251bca8735d11ffa9d6987c11f1"
            },
            "downloads": -1,
            "filename": "wreqs-1.2.1.tar.gz",
            "has_sig": false,
            "md5_digest": "9c56c82e3e4922463db5ad2c52514ee4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 19196,
            "upload_time": "2024-07-24T20:39:28",
            "upload_time_iso_8601": "2024-07-24T20:39:28.198466Z",
            "url": "https://files.pythonhosted.org/packages/8a/42/79de294605d3d9c8dfc365a93b39f6e3281e1e3e80b0bd21f5f75000624c/wreqs-1.2.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-24 20:39:28",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "munozarturo",
    "github_project": "wreqs",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "wreqs"
}
        
Elapsed time: 4.57238s