Skip to content

GistSharer

Base classes

Name Children Inherits
TextSharer
anyenv.text_sharing.base
Base class for text sharing services.

â‹” Inheritance diagram

graph TD
  1689502189["github_gist.GistSharer"]
  2782231920["base.TextSharer"]
  925645236["abc.ABC"]
  2751680898["builtins.object"]
  2782231920 --> 1689502189
  925645236 --> 2782231920
  2751680898 --> 925645236

🛈 DocStrings

Bases: TextSharer

Share text via GitHub Gists.

Source code in src/anyenv/text_sharing/github_gist.py
class GistSharer(TextSharer):
    """Share text via GitHub Gists."""

    def __init__(self, token: str | None = None) -> None:
        """Initialize the GitHub Gist sharer.

        Args:
            token: GitHub personal access token. If not provided,
                   reads from GITHUB_TOKEN environment variable.
        """
        self._token = token or os.environ.get("GITHUB_TOKEN") or os.environ.get("GH_TOKEN")
        if not self._token:
            msg = "GitHub token required. Set GITHUB_TOKEN/GH_TOKEN or pass token parameter."
            raise ValueError(msg)

    @property
    def name(self) -> str:
        """Name of the sharing service."""
        return "GitHub Gist"

    async def share(
        self,
        content: str,
        *,
        title: str | None = None,
        syntax: str | None = None,
        visibility: Visibility = "unlisted",
        expires_in: int | None = None,
    ) -> ShareResult:
        """Share content as a GitHub Gist.

        Args:
            content: The text content to share
            title: Filename for the gist (defaults to "shared.txt" or "shared.{syntax}")
            syntax: File extension hint (e.g. "py", "md")
            visibility: "public" or "unlisted" (private not supported)
            expires_in: Ignored (gists don't expire)
        """
        import anyenv

        filename = title or f"shared.{syntax or 'txt'}"
        public = visibility == "public"
        payload: dict[str, Any] = {"files": {filename: {"content": content}}, "public": public}
        headers = {
            "Authorization": f"Bearer {self._token}",
            "Accept": "application/vnd.github+json",
            "X-GitHub-Api-Version": "2022-11-28",
        }
        url = "https://api.github.com/gists"
        response: dict[str, Any] = await anyenv.post_json(url, payload, headers=headers)
        raw_url = response["files"][filename]["raw_url"]
        return ShareResult(url=response["html_url"], raw_url=raw_url, id=response["id"])

name property

name: str

Name of the sharing service.

__init__

__init__(token: str | None = None) -> None

Initialize the GitHub Gist sharer.

Parameters:

Name Type Description Default
token str | None

GitHub personal access token. If not provided, reads from GITHUB_TOKEN environment variable.

None
Source code in src/anyenv/text_sharing/github_gist.py
def __init__(self, token: str | None = None) -> None:
    """Initialize the GitHub Gist sharer.

    Args:
        token: GitHub personal access token. If not provided,
               reads from GITHUB_TOKEN environment variable.
    """
    self._token = token or os.environ.get("GITHUB_TOKEN") or os.environ.get("GH_TOKEN")
    if not self._token:
        msg = "GitHub token required. Set GITHUB_TOKEN/GH_TOKEN or pass token parameter."
        raise ValueError(msg)

share async

share(
    content: str,
    *,
    title: str | None = None,
    syntax: str | None = None,
    visibility: Visibility = "unlisted",
    expires_in: int | None = None,
) -> ShareResult

Share content as a GitHub Gist.

Parameters:

Name Type Description Default
content str

The text content to share

required
title str | None

Filename for the gist (defaults to "shared.txt" or "shared.{syntax}")

None
syntax str | None

File extension hint (e.g. "py", "md")

None
visibility Visibility

"public" or "unlisted" (private not supported)

'unlisted'
expires_in int | None

Ignored (gists don't expire)

None
Source code in src/anyenv/text_sharing/github_gist.py
async def share(
    self,
    content: str,
    *,
    title: str | None = None,
    syntax: str | None = None,
    visibility: Visibility = "unlisted",
    expires_in: int | None = None,
) -> ShareResult:
    """Share content as a GitHub Gist.

    Args:
        content: The text content to share
        title: Filename for the gist (defaults to "shared.txt" or "shared.{syntax}")
        syntax: File extension hint (e.g. "py", "md")
        visibility: "public" or "unlisted" (private not supported)
        expires_in: Ignored (gists don't expire)
    """
    import anyenv

    filename = title or f"shared.{syntax or 'txt'}"
    public = visibility == "public"
    payload: dict[str, Any] = {"files": {filename: {"content": content}}, "public": public}
    headers = {
        "Authorization": f"Bearer {self._token}",
        "Accept": "application/vnd.github+json",
        "X-GitHub-Api-Version": "2022-11-28",
    }
    url = "https://api.github.com/gists"
    response: dict[str, Any] = await anyenv.post_json(url, payload, headers=headers)
    raw_url = response["files"][filename]["raw_url"]
    return ShareResult(url=response["html_url"], raw_url=raw_url, id=response["id"])

:fa-brands-github: Show source on GitHub