Skip to content

Commit 3bdd994

Browse files
geoffreybauduinrbeuque74
authored andcommitted
feat: added support for headers when calling ovh.Client.raw_call
This feature aims at making available the use of headers while calling the OVH API. The original behavior is preserved, as the signature changes with the introduction of a non-mandatory parameter "header", which is None by default. Added a piece of documentation to warn the user about the possbility to have some headers being rewritten by the Client engine. Signed-off-by: Geoffrey Bauduin <[email protected]>
1 parent 7ecba81 commit 3bdd994

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

ovh/client.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ def call(self, method, path, data=None, need_auth=True):
471471
else:
472472
raise APIError(json_result.get('message'), response=result)
473473

474-
def raw_call(self, method, path, data=None, need_auth=True):
474+
def raw_call(self, method, path, data=None, need_auth=True, headers=None):
475475
"""
476476
Lowest level call helper. If ``consumer_key`` is not ``None``, inject
477477
authentication headers and sign the request.
@@ -490,12 +490,17 @@ def raw_call(self, method, path, data=None, need_auth=True):
490490
:param str path: api entrypoint to call, relative to endpoint base path
491491
:param data: any json serializable data to send as request's body
492492
:param boolean need_auth: if False, bypass signature
493+
:param dict headers: A dict containing the headers that should be sent to
494+
the OVH API. ``raw_call`` will override the
495+
OVH API authentication headers, as well as
496+
the Content-Type header.
493497
"""
494498
body = ''
495499
target = self._endpoint + path
496-
headers = {
497-
'X-Ovh-Application': self._application_key
498-
}
500+
501+
if headers is None:
502+
headers = {}
503+
headers['X-Ovh-Application'] = self._application_key
499504

500505
# include payload
501506
if data is not None:

tests/test_client.py

+17
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,23 @@ def test_raw_call(self, m_req):
433433
r = api.raw_call(FAKE_METHOD, FAKE_PATH, None, False)
434434
self.assertEqual(r, "Let's assume requests will return this")
435435

436+
@mock.patch('ovh.client.Session.request', return_value="Let's assume requests will return this")
437+
def test_raw_call_with_headers(self, m_req):
438+
api = Client(ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET)
439+
r = api.raw_call(FAKE_METHOD, FAKE_PATH, None, False, headers={
440+
'Custom-Header': "1",
441+
})
442+
self.assertEqual(r, "Let's assume requests will return this")
443+
m_req.assert_called_once_with(
444+
FAKE_METHOD, BASE_URL+FAKE_PATH,
445+
headers={
446+
'Custom-Header': "1",
447+
'X-Ovh-Application': APPLICATION_KEY,
448+
},
449+
data='',
450+
timeout=TIMEOUT
451+
)
452+
436453
# Perform real API tests.
437454
def test_endpoints(self):
438455
for endpoint in ENDPOINTS.keys():

0 commit comments

Comments
 (0)