make connect build the correct type.

This commit is contained in:
Hellowlol 2017-07-27 01:28:14 +02:00
parent 36630d7448
commit fa2bbeabb8

View file

@ -449,11 +449,11 @@ class MyPlexResource(PlexObject):
self.connections = self.findItems(data, ResourceConnection) self.connections = self.findItems(data, ResourceConnection)
def connect(self, ssl=None, timeout=None): def connect(self, ssl=None, timeout=None):
""" Returns a new :class:`~server.PlexServer` object. Often times there is more than """ Returns a new :class:`~server.PlexServer` or :class:`~client.PlexClient` object.
one address specified for a server or client. This function will prioritize local Often times there is more than one address specified for a server or client.
connections before remote and HTTPS before HTTP. After trying to connect to all This function will prioritize local connections before remote and HTTPS before HTTP.
available addresses for this resource and assuming at least one connection was After trying to connect to all available addresses for this resource and
successful, the PlexServer object is built and returned. assuming at least one connection was successful, the PlexServer object is built and returned.
Parameters: Parameters:
ssl (optional): Set True to only connect to HTTPS connections. Set False to ssl (optional): Set True to only connect to HTTPS connections. Set False to
@ -469,13 +469,19 @@ class MyPlexResource(PlexObject):
owned_or_unowned_non_local = lambda x: self.owned or (not self.owned and not x.local) owned_or_unowned_non_local = lambda x: self.owned or (not self.owned and not x.local)
https = [c.uri for c in connections if owned_or_unowned_non_local(c)] https = [c.uri for c in connections if owned_or_unowned_non_local(c)]
http = [c.httpuri for c in connections if owned_or_unowned_non_local(c)] http = [c.httpuri for c in connections if owned_or_unowned_non_local(c)]
if 'server' in self.provides:
klass = PlexServer
elif 'client' in self.provides:
klass = PlexClient
# Force ssl, no ssl, or any (default) # Force ssl, no ssl, or any (default)
if ssl is True: connections = https if ssl is True: connections = https
elif ssl is False: connections = http elif ssl is False: connections = http
else: connections = https + http else: connections = https + http
# Try connecting to all known resource connections in parellel, but # Try connecting to all known resource connections in parellel, but
# only return the first server (in order) that provides a response. # only return the first server (in order) that provides a response.
listargs = [[PlexServer, url, self.accessToken, timeout] for url in connections] listargs = [[klass, url, self.accessToken, timeout] for url in connections]
log.info('Testing %s resource connections..', len(listargs)) log.info('Testing %s resource connections..', len(listargs))
results = utils.threaded(_connect, listargs) results = utils.threaded(_connect, listargs)
return _chooseConnection('Resource', self.name, results) return _chooseConnection('Resource', self.name, results)
@ -560,15 +566,20 @@ class MyPlexDevice(PlexObject):
self.connections = [connection.attrib.get('uri') for connection in data.iter('Connection')] self.connections = [connection.attrib.get('uri') for connection in data.iter('Connection')]
def connect(self, timeout=None): def connect(self, timeout=None):
""" Returns a new :class:`~plexapi.client.PlexClient` object. Sometimes there is more than """ Returns a new :class:`~plexapi.client.PlexClient` or :class:`~plexapi.server.PlexServer`
one address specified for a server or client. After trying to connect to all available Sometimes there is more than one address specified for a server or client.
addresses for this client and assuming at least one connection was successful, the After trying to connect to all available addresses for this client and assuming
PlexClient object is built and returned. at least one connection was successful, the PlexClient object is built and returned.
Raises: Raises:
:class:`~plexapi.exceptions.NotFound`: When unable to connect to any addresses for this device. :class:`~plexapi.exceptions.NotFound`: When unable to connect to any addresses for this device.
""" """
listargs = [[PlexClient, url, self.token, timeout] for url in self.connections] if 'server' in self.provides:
klass = PlexServer
elif 'client' in self.provides:
klass = PlexClient
listargs = [[klass, url, self.token, timeout] for url in self.connections]
log.info('Testing %s device connections..', len(listargs)) log.info('Testing %s device connections..', len(listargs))
results = utils.threaded(_connect, listargs) results = utils.threaded(_connect, listargs)
_chooseConnection('Device', self.name, results) _chooseConnection('Device', self.name, results)