Skip to content

Commit 5e8b326

Browse files
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 11d0335 commit 5e8b326

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
@@ -460,7 +460,7 @@ def call(self, method, path, data=None, need_auth=True):
460460
else:
461461
raise APIError(json_result.get('message'), response=result)
462462

463-
def raw_call(self, method, path, data=None, need_auth=True):
463+
def raw_call(self, method, path, data=None, need_auth=True, headers=None):
464464
"""
465465
Lowest level call helper. If ``consumer_key`` is not ``None``, inject
466466
authentication headers and sign the request.
@@ -479,12 +479,17 @@ def raw_call(self, method, path, data=None, need_auth=True):
479479
:param str path: api entrypoint to call, relative to endpoint base path
480480
:param data: any json serializable data to send as request's body
481481
:param boolean need_auth: if False, bypass signature
482+
:param dict headers: A dict containing the headers needing to be sent to
483+
the OVH API. The function may override the
484+
OVH API authentication headers, as well as
485+
the Content-Type header.
482486
"""
483487
body = ''
484488
target = self._endpoint + path
485-
headers = {
486-
'X-Ovh-Application': self._application_key
487-
}
489+
490+
if headers is None:
491+
headers = {}
492+
headers['X-Ovh-Application'] = self._application_key
488493

489494
# include payload
490495
if data is not None:

tests/test_client.py

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

395+
@mock.patch('ovh.client.Session.request', return_value="Let's assume requests will return this")
396+
def test_raw_call_with_headers(self, m_req):
397+
api = Client(ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET)
398+
r = api.raw_call(FAKE_METHOD, FAKE_PATH, None, False, headers={
399+
'Custom-Header': "1",
400+
})
401+
self.assertEqual(r, "Let's assume requests will return this")
402+
m_req.assert_called_once_with(
403+
FAKE_METHOD, BASE_URL+FAKE_PATH,
404+
headers={
405+
'Custom-Header': "1",
406+
'X-Ovh-Application': APPLICATION_KEY,
407+
},
408+
data='',
409+
timeout=TIMEOUT
410+
)
411+
395412
# Perform real API tests.
396413
def test_endpoints(self):
397414
for endpoint in ENDPOINTS.keys():

0 commit comments

Comments
 (0)