You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Lidarr/src/Lidarr.Http/LidarrRestModule.cs

57 lines
1.7 KiB

using System;
using NzbDrone.Core.Datastore;
using Lidarr.Http.REST;
using Lidarr.Http.Validation;
namespace Lidarr.Http
{
public abstract class LidarrRestModule<TResource> : RestModule<TResource> where TResource : RestResource, new()
{
protected string Resource { get; private set; }
private static string BaseUrl()
{
var isV1 = typeof(TResource).Namespace.Contains(".V1.");
if (isV1)
{
return "/api/v1/";
}
return "/api/";
}
private static string ResourceName()
{
return new TResource().ResourceName.Trim('/').ToLower();
}
protected LidarrRestModule()
: this(ResourceName())
{
}
protected LidarrRestModule(string resource)
: base(BaseUrl() + resource.Trim('/').ToLower())
{
Resource = resource;
PostValidator.RuleFor(r => r.Id).IsZero();
PutValidator.RuleFor(r => r.Id).ValidId();
}
protected PagingResource<TResource> ApplyToPage<TModel>(Func<PagingSpec<TModel>, PagingSpec<TModel>> function, PagingSpec<TModel> pagingSpec, Converter<TModel, TResource> mapper)
{
pagingSpec = function(pagingSpec);
return new PagingResource<TResource>
{
Page = pagingSpec.Page,
PageSize = pagingSpec.PageSize,
SortDirection = pagingSpec.SortDirection,
SortKey = pagingSpec.SortKey,
TotalRecords = pagingSpec.TotalRecords,
Records = pagingSpec.Records.ConvertAll(mapper)
};
}
}
}