s0h3ck / finnhub-api-python-client

This library provides convenient way to use Finnhub.io API in Python.
https://finnhub.io/api/v1
MIT License
25 stars 16 forks source link

Can't set the format argument on candles data to csv. #3

Closed spencecopper closed 4 years ago

spencecopper commented 4 years ago

Module expects the api to only ever return json. However historical candles data can be returned as a csv.

ex:

`data = client.stock_candle(symbol='AAPL', resolution=1, {'from': 1572651390, 'to': 1572655390, 'format': 'csv'}) Traceback (most recent call last): File "/Users/chris_nielsen/github_projects/finnhub-api-python-client/finnhub/client.py", line 56, in _handle_response raise FinnhubRequestException("Invalid Response: {}".format(response.text)) File "/usr/local/lib/python3.7/site-packages/requests/models.py", line 898, in json return complexjson.loads(self.text, kwargs) File "/usr/local/Cellar/python/3.7.6/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/init.py", line 348, in loads return _default_decoder.decode(s) File "/usr/local/Cellar/python/3.7.6/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/local/Cellar/python/3.7.6/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "", line 1, in File "/Users/chris_nielsen/github_projects/finnhub-api-python-client/finnhub/client.py", line 100, in stock_candle def stock_tick(self, params): File "/Users/chris_nielsen/github_projects/finnhub-api-python-client/finnhub/client.py", line 61, in _get def covid(self): File "/Users/chris_nielsen/github_projects/finnhub-api-python-client/finnhub/client.py", line 47, in _request_api uri = self._create_api_uri(path) File "/Users/chris_nielsen/github_projects/finnhub-api-python-client/finnhub/client.py", line 40, in _request else: File "/Users/chris_nielsen/github_projects/finnhub-api-python-client/finnhub/client.py", line 58, in _handle_response def _get(self, path, kwargs): finnhub.exceptions.FinnhubRequestException: FinnhubRequestException: Invalid Response: t,o,h,l,c,v 1572651420,255.96,255.96,255.96,255.96,396 1572651480,255.96,255.96,255.96,255.96,719 1572651540,255.96,255.96,255.97,255.96,525`

See commit: https://github.com/spencecopper/finnhub-api-python-client/commit/a5cfa4e3b04b89bb88f6edbfef8f14c0f823396a

s0h3ck commented 4 years ago

Hey, the commit you provide will cause a KeyError: 'format'(check line 38) if you do not provide the format parameter. You would need to do a safe getter and assign a default value.

Since we know it returns 200, it would be possible to do a fix with the following code at line 51:

    try:
        return response.json()
    except:
        return response.text

Another alternative is to check the content-type of the response with response.headers.get('content-type'). If it is text/csv, then return response.text.