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

Does this work with a http_proxy? #4

Closed
SantoshSrinivas79 opened this issue Jul 14, 2015 · 7 comments
Closed

Does this work with a http_proxy? #4

SantoshSrinivas79 opened this issue Jul 14, 2015 · 7 comments

Comments

@SantoshSrinivas79
Copy link

Does this work with a http_proxy? I am getting a timeout error.

@xjsender
Copy link
Owner

@fountainhead , this plugin uses requests to perform http request, so you can get help on setting proxy by http://docs.python-requests.org/en/latest/user/advanced/#proxies

@xjsender
Copy link
Owner

@fountainhead , I don't have proxy environment, you need to try it by yourself, sorry.

@SantoshSrinivas79
Copy link
Author

@xjsender thanks for your response. Can you please guide me where to set the configuration for HaoGist? I've never really used Python and hence wondering if you can help out.

The code from the Gist package which does this is below:

def api_request_native(url, data=None, token=None, https_proxy=None, method=None):
    request = urllib.Request(url)
    # print('API request url:', request.get_full_url())
    if method:
        request.get_method = lambda: method
    token = token if token != None else token_auth_string()
    request.add_header('Authorization', 'token ' + token)
    request.add_header('Accept', 'application/json')
    request.add_header('Content-Type', 'application/json')

    if data is not None:
        request.add_data(bytes(data.encode('utf8')))

    # print('API request data:', request.get_data())
    # print('API request header:', request.header_items())
    https_proxy = https_proxy if https_proxy != None else settings.get('https_proxy')
    if https_proxy:
        opener = urllib.build_opener(urllib.HTTPHandler(), urllib.HTTPSHandler(),
                                     urllib.ProxyHandler({'https': https_proxy}))

        urllib.install_opener(opener)

    try:
        with contextlib.closing(urllib.urlopen(request)) as response:
            if response.code == 204:  # No Content
                return None
            else:
                return json.loads(response.read().decode('utf8', 'ignore'))

    except urllib.HTTPError as err:
        with contextlib.closing(err):
            raise SimpleHTTPError(err.code, err.read())

@xjsender
Copy link
Owner

@fountainhead , just set environment variable for your operation system, you don't need to know anything about python.

@SantoshSrinivas79
Copy link
Author

@xjsender

Thank you. I don't want to do this through the environment variable so that other code I have does not break.

I got this working as follows.

import urllib.request as urllib

  proxyDict = {
              "http"  : "http://user:%23Cpassword%23C:server:port",
              "https" : "http://user:%23Cpassword%23C:server:port"
            }


self.res = requests.get(GIST_BASE_URL % "gists?per_page=1000", 
                headers=self.headers, proxies=proxyDict)

Is there a way I can add proxies globally so that every request call is not to be modified?

@xjsender
Copy link
Owner

I will release a new version to allow you to do it. thanks.

@xjsender
Copy link
Owner

@fountainhead , release v0.2.4 is available, I have added a new setting named http_proxies to perform request by proxies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants