fastai / ghapi

A delightful and complete interface to GitHub's amazing API
https://ghapi.fast.ai/
Apache License 2.0
608 stars 62 forks source link

Proxy settings #50

Open chris-sanders opened 3 years ago

chris-sanders commented 3 years ago

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?

hamelsmu commented 3 years ago

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!

chris-sanders commented 3 years ago

@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 commented 3 years ago

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.