Esempio n. 1
0
def test_build_on_insanity_not_any(mocker):
    obj = {} # mock
    cfg = config.mk('https://api', 'ABCDEF')

    # insanity here is all patched methods return None, so neither data nor
    # errors nor meta are present
    mocker.patch('furrycorn.model.mk_maybe_data', return_value=None)
    mocker.patch('furrycorn.model.mk_maybe_errors', return_value=None)
    mocker.patch('furrycorn.model.common.meta.mk_maybe', return_value=None)
    mocker.patch('furrycorn.model.mk_maybe_jsonapi')
    mocker.patch('furrycorn.model.mk_maybe_links')
    mocker.patch('furrycorn.model.mk_maybe_included')

    with pytest.raises(RuntimeError) as e_info:
        model.build(obj, config)
Esempio n. 2
0
def test_build_on_insanity_data_and_errors(mocker):
    obj = {} # mock
    cfg = config.mk('https://api', 'ABCDEF')

    # insanity here is that both data and errors are present
    fake_data = data.Data(None)
    mocker.patch('furrycorn.model.mk_maybe_data', return_value=fake_data,
                 autospec=True)
    fake_errors = errors.Errors([])
    mocker.patch('furrycorn.model.mk_maybe_errors',
                 return_value=fake_errors, autospec=True)
    mocker.patch('furrycorn.model.common.meta.mk_maybe')
    mocker.patch('furrycorn.model.mk_maybe_jsonapi')
    mocker.patch('furrycorn.model.mk_maybe_links')
    mocker.patch('furrycorn.model.mk_maybe_included')

    with pytest.raises(RuntimeError) as e_info:
        model.build(obj, config)
Esempio n. 3
0
def test_build(mocker):
    obj = {} # mock
    cfg = config.mk('https://api', 'ABCDEF')

    fake_data = data.Data(None)
    mocker.patch('furrycorn.model.mk_maybe_data', return_value=fake_data,
                 autospec=True)
    mocker.patch('furrycorn.model.mk_maybe_errors', return_value=None)
    mocker.patch('furrycorn.model.common.meta.mk_maybe')
    mocker.patch('furrycorn.model.mk_maybe_jsonapi')
    mocker.patch('furrycorn.model.mk_maybe_links')
    mocker.patch('furrycorn.model.mk_maybe_included')

    result = model.build(obj, config)

    assert model.mk_maybe_data.call_count == 1
    assert model.mk_maybe_errors.call_count == 1
    assert meta.mk_maybe.call_count == 1
    assert model.mk_maybe_jsonapi.call_count == 1
    assert model.mk_maybe_links.call_count == 1
    assert model.mk_maybe_included.call_count == 1
    assert type(result) is model.Root
Esempio n. 4
0
from furrycorn import config, model, toolkit
from furrycorn.location import mk_origin, mk_path, mk_query, to_url
from furrycorn.toolkit.document import Data, Errors, Meta

api_key = os.environ['BATTLERITE_API_KEY']
origin = mk_origin('https', 'api.dc01.gamelockerapp.com', '/shards/global')
headers = {
    'Accept': 'application/vnd.api+json',
    'Authorization': 'Bearer {0}'.format(api_key)
}
url = to_url(origin, mk_path('/matches'))
request = Request('GET', url, headers=headers).prepare()

with Session() as session:
    response = session.send(request)
    root = model.build(response.json(), config)
    document = toolkit.process(root)

    if type(document) is Data:
        for match in document:
            print('match id "{0}"'.format(match.resource_id.r_id))

            # We know before the 'assets' has one entry--the telmetry. But...
            # madglory exposes this as 'to many', so we dig.
            for asset in match.traverse('assets'):
                if asset.maybe_dict_attrs.get('name', None):
                    url = asset.maybe_dict_attrs['URL']
                    print('  telemetry at: {0}'.format(url))

            # Let's see how many rounds happened this match:
            round_ct = len(match.traverse('rounds'))
Esempio n. 5
0
player_names = os.environ['BATTLERITE_PLAYER_NAME']
each_player_name = player_names.split(',')
player_names = list(map(lambda n: quote(n), each_player_name))

origin = mk_origin('https', 'api.dc01.gamelockerapp.com', '/shards/global')
headers = {
    'Accept': 'application/vnd.api+json',
    'Authorization': 'Bearer {0}'.format(api_key)
}
url = to_url(origin, mk_path('/players'),
             mk_query({'filter[playerNames]': player_names}))
request = Request('GET', url, headers=headers).prepare()

with Session() as session:
    response = session.send(request)
    root = model.build(response.json(), config.mk(origin, api_key))
    document = toolkit.process(root)

    if type(document) is Data:
        for player in document:
            from pprint import pprint
            pprint(player.maybe_dict_attrs)
    elif type(document) is Errors:
        print("Your request produced a document with errors:")
        from pprint import pprint
        pprint(document.produce_errors())
    elif type(document) is Meta:
        print("Your request produced a document with only metadata:")
        from pprint import pprint
        pprint(document.produce_meta())
    else: