API Reference¶
This page contains the full reference to this package’s API.
Classes¶
- [source]class ntfy_api.actions.BroadcastAction(label: str, intent: str | None = None, extras: Mapping[str, str] | None = None, clear: bool | None = None)¶
Bases:
_ActionA serializable broadcast action.
- Parameters:
- [source]class ntfy_api.creds.Credentials(basic: str | tuple[str, str] | None = None, bearer: str | None = None)¶
Stores access credentials.
- Parameters:
basic¶ (str | tuple[str, str] | None, optional) – Basic HTTP credentials. If a single
stris given, it is assumed to already be base64-encoded (encoded from astrof the format<user>:<pass>). If a 2-tuple is given, it should be of the form(<user>, <pass>).bearer¶ (str | None, optional) – Bearer HTTP credentials. If both
basicandbearerare defined,bearerwill be used.
- [source]method get_header() MappingProxyType¶
Create the authorization header with the given credentials. Result may be empty.
- Returns:
The authentication header as an immutable mapping.
- Return type:
- Raises:
ValueError – If no valid credentials are present.
- [source]class ntfy_api.enums.Event¶
Bases:
StrEnumRepresents an event in
ReceivedMessageinstances.
- [source]class ntfy_api.filter.Filter(since: str | int | None = None, scheduled: bool | None = None, id: str | None = None, message: str | None = None, title: str | None = None, priority: int | Iterable[int] | None = None, tags: str | Iterable[str] | None = None)¶
Bases:
_FilterA filter dataclass that stores filtering preferences used when polling and subscribing.
- Parameters:
since¶ (str | int | None) – Return cached messages since timestamp, duration or message ID.
scheduled¶ (bool | None) – Include scheduled/delayed messages in message response.
id¶ (str | None) – Only return messages that match this exact message ID.
message¶ (str | None) – Only return messages that match this exact message string.
title¶ (str | None) – Only return messages that match this exact title string.
priority¶ (int | Iterable[int] | None) – Only return messages that match any priority given.
tags¶ (str | Iterable[str] | None) – Only return messages that match all listed tags.
- [source]class ntfy_api.actions.HTTPAction(label: str, url: str, method: str | None = None, headers: Mapping[str, str] | None = None, body: str | None = None, clear: bool | None = None)¶
Bases:
_ActionA serializable HTTP action.
- [source]class ntfy_api.enums.HTTPMethod¶
Bases:
StrEnumRepresents an HTTP method (e.g. GET, POST, and so on).
- [source]class ntfy_api.message.Message(topic: str | None = None, message: str | None = None, title: str | None = None, priority: int | None = None, tags: str | Iterable[str] | None = None, markdown: bool | None = None, delay: int | str | None = None, templating: bool | None = None, actions: Iterable[_Action] | None = None, click: str | None = None, attachment: str | None = None, filename: str | None = None, icon: str | None = None, email: str | None = None, call: str | None = None, cache: bool | None = None, firebase: bool | None = None, unified_push: bool | None = None, data: BinaryIO | dict[str, Any] | None = None)¶
Bases:
_MessageRepresents a message that will be published via
NtfyClient.publish().- Parameters:
tags¶ (str | Iterable[str]) – A set of tags that will be displayed along with the message. If the tag is a supported emoji short code (i.e. those found in the
Tagenum), they will be displayed to the left of the title. Unrecognized tags will be displayed below the message.markdown¶ (bool) – Whether or not to render the message with markdown.
delay¶ (int | str) – When to schedule the message. This should be either a unix timestamp or a natural-language date/time or offset (see https://github.com/olebedev/when). Any value between 10 seconds and 3 days may be used.
templating¶ (bool) – If
True,datawill be interpreted as JSON and used as the template context of Go-styled templates in themessageandtitle. Mutually exclusive withfilename.actions¶ (Iterable[_Action]) – Actions attached to this message.
click¶ (str) – The redirect URL that will be activated when the notification is clicked.
attachment¶ (str) – An externally-hosted attachment to be included in the message.
filename¶ (str) – The filename of the included attachment. If not provided, it will be derived from the URL resource identifier. Mutually exclusive with
templating.icon¶ (str) – An externally-hosted icon that will be displayed next to the notification.
email¶ (str) – The email address to forward this message to.
cache¶ (bool) – Whether or not to make use of the message cache.
unified_push¶ (bool) – Whether or not to enable UnifiedPush.
data¶ (BinaryIO | dict[str, Any]) –
If defined, it should be one of the following:
- If sending a local file as an attachment, the raw
attachment file data. If doing so, it is recommended to use the
filenameoption in addition to this.
If using templating, the template data.
- ivar templating: bool | None¶
See the
templatingparameter.
- ivar attachment: str | None¶
See the
attachmentparameter.
- ivar unified_push: bool | None¶
See the
unified_pushparameter.
- [source]class ntfy_api.client.NtfyClient(base_url: str, default_topic: str | None = None, credentials: Credentials | None = None)¶
The class that handles publishing
Messageinstances and creation ofNtfySubscriptioninstances.- Parameters:
default_topic¶ (str, optional) – If provided, it will be used as a fallback topic in cases where no topic is provided in the
Messageinstance being published, or as an argument (publish.topic) to thepublishmethod.credentials¶ (Credentials, optional) – The user credentials, if any.
- ivar default_topic: str | None¶
See the
default_topicparameter.
- ivar credentials: Credentials | None¶
See the
credentialsparameter.
- [source]method connect(client: Client | None = None) Self¶
Start a new connection pool.
- Parameters:
client¶ (httpx.Client | None, optional) – If defined, uses the provided client. Otherwise, a new one is created.
- Returns:
This
NtfyClientinstance.- Return type:
- [source]method publish(msg: Message, topic: str | None = None, topic_override: bool = False) Response¶
Publish a
Messageinstance.Note
If no connection pool exists (i.e.
connect()wasn’t called and this client is not being used as a context manager), a single-use connection will be used.- Parameters:
msg¶ (Message) – The
Messageinstance containing the content to publish.topic¶ (str | None, optional) – A topic that will be used if no topic was provided in the given Message object.
topic_override¶ (bool, optional) – A flag that, if set to
True, will cause the giventopic(if provided) to be used regardless of whether or not a topic is present in the givenMessageinstance.
- Returns:
The httpx.Response instance.
- Return type:
httpx.Response
- Raises:
ValueError – If no topic was provided as an argument, no topic was found in the provided
Messageinstance, and nodefault_topicwas given when creating thisNtfyClientinstance.
- [source]for ... in poll(topic: str | None = None, filter: Filter | None = None) Generator[ReceivedMessage, None, None]¶
Poll for messages using an HTTP connection.
Note
If no connection pool exists (i.e.
connect()wasn’t called and this client is not being used as a context manager), a single-use connection will be used.- Parameters:
- Returns:
A generator of
ReceivedMessageinstances.- Return type:
- Raises:
ValueError – If no topic was provided as an argument, and no
default_topicwas given when creating thisNtfyClientinstance.
- [source]method subscribe(topics: str | Iterable[str] | None = None, filter: Filter | None = None, max_queue_size: int = 0) NtfySubscription¶
A factory for
NtfySubscriptioninstances, which uses the same base URL and credentials as thisNtfyClientinstance.Note
The
NtfySubscriptioninstance is created but the connection is not yet started. Use itsNtfySubscription.connect()method or the context manager protocol to start receiving messages.- Parameters:
topics¶ (str | Iterable[str] | None) – Zero or more topics to subscribe to. If none are given, this instance’s default topic will be used.
filter¶ (Filter | None) – An optional
Filterinstance used to filter responses.max_queue_size¶ (int, optional) – The maximum size of the message queue. If
<=0, the queue is unbounded. If the queue is filled, all new messages are discarded. Only when the queue has room for another message, will messages start being added again. This means that, if bounded, some messages may be dropped if the frequency of received messages is greater than your program’s ability to handle those messages. Defaults to0.
- Returns:
The created
NtfySubscriptioninstance.- Return type:
- Raises:
ValueError – If both the
subscribe.topicsargument and this instance’sdefault_topicareNone.
- [source]method __enter__() Self¶
Enter the context manager protocol.
- Returns:
This
NtfyClientinstance.- Return type:
- [source]method __exit__(exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None) Literal[False]¶
Exit the context manager protocol.
This ensures the client is closed.
- Returns:
Always
False. Seeobject.__exit__()for more information on what this return value means.- Return type:
Literal[False]
- [source]class ntfy_api.subscription.NtfySubscription(base_url: str, topics: tuple[str] | tuple[str, ...], credentials: Credentials | None = None, filter: Filter | None = None, max_queue_size: int = 0)¶
The class that handles subscriptions.
- Parameters:
base_url¶ – The base URL of a ntfy server.
topics¶ – The topics to subscribe to.
credentials¶ – The user credentials, if any.
filter¶ – Optional response filters.
max_queue_size¶ – The maximum size of the message queue. If
<=0, the queue is unbounded. If the queue is filled, all new messages are discarded. Only when the queue has room for another message, will messages start being added again. This means that, if bounded, some messages may be dropped if the frequency of received messages is greater than your program’s ability to handle those messages.
- ivar credentials: Credentials | None¶
See the
credentialsparameter.
- ivar max_queue_size: int¶
See the
max_queue_sizeparameter.
- ivar messages: ClearableQueue[ReceivedMessage]¶
The message queue.
This attribute stores received messages. See
queue.Queuefor details on how to interact with this attribute.
- [source]method connect(connection: ClientConnection | None = None) Self¶
Initiate the websocket connection.
Note
This also clears
messages.- Parameters:
connection¶ (websockets.sync.client.ClientConnection | None, optional) – The websocket connection to use. If not provided, one will be created.
- Returns:
This
NtfySubscriptioninstance.- Return type:
- [source]method __enter__() Self¶
Enter the context manager protocol.
- Returns:
The
NtfySubscriptioninstance.- Return type:
- [source]method __exit__(exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None) Literal[False]¶
Exit the context manager protocol.
This ensures the client is closed.
- Returns:
Always
False. Seeobject.__exit__()for more information on what this return value means.- Return type:
Literal[False]
- [source]class ntfy_api.enums.Priority¶
Bases:
IntEnumRepresents message priority in
MessageandReceivedMessage.
- [source]class ntfy_api.message.ReceivedAttachment(*args, **kwargs)¶
Bases:
ProtocolProtocol defining the interface for an attachment in a received message.
- [source]class ntfy_api.actions.ReceivedBroadcastAction(id: str, label: str, clear: bool, intent: str | None = None, extras: dict[str, str] | None = None)¶
Bases:
WrappingDataclassA received broadcast action. Similar to
BroadcastAction, but cannot be serialized.- Parameters:
- classmethod from_json(data: Mapping[str, Any]) Self¶
Parse a new
WrappingDataclassinstance from the given data.Note
datais not modified when wrappers (if any) are applied. Instead, a shallow copy of the mapping is created and used. Keep in mind that, because it is a shallow copy, the wrappers may still modify the mapping values in-place.- Parameters:
- Returns:
The parsed
WrappingDataclassinstance.- Return type:
- [source]class ntfy_api.actions.ReceivedHTTPAction¶
Bases:
WrappingDataclassA received HTTP action. Similar to
HTTPAction, but cannot be serialized.- Parameters:
- ivar method: HTTPMethod | None¶
See the
methodparameter.
- classmethod from_json(data: Mapping[str, Any]) Self¶
Parse a new
WrappingDataclassinstance from the given data.Note
datais not modified when wrappers (if any) are applied. Instead, a shallow copy of the mapping is created and used. Keep in mind that, because it is a shallow copy, the wrappers may still modify the mapping values in-place.- Parameters:
- Returns:
The parsed
WrappingDataclassinstance.- Return type:
- [source]class ntfy_api.message.ReceivedMessage¶
Bases:
ProtocolProtocol defining the interface for a message received from the ntfy server.
- Parameters:
tags¶ (tuple[Tag, ...] | None) – List of tags that may map to emojis.
click¶ (str | None) – Website opened when notification is clicked.
actions¶ (tuple[ReceivedAction, ...] | None) – Action buttons that can be displayed.
attachment¶ (ReceivedAttachment | None) – Details about an attachment if present.
- ivar actions: tuple[ReceivedViewAction | ReceivedBroadcastAction | ReceivedHTTPAction, ...] | None¶
See the
actionsparameter.
- ivar attachment: ReceivedAttachment | None¶
See the
attachmentparameter.
- ivar content_type: str | None¶
See the
content_typeparameter.
- [source]class ntfy_api.actions.ReceivedViewAction(id: str, label: str, url: str, clear: bool)¶
Bases:
WrappingDataclassA received view action. Similar to
ViewAction, but cannot be serialized.- Parameters:
- classmethod from_json(data: Mapping[str, Any]) Self¶
Parse a new
WrappingDataclassinstance from the given data.Note
datais not modified when wrappers (if any) are applied. Instead, a shallow copy of the mapping is created and used. Keep in mind that, because it is a shallow copy, the wrappers may still modify the mapping values in-place.- Parameters:
- Returns:
The parsed
WrappingDataclassinstance.- Return type:
- [source]class ntfy_api.enums.Tag¶
Bases:
StrEnumRepresents message tags in
MessageandReceivedMessage.
Constants¶
- ntfy_api.__version__.__author__ = 'Tanner Corcoran'¶
The package author.
This value is imported into all modules within this package.
- ntfy_api.__version__.__author_email__ = 'tannerbcorcoran@gmail.com'¶
The primary contact email of the package author.
This value is imported into all modules within this package.
- ntfy_api.__version__.__cookie__ = '✨ 🍪 ✨'¶
A cookie, just for you!
This value is imported into all modules within this package.
- ntfy_api.__version__.__copyright__ = 'Copyright (c) 2024 Tanner Corcoran'¶
The package copyright.
This value is imported into all modules within this package.
- ntfy_api.__version__.__description__ = 'A wrapper around the `ntfy <https://ntfy.sh>`_ API.'¶
A short description of this project.
This value is imported into all modules within this package.
- ntfy_api.__version__.__download_url__ = 'https://pypi.org/project/ntfy-api'¶
The download URL for this package.
This value is imported into all modules within this package.
- ntfy_api.__version__.__email__ = 'tannerbcorcoran@gmail.com'¶
The primary contact email of the package author.
This value is the same as
__author_email__and is simply used for compatability. It is imported into all modules within this package.
- ntfy_api.__version__.__license__ = 'Apache 2.0 License'¶
The package copyright.
This value is imported into all modules within this package.
- ntfy_api.__version__.__title__ = 'ntfy-api'¶
The package name/title.
Note
This value may be different than the module name.
This value is imported into all modules within this package.
- ntfy_api.__version__.__url__ = 'https://github.com/tanrbobanr/ntfy-api'¶
The URL to the package source repository.
This value is imported into all modules within this package.
- ntfy_api.__version__.__version__ = '1.0.0'¶
The package version.
This corresponds to the package release segment (see version specifiers for more information).
This value is imported into all modules within this package.
- ntfy_api.__version__.__release__ = '1.0.0.pre1'¶
The package release.
This corresponds to the package release, pre-release, post-release, and development release segments (see version specifiers for more information).
Note
This value may differ from
__version__, as__version__doesn’t include the pre-, post-, or dev-release segments.This value is imported into all modules within this package.
- ntfy_api.__version__.version_info: _version_info = (1, 0, 0, 1, 0, 0)¶
The version information. In many ways similar to
sys.version_info.This value is imported into all modules within this package.
Exceptions¶
Type aliases¶
- type alias ntfy_api.actions.ReceivedAction¶
An alias of
ReceivedViewAction|ReceivedBroadcastAction|ReceivedHTTPAction.