コード例 #1
0
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
コード例 #2
0
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
コード例 #3
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
コード例 #4
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
コード例 #5
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
コード例 #6
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
コード例 #7
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
コード例 #8
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
コード例 #9
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
コード例 #10
0
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
コード例 #11
0
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'
コード例 #12
0
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'
コード例 #13
0
def client(request):
    httpretty.enable()

    def client_teardown():
        httpretty.disable()
        httpretty.reset()

    request.addfinalizer(client_teardown)
    return GavagaiClient('foo', host='http://api.local')
コード例 #14
0
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')
コード例 #15
0
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')
コード例 #16
0
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'
コード例 #17
0
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)
コード例 #18
0
def test_base_url():
    client = GavagaiClient('x')
    assert client.base_url() == 'https://api.gavagai.se/v3'
コード例 #19
0
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'
コード例 #20
0
def test_default_host():
    client = GavagaiClient('x')
    assert client.host == 'https://api.gavagai.se'
コード例 #21
0
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
コード例 #22
0
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'
コード例 #23
0
def test_connection_error_exception_on_host_unreachable():
    client = GavagaiClient('x', host='http://unreachablehost')
    with pytest.raises(ConnectionError):
        client.request('test', method='get')
コード例 #24
0
def test_constructor_apikey():
    client = GavagaiClient('this_is_a_fake_apikey')
    assert client.apikey == 'this_is_a_fake_apikey'
コード例 #25
0
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
コード例 #26
0
def test_default_api_version():
    client = GavagaiClient('x')
    assert client.api_version == 'v3'
コード例 #27
0
ファイル: test_api.py プロジェクト: dewe/gavagai-python
def client(request):
    if not os.environ['GAVAGAI_APIKEY']:
        raise Exception('Environment variable GAVAGAI_APIKEY must be set.')
    return GavagaiClient()
コード例 #28
0
def test_base_url():
    client = GavagaiClient('x')
    assert client.base_url() == 'https://api.gavagai.se/v3'
コード例 #29
0
#!/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)
コード例 #30
0
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
コード例 #31
0
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;
コード例 #32
0
#!/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)