From 88588a2e09f77c4827bbb3d86dcf25e346f53404 Mon Sep 17 00:00:00 2001 From: Timothy Schroeder Date: Tue, 2 Aug 2016 10:09:56 -0700 Subject: [PATCH] Allow using a proxy. --- pgoapi/pgoapi.py | 14 ++++++++++---- pgoapi/rpc_api.py | 9 ++++++--- requirements.txt | 1 + 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/pgoapi/pgoapi.py b/pgoapi/pgoapi.py index 03ba6eaf..bb8db057 100755 --- a/pgoapi/pgoapi.py +++ b/pgoapi/pgoapi.py @@ -28,7 +28,6 @@ import re import six import logging -import requests from . import __title__, __version__, __copyright__ from pgoapi.rpc_api import RpcApi @@ -49,6 +48,7 @@ def __init__(self): self._auth_provider = None self._api_endpoint = 'https://pgorelease.nianticlabs.com/plfe/rpc' + self._proxy = None self._position_lat = None self._position_lng = None @@ -58,6 +58,9 @@ def __init__(self): def set_logger(self, logger = None): self.log = logger or logging.getLogger(__name__) + + def set_proxy(self, proxy_config): + self._proxy = proxy_config def get_api_endpoint(self): return self._api_endpoint @@ -73,7 +76,8 @@ def set_position(self, lat, lng, alt): self._position_alt = alt def create_request(self): - request = PGoApiRequest(self._api_endpoint, self._auth_provider, self._position_lat, self._position_lng, self._position_alt) + request = PGoApiRequest(self._api_endpoint, self._auth_provider, self._position_lat, + self._position_lng, self._position_alt, self._proxy) return request def __getattr__(self, func): @@ -150,7 +154,7 @@ def login(self, provider, username, password, lat = None, lng = None, alt = None class PGoApiRequest: - def __init__(self, api_endpoint, auth_provider, position_lat, position_lng, position_alt): + def __init__(self, api_endpoint, auth_provider, position_lat, position_lng, position_alt, proxy=None): self.log = logging.getLogger(__name__) """ Inherit necessary parameters """ @@ -161,6 +165,8 @@ def __init__(self, api_endpoint, auth_provider, position_lat, position_lng, posi self._position_lng = position_lng self._position_alt = position_alt + self._proxy = proxy + self._req_method_list = [] def call(self): @@ -174,7 +180,7 @@ def call(self): self.log.info('Not logged in') return NotLoggedInException() - request = RpcApi(self._auth_provider) + request = RpcApi(self._auth_provider, self._proxy) self.log.info('Execution of RPC') response = None diff --git a/pgoapi/rpc_api.py b/pgoapi/rpc_api.py index 258dc79f..43e66b9e 100755 --- a/pgoapi/rpc_api.py +++ b/pgoapi/rpc_api.py @@ -49,11 +49,14 @@ class RpcApi: RPC_ID = 0 - def __init__(self, auth_provider): + def __init__(self, auth_provider, proxy_config=None): self.log = logging.getLogger(__name__) - self._session = requests.session() + + if proxy_config is not None: + self._session.proxies = proxy_config + self._session.headers.update({'User-Agent': 'Niantic App'}) self._session.verify = True @@ -90,7 +93,7 @@ def _make_rpc(self, endpoint, request_proto_plain): request_proto_serialized = request_proto_plain.SerializeToString() try: http_response = self._session.post(endpoint, data=request_proto_serialized) - except requests.exceptions.ConnectionError as e: + except requests.exceptions.ConnectionError: raise ServerBusyOrOfflineException return http_response diff --git a/requirements.txt b/requirements.txt index 872071bb..d27efc92 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ requests==2.10.0 s2sphere==0.2.4 gpsoauth==0.3.0 six +requests[socks] \ No newline at end of file