Skip to content
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

client() method returns None when used outside of a hook context #19

Open
qlyoung opened this issue Sep 7, 2021 · 3 comments
Open

client() method returns None when used outside of a hook context #19

qlyoung opened this issue Sep 7, 2021 · 3 comments

Comments

@qlyoung
Copy link

qlyoung commented Sep 7, 2021

I have an application that reacts to webhooks by scheduling a task to execute at a future time (say, in 1 hour). At that time it needs to call the GH API to perform some action, so it needs a GhApi client. Despite the recent patch to allow getting a client outside of a hook context, when I do it, I get None. Works fine inside a hook context though.

Any idea what I'm doing wrong? Is this supported?

@qlyoung
Copy link
Author

qlyoung commented Sep 7, 2021

I was able to fix this by saving the installation id when I get the request, and then when the job is ready to execute, I do this:

def do_job(..., installation_id):
  with app.app_context():
    client = ghapp.client(installation_id);
    # do stuff

Thinking about this it makes sense that I would need to save the installation Id aside somwhere, otherwise I wouldn't know which installation to act as. However, does the client() method really need the app context? I guess since the actual GhApi instance is saved in the context it makes sense.

Anyway this might be a good example to put in the docs somewhere.

Btw nice project!

@ffalor
Copy link
Owner

ffalor commented Sep 8, 2021

Hey qlyoung,

Thank you very much for the filing the issue, we have the exact use case you are doing, and we implemented exactly what you did.

The reason we would use app_context is because private_key, app_id, etc are stored in app.config. After initialization, we use the app config to get the private_key etc. From my understanding this is how Flask recommended extensions to work.

If you try to create a client by doing github_app.client() it will try to create a client using the installation_id sent by github. If you want to do this outside of a actual request, you will have to pass in installation_id for the reasons you already stated.

If you're okay with this approach then I'd agree some documentation around the use case will be extremely beneficial.

I'm glad you're finding the project useful!

@qlyoung
Copy link
Author

qlyoung commented Sep 8, 2021

If you're okay with this approach then I'd agree some documentation around the use case will be extremely beneficial.

I think the approach is fine. Unfortunately I doubt I'll have much bandwidth to contribute a docs change in the near future.

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

No branches or pull requests

2 participants