Lidarr/src/NzbDrone.Core/Notifications/MediaBrowser/MediaBrowserProxy.cs

117 lines
3.5 KiB
C#
Raw Normal View History

2018-04-08 04:38:12 +00:00
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using NLog;
2014-08-03 07:26:55 +00:00
using NzbDrone.Common.Http;
using NzbDrone.Common.Serializer;
2018-04-08 04:38:12 +00:00
using NzbDrone.Core.Notifications.MediaBrowser.Model;
2014-08-03 07:26:55 +00:00
2016-12-28 07:52:20 +00:00
namespace NzbDrone.Core.Notifications.Emby
2014-08-03 07:26:55 +00:00
{
public class MediaBrowserProxy
{
private readonly IHttpClient _httpClient;
private readonly Logger _logger;
public MediaBrowserProxy(IHttpClient httpClient, Logger logger)
{
_httpClient = httpClient;
_logger = logger;
}
public void Notify(MediaBrowserSettings settings, string title, string message)
{
var path = "/Notifications/Admin";
var request = BuildRequest(path, settings);
request.Headers.ContentType = "application/json";
request.Method = HttpMethod.Post;
2014-08-03 07:26:55 +00:00
request.SetContent(new
2018-04-08 04:38:12 +00:00
{
Name = title,
Description = message,
ImageUrl = "https://raw.github.com/lidarr/Lidarr/develop/Logo/64.png"
}.ToJson());
2014-08-03 07:26:55 +00:00
ProcessRequest(request, settings);
}
2018-04-08 04:38:12 +00:00
public void Update(MediaBrowserSettings settings, List<string> musicCollectionPaths)
2014-08-03 07:26:55 +00:00
{
2018-04-08 04:38:12 +00:00
string path;
HttpRequest request;
if (musicCollectionPaths.Any())
{
path = "/Library/Media/Updated";
request = BuildRequest(path, settings);
request.Headers.ContentType = "application/json";
var updateInfo = new List<EmbyMediaUpdateInfo>();
foreach (var colPath in musicCollectionPaths)
{
updateInfo.Add(new EmbyMediaUpdateInfo
{
Path = colPath,
UpdateType = "Created"
});
}
request.SetContent(new
{
Updates = updateInfo
}.ToJson());
}
else
{
path = "/Library/Refresh";
request = BuildRequest(path, settings);
}
request.Method = HttpMethod.Post;
2014-08-03 07:26:55 +00:00
ProcessRequest(request, settings);
}
private string ProcessRequest(HttpRequest request, MediaBrowserSettings settings)
2014-08-03 07:26:55 +00:00
{
request.Headers.Add("X-MediaBrowser-Token", settings.ApiKey);
2018-04-08 04:38:12 +00:00
var response = _httpClient.Execute(request);
2014-08-03 07:26:55 +00:00
_logger.Trace("Response: {0}", response.Content);
CheckForError(response);
return response.Content;
}
private HttpRequest BuildRequest(string path, MediaBrowserSettings settings)
{
2019-06-12 02:44:49 +00:00
var scheme = settings.UseSsl ? "https" : "http";
var url = $@"{scheme}://{settings.Address}/mediabrowser";
2018-04-08 04:38:12 +00:00
return new HttpRequestBuilder(url).Resource(path).Build();
2014-08-03 07:26:55 +00:00
}
private void CheckForError(HttpResponse response)
{
_logger.Debug("Looking for error in response: {0}", response);
// TODO: actually check for the error
2014-08-03 07:26:55 +00:00
}
2018-04-08 04:38:12 +00:00
public List<EmbyMediaFolder> GetArtist(MediaBrowserSettings settings)
{
var path = "/Library/MediaFolders";
var request = BuildRequest(path, settings);
request.Method = HttpMethod.Get;
2018-04-08 04:38:12 +00:00
var response = ProcessRequest(request, settings);
return Json.Deserialize<EmbyMediaFoldersResponse>(response).Items;
}
2014-08-03 07:26:55 +00:00
}
}