Skip to content

Commit 39ec752

Browse files
Kevin JensenKevin Jensen
authored andcommitted
added GetSessionByPlayerId to Server API
1 parent 40519f8 commit 39ec752

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

Source/Plex.ServerApi/Clients/PlexServerClient.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace Plex.ServerApi.Clients
22
{
33
using System;
44
using System.Collections.Generic;
5+
using System.Linq;
56
using System.Net.Http;
67
using System.Threading.Tasks;
78
using Api;
@@ -245,9 +246,26 @@ public async Task<SessionContainer> GetSessionsAsync(string authToken, string pl
245246
}
246247

247248
/// <inheritdoc/>
248-
public Task<SessionContainer> GetSessionByPlayerIdAsync(string authToken, string plexServerHost,
249+
public async Task<SessionContainer> GetSessionByPlayerIdAsync(string authToken, string plexServerHost,
249250
string playerKey)
250-
=> throw new NotImplementedException();
251+
{
252+
var sessionContainer = await this.GetSessionsAsync(authToken, plexServerHost);
253+
254+
var sessionMetadata = sessionContainer.Metadata.SingleOrDefault(c => c.Player.MachineIdentifier == playerKey);
255+
256+
if (sessionMetadata != null)
257+
{
258+
sessionContainer.Size = 1;
259+
sessionContainer.Metadata = new List<SessionMetadata> {sessionMetadata};
260+
}
261+
else
262+
{
263+
sessionContainer.Size = 0;
264+
sessionContainer.Metadata = null;
265+
}
266+
267+
return sessionContainer;
268+
}
251269

252270
/// <inheritdoc/>
253271
public async Task UnScrobbleItemAsync(string authToken, string plexServerHost, string key)

Tests/Plex.Library.Test/Tests/ServerTest.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,27 @@ public async void Test_GetPlayHistory()
4747
Assert.True(server.Size > 0);
4848
}
4949

50+
[Fact]
51+
public async void Test_GetPlexServer_GetSessions()
52+
{
53+
var session = await this.plexServerClient.GetSessionsAsync(this.config.AuthenticationKey,
54+
this.config.Host);
55+
56+
Assert.NotNull(session);
57+
Assert.True(session.Size > 0);
58+
}
59+
60+
[Fact]
61+
public async void Test_GetPlexServer_GetSessionByPlayer()
62+
{
63+
const string playerKey = "jjmgbymh449v5jc7ip90sbj7";
64+
var session = await this.plexServerClient.GetSessionByPlayerIdAsync(this.config.AuthenticationKey,
65+
this.config.Host, playerKey);
66+
67+
Assert.NotNull(session);
68+
Assert.True(session.Size > 0);
69+
}
70+
5071
[Fact]
5172
public async void Test_GetPosterArt()
5273
{

0 commit comments

Comments
 (0)