private async Task LoadThumbImageIfRequired(Video video) { if (!video.VideoImageSource.IsNullOrEmpty() && video.ThumbImageSource == null) { Debug.WriteLine("Loading image for " + video.Title + " from " + video.VideoImageSource + " with token " + video.PlexServerConnection.User.AuthenticationToken); var image = await _imageHelper.GetImageAsync(video.PlexServerConnection.ConnectionUri, video.VideoImageSource, headers : PlexHeaders.CreatePlexRequest(video.PlexServerConnection.User), canCache : true); Debug.WriteLine(image == null ? "Loading image failed." : "Loading image success!"); if (image != null) { video.ThumbImageSource = image; } } if (!video.Art.IsNullOrEmpty() && video.ArtImageSource == null) { Debug.WriteLine("Loading image for " + video.Title + " from " + video.Art + " with token " + video.PlexServerConnection.User.AuthenticationToken); var image = await _imageHelper.GetImageAsync(video.PlexServerConnection.ConnectionUri, video.Art, headers : PlexHeaders.CreatePlexRequest(video.PlexServerConnection.User), canCache : true); Debug.WriteLine(image == null ? "Loading image failed." : "Loading image success!"); if (image != null) { video.ArtImageSource = image; } } }
private async Task <RestResponse <T> > MakePlexRequestAsync <T, TData>(string uri, string resource) where T : class, new() where TData : class { if (_needsAuth) { return(await _restConnection.MakeRequestAsync <T, TData>(Method.Get, ResponseType.Xml, uri, resource, headers : PlexHeaders.CreatePlexRequest(User))); } var retVal = await _restConnection.MakeRequestAsync <T, TData>(Method.Get, ResponseType.Xml, uri, resource, headers : PlexHeaders.CreatePlexRequest()); if ((retVal == null || retVal.ResponseObject == null) && User != null) { _needsAuth = true; retVal = await _restConnection.MakeRequestAsync <T, TData>(Method.Get, ResponseType.Xml, uri, resource, headers : PlexHeaders.CreatePlexRequest(User)); } return(retVal); }
public async Task ConnectAsync(string username, string password) { await Task.Run(() => { lock (_connectSynObj) { if (ConnectionStatus != MyPlexConnectionStatus.Connected || _username != username || _password != password) { ConnectionStatus = MyPlexConnectionStatus.Connecting; User = null; _username = username; _password = password; try { // fix for https://bugzilla.xamarin.com/show_bug.cgi?id=30869 // create the authentication headers manually instead of using HttpClient // to fix an issue on iOS var user = AsyncHelper.RunSync(() => _restConnection.MakeRequestAsync <PlexUser, string>(Method.Post, ResponseType.Xml, PlexResources.MyPlexBaseUrl, PlexResources.MyPlexSignIn, headers: PlexHeaders.CreatePlexRequest(userName: _username, password: _password))); if (user.ResponseObject != null) { User = user.ResponseObject; ConnectionStatus = MyPlexConnectionStatus.Connected; } else { ConnectionStatus = MyPlexConnectionStatus.AuthorizationFailed; } } catch (Exception ex) { ConnectionStatus = MyPlexConnectionStatus.AuthorizationFailed; Debug.WriteLine("Exception connecting to MyPlex" + ex); User = null; } } AsyncHelper.RunSync(RefreshContainerAsync); } }); }
public async Task RefreshContainerAsync() { Guid token; lock (_tokenSyncObj) { token = Guid.NewGuid(); _refreshToken = token; } try { var container = await _restConnection.MakeRequestAsync <MediaContainer, string>(Method.Get, ResponseType.Xml, PlexResources.MyPlexBaseUrl, PlexResources.MyPlexDevices, headers : PlexHeaders.CreatePlexRequest(User)); if (container != null && container.ResponseObject != null) { bool updated; lock (_deviceSyncObj) { if (token != _refreshToken) { return; } updated = _devices.UpdateToMatch(container.ResponseObject.Devices, d => d.ClientIdentifier, UpdateDevice); _servers.UpdateToMatch(GetByProvides(container.ResponseObject, "server"), d => d.ClientIdentifier); _players.UpdateToMatch(GetByProvides(container.ResponseObject, "player"), d => d.ClientIdentifier); } if (updated) { OnDevicesUpdated(); } } } catch { var updated = false; lock (_deviceSyncObj) { if (token != _refreshToken) { return; } // lost connection, so clear everything if (_devices.Any()) { _devices.Clear(); _servers.Clear(); _players.Clear(); updated = true; } } if (updated) { OnDevicesUpdated(); } } }