diff --git a/plexapi/myplex.py b/plexapi/myplex.py index 925a189f8..15da2115d 100644 --- a/plexapi/myplex.py +++ b/plexapi/myplex.py @@ -923,6 +923,10 @@ class MyPlexResource(PlexObject): TAG = 'Device' key = 'https://plex.tv/api/resources?includeHttps=1&includeRelay=1' + # Default order to prioritize available resource connections + DEFAULT_LOCATION_ORDER = ['local', 'remote', 'relay'] + DEFAULT_SCHEME_ORDER = ['https', 'http'] + def _loadData(self, data): self._data = data self.name = data.attrib.get('name') @@ -947,38 +951,64 @@ def _loadData(self, data): self.ownerid = utils.cast(int, data.attrib.get('ownerId', 0)) self.sourceTitle = data.attrib.get('sourceTitle') # owners plex username. - def connect(self, ssl=None, timeout=None): - """ Returns a new :class:`~plexapi.server.PlexServer` or :class:`~plexapi.client.PlexClient` object. + def preferred_connections( + self, + ssl=None, + timeout=None, + locations=DEFAULT_LOCATION_ORDER, + schemes=DEFAULT_SCHEME_ORDER, + ): + """ Returns a sorted list of the available connection addresses for this resource. Often times there is more than one address specified for a server or client. - This function will prioritize local connections before remote or relay and HTTPS before HTTP. - After trying to connect to all available addresses for this resource and - assuming at least one connection was successful, the PlexServer object is built and returned. + Default behavior will prioritize local connections before remote or relay and HTTPS before HTTP. Parameters: ssl (bool, optional): Set True to only connect to HTTPS connections. Set False to only connect to HTTP connections. Set None (default) to connect to any HTTP or HTTPS connection. timeout (int, optional): The timeout in seconds to attempt each connection. - - Raises: - :exc:`~plexapi.exceptions.NotFound`: When unable to connect to any addresses for this resource. """ - # Keys in the order we want the connections to be sorted - locations = ['local', 'remote', 'relay'] - schemes = ['https', 'http'] connections_dict = {location: {scheme: [] for scheme in schemes} for location in locations} for connection in self.connections: # Only check non-local connections unless we own the resource if self.owned or (not self.owned and not connection.local): location = 'relay' if connection.relay else ('local' if connection.local else 'remote') - connections_dict[location]['http'].append(connection.httpuri) - connections_dict[location]['https'].append(connection.uri) + if location not in locations: + continue + if 'http' in schemes: + connections_dict[location]['http'].append(connection.httpuri) + if 'https' in schemes: + connections_dict[location]['https'].append(connection.uri) if ssl is True: schemes.remove('http') elif ssl is False: schemes.remove('https') connections = [] for location in locations: for scheme in schemes: connections.extend(connections_dict[location][scheme]) + return connections + + def connect( + self, + ssl=None, + timeout=None, + locations=DEFAULT_LOCATION_ORDER, + schemes=DEFAULT_SCHEME_ORDER, + ): + """ Returns a new :class:`~plexapi.server.PlexServer` or :class:`~plexapi.client.PlexClient` object. + Uses `MyPlexResource.preferred_connections()` to generate the priority order of connection addresses. + After trying to connect to all available addresses for this resource and + assuming at least one connection was successful, the PlexServer object is built and returned. + + Parameters: + ssl (bool, optional): Set True to only connect to HTTPS connections. Set False to + only connect to HTTP connections. Set None (default) to connect to any + HTTP or HTTPS connection. + timeout (int, optional): The timeout in seconds to attempt each connection. + + Raises: + :exc:`~plexapi.exceptions.NotFound`: When unable to connect to any addresses for this resource. + """ + connections = self.preferred_connections(ssl, timeout, locations, schemes) # Try connecting to all known resource connections in parellel, but # only return the first server (in order) that provides a response. cls = PlexServer if 'server' in self.provides else PlexClient