Use tornado's AsyncHttpClient for ApiClient
This commit is contained in:
parent
1ecb7bce78
commit
72258ef46a
@ -1,15 +1,13 @@
|
|||||||
import requests
|
|
||||||
import tornado.gen
|
import tornado.gen
|
||||||
from concurrent.futures import ThreadPoolExecutor
|
|
||||||
from tornado.httpclient import AsyncHTTPClient
|
from tornado.httpclient import AsyncHTTPClient
|
||||||
|
from json import dumps, loads
|
||||||
|
|
||||||
|
|
||||||
class ApiClient(object):
|
class ApiClient(object):
|
||||||
|
|
||||||
def __init__(self, base_url):
|
def __init__(self, base_url):
|
||||||
self.base_url = base_url
|
self.base_url = base_url
|
||||||
self.session = requests.Session()
|
self.client = AsyncHTTPClient()
|
||||||
self.executor = ThreadPoolExecutor()
|
|
||||||
|
|
||||||
@tornado.gen.coroutine
|
@tornado.gen.coroutine
|
||||||
def get(self, path, **kwargs):
|
def get(self, path, **kwargs):
|
||||||
@ -17,19 +15,32 @@ class ApiClient(object):
|
|||||||
|
|
||||||
@tornado.gen.coroutine
|
@tornado.gen.coroutine
|
||||||
def put(self, path, **kwargs):
|
def put(self, path, **kwargs):
|
||||||
return self.make_request('PUT', self.base_url + path, **kwargs)
|
return (yield self.make_request('PUT', self.base_url + path, **kwargs))
|
||||||
|
|
||||||
@tornado.gen.coroutine
|
@tornado.gen.coroutine
|
||||||
def post(self, path, **kwargs):
|
def post(self, path, **kwargs):
|
||||||
return self.make_request('POST', self.base_url + path, **kwargs)
|
return (yield self.make_request('POST', self.base_url + path, **kwargs))
|
||||||
|
|
||||||
@tornado.gen.coroutine
|
@tornado.gen.coroutine
|
||||||
def delete(self, path, **kwargs):
|
def delete(self, path, **kwargs):
|
||||||
return self.make_request('DELETE', self.base_url + path, **kwargs)
|
return (yield self.make_request('DELETE', self.base_url + path, **kwargs))
|
||||||
|
|
||||||
@tornado.gen.coroutine
|
@tornado.gen.coroutine
|
||||||
def make_request(self, method, url, **kwargs):
|
def make_request(self, method, url, **kwargs):
|
||||||
def _make_request(_method, _url, **kwargs):
|
# If 'json' kwarg is specified, serialize it to 'body' and update
|
||||||
return requests.request(_method, _url, **kwargs)
|
# the Content-Type.
|
||||||
|
if 'json' in kwargs:
|
||||||
|
kwargs['body'] = dumps(kwargs['json'])
|
||||||
|
del kwargs['json']
|
||||||
|
headers = kwargs.get('headers', {})
|
||||||
|
headers['Content-Type'] = 'application-json'
|
||||||
|
kwargs['headers'] = headers
|
||||||
|
|
||||||
return (yield self.executor.submit(_make_request, 'GET', url))
|
response = yield self.client.fetch(url, method=method, **kwargs)
|
||||||
|
return self.adapt_response(response)
|
||||||
|
|
||||||
|
def adapt_response(self, response):
|
||||||
|
if response.headers['Content-Type'] == 'application/json':
|
||||||
|
json = loads(response.body)
|
||||||
|
setattr(response, 'json', json)
|
||||||
|
return response
|
||||||
|
10
tests/test_api_client.py
Normal file
10
tests/test_api_client.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
|
from atst.api_client import ApiClient
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.gen_test
|
||||||
|
def test_api_client(http_client, base_url):
|
||||||
|
client = ApiClient(base_url)
|
||||||
|
response = yield client.get('')
|
||||||
|
assert response.code == 200
|
Loading…
x
Reference in New Issue
Block a user