Skip to content

Commit

Permalink
Allow using a proxy.
Browse files Browse the repository at this point in the history
  • Loading branch information
tschroeder-zendesk committed Aug 3, 2016
1 parent 0811db2 commit 2829970
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
14 changes: 10 additions & 4 deletions pgoapi/pgoapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import re
import six
import logging
import requests

from . import __title__, __version__, __copyright__
from pgoapi.rpc_api import RpcApi
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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 """
Expand All @@ -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):
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions pgoapi/rpc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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, requesocks.exceptions.ConnectionError) as e:
raise ServerBusyOrOfflineException

return http_response
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ requests==2.10.0
s2sphere==0.2.4
gpsoauth==0.3.0
six
requests[socks]

0 comments on commit 2829970

Please sign in to comment.