td-ameritrade-python-api icon indicating copy to clipboard operation
td-ameritrade-python-api copied to clipboard

Watch List create and delete raising errors

Open dFHcqXxdCludWt opened this issue 4 years ago • 0 comments

There are two errors I discovered in client.py

  1. create_watchlist function I needed to change the _make_request call, previously it was using PUT and data=payload, this was throwing strange errors. the new code I used is _return self.make_request(method='post', endpoint=endpoint, mode='json', json=payload,order_details=False)
  2. _make_request function I needed to check for additional response codes and non-json responses for example delete_watchlist returns a 204 with nobody - below is the code that I added into the _make_request function. PS print statements are for testing.

If it's okay and no details.

    elif response.ok:

        json_content_type = False
        try :
            json_content_type = response.headers["content-type"].strip().startswith("application/json")
        except KeyError :
            json_content_type = False
        if (
                response.status_code != 204 and
                json_content_type
        ):
            try:
                return response.json()
            except ValueError:
                raise ValueError
                # decide how to handle a server that's misbehaving to this extent
        else:
            print("response.status_code : " + str(response.status_code))
            print("response headers" + str(response.headers))
            print("response json" + str(response.request.body))
            response_dict = {
                'headers': response_headers,
                'content': response.content,
                'status_code': status_code,
                'request_body': response.request.body,
                'request_method': response.request.method
            }
            print (response_dict)
            return response_dict

    else:

dFHcqXxdCludWt avatar Nov 16 '21 13:11 dFHcqXxdCludWt