sbyt32 / Preordain

Scrape data from Scryfall and TCGPlayer and serve that data locally.
0 stars 0 forks source link

Custom Responses Models #4

Closed sbyt32 closed 1 year ago

sbyt32 commented 1 year ago

Having response models will make it easy to have a unified format for each section.

card/ response

image image image

sbyt32 commented 1 year ago

Started working on response models and verification https://github.com/sbyt32/mtg_price_fetcher/commit/16fb67c601cc0d49faf12df34663057cf42a31ba

sbyt32 commented 1 year ago

Having an issue with nested Pydantic models. I want to have the price information in a dict that is part of the response.

I attempted to create a row factory, which only returned the first result and all of its parts.

class DictPriceFactory:
    def __init__(self, cursor: Cursor[Any]):
        self.fields = [c.name for c in cursor.description]
        self.dict_format = {'prices': {}}
        for c in cursor.description:
            if c.name in ['usd','usd_foil','euro','euro_foil','tix']:
                self.dict_format['prices'][c.name] =  self.dict_format.get(c.name, None)
            else:
                self.dict_format[c.name] = self.dict_format.get(c.name, c.name)
        print(self.dict_format)

    def __call__(self, values: Sequence[Any]):
        card_info_data = dict(zip(self.fields[:5], values[:5]))
        card_info_prices = dict(zip(self.fields[5:], values[5:]))

        self.dict_format['prices'].update(card_info_prices)
        self.dict_format.update(card_info_data)
        return self.dict_format

At the moment, I'm using a function to just return the data, formatted already.

def parse_data_for_response(data: list):
    """
    Parse the data you recieved for this format.
    """
    card_data = []
    for cards in data:
        card_data.append(
            {
                'name': cards['name'],
                'set': cards['set'],
                'set_full': cards['set_full'],
                'id': cards['id'],
                'last_updated': cards['last_updated'],
                'prices': {
                    'usd': cards['usd'],
                    'usd_foil': cards['usd_foil'],
                    'euro': cards['euro'],
                    'euro_foil': cards['euro_foil'],
                    'tix': cards['tix'],
                }
            }
        )
    return card_data