viur-framework / viur-scriptor

A development environment and API for scripting and automated data management on ViUR-based systems
MIT License
3 stars 4 forks source link

generic view function broken #13

Open theVAX opened 9 months ago

theVAX commented 9 months ago

if you call the view()-function on a generated viur module you'll get this Error:

Traceback (most recent call last): 
File "<exec>", line 25, in <module> 
File "<exec>", line 20, in main 
File "/lib/python3.11/site-packages/viur/scriptor/module.py", line 44, in view return await viur.view(module=self._name, key=key, group=group, **kwargs)
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
File "/lib/python3.11/site-packages/viur/scriptor/viur.py", line 96, in view return ret["values"] 
~~~^^^^^^^^^^ KeyError: 'values'

I made a example script to demonstate it:

from viur.scriptor.module import user

async def main():
    userkey=""
    auser=False
    async for au in user.list({"limit":1},renderer="vi"):
        auser=au
        break
    if auser:
        print(auser)
        userview = await user.view({"key":auser["key"]})
        print (userview)
ciansen commented 8 months ago

Hello,

Thanks to the report, the Scriptor API "Module" class includes some built-in functions, which are:

These functions have a static method signature (you can check it here: https://github.com/viur-framework/viur-scriptor-api/blob/main/viur/scriptor/module.py).

The view function requires a string argument, which is the key.

from viur.scriptor.module import user
async def main():
    userkey=""
    auser=False
    async for au in user.list({"limit":1},renderer="vi"):
        auser=au
        break
    if auser:
        print(auser)
        userview = await user.view(auser["key"])
        print (userview)
phorward commented 8 months ago

Hello @ciansen,

the problem here is totally different: The module access functions in the scriptor-api don't ever check on HTTP status codes.

This example here:

from viur.scriptor.module import user

async def main():
    userview = await user.view("self")  # works
    print(userview)
    userview = await user.view("lol")  # 404...
    print(userview)

Fails in the second case, as the api receives a JSON-parsable string containing the error message, but the status_code of 404 is never evaluated. In this case it would be correct that the result is None, and not that anything which is returned is JSON-parsed and then relied that there is a "values" key in the result.