|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | | -using System.IO; |
4 | 3 | using System.Linq; |
5 | | -using System.Net; |
6 | | -using System.Text; |
7 | 4 | using MediaBrowser.Common.Net; |
8 | | -using MediaBrowser.Controller.Net; |
9 | 5 | using MediaBrowser.Model.Serialization; |
10 | 6 | using Microsoft.Extensions.Logging; |
11 | 7 | using MediaBrowser.Model.Services; |
12 | 8 | using Pushbullet.Configuration; |
13 | | -using System.Threading; |
14 | 9 | using System.Threading.Tasks; |
15 | 10 |
|
16 | 11 | namespace Pushbullet.Api |
17 | 12 | { |
18 | | - [Route("/Notification/Pushbullet/Test/{UserID}", "POST", Summary = "Tests Pushbullet")] |
| 13 | + [Route("/Notification/Pushbullet/Test/{UserId}", "POST", Summary = "Tests Pushbullet")] |
19 | 14 | public class TestNotification : IReturnVoid |
20 | 15 | { |
21 | | - [ApiMember(Name = "UserID", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] |
22 | | - public string UserID { get; set; } |
| 16 | + [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", |
| 17 | + ParameterType = "path", Verb = "GET")] |
| 18 | + public string UserId { get; set; } |
23 | 19 | } |
24 | 20 |
|
25 | | - class ServerApiEndpoints : IService |
| 21 | + public class ServerApiEndpoints : IService |
26 | 22 | { |
27 | 23 | private readonly IHttpClient _httpClient; |
28 | 24 | private readonly IJsonSerializer _jsonSerializer; |
29 | | - private readonly ILogger _logger; |
30 | 25 |
|
31 | | - public ServerApiEndpoints(ILogger logger, IJsonSerializer jsonSerializer, IHttpClient httpClient) |
| 26 | + public ServerApiEndpoints(IJsonSerializer jsonSerializer, IHttpClient httpClient) |
32 | 27 | { |
33 | | - _logger = logger; |
34 | | - _jsonSerializer = jsonSerializer; |
35 | | - _httpClient = httpClient; |
| 28 | + _jsonSerializer = jsonSerializer; |
| 29 | + _httpClient = httpClient; |
36 | 30 | } |
37 | | - private PushbulletOptions GetOptions(String userID) |
| 31 | + |
| 32 | + private static PushbulletOptions GetOptions(string userId) |
38 | 33 | { |
39 | 34 | return Plugin.Instance.Configuration.Options |
40 | | - .FirstOrDefault(i => string.Equals(i.MediaBrowserUserId, userID, StringComparison.OrdinalIgnoreCase)); |
| 35 | + .FirstOrDefault(i => string.Equals(i.UserId, userId, StringComparison.OrdinalIgnoreCase)); |
41 | 36 | } |
42 | 37 |
|
43 | 38 | public void Post(TestNotification request) |
44 | 39 | { |
45 | | - var task = PostAsync(request); |
46 | | - Task.WaitAll(task); |
| 40 | + PostAsync(request) |
| 41 | + .GetAwaiter() |
| 42 | + .GetResult(); |
47 | 43 | } |
48 | 44 |
|
49 | 45 | public async Task PostAsync(TestNotification request) |
50 | 46 | { |
51 | | - var options = GetOptions(request.UserID); |
| 47 | + var options = GetOptions(request.UserId); |
52 | 48 |
|
53 | 49 | var parameters = new Dictionary<string, string> |
54 | 50 | { |
55 | 51 | {"type", "note"}, |
56 | | - {"title", "Test Notification" }, |
| 52 | + {"title", "Test Notification"}, |
57 | 53 | {"body", "This is a test notification from Jellyfin"} |
58 | 54 | }; |
59 | 55 |
|
60 | | - var _httpRequest = new HttpRequestOptions(); |
61 | | - |
62 | | - //Create Basic HTTP Auth Header... |
63 | | - |
64 | | - string authInfo = options.Token; |
65 | | - authInfo = Convert.ToBase64String(Encoding.UTF8.GetBytes(authInfo)); |
66 | | - |
67 | 56 | var requestOptions = new HttpRequestOptions |
68 | 57 | { |
69 | | - Url = "https://api.pushbullet.com/v2/pushes", |
| 58 | + Url = PluginConfiguration.Url, |
70 | 59 | RequestContent = _jsonSerializer.SerializeToString(parameters), |
71 | | - BufferContent = false, |
72 | 60 | RequestContentType = "application/json", |
73 | 61 | LogErrorResponseBody = true, |
74 | | - DecompressionMethod = CompressionMethod.None, |
75 | | - EnableKeepAlive = false |
| 62 | + RequestHeaders = {["Access-Token"] = options.Token} |
76 | 63 | }; |
77 | | - requestOptions.RequestHeaders["Authorization"] = "Basic " + authInfo; |
| 64 | + |
78 | 65 | await _httpClient.Post(requestOptions).ConfigureAwait(false); |
79 | 66 | } |
80 | 67 | } |
|
0 commit comments