def test_empty_response(): httpretty.register_uri(httpretty.GET, 'http://api.local/v3/test', status=204) client = GavagaiClient('foo', host='http://api.local') response = client.request('test', method='get') assert response.status_code == 204
def test_raise_exception_on_300_range_status(): httpretty.register_uri(httpretty.GET, 'http://api.local/v3/test', status=302) client = GavagaiClient('foo', host='http://api.local') with pytest.raises(GavagaiHttpException) as excinfo: client.request('test', method='get') assert excinfo.value.status_code == 302
def test_tonality(): client = GavagaiClient() texts = get_swagger_json('texts', 'https://developer.gavagai.se/swagger/spec/tonality.json') r = client.tonality(texts) assert r.status_code == 200 assert 'texts' in r.json() assert len(r.json()['texts']) > 0
def test_keywords(): client = GavagaiClient() texts = get_swagger_json('texts', 'https://developer.gavagai.se/swagger/spec/keywords.json') r = client.keywords(texts) assert r.status_code == 200 assert 'keywords' in r.json() assert len(r.json()['keywords']) > 0
def test_stories(): client = GavagaiClient() texts = get_swagger_json('texts', 'https://developer.gavagai.se/swagger/spec/topics.json') r = client.stories(texts) assert r.status_code == 200 assert 'stories' in r.json() assert len(r.json()['stories']) > 0
def test_stories(): client = GavagaiClient() texts = get_swagger_json( 'texts', 'https://developer.gavagai.se/swagger/spec/topics.json') r = client.stories(texts) assert r.status_code == 200 assert 'stories' in r.json() assert len(r.json()['stories']) > 0
def test_keywords(): client = GavagaiClient() texts = get_swagger_json( 'texts', 'https://developer.gavagai.se/swagger/spec/keywords.json') r = client.keywords(texts) assert r.status_code == 200 assert 'keywords' in r.json() assert len(r.json()['keywords']) > 0
def test_tonality(): client = GavagaiClient() texts = get_swagger_json( 'texts', 'https://developer.gavagai.se/swagger/spec/tonality.json') r = client.tonality(texts) assert r.status_code == 200 assert 'texts' in r.json() assert len(r.json()['texts']) > 0
def test_default_exception_message_if_empty_error_message_from_api(): httpretty.register_uri(httpretty.GET, 'http://api.local/v3/test', status=500, body=None) client = GavagaiClient('foo', host='http://api.local') with pytest.raises(GavagaiHttpException) as excinfo: client.request('test', method='get') assert excinfo.value.status_code == 500 assert 'Unable to complete HTTP request' in excinfo.value.message
def test_raise_exception_on_400_range_status(): httpretty.register_uri(httpretty.GET, 'http://api.local/v3/test', status=401, body='{"message": "fake api error message"}', content_type='application/json') client = GavagaiClient('foo', host='http://api.local') with pytest.raises(GavagaiHttpException) as excinfo: client.request('test', method='get') assert excinfo.value.message == '{"message": "fake api error message"}' assert excinfo.value.status_code == 401
def test_raise_exception_on_500_range_status(): httpretty.register_uri(httpretty.GET, 'http://api.local/v3/test', status=502, body='Fake bad gateway error', content_type='text/plain') client = GavagaiClient('foo', host='http://api.local') with pytest.raises(GavagaiHttpException) as excinfo: client.request('test', method='get') assert excinfo.value.status_code == 502 assert excinfo.value.message == 'Fake bad gateway error'
def test_post_request(): httpretty.register_uri(httpretty.POST, 'http://api.local/v3/test', body='{"hello": "world"}', content_type='application/json') client = GavagaiClient('foo', host='http://api.local') response = client.request('/test', method='POST', body={'test': 'value'}) assert response.json() == {'hello': 'world'} assert httpretty.last_request().method == 'POST' assert httpretty.last_request( ).headers['content-type'] == 'application/json' assert httpretty.last_request().path == '/v3/test?apiKey=foo'
def client(request): httpretty.enable() def client_teardown(): httpretty.disable() httpretty.reset() request.addfinalizer(client_teardown) return GavagaiClient('foo', host='http://api.local')
def client(request): httpretty.enable() httpretty.register_uri(httpretty.POST, 'http://api.local/v3/stories', body='{"foo": "bar"}', content_type='application/json') def client_teardown(): httpretty.disable() httpretty.reset() request.addfinalizer(client_teardown) return GavagaiClient('foo', host='http://api.local')
def client(request): dir = os.path.dirname(__file__) with open(os.path.join(dir, 'data/tonality_response.json')) as json_file: data = json_file.read() httpretty.enable() httpretty.register_uri(httpretty.POST, 'http://api.local/v3/tonality', body=data, content_type='application/json') def client_teardown(): httpretty.disable() httpretty.reset() request.addfinalizer(client_teardown) return GavagaiClient('foo', host='http://api.local')
def test_custom_host(): client = GavagaiClient('x', host='http://example.com') assert client.base_url() == 'http://example.com/v3' assert client.host == 'http://example.com'
from gavagai.client import GavagaiClient from pprint import pprint texts = [ 'Stayed here for 3 nights at the beginning of a trip of California. Could not say enough good things about the hotel Monaco. Amazing staff, amazing rooms and the location is brilliant! First stay at a Kimpton hotel, but definitely not the last!!!', 'I did a lot of research looking for a hotel suite for our family vacation in San Francisco. The Hotel Monaco was a perfect choice. What friendly and delightful staff. I will miss the Grand Cafe, but I will make sure to come back to see their new offerings.', 'My partner and I spent four nights here over New Years and loved it. Super staff; lovely, quiet room; excellent location within easy walking to much of Downtown and an overall experience that was perfect.' ] client = GavagaiClient( 'my_apikey') # get your own apikey at https://developer.gavagai.se result = client.keywords(texts) keywords = result.json() pprint(keywords)
def test_base_url(): client = GavagaiClient('x') assert client.base_url() == 'https://api.gavagai.se/v3'
def test_default_host(): client = GavagaiClient('x') assert client.host == 'https://api.gavagai.se'
def test_path_with_slashes(): httpretty.register_uri(httpretty.POST, 'http://api.local/v3/test') client = GavagaiClient('foo', host='http://api.local') path = '/test/' response = client.request(path) assert response.status_code == 200
def test_default_method_post(): httpretty.register_uri(httpretty.POST, 'http://api.local/v3/test') client = GavagaiClient('foo', host='http://api.local') client.request('test') assert httpretty.last_request().method == 'POST'
def test_connection_error_exception_on_host_unreachable(): client = GavagaiClient('x', host='http://unreachablehost') with pytest.raises(ConnectionError): client.request('test', method='get')
def test_constructor_apikey(): client = GavagaiClient('this_is_a_fake_apikey') assert client.apikey == 'this_is_a_fake_apikey'
def test_request_kwargs(): client = GavagaiClient('x', api_version='v5', default_option='foo') print((client.default_request_options)) assert 'default_option' in client.default_request_options
def test_default_api_version(): client = GavagaiClient('x') assert client.api_version == 'v3'
def client(request): if not os.environ['GAVAGAI_APIKEY']: raise Exception('Environment variable GAVAGAI_APIKEY must be set.') return GavagaiClient()
#!/usr/bin/python # -*- coding: utf-8 -*- from gavagai.client import GavagaiClient from pprint import pprint texts = [u'Din idiot!', u'Jag älskar dig.', u'Hen hatar det.'] client = GavagaiClient() # get your own apikey at https://developer.gavagai.se data = client.tonality(texts, language='sv').json() pprint(data)
def test_environment_variable(): old_apikey = os.environ['GAVAGAI_APIKEY'] os.environ['GAVAGAI_APIKEY'] = 'foo' client = GavagaiClient() assert client.apikey == 'foo' os.environ['GAVAGAI_APIKEY'] = old_apikey
def test_no_apikey(): old_apikey = os.environ['GAVAGAI_APIKEY'] del os.environ['GAVAGAI_APIKEY'] with pytest.raises(GavagaiException): GavagaiClient() os.environ['GAVAGAI_APIKEY'] = old_apikey;