Add preliminary session support

This commit is contained in:
Håvard Gulldahl 2016-01-30 00:13:57 +01:00
parent 382a4f45aa
commit b6569dc6a3

View file

@ -24,9 +24,10 @@ DEFAULT_BASEURI = 'http://localhost:32400'
class PlexServer(object):
def __init__(self, baseuri=None, token=None):
def __init__(self, baseuri=None, token=None, session=None):
self.baseuri = baseuri or DEFAULT_BASEURI
self.token = token
self.session = session # set this as a requests.session to use that session
data = self._connect()
self.friendlyName = data.attrib.get('friendlyName')
self.machineIdentifier = data.attrib.get('machineIdentifier')
@ -80,10 +81,15 @@ class PlexServer(object):
headers['X-Plex-Token'] = self.token
return headers
def query(self, path, method=requests.get, **kwargs):
def query(self, path, method=None, **kwargs):
global TOTAL_QUERIES
TOTAL_QUERIES += 1
url = self.url(path)
if method is None:
if self.session is not None:
method = self.session.get
else:
method = requests.get
log.info('%s %s', method.__name__.upper(), url)
response = method(url, headers=self.headers(), timeout=TIMEOUT, **kwargs)
if response.status_code not in [200, 201]: