gateio / gateapi-python

247 stars 92 forks source link

What's the best approach to convert a collection pf Contract objects to Pandas DataFrame #79

Closed soaked-reflection closed 2 years ago

soaked-reflection commented 2 years ago

I just started to use gateapi and look for opportunities on futures. I saw this function list_futures_contracts( ). As my habit I tend to store things in a DataFrame cause it's easy to view. I saw the return of this function and I thought it was a list of dictionaries so I tried the normal approach but failed. Turned out it's a collection of Contract objects which the compiler told me not subscriptable. So what's the easiest to to store them in a Dataframe, thanks in advance.

revilwang commented 2 years ago

The response data are all deserialized into predefined models. If you want the original raw data. There are two possible ways:

  1. Every model has a to_dict method which can be used to convert to a Python native dict. For list responses, you need to iterate and to_dict each model inside
  2. Pass _preload_content=False when you call API methods, e.g., list_futures_contracts(_preload_content=False). No deserialization will be done and an urllib3.HTTPResponse will be returned, so you can retrieve the raw response with urllib3.HTTPResponse.data attribute. Details about the deserialization can be found here
soaked-reflection commented 2 years ago

Thanks.