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

How to specify that headers must be regenerated. #126

Open
chebee7i opened this issue Mar 24, 2022 · 1 comment
Open

How to specify that headers must be regenerated. #126

chebee7i opened this issue Mar 24, 2022 · 1 comment

Comments

@chebee7i
Copy link

chebee7i commented Mar 24, 2022

I've have a GitHub app which uses a jwt that can expire. Ultimately, I generate an installation access token that can also expire and I am trying to use ghapi with that installation access token.

I was hoping that I could subclass GhApi.__call__ to dynamically generate my access token, but this does not seem to work.

jwtm = JwtManager(app_id, private_key)                                                           
iatm = IatManager(jwtm)                                                                          
token = iatm.token(installation_id)                                                              
gh_host = baseurl()                                                                         
                                                                                                 
class GhApi(ghapi_all.GhApi):                                                                    
    def __call__(self, path, verb=None, headers=None, route=None, query=None, data=None):        
        headers = headers or {}                                                                  
        headers.update(iatm.headers(installation_id))                                            
        super().__call__(path, verb, headers, route, query, data)                     
                                                                                                 
api = GhApi(                                                                                     
    owner=owner,                                                                                 
    repo=repo,                                                                                   
    token=token,                                                                                 
    debug=debug,                                                                                 
    limit_cb=limit_cb,                                                                           
    gh_host=gh_host,                                                                             
)             

IatManager.headers() will generate a new jwt token whenever it is expired and then will use this to generate an access token for the specified installation id. The headers it generates look like this:

In [5]: iatm.headers(2)
Out[5]:
{'Authorization': 'token ghs_randomstuffhere',
 'Accept': 'application/vnd.github.v3+json'}

The reason that this did not work is that ultimately GhApi.__call__ is not what was called. Rather it was some lower level thing. Any suggestion on how I can use ghapi for a long-running GitHub app where the headers need to be dynamically updated in time?

@chebee7i
Copy link
Author

I guess I could just recreate the api from time to time....seems a bit clunky, though functional.

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

1 participant