webpush


Namewebpush JSON
Version 1.0.2 PyPI version JSON
download
home_pagehttps://github.com/delvinru/webpush-py
SummaryWebPush library for python
upload_time2024-02-10 05:03:30
maintainer
docs_urlNone
authorAlexey
requires_python>=3.10,<4.0
licenseMIT
keywords webpush pwa pydantic
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # WebPush-Py

Simple library for working with [WebPush](https://web.dev/articles/push-notifications-web-push-protocol) in python

## Usage

### Installation

```bash
pip install webpush
```

### Basic Usage

```python
import requests
from webpush import WebPush, WebPushSubscription

wp = WebPush(private_key="./private_key.pem", public_key="./public_key.pem")

# example subscription info
subscription = WebPushSubscription.model_validate({
    "endpoint": "https://fcm.googleapis.com/fcm/send/...",
    "keys": {
        "auth": "...",
        "p256dh": "..."
    }
})

message = wp.get(message='Hello, world!', subscription=subscription)

requests.post(subscription.endpoint, data=message.encrypted, headers=message.headers)
```

Generate VAPID keys and get applicationServerKey:
```
vapid-gen
```

Private key saved in `public_key.pem` and public key saved in `public_key.pem`.
Application Server Key saved in `applicationServerKey`

### simple usage with fastapi

```python
import aiohttp
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from webpush import WebPush, WebPushSubscription

app = FastAPI()

wp = WebPush(
    public_key="./public_key.pem",
    private_key="./private_key.pem",
    subscriber="admin@mail.com",
)


@app.get("/notification/key")
async def get_public_key() -> JSONResponse:
    application_server_key = "<generated from vapid-gen>"
    return JSONResponse(content={"public_key": application_server_key})


@app.post("/notification/subscribe")
async def subscribe_user(subscription: WebPushSubscription) -> JSONResponse:
    message = wp.get(message="Hello, world", subscription=subscription)
    async with aiohttp.ClientSession() as session:
        await session.post(
            url=str(subscription.endpoint),
            data=message.encrypted,
            headers=message.headers,
        )
    return JSONResponse(content={"status": "ok"})
```

## FAQ
- Why do I need another library?

The current python libraries that work with Web Push have been written for a very long time, so they do not support typing, try to support outdated encryption algorithms and pull a lot of deprecated dependencies.

- Why is only `aes128gcm` supported?

According to the [RFC8192](https://datatracker.ietf.org/doc/html/rfc8291), this is the recommended format. At the moment, all modern systems support this encryption.

- Will there be support for other encryption modes?

New, yes, but there are no old ones, for example `aesgcm`

- Who is this library for?

You need type support, you're writing a modern backend, minimum number of dependencies.

And last one, if you have ideas for improvements, bug fixes, feel free to contribute.

## Change log

- 1.0.0 - initial release

## Credits

- [pywebpush](https://github.com/web-push-libs/pywebpush)
- [http-ece](https://github.com/web-push-libs/encrypted-content-encoding)
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/delvinru/webpush-py",
    "name": "webpush",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10,<4.0",
    "maintainer_email": "",
    "keywords": "webpush,pwa,pydantic",
    "author": "Alexey",
    "author_email": "delvinru@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/3e/43/a24b87dad97baf26df60f6cc40d51681b447b5eb4b2fd19e8d920404e1e0/webpush-1.0.2.tar.gz",
    "platform": null,
    "description": "# WebPush-Py\n\nSimple library for working with [WebPush](https://web.dev/articles/push-notifications-web-push-protocol) in python\n\n## Usage\n\n### Installation\n\n```bash\npip install webpush\n```\n\n### Basic Usage\n\n```python\nimport requests\nfrom webpush import WebPush, WebPushSubscription\n\nwp = WebPush(private_key=\"./private_key.pem\", public_key=\"./public_key.pem\")\n\n# example subscription info\nsubscription = WebPushSubscription.model_validate({\n    \"endpoint\": \"https://fcm.googleapis.com/fcm/send/...\",\n    \"keys\": {\n        \"auth\": \"...\",\n        \"p256dh\": \"...\"\n    }\n})\n\nmessage = wp.get(message='Hello, world!', subscription=subscription)\n\nrequests.post(subscription.endpoint, data=message.encrypted, headers=message.headers)\n```\n\nGenerate VAPID keys and get applicationServerKey:\n```\nvapid-gen\n```\n\nPrivate key saved in `public_key.pem` and public key saved in `public_key.pem`.\nApplication Server Key saved in `applicationServerKey`\n\n### simple usage with fastapi\n\n```python\nimport aiohttp\nfrom fastapi import FastAPI\nfrom fastapi.responses import JSONResponse\nfrom webpush import WebPush, WebPushSubscription\n\napp = FastAPI()\n\nwp = WebPush(\n    public_key=\"./public_key.pem\",\n    private_key=\"./private_key.pem\",\n    subscriber=\"admin@mail.com\",\n)\n\n\n@app.get(\"/notification/key\")\nasync def get_public_key() -> JSONResponse:\n    application_server_key = \"<generated from vapid-gen>\"\n    return JSONResponse(content={\"public_key\": application_server_key})\n\n\n@app.post(\"/notification/subscribe\")\nasync def subscribe_user(subscription: WebPushSubscription) -> JSONResponse:\n    message = wp.get(message=\"Hello, world\", subscription=subscription)\n    async with aiohttp.ClientSession() as session:\n        await session.post(\n            url=str(subscription.endpoint),\n            data=message.encrypted,\n            headers=message.headers,\n        )\n    return JSONResponse(content={\"status\": \"ok\"})\n```\n\n## FAQ\n- Why do I need another library?\n\nThe current python libraries that work with Web Push have been written for a very long time, so they do not support typing, try to support outdated encryption algorithms and pull a lot of deprecated dependencies.\n\n- Why is only `aes128gcm` supported?\n\nAccording to the [RFC8192](https://datatracker.ietf.org/doc/html/rfc8291), this is the recommended format. At the moment, all modern systems support this encryption.\n\n- Will there be support for other encryption modes?\n\nNew, yes, but there are no old ones, for example `aesgcm`\n\n- Who is this library for?\n\nYou need type support, you're writing a modern backend, minimum number of dependencies.\n\nAnd last one, if you have ideas for improvements, bug fixes, feel free to contribute.\n\n## Change log\n\n- 1.0.0 - initial release\n\n## Credits\n\n- [pywebpush](https://github.com/web-push-libs/pywebpush)\n- [http-ece](https://github.com/web-push-libs/encrypted-content-encoding)",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "WebPush library for python",
    "version": "1.0.2",
    "project_urls": {
        "Homepage": "https://github.com/delvinru/webpush-py",
        "Repository": "https://github.com/delvinru/webpush-py"
    },
    "split_keywords": [
        "webpush",
        "pwa",
        "pydantic"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bc356f63c7dcadce9e9933158752c995109394d7b5f76fde3dbcf82899b2d56d",
                "md5": "b3a8a207211807d0950f0c2b3bfbb7b0",
                "sha256": "658496ad9d348c0482a326282bbceef7ce9de8a2b41f05f8a9f059d2e6d3b667"
            },
            "downloads": -1,
            "filename": "webpush-1.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b3a8a207211807d0950f0c2b3bfbb7b0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10,<4.0",
            "size": 7403,
            "upload_time": "2024-02-10T05:03:28",
            "upload_time_iso_8601": "2024-02-10T05:03:28.877013Z",
            "url": "https://files.pythonhosted.org/packages/bc/35/6f63c7dcadce9e9933158752c995109394d7b5f76fde3dbcf82899b2d56d/webpush-1.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3e43a24b87dad97baf26df60f6cc40d51681b447b5eb4b2fd19e8d920404e1e0",
                "md5": "516300f08bf8685978e25e5c0354454d",
                "sha256": "890e004b21f4782a5feff2e477db974cf838dfa029cfeab1e600ad7f7d2ca284"
            },
            "downloads": -1,
            "filename": "webpush-1.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "516300f08bf8685978e25e5c0354454d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10,<4.0",
            "size": 5887,
            "upload_time": "2024-02-10T05:03:30",
            "upload_time_iso_8601": "2024-02-10T05:03:30.123851Z",
            "url": "https://files.pythonhosted.org/packages/3e/43/a24b87dad97baf26df60f6cc40d51681b447b5eb4b2fd19e8d920404e1e0/webpush-1.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-10 05:03:30",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "delvinru",
    "github_project": "webpush-py",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "webpush"
}
        
Elapsed time: 0.20306s