rbw / aiosnow

Asynchronous ServiceNow Library
MIT License
73 stars 12 forks source link

Calling with __init__ in a class. #92

Closed michaeldcanady closed 3 years ago

michaeldcanady commented 3 years ago

I have this snippet with a class:

async def CSLookup(self):
        async with CompSupModel(self.serviceNow, table_name="u_computer_support") as CS:
            # Fetches ticket with specified CS
            response = await CS.get_one(CompSupModel.number == CSNumber)
            TicketInfo = {}
            for key,value in response.data.items():
                if key == 'u_device_type':
                    value = deviceTypeDic[value]
                TicketInfo[key] = value
                if key == 'u_serial_number' and value != None or key == 'u_asset' and value != None:
                    TicketInfo["serial_number"] = value
        return TicketInfo

when i call it like this: self.ticketDict = self.CSLookup()

the return outputs as: <coroutine object gatherer.CSLookup at 0x00000242508A12C0>

How do I get self.ticketDict to be a dictionary as apposed to a coroutine object?

rbw commented 3 years ago

Your asynchronous CSLookup function returns a coroutine and must be called with await. Try this:

self.ticketDict = await self.CSLookup()

btw - your code seems to be doing some nested lookups and expansions (e.g. value = deviceTypeDic[value]). Do you know about nested schemas? If not, check out this example: https://github.com/rbw/aiosnow/blob/master/examples/table/read/nested.py

michaeldcanady commented 3 years ago

I ended up needing to get another module, I was trying to create an asyncio class (not natively supported). Thank you though, those resources will be useful later on!