Various Examples

The following example prints out the names of all the endpoints queried:

 1from yfrake import client
 2import asyncio
 3
 4@client.session
 5async def main():
 6    results = client.get_all(symbol='msft')
 7    async for resp in results.gather():
 8        print(f'Endpoint: {resp.endpoint}')
 9
10if __name__ == '__main__':
11    asyncio.run(main())

The following example prints out either the error or the data property of the ClientResponse objects:

 1from yfrake import client
 2import asyncio
 3
 4@client.session
 5async def main():
 6    queries = [
 7        dict(endpoint='quote_type', symbol='msft'),
 8        dict(endpoint='price_overview', symbol='gme_to_the_moon'),
 9        dict(endpoint='key_statistics', symbol='tsla')
10    ]
11    results = client.batch_get(queries)
12    await results.wait()
13    for resp in results:
14        if resp.error:
15            print(f'Error: {resp.error}')
16        else:
17            print(f'Data: {resp.data}')
18
19if __name__ == '__main__':
20    asyncio.run(main())

The following example creates a batch request of 3 endpoints for 3 symbols:

 1from yfrake import client
 2
 3@client.session
 4def main():
 5    all_queries = list()
 6    for symbol in ['msft', 'aapl', 'tsla']:
 7        queries = [
 8            dict(endpoint='quote_type', symbol=symbol),
 9            dict(endpoint='price_overview', symbol=symbol),
10            dict(endpoint='key_statistics', symbol=symbol)
11        ]
12        all_queries.extend(queries)
13
14    results = client.batch_get(all_queries)
15    results.wait()
16
17    count = len(results)
18    print(f'ClientResponse objects: {count}')  # 9
19
20if __name__ == '__main__':
21    main()

The following example demonstrates the usage of the get method inside a non-decorated function (or coroutine):

 1from yfrake import client
 2
 3def make_the_request(symbol):
 4    resp = client.get('quote_type', symbol=symbol)
 5    resp.wait()
 6    return resp
 7
 8@client.session
 9def main():
10    resp = make_the_request('msft')
11    print(f'Data: {resp.data}')
12
13if __name__ == '__main__':
14    main()