carsonyl / pypac

Find and use proxy auto-config (PAC) files with Python and Requests.
https://pypac.readthedocs.io
Apache License 2.0
71 stars 18 forks source link

pypac with google analytics? #36

Closed rikengct closed 5 years ago

rikengct commented 5 years ago

Thank you for pypac. I have used it successfully to make api requests through my pac This is the code I used:

def proxyconnect(): from pypac import PACSession, get_pac return PACSession(get_pac(url='the url of my pac'))

then

def myfunction(variable): session = proxyconnect() api = 'the api url' return session.get(api + variable).json()

This was basically from your usage examples and I found it very helpful for the particular api I needed to get data from

But, I'm also trying to query google analytics. My code is fine when not needing to go through my pac

I'm wondering if you are familiar with the python code provided by google analytics api v4 and if pypac can be made to work through it? I am not so skilled to know what to do.

this is the code I have working (when not needing to go through the proxy)

from oauth2client.service_account import ServiceAccountCredentials from googleapiclient.discovery import build

def initialize_analyticsreporting(): credentials = ServiceAccountCredentials.from_json_keyfile_name( KEY_FILE_LOCATION, SCOPES) return build('analyticsreporting', 'v4', credentials = credentials)

def get_report(analytics, query): return analytics.reports().batchGet(body=query).execute()

I'm really stuck. Is it possible to set python (I'm using Spyder through anaconda) to always go through a pac when trying to access the internet? (this pac has no user login or password) Sorry if these questions are too ignorant.

carsonyl commented 5 years ago

I'm not familiar with Google's Python SDKs, but if they honour the HTTP_PROXY environment variable, you should be able to find success using something like:

with pac_context_for_url('https://example.google.com'):
    # do all your Google Analytics stuff here

Or, since you seem to already be supplying the PAC file URL manually, you could use the methods on PACFile to obtain a http://host:port proxy URL, which you can set as an environment variable using os.putenv('HTTP_PROXY', proxy_url) and os.putenv('HTTPS_PROXY', proxy_url) before proceeding with your Google Analytics code.