uberfastman / yfpy

Python API wrapper for the Yahoo Fantasy Sports public API (supports NFL, NHL, MLB, and NBA).
https://pypi.org/project/yfpy/
GNU General Public License v3.0
163 stars 44 forks source link

'all_output_as_json=True ' in query object prevents access to its own methods #33

Closed lchu122 closed 1 year ago

lchu122 commented 1 year ago

I wanted to output as json the queries generated from the YahooFantasySportsQuery so I set the above parameter to True. I get the following error: File "/Users/lukechu/Projects/TracyBowl/lib/python3.8/site-packages/yfpy/query.py", line 780, in get_league_key return self.get_game_metadata_by_game_id(self.game_id).game_key + ".l." + self.league_id AttributeError: 'str' object has no attribute 'game_key'

As an alternative I tried calling clean_data_dict() method on query output of a Player object, specifically on query.get_team_roster_player_stats_by_week(manager_id, week), and checked the types of both keys and values, and they were native Python types.

But I was unable to serialize the cleaned Player dict via json.dump() with the following error: raise TypeError(f'Object of type {o.__class__.__name__} ' TypeError: Object of type Stat is not JSON serializable

Not sure how that object type was being picked up or how it remains for the cleaned Player dict.

Any ideas?

lchu122 commented 1 year ago

Also as an aside, not sure if it was done on purpose, but the output of 'player' in the Player Object is single quoted vs double quoted for what JSON expects.

uberfastman commented 1 year ago

@lchu122 thanks so much for the bug report. I've now released YFPY v9.0.0 which should fix the issues you are running into when trying to run some of the queries with JSON output.

Just as a clarification though, I've updated the all_output_as_json parameter to be all_output_as_json_str, as the purpose of that parameter is to output the data as a JSON string, not as Python JSON objects.

As for serialization, YFPY objects require the custom JSON serialization function in order to be serialized.

Lastly, in regards to your quoting comment, I wasn't able to recreate any issues with JSON quoting for the YFPY objects, but as far as I can tell now, all the integration tests pass both when all_output_as_json_str=True and all_output_as_json_str=False, and outputting all objects as JSON strings in code seems to work fine, so hopefully that isn't an issue for you anymore.