Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add X-Goog-Api-Client metric header to requests #826

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions firebase_admin/_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import requests
from requests.packages.urllib3.util import retry # pylint: disable=import-error

from firebase_admin import _utils

if hasattr(retry.Retry.DEFAULT, 'allowed_methods'):
_ANY_METHOD = {'allowed_methods': None}
Expand All @@ -36,6 +37,9 @@

DEFAULT_TIMEOUT_SECONDS = 120

METRICS_HEADERS = {
'X-GOOG-API-CLIENT': _utils.get_metrics_header(),
}

class HttpClient:
"""Base HTTP client used to make HTTP calls.
Expand Down Expand Up @@ -72,6 +76,7 @@ def __init__(

if headers:
self._session.headers.update(headers)
self._session.headers.update(METRICS_HEADERS)
if retries:
self._session.mount('http://', requests.adapters.HTTPAdapter(max_retries=retries))
self._session.mount('https://', requests.adapters.HTTPAdapter(max_retries=retries))
Expand Down
3 changes: 3 additions & 0 deletions firebase_admin/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Internal utilities common to all modules."""

import json
from platform import python_version

import google.auth
import requests
Expand Down Expand Up @@ -75,6 +76,8 @@
16: exceptions.UNAUTHENTICATED,
}

def get_metrics_header():
return f'gl-python/{python_version()} fire-admin/{firebase_admin.__version__}'

def _get_initialized_app(app):
"""Returns a reference to an initialized App instance."""
Expand Down
7 changes: 6 additions & 1 deletion firebase_admin/app_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class _AppCheckService:
_scoped_project_id = None
_jwks_client = None

_APP_CHECK_HEADERS = {
'X-GOOG-API-CLIENT': _utils.get_metrics_header(),
}

def __init__(self, app):
# Validate and store the project_id to validate the JWT claims
self._project_id = app.project_id
Expand All @@ -62,7 +66,8 @@ def __init__(self, app):
'GOOGLE_CLOUD_PROJECT environment variable.')
self._scoped_project_id = 'projects/' + app.project_id
# Default lifespan is 300 seconds (5 minutes) so we change it to 21600 seconds (6 hours).
self._jwks_client = PyJWKClient(self._JWKS_URL, lifespan=21600)
self._jwks_client = PyJWKClient(
self._JWKS_URL, lifespan=21600, headers=self._APP_CHECK_HEADERS)


def verify_token(self, token: str) -> Dict[str, Any]:
Expand Down
7 changes: 6 additions & 1 deletion firebase_admin/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ def bucket(name=None, app=None) -> storage.Bucket:
class _StorageClient:
"""Holds a Google Cloud Storage client instance."""

STORAGE_HEADERS = {
'X-GOOG-API-CLIENT': _utils.get_metrics_header(),
}

def __init__(self, credentials, project, default_bucket):
self._client = storage.Client(credentials=credentials, project=project)
self._client = storage.Client(
credentials=credentials, project=project, extra_headers=self.STORAGE_HEADERS)
self._default_bucket = default_bucket

@classmethod
Expand Down
Loading