Skip to content

Commit 48ade28

Browse files
Kevin JensenKevin Jensen
authored andcommitted
updated test
1 parent 9f42934 commit 48ade28

4 files changed

Lines changed: 91 additions & 3 deletions

File tree

Source/Plex.ServerApi/Clients/Interfaces/IPlexAccountClient.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,22 @@ public interface IPlexAccountClient
189189
/// <param name="authToken">Plex Auth Token</param>
190190
/// <returns>List of Shared Items and the Users associated</returns>
191191
Task<List<SharedItemContainer>> GetSharedItems(string authToken);
192+
193+
/// <summary>
194+
/// Remove Shared Item for Admin Account
195+
/// </summary>
196+
/// <param name="authToken">Plex Auth Token</param>
197+
/// <param name="sharedItemId">Shared Item Identifier</param>
198+
/// <returns></returns>
199+
Task RemoveSharedItem(string authToken, int sharedItemId);
200+
201+
/// <summary>
202+
/// Add Shared items from Admin Account to another user
203+
/// </summary>
204+
/// <param name="authToken">Plex Auth Token</param>
205+
/// <param name="sharedUserId">User Id to share with</param>
206+
/// <param name="sharedItems">Items to share</param>
207+
/// <returns></returns>
208+
Task AddSharedItems(string authToken, int sharedUserId, List<SharedItemModelRequest> sharedItems);
192209
}
193210
}

Source/Plex.ServerApi/Clients/PlexAccountClient.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ public async Task<User> SignInAsync(string username, string password)
110110
/// <inheritdoc/>
111111
public async Task<PlexModels.Account.PlexAccount> GetPlexHomeAccountAsync(string authToken, string userUuid)
112112
{
113-
114113
var apiRequest =
115114
new ApiRequestBuilder($"https://plex.tv/api/v2/home/users/{userUuid}/switch.json", string.Empty, HttpMethod.Post)
116115
.AddRequestHeaders(ClientUtilities.GetClientIdentifierHeader(this.clientOptions.ClientId))
@@ -425,5 +424,42 @@ public async Task<List<SharedItemContainer>> GetSharedItems(string authToken)
425424

426425
return items;
427426
}
427+
428+
/// <inheritdoc/>
429+
public async Task RemoveSharedItem(string authToken, int sharedItemId)
430+
{
431+
var apiRequest =
432+
new ApiRequestBuilder($"https://plex.tv/api/v2/shared_sources/{sharedItemId}", string.Empty,
433+
HttpMethod.Delete)
434+
.AddPlexToken(authToken)
435+
.AddRequestHeaders(ClientUtilities.GetClientMetaHeaders(this.clientOptions))
436+
.AddRequestHeaders(ClientUtilities.GetClientIdentifierHeader(this.clientOptions.ClientId))
437+
.AcceptJson()
438+
.Build();
439+
440+
await this.apiService.InvokeApiAsync(apiRequest);
441+
}
442+
443+
/// <inheritdoc/>
444+
public async Task AddSharedItems(string authToken, int sharedUserId, List<SharedItemModelRequest> sharedItems)
445+
{
446+
var queryParams =
447+
new Dictionary<string, string>
448+
{
449+
{ "invitedId", sharedUserId.ToString() },
450+
{ "items", System.Text.Json.JsonSerializer.Serialize(sharedItems) }
451+
};
452+
453+
var apiRequest =
454+
new ApiRequestBuilder($"https://plex.tv/api/v2/shared_sources", string.Empty,
455+
HttpMethod.Post)
456+
.AddPlexToken(authToken)
457+
.AddRequestHeaders(ClientUtilities.GetClientMetaHeaders(this.clientOptions))
458+
.AddRequestHeaders(ClientUtilities.GetClientIdentifierHeader(this.clientOptions.ClientId))
459+
.AcceptJson()
460+
.Build();
461+
462+
await this.apiService.InvokeApiAsync(apiRequest);
463+
}
428464
}
429465
}
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
namespace Plex.ServerApi.PlexModels.Account.SharedItems;
22

3+
using System.Collections.Generic;
4+
35
public class SharedItemAddModel
46
{
5-
7+
public int InvitedId { get; set; }
8+
public List<SharedItemModelRequest> Items { get; set; } = new();
9+
}
10+
11+
public class SharedItemModelRequest
12+
{
13+
public string Uri { get; set; }
14+
public string Type { get; set; }
15+
public string Title { get; set; }
616
}

Tests/Plex.Library.Test/Tests/AccountTest.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
namespace Plex.Library.Test.Tests
22
{
3+
using System.Collections.Generic;
34
using Microsoft.Extensions.DependencyInjection;
45
using ServerApi.Clients.Interfaces;
56
using ServerApi.Enums;
7+
using ServerApi.PlexModels.Account.SharedItems;
68
using Xunit;
79
using Xunit.Abstractions;
810

@@ -34,10 +36,33 @@ public async void Test_GetWatchlist()
3436
public async void Test_GetAccountSharedItems()
3537
{
3638
var sharedItems = await this.plexAccountClient.GetSharedItems(this.config.AuthenticationKey);
37-
3839
Assert.NotEmpty(sharedItems);
3940
}
4041

42+
43+
[Fact]
44+
public async void Test_RemoveSharedItem()
45+
{
46+
const int sharedItemId = 30279190;
47+
await this.plexAccountClient.RemoveSharedItem(this.config.AuthenticationKey, sharedItemId);
48+
}
49+
50+
[Fact]
51+
public async void Test_AddSharedItem()
52+
{
53+
var request = new List<SharedItemModelRequest>
54+
{
55+
new()
56+
{
57+
Uri = "server://31a76d623a6ec28b1619bd7679650d84d9552728/com.plexapp.plugins.library/library/metadata/350061",
58+
Title = "The Northman",
59+
Type = "movie"
60+
}
61+
};
62+
63+
await this.plexAccountClient.AddSharedItems(this.config.AuthenticationKey, 20071940, request);
64+
}
65+
4166
[Fact]
4267
public async void Test_OnWatchlist()
4368
{

0 commit comments

Comments
 (0)