Skip to content

github_gist

Class info

Classes

Name Children Inherits
GistSharer
anyenv.text_sharing.github_gist
Share text via GitHub Gists.

    🛈 DocStrings

    GitHub Gist text sharing provider.

    GistSharer

    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"])
    

    main async

    main() -> None
    

    Example usage of the GistSharer class.

    Source code in src/anyenv/text_sharing/github_gist.py
    async def main() -> None:
        """Example usage of the GistSharer class."""
        sharer = GistSharer()
        result = await sharer.share("# Test Gist!", title="test.md", syntax="md")
        print(f"URL: {result.url}")
        print(f"Raw: {result.raw_url}")
        print(f"ID: {result.id}")