mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-02-05 19:03:30 -05:00
- 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.
29 lines
869 B
Python
29 lines
869 B
Python
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):
|
|
return dict(
|
|
created_by=current_user.id,
|
|
**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
|