Skip to content

Commit

Permalink
Add dev_mode option (#75)
Browse files Browse the repository at this point in the history
* Add `dev_mode` option to make running against a local instance of Metron easier
* Update deps
  • Loading branch information
bpepple authored Jan 27, 2025
1 parent 13545a3 commit db09eea
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 38 deletions.
6 changes: 5 additions & 1 deletion mokkari/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def api(
passwd: str | None = None,
cache: sqlite_cache.SqliteCache | None = None,
user_agent: str | None = None,
dev_mode: bool = False,
) -> session.Session:
"""Entry function the sets login credentials for metron.cloud.
Expand All @@ -22,6 +23,7 @@ def api(
cache (SqliteCache): SqliteCache to use
user_agent (str): The user agent string for the application using Mokkari.
For example 'Foo Bar/1.0'.
dev_mode (bool): Whether the library should be run against a local Metron instance.
Returns:
-------
Expand All @@ -39,4 +41,6 @@ def api(
if username is None or passwd is None:
raise exceptions.AuthenticationError

return session.Session(username, passwd, cache=cache, user_agent=user_agent)
return session.Session(
username, passwd, cache=cache, user_agent=user_agent, dev_mode=dev_mode
)
6 changes: 5 additions & 1 deletion mokkari/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
from mokkari.schemas.universe import Universe

METRON_MINUTE_RATE_LIMIT = 30
METRON_URL = "https://metron.cloud/api/{}/"
LOCAL_URL = "http://127.0.0.1:8000/api/{}/"


def rate_mapping(*args: any, **kwargs: any) -> tuple[str, int]: # NOQA: ARG001
Expand All @@ -50,6 +52,7 @@ class Session:
passwd: A string representing the password for authentication.
cache: An optional SqliteCache object for caching data.
user_agent: An optional string representing the user agent for the session.
dev_mode: A boolean indicating whether the session should be run against a local Metron instance.
"""

_minute_rate = Rate(METRON_MINUTE_RATE_LIMIT, Duration.MINUTE)
Expand All @@ -64,6 +67,7 @@ def __init__(
passwd: str,
cache: sqlite_cache.SqliteCache | None = None,
user_agent: str | None = None,
dev_mode: bool = False,
) -> None:
"""Initialize a Session object with the provided username, password, cache, and user agent."""
self.username = username
Expand All @@ -72,7 +76,7 @@ def __init__(
"User-Agent": f"{f'{user_agent} ' if user_agent is not None else ''}"
f"Mokkari/{__version__} ({platform.system()}; {platform.release()})"
}
self.api_url = "https://metron.cloud/api/{}/"
self.api_url = LOCAL_URL if dev_mode else METRON_URL
self.cache = cache

def _call(
Expand Down
72 changes: 36 additions & 36 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit db09eea

Please sign in to comment.