async def request(self, method, path, data=None, params=None, headers=None, json_dumps=True): if json_dumps and (data is not None): data = jsonify(data).encode('utf-8') url = '{}/{}/{}'.format(self._url, self._admin_prefix, path) resp = await self._session.request(method, url, params=params, data=data, headers=headers) body = await resp.read() if resp.status in (200, 201): jsoned = await resp.json() return jsoned elif resp.status == 500: raise PlainRestError(body.decode('utf-8')) else: try: jsoned = await resp.json(encoding='utf-8') except ValueError: raise PlainRestError(body.decode('utf-8')) else: raise JsonRestError(resp.status, jsoned)
async def request(self, method, path, data=None, params=None, headers=None, json_dumps=True, token=None, **kwargs): url = self._url / path if json_dumps and (data is not None): data = jsonify(data).encode('utf-8') h = self._headers.copy() if headers: h.update(headers) if token: h.update({"Authorization": token}) resp = await self._session.request(method, str(url), params=params, data=data, headers=h, **kwargs) return resp
def test_jsonify_failed(): with pytest.raises(TypeError): jsonify(object())
def test_jsonify_object_id(): obj = {'foo': ObjectId('1' * 24)} jsoned = jsonify(obj) assert jsoned == '{"foo": "111111111111111111111111"}'
def test_jsonify(): obj = {'foo': 'bar'} jsoned = jsonify(obj) assert jsoned == '{"foo": "bar"}'