Skip to content

Commit c9ce0e8

Browse files
feat: Add extra headers on APIModel (#97)
* feat: Add extra headers APIModel --------- Co-authored-by: Pascal Zimmermann <[email protected]>
1 parent 77c0322 commit c9ce0e8

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

docs/content/grafana_api/model.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ The class includes all necessary variables to establish a connection to the Graf
8585
- `username` _str_ - Specify the username of the Grafana system
8686
- `password` _str_ - Specify the password of the Grafana system
8787
- `timeout` _float_ - Specify the timeout of the Grafana system
88+
- `headers` _dict_ - Specify the headers of the Grafana system
8889
- `http2_support` _bool_ - Specify if you want to use HTTP/2
8990
- `ssl_context` _ssl.SSLContext_ - Specify the custom ssl context of the Grafana system
9091
- `num_pools` _int_ - Specify the number of the connection pool

grafana_api/api.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ def call_the_api(
5050
"""
5151

5252
api_url: str = f"{self.grafana_api_model.host}{api_call}"
53-
headers: dict = dict(
53+
54+
headers: dict = dict()
55+
if self.grafana_api_model.headers is not None:
56+
headers: dict = self.grafana_api_model.headers
57+
58+
headers.update(
5459
{"Authorization": f"Bearer {self.grafana_api_model.token}"},
5560
)
5661

grafana_api/model.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class APIModel:
8585
username (str): Specify the username of the Grafana system
8686
password (str): Specify the password of the Grafana system
8787
timeout (float): Specify the timeout of the Grafana system
88+
headers (dict): Specify the headers of the Grafana system
8889
http2_support (bool): Specify if you want to use HTTP/2
8990
ssl_context (ssl.SSLContext): Specify the custom ssl context of the Grafana system
9091
num_pools (int): Specify the number of the connection pool
@@ -95,6 +96,7 @@ class APIModel:
9596
token: str = None
9697
username: str = None
9798
password: str = None
99+
headers: dict = None
98100
timeout: float = 10.0
99101
http2_support: bool = False
100102
ssl_context: ssl.SSLContext = httpx.create_ssl_context(http2=http2_support)

tests/unittests/test_api.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ def test_call_the_api_basic_auth(self, httpx_client_mock):
3535
api.call_the_api(api_call=MagicMock())["status"],
3636
)
3737

38+
@patch("httpx.Client")
39+
def test_call_the_api_with_proxy_headers(self, httpx_client_mock):
40+
model: APIModel = APIModel(
41+
host="https://test.test.de", username="test", password="test",
42+
headers=dict({"X-Custom-Header": "custom_value"})
43+
)
44+
api: Api = Api(grafana_api_model=model)
45+
46+
httpx_client_mock.return_value.request.return_value.text = '{"status": "success"}'
47+
48+
self.assertEqual(
49+
"success",
50+
api.call_the_api(api_call=MagicMock())["status"],
51+
)
52+
53+
httpx_client_mock.assert_called()
54+
self.assertEqual(dict({"X-Custom-Header": "custom_value", "Authorization": "Basic dGVzdDp0ZXN0", "Content-Type": "application/json", "Accept": "application/json"}), httpx_client_mock.call_args[1]["headers"])
55+
3856
@patch("httpx.Client")
3957
def test_call_the_api_org_id(self, httpx_client_mock):
4058
httpx_client_mock.return_value.request.return_value.text = (

0 commit comments

Comments
 (0)