Skip to main content

Python SDK for the Mosir public GraphQL API

Project description

mosir-sdk-python

Python SDK for the Mosir public GraphQL API.

Install

For package users:

pip install mosir-sdk-python

or:

uv add mosir-sdk-python

For local development in this repository:

uv sync

Quick example

Anonymous/public request

Only public data needs no token.

import asyncio

from mosir_sdk import AsyncMosirClient


async def main() -> None:
    async with AsyncMosirClient() as client:
        post = await client.get_post(post_id="VLO8u7UXqclQ7byjfMEX0")
        print(post["getPost"]["content"])


asyncio.run(main())

Authenticated request

Use a token for authenticated operations such as notifications.

import asyncio
import os

from mosir_sdk import AsyncMosirClient


async def main() -> None:
    async with AsyncMosirClient(token=os.getenv("MOSIR_API_TOKEN")) as client:
        notifications = await client.get_notifications(limit=20)
        print(notifications["getNotifications"])


asyncio.run(main())

More examples

Fetch media bytes from a Media result

import asyncio

from mosir_sdk import AsyncMosirClient


async def main() -> None:
    async with AsyncMosirClient() as client:
        post = await client.get_post(post_id="VLO8u7UXqclQ7byjfMEX0")
        media = post["getPost"]["attachments"][0]["media"]
        media_bytes = await client.fetch_media(media)
        print(len(media_bytes))


asyncio.run(main())

Fetch preview image for a post, profile, or collection

import asyncio

from mosir_sdk import AsyncMosirClient


async def main() -> None:
    async with AsyncMosirClient() as client:
        print(client.get_preview_image_url("post", "VLO8u7UXqclQ7byjfMEX0"))
        preview_bytes = await client.fetch_preview_image("post", "VLO8u7UXqclQ7byjfMEX0")
        print(len(preview_bytes))


asyncio.run(main())

SSE subscription example

Subscriptions let your app receive updates from Mosir in near real time without polling. This SDK uses SSE (Server-Sent Events) for subscriptions.

A good example is a Discord bot:

  • subscribe to post_created_by_author(...)
  • when a creator publishes something new, turn it into a message
  • send that message into a Discord channel

That way the bot reacts immediately instead of polling the API on a timer. SSE works especially well for long-running workers, bots, and notification bridges that only need a one-way event stream from the server. For public subscriptions like post_created_by_author(...), a token is not required.

Note: each SSE connection lasts at most 1 hour. In practice, network conditions may cause it to end earlier. If you build a bot, worker, or relay process, make sure you implement reconnect logic.

import asyncio

from mosir_sdk import AsyncMosirClient


async def main() -> None:
    async with AsyncMosirClient() as client:
        profile = await client.get_account_profile(username="leemiyinghao")
        author_id = profile["getAccountProfile"]["id"]

        async for event in client.post_created_by_author(
            author_id=author_id,
            post_type="POST",
        ):
            print(event["postCreatedByAuthor"]["id"])
            print(event["postCreatedByAuthor"]["content"])
            break


asyncio.run(main())

Notes

  • default endpoint: https://beta.mosir.app/api/v1
  • token is optional for public data and required only for authenticated operations
  • the same applies to subscriptions: public subscription data does not require a token
  • snake_case methods are resolved dynamically from the operation registry, for example:
    • get_post(...)
    • get_notifications(...)
    • post_created_by_author(...)
  • media helpers are available through:
    • select_media_file(...)
    • await client.fetch_media(...)
  • preview image helpers are available through:
    • get_preview_image_url(...)
    • await client.fetch_preview_image(...)
  • explicit operation access is also available:
    • await client.operation("get_post", post_id="...")
    • client.subscribe_operation("post_created_by_author", author_id="...", post_type="POST")
      • you can resolve author_id first with get_account_profile(username="...")
  • raw GraphQL is still available through:
    • await client.request(...)
    • client.subscribe(...)

Release / publish prep

Build and validate distributions before uploading to PyPI:

task build
task package-check

Typical upload flow:

uvx twine upload dist/*

Tasks

task install
task codegen
task pyright
task test
task build
task package-check
task smoke

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

mosir_sdk_python-0.1.1.tar.gz (32.9 kB view details)

Uploaded Source

Built Distribution

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

mosir_sdk_python-0.1.1-py3-none-any.whl (25.3 kB view details)

Uploaded Python 3

File details

Details for the file mosir_sdk_python-0.1.1.tar.gz.

File metadata

  • Download URL: mosir_sdk_python-0.1.1.tar.gz
  • Upload date:
  • Size: 32.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.4 {"installer":{"name":"uv","version":"0.11.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for mosir_sdk_python-0.1.1.tar.gz
Algorithm Hash digest
SHA256 af76eec2bc098444a663b6daa3ce92d419f367e34e9a444b11bd8fea526c10f2
MD5 f6b15462afc53068a8d77a164acf48d5
BLAKE2b-256 35f2b736b90f416c82538483475a657c7159b8ebe34c5c63ae7821cafdd0c03c

See more details on using hashes here.

File details

Details for the file mosir_sdk_python-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: mosir_sdk_python-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 25.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.4 {"installer":{"name":"uv","version":"0.11.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for mosir_sdk_python-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3bbd05a1ec1eeafc9c8e1f8c59bfe85b9dad2eb98dbbd3788f1d097825cd2ba8
MD5 f2286ffae6dd54e45a2789d61db51e5d
BLAKE2b-256 c5a0eadc9a240e3959ccae028d7805ee8a3636740529a904f25093b56df2fe73

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