Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added support for headers when calling ovh.Client.raw_call #84

Merged
merged 1 commit into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
rbeuque74 marked this conversation as resolved.
Show resolved Hide resolved
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