Skip to content

Conversation

@BertKiv
Copy link

@BertKiv BertKiv commented Oct 23, 2021

Error when service registration: TypeError: on_event() takes 2 positional arguments but 3 were given:

line 34 - PUT method not allowed - 405 error
line 82 - call bad method :-)
line 85 - resolved: on_event() takes 2 positional arguments but 3 were given

Copy link
Owner

@schlerp schlerp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into this issue, please see comments.

You're a python developer now!

picoapi/api.py Outdated
}

requests.put(os.getenv("API_REGISTER_PATH"), json=uservice_definition)
requests.get(os.getenv("API_REGISTER_PATH"), json=uservice_definition)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be a put or a post as the service definition is important information (GET requests technically are allowed body's but important information is not allowed to be included there, and really according to the HTTP spec, this type of request should be POST or PUT since we are posting/putting data into the server side).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. To resolve this, we need change in api.py a line 79
from:

self.add_api_route("/register", self.add_service)

to:

self.add_api_route("/register", self.add_service, , methods=["PUT"])

picoapi/api.py Outdated
allow_headers: List[str] = [
x for x in os.getenv("API_CORS_ALLOW_HEADERS", "*").split()
],
on_startup = None,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*args, and **kwargs are automatically passed into the fastapi and starlette class (the parent and grandparent we inherit from). See here.

in this case we actually want to move the if self.is_master up above the super().__init__() call, replace the logic in the else section with the following:

# append register_uservice method into kwargs dict on_startup member.
kwargs["on_startup"] = [register_uservice, *[x for x in kwargs.get("on_startup")]]

then call the super as it currently stands:

# call super class __init__
super().__init__(*args, **kwargs)

What we have actually done here is added register_uservice method to the kwargs dict only if is_master is false. Then passed that dict to the underlying FastAPI class we inherit from, which in turn passes it to the Starlette class that it inherits from.

Copy link
Author

@BertKiv BertKiv Oct 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a check to see if kwargs ["on_startup"] exists because if it doesn't, it gives an error. But...
there is another problem. We cannot move self.is_master above the super ().__ init __ () call, because we need the FastAPI methods to register the routes. So, I move call super().__init__ into if else and add middleware on the end.

picoapi/api.py Outdated
else:
# add the service registration event
self.router.on_event("startup", register_uservice)
self.on_startup = [ register_uservice() ]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above comment.

A note about this line of code: we don't need to round brackets here as we want to pass the function in as a function object, rather than call the function directly.

@BertKiv BertKiv force-pushed the resolve-of-#2-issue branch from fda99fd to 8ccea7b Compare October 27, 2021 09:10
@BertKiv BertKiv marked this pull request as draft October 27, 2021 10:07
@BertKiv BertKiv marked this pull request as ready for review October 27, 2021 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants