Spaces:
Sleeping
Sleeping
Download modules/google/auth.py from sky-meilin/SkyOSFullCore: direct link, hf CLI and curl.
- Browser
- Download file 2.5 kB
-
https://huggingface.co/spaces/sky-meilin/SkyOSFullCore/resolve/main/modules/google/auth.py
- Command line
-
hf download hf://spaces/sky-meilin/SkyOSFullCore/modules/google/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/sky-meilin/SkyOSFullCore/resolve/main/modules/google/auth.py
2.5 kB
| """ | |
| auth.py | |
| SkyOS Google Authentication | |
| Stellt authentifizierte Google Services für alle Module bereit. | |
| Autor: Sky Meilin | |
| Projekt: SkyOS Full Core | |
| """ | |
| from __future__ import annotations | |
| import logging | |
| from googleapiclient.discovery import build | |
| from Module.google.oauth import oauth | |
| logger = logging.getLogger("SkyOS.GoogleAuth") | |
| class GoogleAuth: | |
| """Zentrale Google-Authentifizierung.""" | |
| _credentials = None | |
| _services = {} | |
| def credentials(cls): | |
| """Liefert gültige OAuth-Credentials.""" | |
| if cls._credentials is None: | |
| cls._credentials = oauth.authenticate() | |
| return cls._credentials | |
| def service( | |
| cls, | |
| api_name: str, | |
| api_version: str, | |
| ): | |
| """ | |
| Erzeugt oder liefert einen zwischengespeicherten Google-Service. | |
| Beispiel: | |
| GoogleAuth.service("drive", "v3") | |
| """ | |
| key = f"{api_name}:{api_version}" | |
| if key not in cls._services: | |
| logger.info( | |
| "Initialisiere Google Service %s (%s)", | |
| api_name, | |
| api_version, | |
| ) | |
| cls._services[key] = build( | |
| api_name, | |
| api_version, | |
| credentials=cls.credentials(), | |
| cache_discovery=False, | |
| ) | |
| return cls._services[key] | |
| def reset(cls): | |
| """Setzt alle gecachten Services zurück.""" | |
| cls._services.clear() | |
| cls._credentials = None | |
| # ------------------------------------------------- | |
| # Komfortfunktionen | |
| # ------------------------------------------------- | |
| def get_credentials(): | |
| """Liefert gültige OAuth-Credentials.""" | |
| return GoogleAuth.credentials() | |
| def get_service( | |
| api_name: str, | |
| api_version: str, | |
| ): | |
| """ | |
| Liefert einen Google API Service. | |
| Beispiele: | |
| get_service("drive", "v3") | |
| get_service("calendar", "v3") | |
| get_service("gmail", "v1") | |
| """ | |
| return GoogleAuth.service( | |
| api_name, | |
| api_version, | |
| ) | |
| # ------------------------------------------------- | |
| # Spezielle Kurzfunktionen | |
| # ------------------------------------------------- | |
| def drive(): | |
| return get_service("drive", "v3") | |
| def docs(): | |
| return get_service("docs", "v1") | |
| def sheets(): | |
| return get_service("sheets", "v4") | |
| def gmail(): | |
| return get_service("gmail", "v1") | |
| def calendar(): | |
| return get_service("calendar", "v3") |