Skip to content

Commit

Permalink
Merge pull request #84 from ovh/support-headers
Browse files Browse the repository at this point in the history
feat: added support for headers when calling ovh.Client.raw_call
  • Loading branch information
rbeuque74 authored Mar 14, 2022
2 parents 7ecba81 + 3bdd994 commit 6b85e1b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
13 changes: 9 additions & 4 deletions ovh/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ def call(self, method, path, data=None, need_auth=True):
else:
raise APIError(json_result.get('message'), response=result)

def raw_call(self, method, path, data=None, need_auth=True):
def raw_call(self, method, path, data=None, need_auth=True, headers=None):
"""
Lowest level call helper. If ``consumer_key`` is not ``None``, inject
authentication headers and sign the request.
Expand All @@ -490,12 +490,17 @@ def raw_call(self, method, path, data=None, need_auth=True):
:param str path: api entrypoint to call, relative to endpoint base path
:param data: any json serializable data to send as request's body
:param boolean need_auth: if False, bypass signature
:param dict headers: A dict containing the headers that should be sent to
the OVH API. ``raw_call`` will override the
OVH API authentication headers, as well as
the Content-Type header.
"""
body = ''
target = self._endpoint + path
headers = {
'X-Ovh-Application': self._application_key
}

if headers is None:
headers = {}
headers['X-Ovh-Application'] = self._application_key

# include payload
if data is not None:
Expand Down
17 changes: 17 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,23 @@ def test_raw_call(self, m_req):
r = api.raw_call(FAKE_METHOD, FAKE_PATH, None, False)
self.assertEqual(r, "Let's assume requests will return this")

@mock.patch('ovh.client.Session.request', return_value="Let's assume requests will return this")
def test_raw_call_with_headers(self, m_req):
api = Client(ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET)
r = api.raw_call(FAKE_METHOD, FAKE_PATH, None, False, headers={
'Custom-Header': "1",
})
self.assertEqual(r, "Let's assume requests will return this")
m_req.assert_called_once_with(
FAKE_METHOD, BASE_URL+FAKE_PATH,
headers={
'Custom-Header': "1",
'X-Ovh-Application': APPLICATION_KEY,
},
data='',
timeout=TIMEOUT
)

# Perform real API tests.
def test_endpoints(self):
for endpoint in ENDPOINTS.keys():
Expand Down

0 comments on commit 6b85e1b

Please sign in to comment.