Skip to main content

Access your gotify server.

Project description

python-gotify

PyPI Python License Code Style CI Codecov

python-gotify is a python client library to interact with your gotify server without having to handle requests manually.

It offers both a synchronous and an asynchronous interface powered by httpx. Optionally, push messages can be received via websockets.

Installation

python-gotify can be installed from PyPi using pip:

pip install gotify

If you want to listen to push messages, the additional dependency on websockets can be installed with

pip install gotify[stream]

Usage

To send messages:

from gotify import Gotify

gotify = Gotify(
    base_url="https://gotify.example.com",
    app_token="AsWIJhvlHb.xgKe",
)

gotify.create_message(
    "Hello you wonderful people!",
    title="Hello World",
    priority=0,
)

Note: To send messages you need to create a new application and set app_token accordingly.

You can also manage things like applications:

from gotify import Gotify

gotify = Gotify(
    base_url="https://gotify.example.com",
    client_token="CoLwHBCAr8z2MMA",
)

app = gotify.create_application("foobar", description="test application")

print("Created new application:", app)

would result in

Created new application: {'id': 42, 'token': 'ArHD_yGYf63-A13', 'name': 'foobar', 'description': 'test application', 'internal': False, 'image': 'static/defaultapp.png'}

Note: For most things you need to create a new client and set client_token accordingly.

This library tries to implement every endpoint of the gotify API as an instance method of the Gotify class.

More details about the capabilities of gotify's API can be found in its API documentation.

Note: since I don't use any gotify plugins, plugin-related functions are currently completely untested.

Async Usage

python-gotify's asynchronous client works similar to the synchronous one, you just need to await all methods. It is recommended to use it as a context manager if you want to send multiple requests.

import asyncio
from gotify import AsyncGotify

async def send_message_async():
    async_gotify = AsyncGotify(
        base_url="https://gotify.example.com",
        app_token="AsWIJhvlHb.xgKe",
    )

    await async_gotify.create_message(
        "This message was sent asynchronously!",
        title="Hello Asynchronous World",
    )

asyncio.run(send_message_async())

Reusing HTTP sessions

If you want to send multiple requests to a server you can use both Gotify and AsyncGotify as a (asynchronous) context manager which will use a single HTTP session to reduce some connection overhead.

with Gotify(...) as gotify:
    ...

async with AsyncGotify(...) as async_gotify:
    ...

Receive push messages via websockets

AsyncGotify implements gotify's /stream endpoint which allows to receive push messages via websockets. To use it make sure you installed python-gotify with pip install gotify[stream].

AsyncGotify.stream() is implemented as an asynchronous generator that waits for incoming messages and yields Message dictionaries.

import asyncio
from gotify import AsyncGotify

async def log_push_messages():
    async_gotify = AsyncGotify(
        base_url="https://gotify.example.com",
        client_token="CoLwHBCAr8z2MMA",
    )

    async for msg in async_gotify.stream():
        print(msg)

asyncio.run(log_push_messages())

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please use nox to format, lint, type-check and test your code by calling nox in your project directory.

The test suite downloads a server binary and starts a preconfigured test server on port 30080 (this doesn't work on MacOS). If you encounter issues starting the test server, please create an issue.

License

This project is licensed under the MIT License.

See LICENSE for more information.

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

gotify-0.5.0.tar.gz (22.6 kB view details)

Uploaded Source

Built Distribution

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

gotify-0.5.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file gotify-0.5.0.tar.gz.

File metadata

  • Download URL: gotify-0.5.0.tar.gz
  • Upload date:
  • Size: 22.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.27.1

File hashes

Hashes for gotify-0.5.0.tar.gz
Algorithm Hash digest
SHA256 9244d2b035a1955cf6c8d14a3072ece104ce2ea27ebe6aa85acb166a1a99fce7
MD5 5f543f705ebe65e3132c8aa177c190e0
BLAKE2b-256 03ea6b7a8399b111508809fd5ba51c0f8d5591654429f26b194cd3b9db7b088b

See more details on using hashes here.

File details

Details for the file gotify-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: gotify-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.27.1

File hashes

Hashes for gotify-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f149e61bad38347d5a585c52f2c673a181b364541e596c041b5e41f2b9a38f4a
MD5 a0499d68bde9d22443c9cac7aeea33e0
BLAKE2b-256 fdcbdc3891f845cd84e1003c902930fa25b64aaa7bb5e36fe6af72045b2fd368

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