mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-24 00:07:02 -04:00
Override the BaseAPI Client
- This allows us to set a custom header for admin calls only (not needed in client calls) - Adds request-id from Middleware to the API call to ensure the API logs against the same request ID.
This commit is contained in:
@@ -1,4 +1,7 @@
|
|||||||
from flask_login import current_user
|
from flask_login import current_user
|
||||||
|
from flask import has_request_context, request
|
||||||
|
from notifications_python_client.base import BaseAPIClient
|
||||||
|
from notifications_python_client.version import __version__
|
||||||
|
|
||||||
|
|
||||||
def _attach_current_user(data):
|
def _attach_current_user(data):
|
||||||
@@ -6,3 +9,20 @@ def _attach_current_user(data):
|
|||||||
created_by=current_user.id,
|
created_by=current_user.id,
|
||||||
**data
|
**data
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class NotifyAdminAPIClient(BaseAPIClient):
|
||||||
|
def generate_headers(self, api_token):
|
||||||
|
headers = {
|
||||||
|
"Content-type": "application/json",
|
||||||
|
"Authorization": "Bearer {}".format(api_token),
|
||||||
|
"User-agent": "NOTIFY-API-PYTHON-CLIENT/{}".format(__version__)
|
||||||
|
}
|
||||||
|
return self._add_request_id_header(headers)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _add_request_id_header(headers):
|
||||||
|
if not has_request_context():
|
||||||
|
return headers
|
||||||
|
headers['NotifyRequestID'] = request.request_id
|
||||||
|
return headers
|
||||||
|
|||||||
Reference in New Issue
Block a user