tastyware / tastytrade

An unofficial, sync/async Python SDK for Tastytrade!
https://tastyworks-api.rtfd.io
MIT License
127 stars 43 forks source link

key update in validate_response #61

Closed jimbkim closed 1 year ago

jimbkim commented 1 year ago

in utils.py, validate_response was throwing key errors. the keys have changed from 'code' and 'message' to 'domain' and 'reason'.

IThe following changes work for me.

    if errors is not None:
        for error in errors:
            error_message += f"\n{error['domain']}: {error['reason']}"
Graeme22 commented 1 year ago

Hey! I'm not able to reproduce, I tried placing some invalid orders and making some invalid requests and everything was working normally. Could you post your code?

jimbkim commented 1 year ago

for example, in the Session params (notice that the keys are all incorrect)..

    params = {
        'rlogin': config.get('Authentication', 'username'),
        'rpassword': config.get('Authentication', 'password'),
        'rremember-me': config.getboolean('Authentication', 'remember_me'),
    }

the json response is

{ "error": { "code": "validation_error", "message": "Request validation failed", "errors": [ { "domain": "login", "reason": "is missing" } ] } }

Graeme22 commented 1 year ago

Are you actually using the Session object? The config dict is internal, looks like you're using the wrong keys; should be 'login' not 'rlogin'.

jimbkim commented 1 year ago

As a test case, I intentionally passed incorrect keys so that the API would return error codes, specifically to cause the nested errors[] to be generated.

the keys for error[] are code and message, but for errors[], it's domain and reason for each item that failed on their side.

def validate_response(response: Response) -> None:
    if response.status_code // 100 != 2:
        content = response.json()['error']
        error_message = f"{content['code']}: {content['message']}"
        errors = content.get('errors')
        if errors is not None:
            for error in errors:
                error_message += f"\n{error['code']}: {error['message']}"

        raise TastytradeError(error_message)

But it should be

def validate_response(response: Response) -> None:
    if response.status_code // 100 != 2:
        content = response.json()['error']
        error_message = f"{content['code']}: {content['message']}"
        errors = content.get('errors')
        if errors is not None:
            for error in errors:
                error_message += f"\n{error['domain']}: {error['reason']}"

        raise TastytradeError(error_message)

hope that helps

Graeme22 commented 1 year ago

Hmm... What other scenarios did this come up? I wonder if it has to do with incorrect keys. I still can't reproduce.

jimbkim commented 1 year ago

I guess I'll close the issue. you have to force-send the wrong keys to the sessions end point to produce the error when the validate_response processes the returned data.

But since keys will always be correct, this error probably won't be tripped upon again.

thanks for the help!