fastai / ghapi

A delightful and complete interface to GitHub's amazing API

Home Page:https://ghapi.fast.ai/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to specify that headers must be regenerated.

chebee7i opened this issue · comments

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?

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