Skip to content

Commit

Permalink
chore: Add X-Goog-Api-Client metric header to requests (#826)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanedey authored Nov 4, 2024
1 parent 32e8dd2 commit be56a0f
Show file tree
Hide file tree
Showing 14 changed files with 255 additions and 279 deletions.
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

0 comments on commit be56a0f

Please sign in to comment.