jellyfin/MediaBrowser.Api/PackageReviewService.cs

163 lines
6.5 KiB
C#
Raw Normal View History

2014-12-01 12:43:34 +00:00
using MediaBrowser.Common.Net;
2015-01-10 19:42:14 +00:00
using MediaBrowser.Controller;
2014-07-02 18:34:08 +00:00
using MediaBrowser.Controller.Net;
2013-11-08 20:53:09 +00:00
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Serialization;
2014-03-23 20:07:02 +00:00
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.Services;
2013-11-07 17:27:05 +00:00
namespace MediaBrowser.Api
{
/// <summary>
/// Class InstallPackage
/// </summary>
2014-03-23 20:07:02 +00:00
[Route("/Packages/Reviews/{Id}", "POST", Summary = "Creates or updates a package review")]
2013-11-07 17:27:05 +00:00
public class CreateReviewRequest : IReturnVoid
{
/// <summary>
/// Gets or sets the Id.
/// </summary>
/// <value>The Id.</value>
[ApiMember(Name = "Id", Description = "Package Id", IsRequired = true, DataType = "int", ParameterType = "path", Verb = "POST")]
public int Id { get; set; }
/// <summary>
/// Gets or sets the rating.
/// </summary>
/// <value>The review.</value>
[ApiMember(Name = "Rating", Description = "The rating value (1-5)", IsRequired = true, DataType = "int", ParameterType = "query", Verb = "POST")]
public int Rating { get; set; }
/// <summary>
/// Gets or sets the recommend value.
/// </summary>
/// <value>Whether or not this review recommends this item.</value>
[ApiMember(Name = "Recommend", Description = "Whether or not this review recommends this item", IsRequired = true, DataType = "bool", ParameterType = "query", Verb = "POST")]
public bool Recommend { get; set; }
/// <summary>
/// Gets or sets the title.
/// </summary>
/// <value>The title.</value>
[ApiMember(Name = "Title", Description = "Optional short description of review.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public string Title { get; set; }
/// <summary>
/// Gets or sets the full review.
/// </summary>
/// <value>The full review.</value>
[ApiMember(Name = "Review", Description = "Optional full review.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public string Review { get; set; }
}
2013-11-08 20:53:09 +00:00
/// <summary>
/// Class InstallPackage
/// </summary>
2014-03-23 20:07:02 +00:00
[Route("/Packages/{Id}/Reviews", "GET", Summary = "Gets reviews for a package")]
2013-11-08 20:53:09 +00:00
public class ReviewRequest : IReturn<List<PackageReviewInfo>>
{
/// <summary>
/// Gets or sets the Id.
/// </summary>
/// <value>The Id.</value>
[ApiMember(Name = "Id", Description = "Package Id", IsRequired = true, DataType = "int", ParameterType = "path", Verb = "GET")]
public int Id { get; set; }
/// <summary>
/// Gets or sets the max rating.
/// </summary>
/// <value>The max rating.</value>
[ApiMember(Name = "MaxRating", Description = "Retrieve only reviews less than or equal to this", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int MaxRating { get; set; }
/// <summary>
/// Gets or sets the min rating.
/// </summary>
/// <value>The max rating.</value>
[ApiMember(Name = "MinRating", Description = "Retrieve only reviews greator than or equal to this", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int MinRating { get; set; }
/// <summary>
/// Only retrieve reviews with at least a short review.
/// </summary>
/// <value>True if should only get reviews with a title.</value>
[ApiMember(Name = "ForceTitle", Description = "Whether or not to restrict results to those with a title", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public bool ForceTitle { get; set; }
/// <summary>
/// Gets or sets the limit for the query.
/// </summary>
/// <value>The max rating.</value>
[ApiMember(Name = "Limit", Description = "Limit the result to this many reviews (ordered by latest)", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int Limit { get; set; }
}
2013-11-07 17:27:05 +00:00
2014-07-02 18:34:08 +00:00
[Authenticated]
2013-11-07 17:27:05 +00:00
public class PackageReviewService : BaseApiService
{
private readonly IHttpClient _httpClient;
2013-11-08 20:53:09 +00:00
private readonly IJsonSerializer _serializer;
2016-02-09 17:13:50 +00:00
private const string MbAdminUrl = "https://www.mb3admin.com/admin/";
2015-01-10 19:42:14 +00:00
private readonly IServerApplicationHost _appHost;
2013-11-07 17:27:05 +00:00
2015-01-10 19:42:14 +00:00
public PackageReviewService(IHttpClient httpClient, IJsonSerializer serializer, IServerApplicationHost appHost)
2013-11-07 17:27:05 +00:00
{
2015-01-10 19:42:14 +00:00
_httpClient = httpClient;
2013-11-08 20:53:09 +00:00
_serializer = serializer;
2015-01-10 19:42:14 +00:00
_appHost = appHost;
2013-11-08 20:53:09 +00:00
}
2016-06-19 06:18:29 +00:00
public async Task<object> Get(ReviewRequest request)
2013-11-08 20:53:09 +00:00
{
var parms = "?id=" + request.Id;
2014-03-23 20:07:02 +00:00
2013-11-08 20:53:09 +00:00
if (request.MaxRating > 0)
{
parms += "&max=" + request.MaxRating;
}
if (request.MinRating > 0)
{
parms += "&min=" + request.MinRating;
}
if (request.MinRating > 0)
{
parms += "&limit=" + request.Limit;
}
if (request.ForceTitle)
{
parms += "&title=true";
}
2016-06-19 06:18:29 +00:00
using (var result = await _httpClient.Get(MbAdminUrl + "/service/packageReview/retrieve" + parms, CancellationToken.None)
.ConfigureAwait(false))
{
var reviews = _serializer.DeserializeFromStream<List<PackageReviewInfo>>(result);
2013-11-08 20:53:09 +00:00
2016-06-19 06:18:29 +00:00
return ToOptimizedResult(reviews);
}
2013-11-07 17:27:05 +00:00
}
public void Post(CreateReviewRequest request)
{
2013-11-08 21:39:57 +00:00
var reviewText = WebUtility.HtmlEncode(request.Review ?? string.Empty);
var title = WebUtility.HtmlEncode(request.Title ?? string.Empty);
2013-11-07 17:27:05 +00:00
var review = new Dictionary<string, string>
{ { "id", request.Id.ToString(CultureInfo.InvariantCulture) },
2015-01-10 19:42:14 +00:00
{ "mac", _appHost.SystemId },
2013-11-07 17:27:05 +00:00
{ "rating", request.Rating.ToString(CultureInfo.InvariantCulture) },
{ "recommend", request.Recommend.ToString() },
2013-11-08 21:39:57 +00:00
{ "title", title },
{ "review", reviewText },
2013-11-07 17:27:05 +00:00
};
2014-12-01 12:43:34 +00:00
Task.WaitAll(_httpClient.Post(MbAdminUrl + "/service/packageReview/update", review, CancellationToken.None));
2013-11-07 17:27:05 +00:00
}
}
}