Skip to content

Commit 3b1dce7

Browse files
authored
Decouple resource address sorting from connection attempts (#800)
* Decouple resource address sorting from connection attempts * Fix lint * Move defaults to class
1 parent 2c540f6 commit 3b1dce7

File tree

1 file changed

+43
-13
lines changed

1 file changed

+43
-13
lines changed

plexapi/myplex.py

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,10 @@ class MyPlexResource(PlexObject):
923923
TAG = 'Device'
924924
key = 'https://plex.tv/api/resources?includeHttps=1&includeRelay=1'
925925

926+
# Default order to prioritize available resource connections
927+
DEFAULT_LOCATION_ORDER = ['local', 'remote', 'relay']
928+
DEFAULT_SCHEME_ORDER = ['https', 'http']
929+
926930
def _loadData(self, data):
927931
self._data = data
928932
self.name = data.attrib.get('name')
@@ -947,38 +951,64 @@ def _loadData(self, data):
947951
self.ownerid = utils.cast(int, data.attrib.get('ownerId', 0))
948952
self.sourceTitle = data.attrib.get('sourceTitle') # owners plex username.
949953

950-
def connect(self, ssl=None, timeout=None):
951-
""" Returns a new :class:`~plexapi.server.PlexServer` or :class:`~plexapi.client.PlexClient` object.
954+
def preferred_connections(
955+
self,
956+
ssl=None,
957+
timeout=None,
958+
locations=DEFAULT_LOCATION_ORDER,
959+
schemes=DEFAULT_SCHEME_ORDER,
960+
):
961+
""" Returns a sorted list of the available connection addresses for this resource.
952962
Often times there is more than one address specified for a server or client.
953-
This function will prioritize local connections before remote or relay and HTTPS before HTTP.
954-
After trying to connect to all available addresses for this resource and
955-
assuming at least one connection was successful, the PlexServer object is built and returned.
963+
Default behavior will prioritize local connections before remote or relay and HTTPS before HTTP.
956964
957965
Parameters:
958966
ssl (bool, optional): Set True to only connect to HTTPS connections. Set False to
959967
only connect to HTTP connections. Set None (default) to connect to any
960968
HTTP or HTTPS connection.
961969
timeout (int, optional): The timeout in seconds to attempt each connection.
962-
963-
Raises:
964-
:exc:`~plexapi.exceptions.NotFound`: When unable to connect to any addresses for this resource.
965970
"""
966-
# Keys in the order we want the connections to be sorted
967-
locations = ['local', 'remote', 'relay']
968-
schemes = ['https', 'http']
969971
connections_dict = {location: {scheme: [] for scheme in schemes} for location in locations}
970972
for connection in self.connections:
971973
# Only check non-local connections unless we own the resource
972974
if self.owned or (not self.owned and not connection.local):
973975
location = 'relay' if connection.relay else ('local' if connection.local else 'remote')
974-
connections_dict[location]['http'].append(connection.httpuri)
975-
connections_dict[location]['https'].append(connection.uri)
976+
if location not in locations:
977+
continue
978+
if 'http' in schemes:
979+
connections_dict[location]['http'].append(connection.httpuri)
980+
if 'https' in schemes:
981+
connections_dict[location]['https'].append(connection.uri)
976982
if ssl is True: schemes.remove('http')
977983
elif ssl is False: schemes.remove('https')
978984
connections = []
979985
for location in locations:
980986
for scheme in schemes:
981987
connections.extend(connections_dict[location][scheme])
988+
return connections
989+
990+
def connect(
991+
self,
992+
ssl=None,
993+
timeout=None,
994+
locations=DEFAULT_LOCATION_ORDER,
995+
schemes=DEFAULT_SCHEME_ORDER,
996+
):
997+
""" Returns a new :class:`~plexapi.server.PlexServer` or :class:`~plexapi.client.PlexClient` object.
998+
Uses `MyPlexResource.preferred_connections()` to generate the priority order of connection addresses.
999+
After trying to connect to all available addresses for this resource and
1000+
assuming at least one connection was successful, the PlexServer object is built and returned.
1001+
1002+
Parameters:
1003+
ssl (bool, optional): Set True to only connect to HTTPS connections. Set False to
1004+
only connect to HTTP connections. Set None (default) to connect to any
1005+
HTTP or HTTPS connection.
1006+
timeout (int, optional): The timeout in seconds to attempt each connection.
1007+
1008+
Raises:
1009+
:exc:`~plexapi.exceptions.NotFound`: When unable to connect to any addresses for this resource.
1010+
"""
1011+
connections = self.preferred_connections(ssl, timeout, locations, schemes)
9821012
# Try connecting to all known resource connections in parellel, but
9831013
# only return the first server (in order) that provides a response.
9841014
cls = PlexServer if 'server' in self.provides else PlexClient

0 commit comments

Comments
 (0)