ghapi icon indicating copy to clipboard operation
ghapi copied to clipboard

Proxy settings

Open chris-sanders opened this issue 4 years ago • 3 comments

I have an environment that has proxy settings and can't find any way to get ghapi to use the proxy. Is this possible or can it be added?

chris-sanders avatar Apr 15 '21 22:04 chris-sanders

Possibly a dumb question on my part How would you handle proxy settings normally lets say if you were just using CURL or requests? That might help me to try to think about a solution! If you can share any examples of workarounds that you have used in the past for this, that would be helpful too!

hamelsmu avatar Apr 28 '21 18:04 hamelsmu

@hamelsmu this is a dirty hack, but since I've found it works here you go.

import fastcore.net
from ghapi.all import GhApi

if self.proxies:
    # setup proxy for requests
    self.session.proxies.update(self.proxies)
    # add proxy to fastcore which ghapi uses
    proxy = urllib.request.ProxyHandler(self.proxies)
    opener = urllib.request.build_opener(proxy)
    fastcore.net._opener = opener
self.api = GhApi(token=token)

I'm doing this to monkey patch the opener that fastcore uses on a class that's using GhApi. This does work in an environment behind a proxy, but it really would be much better if the ghapi/fastapi modules provided a way to properly configure this.

chris-sanders avatar Apr 29 '21 22:04 chris-sanders

If that snippet isn't clear enough, you can see the whole implementation using requests and ghapi behind a proxy at https://github.com/charmed-kubernetes/github-runner-operator/tree/PAT

Notice that's on a branch that isn't yet merged. If it's merged when you see this the branch will have been deleted just look in the master branch. You're looking for "/src/runner.py" for the use of requests and ghapi.

chris-sanders avatar Apr 30 '21 17:04 chris-sanders