From ea3e192fa382f035caee4d6dd8a8cfb05f2eac9d Mon Sep 17 00:00:00 2001 From: CED0001 Date: Thu, 2 Nov 2023 20:42:14 +0100 Subject: [PATCH 1/5] added instanceToken and forceHttps to PlexAccountClient.GetAccountServers --- .../ApiModels/Accounts/PlexAccount.cs | 4 ++-- .../Clients/Interfaces/IPlexAccountClient.cs | 2 +- .../Clients/PlexAccountClient.cs | 20 ++++++++++++++++++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Source/Plex.Library/ApiModels/Accounts/PlexAccount.cs b/Source/Plex.Library/ApiModels/Accounts/PlexAccount.cs index 6fd29f9..4309f3e 100644 --- a/Source/Plex.Library/ApiModels/Accounts/PlexAccount.cs +++ b/Source/Plex.Library/ApiModels/Accounts/PlexAccount.cs @@ -160,10 +160,10 @@ public async Task ServerSummaries() => /// Get Active Servers tied to this Account /// /// List of Server objects - public async Task> Servers() + public async Task> Servers(bool forceHttps = false, string instanceToken = null) { var servers = new List(); - var accountServerContainer = await this.plexAccountClient.GetAccountServersAsync(this.AuthToken); + var accountServerContainer = await this.plexAccountClient.GetAccountServersAsync(this.AuthToken, forceHttps, instanceToken); foreach (var server in accountServerContainer.Servers) { try diff --git a/Source/Plex.ServerApi/Clients/Interfaces/IPlexAccountClient.cs b/Source/Plex.ServerApi/Clients/Interfaces/IPlexAccountClient.cs index 337f4a5..426aace 100644 --- a/Source/Plex.ServerApi/Clients/Interfaces/IPlexAccountClient.cs +++ b/Source/Plex.ServerApi/Clients/Interfaces/IPlexAccountClient.cs @@ -71,7 +71,7 @@ public interface IPlexAccountClient /// /// Authentication Token. /// AccountServerContainer. - Task GetAccountServersAsync(string authToken); + Task GetAccountServersAsync(string authToken, bool forceHttps = false, string instanceToken = null); /// /// Get Plex Announcements diff --git a/Source/Plex.ServerApi/Clients/PlexAccountClient.cs b/Source/Plex.ServerApi/Clients/PlexAccountClient.cs index 8c5dd1b..47d8b38 100644 --- a/Source/Plex.ServerApi/Clients/PlexAccountClient.cs +++ b/Source/Plex.ServerApi/Clients/PlexAccountClient.cs @@ -143,7 +143,7 @@ public async Task SignInAsync(string username, string password) } /// - public async Task GetAccountServersAsync(string authToken) + public async Task GetAccountServersAsync(string authToken, bool forceHttps = false, string instanceToken = null) { var apiRequest = new ApiRequestBuilder("https://plex.tv/pms/servers.xml", string.Empty, HttpMethod.Get) .AddPlexToken(authToken) @@ -152,6 +152,24 @@ public async Task GetAccountServersAsync(string authToke var serverContainer = await this.apiService.InvokeApiAsync(apiRequest); + if (forceHttps) + { + foreach (var server in serverContainer.Servers) + { + server.Scheme = "https"; + } + } + + if (string.IsNullOrEmpty(instanceToken)) + { + return serverContainer; + } + + foreach (var server in serverContainer.Servers) + { + server.Host = $"{server.Host.Replace(".", "-")}.{instanceToken}.plex.direct"; + } + return serverContainer; } From 2fc851582a0e4d415c5facf50ebb476ff452c62a Mon Sep 17 00:00:00 2001 From: CED0001 Date: Fri, 3 Nov 2023 10:08:37 +0100 Subject: [PATCH 2/5] .servers() now lets user overrider HOST string per owned server --- .../ApiModels/Accounts/PlexAccount.cs | 4 ++-- .../Clients/Interfaces/IPlexAccountClient.cs | 2 +- .../Clients/PlexAccountClient.cs | 19 +++++++------------ 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/Source/Plex.Library/ApiModels/Accounts/PlexAccount.cs b/Source/Plex.Library/ApiModels/Accounts/PlexAccount.cs index 4309f3e..a6f02e5 100644 --- a/Source/Plex.Library/ApiModels/Accounts/PlexAccount.cs +++ b/Source/Plex.Library/ApiModels/Accounts/PlexAccount.cs @@ -160,10 +160,10 @@ public async Task ServerSummaries() => /// Get Active Servers tied to this Account /// /// List of Server objects - public async Task> Servers(bool forceHttps = false, string instanceToken = null) + public async Task> Servers(bool forceHttps = false, Dictionary overrideHost = null) { var servers = new List(); - var accountServerContainer = await this.plexAccountClient.GetAccountServersAsync(this.AuthToken, forceHttps, instanceToken); + var accountServerContainer = await this.plexAccountClient.GetAccountServersAsync(this.AuthToken, forceHttps, overrideHost); foreach (var server in accountServerContainer.Servers) { try diff --git a/Source/Plex.ServerApi/Clients/Interfaces/IPlexAccountClient.cs b/Source/Plex.ServerApi/Clients/Interfaces/IPlexAccountClient.cs index 426aace..91ada74 100644 --- a/Source/Plex.ServerApi/Clients/Interfaces/IPlexAccountClient.cs +++ b/Source/Plex.ServerApi/Clients/Interfaces/IPlexAccountClient.cs @@ -71,7 +71,7 @@ public interface IPlexAccountClient /// /// Authentication Token. /// AccountServerContainer. - Task GetAccountServersAsync(string authToken, bool forceHttps = false, string instanceToken = null); + Task GetAccountServersAsync(string authToken, bool forceHttps = false, Dictionary overrideHost = null); /// /// Get Plex Announcements diff --git a/Source/Plex.ServerApi/Clients/PlexAccountClient.cs b/Source/Plex.ServerApi/Clients/PlexAccountClient.cs index 47d8b38..bc28a92 100644 --- a/Source/Plex.ServerApi/Clients/PlexAccountClient.cs +++ b/Source/Plex.ServerApi/Clients/PlexAccountClient.cs @@ -143,7 +143,7 @@ public async Task SignInAsync(string username, string password) } /// - public async Task GetAccountServersAsync(string authToken, bool forceHttps = false, string instanceToken = null) + public async Task GetAccountServersAsync(string authToken, bool forceHttps = false, Dictionary overrideHost = null) { var apiRequest = new ApiRequestBuilder("https://plex.tv/pms/servers.xml", string.Empty, HttpMethod.Get) .AddPlexToken(authToken) @@ -152,22 +152,17 @@ public async Task GetAccountServersAsync(string authToke var serverContainer = await this.apiService.InvokeApiAsync(apiRequest); - if (forceHttps) + foreach (var server in serverContainer.Servers) { - foreach (var server in serverContainer.Servers) + if (forceHttps) { server.Scheme = "https"; } - } - - if (string.IsNullOrEmpty(instanceToken)) - { - return serverContainer; - } - foreach (var server in serverContainer.Servers) - { - server.Host = $"{server.Host.Replace(".", "-")}.{instanceToken}.plex.direct"; + if (overrideHost != null && overrideHost.TryGetValue(server.Name, out var host)) + { + server.Host = host; + } } return serverContainer; From d64a7731028df8e7900a1a11df61e617a0c4825a Mon Sep 17 00:00:00 2001 From: CED0001 Date: Fri, 3 Nov 2023 10:13:57 +0100 Subject: [PATCH 3/5] Added param descriptions --- Source/Plex.Library/ApiModels/Accounts/PlexAccount.cs | 3 +++ Source/Plex.ServerApi/Clients/Interfaces/IPlexAccountClient.cs | 3 +++ 2 files changed, 6 insertions(+) diff --git a/Source/Plex.Library/ApiModels/Accounts/PlexAccount.cs b/Source/Plex.Library/ApiModels/Accounts/PlexAccount.cs index a6f02e5..7282c7a 100644 --- a/Source/Plex.Library/ApiModels/Accounts/PlexAccount.cs +++ b/Source/Plex.Library/ApiModels/Accounts/PlexAccount.cs @@ -159,6 +159,9 @@ public async Task ServerSummaries() => /// /// Get Active Servers tied to this Account /// + /// Forces URI scheme to https + /// Accepts dictionary that overwrites the HOST part of the servers URI. + /// key = serverName, value = custom host string /// List of Server objects public async Task> Servers(bool forceHttps = false, Dictionary overrideHost = null) { diff --git a/Source/Plex.ServerApi/Clients/Interfaces/IPlexAccountClient.cs b/Source/Plex.ServerApi/Clients/Interfaces/IPlexAccountClient.cs index 91ada74..ed90534 100644 --- a/Source/Plex.ServerApi/Clients/Interfaces/IPlexAccountClient.cs +++ b/Source/Plex.ServerApi/Clients/Interfaces/IPlexAccountClient.cs @@ -70,6 +70,9 @@ public interface IPlexAccountClient /// Retrieves a list of servers tied to your Plex Account. /// /// Authentication Token. + /// Forces URI scheme to https + /// Accepts dictionary that overwrites the HOST part of the servers URI. + /// key = serverName, value = custom host string /// AccountServerContainer. Task GetAccountServersAsync(string authToken, bool forceHttps = false, Dictionary overrideHost = null); From cecab0bbcc73ac78f2ff3184cf5d5888341fe3cc Mon Sep 17 00:00:00 2001 From: CED0001 Date: Fri, 3 Nov 2023 10:19:34 +0100 Subject: [PATCH 4/5] extended new functionality to serverSummaries --- Source/Plex.Library/ApiModels/Accounts/PlexAccount.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Source/Plex.Library/ApiModels/Accounts/PlexAccount.cs b/Source/Plex.Library/ApiModels/Accounts/PlexAccount.cs index 7282c7a..9c0c455 100644 --- a/Source/Plex.Library/ApiModels/Accounts/PlexAccount.cs +++ b/Source/Plex.Library/ApiModels/Accounts/PlexAccount.cs @@ -152,9 +152,12 @@ public async Task ClaimToken(string pinId) => /// Get Server Summaries. This does not return a Server Instance but a summary /// of all servers tied to your Plex Account. The servers may not be active/online. /// + /// Forces URI scheme to https + /// Accepts dictionary that overwrites the HOST part of the servers URI. + /// key = serverName, value = custom host string /// AccountServerContainer - public async Task ServerSummaries() => - await this.plexAccountClient.GetAccountServersAsync(this.AuthToken); + public async Task ServerSummaries(bool forceHttps = false, Dictionary overrideHost = null) => + await this.plexAccountClient.GetAccountServersAsync(this.AuthToken, forceHttps, overrideHost); /// /// Get Active Servers tied to this Account From 85ac7960b1730deb09c03a00bcc4e899f4c274c0 Mon Sep 17 00:00:00 2001 From: CED0001 Date: Fri, 3 Nov 2023 10:23:03 +0100 Subject: [PATCH 5/5] added basic tests --- .../Plex.ServerApi.Test/Tests/AccountTest.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Tests/Plex.ServerApi.Test/Tests/AccountTest.cs b/Tests/Plex.ServerApi.Test/Tests/AccountTest.cs index e4cded6..42bf7e8 100644 --- a/Tests/Plex.ServerApi.Test/Tests/AccountTest.cs +++ b/Tests/Plex.ServerApi.Test/Tests/AccountTest.cs @@ -1,5 +1,6 @@ namespace Plex.ServerApi.Test.Tests { + using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Clients.Interfaces; @@ -209,6 +210,24 @@ public async void Test_Get_Plex_ServerSummaries() Assert.NotNull(container); } + [Fact] + public async void Test_Get_Plex_ServerSummaries_ForceHttps() + { + var container = await this.fixture.PlexAccount.ServerSummaries(true); + Assert.NotNull(container); + } + + [Fact] + public async void Test_Get_Plex_ServerSummaries_OverrideHosts() + { + var overrideHosts = new Dictionary(); + + // overrideHosts.Add("serverName", "customHost"); + + var container = await this.fixture.PlexAccount.ServerSummaries(true, overrideHosts); + Assert.NotNull(container); + } + [Fact] public async void Test_Opt_Out() {