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:
Martyn Inglis
2016-11-30 17:00:42 +00:00
parent 1384d2a61c
commit 202dd7d314

View File

@@ -1,4 +1,7 @@
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):
@@ -6,3 +9,20 @@ def _attach_current_user(data):
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