Skip to content

Commit

Permalink
fix: Correctly send JSON data to the server (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgmacias95 authored Sep 30, 2022
1 parent c3282d2 commit a320c09
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
8 changes: 4 additions & 4 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ def test_patch_object(httpserver):
httpserver.expect_request(
'/api/v3/dummy_types/dummy_id',
method='PATCH',
headers={'X-Apikey': 'dummy_api_key'},
data=json.dumps({'data': obj.to_dict(modified_attributes_only=True)}),
headers={'X-Apikey': 'dummy_api_key', 'Content-Type': 'application/json'},
json={'data': obj.to_dict(modified_attributes_only=True)},
).respond_with_json({
'data': {
'id': 'dummy_id',
Expand All @@ -213,8 +213,8 @@ def test_post_object(httpserver):
httpserver.expect_request(
'/api/v3/dummy_types',
method='POST',
headers={'X-Apikey': 'dummy_api_key'},
data=json.dumps({'data': obj.to_dict()}),
headers={'X-Apikey': 'dummy_api_key', 'Content-Type': 'application/json'},
json={'data': obj.to_dict()},
).respond_with_json({
'data': {
'id': 'dummy_id',
Expand Down
37 changes: 17 additions & 20 deletions vt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ async def get_object_async(self, path, *path_args, params=None):
response = await self.get_async(path, *path_args, params=params)
return await self._response_to_object(response)

def patch(self, path, *path_args, data=None):
def patch(self, path, *path_args, data=None, json_data=None):
"""Sends a PATCH request to a given API endpoint.
This is a low-level function that returns a raw HTTP response, no error
Expand All @@ -543,18 +543,20 @@ def patch(self, path, *path_args, data=None):
:param path_args: A variable number of arguments that are put into any
placeholders used in path.
:param data: Data sent in the request body.
:param json_data: dict containing data to send in the request body as JSON.
:type path: str
:type data: A string or bytes
:type json_data: dict
:returns: An instance of :class:`ClientResponse`.
"""
return make_sync(self.patch_async(path, *path_args, data))
return make_sync(self.patch_async(path, *path_args, data, json_data))

async def patch_async(self, path, *path_args, data=None):
async def patch_async(self, path, *path_args, data=None, json_data=None):
"""Like :func:`patch` but returns a coroutine."""
return ClientResponse(
await self._get_session().patch(
self._full_url(path, *path_args),
data=data, proxy=self._proxy))
data=data, json=json_data, proxy=self._proxy))

def patch_object(self, path, *path_args, obj):
"""Sends a PATCH request for modifying an object.
Expand All @@ -575,16 +577,12 @@ def patch_object(self, path, *path_args, obj):

async def patch_object_async(self, path, *path_args, obj):
"""Like :func:`patch_object` but returns a coroutine."""
data = json.dumps({'data': obj.to_dict(modified_attributes_only=True)})
data = {'data': obj.to_dict(modified_attributes_only=True)}

if self._user_headers is None:
self._user_headers = {}
self._user_headers['Content-Type'] = 'application/json'

response = await self.patch_async(path, *path_args, data=data)
response = await self.patch_async(path, *path_args, json_data=data)
return await self._response_to_object(response)

def post(self, path, *path_args, data=None):
def post(self, path, *path_args, data=None, json_data=None):
"""Sends a POST request to a given API endpoint.
This is a low-level function that returns a raw HTTP response, no error
Expand All @@ -595,18 +593,21 @@ def post(self, path, *path_args, data=None):
:param path_args: A variable number of arguments that are put into any
placeholders used in path.
:param data: Data sent in the request body.
:param json_data: dict containing data to send in the request body as JSON.
:type path: str
:type data: A string or bytes
:type json_data: dict
:returns: An instance of :class:`ClientResponse`.
"""
return make_sync(self.post_async(path, *path_args, data=data))
return make_sync(
self.post_async(path, *path_args, data=data, json_data=json_data))

async def post_async(self, path, *path_args, data=None):
async def post_async(self, path, *path_args, data=None, json_data=None):
"""Like :func:`post` but returns a coroutine."""
return ClientResponse(
await self._get_session().post(
self._full_url(path, *path_args),
data=data, proxy=self._proxy))
data=data, json=json_data, proxy=self._proxy))

def post_object(self, path, *path_args, obj):
"""Sends a POST request for creating an object.
Expand All @@ -627,13 +628,9 @@ def post_object(self, path, *path_args, obj):

async def post_object_async(self, path, *path_args, obj):
"""Like :func:`post_object` but returns a coroutine."""
data = json.dumps({'data': obj.to_dict()})

if self._user_headers is None:
self._user_headers = {}
self._user_headers['Content-Type'] = 'application/json'
data = {'data': obj.to_dict()}

response = await self.post_async(path, *path_args, data=data)
response = await self.post_async(path, *path_args, json_data=data)
return await self._response_to_object(response)

def iterator(self, path, *path_args, params=None, cursor=None,
Expand Down

0 comments on commit a320c09

Please sign in to comment.