Skip to content

Commit

Permalink
Only require Heroku API key when accessing Heroku API
Browse files Browse the repository at this point in the history
  • Loading branch information
RealOrangeOne committed Sep 7, 2023
1 parent f522e8f commit 0f4e6bf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
33 changes: 28 additions & 5 deletions heroku_audit/client.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
import os
import sys
from typing import Any, cast

import heroku3
from heroku3.core import Heroku
from rich import print
from rich.text import Text

try:
heroku = heroku3.from_key(os.environ["HEROKU_API_KEY"])
except KeyError:
print(Text("Please set $HEROKU_API_KEY to a valid Heroku API key.", style="red"))
sys.exit(1)
__all__ = ["heroku"]


class LazyHerokuWrapper:
"""
A lazy heroku wrapper which only requires an API key when it's used
"""

_heroku = None

def __get__(self, obj: Any, objtype: Any = None) -> Heroku:
if self._heroku is None:
try:
self._heroku = heroku3.from_key(os.environ["HEROKU_API_KEY"])
except KeyError:
print(
Text(
"Please set $HEROKU_API_KEY to a valid Heroku API key.",
style="red",
)
)
sys.exit(1)
return self._heroku


heroku = cast(Heroku, LazyHerokuWrapper())
10 changes: 1 addition & 9 deletions stubs/heroku3/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
from requests import Session

from .models.app import App

class Heroku:
_session: Session

def apps(self) -> list[App]: ...
def app(self, id_or_name: str) -> App: ...
from .core import Heroku

def from_key(api_key: str) -> Heroku: ...
9 changes: 9 additions & 0 deletions stubs/heroku3/core.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from requests import Session

from .models.app import App

class Heroku:
_session: Session

def apps(self) -> list[App]: ...
def app(self, id_or_name: str) -> App: ...

0 comments on commit 0f4e6bf

Please sign in to comment.