-
Notifications
You must be signed in to change notification settings - Fork 4
Resolve of #2 issue #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
schlerp
left a comment
There was a problem hiding this 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) |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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() ] |
There was a problem hiding this comment.
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.
fda99fd to
8ccea7b
Compare
…t__ and middleware
…t__ and middleware
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