/src/NzbDrone.Api/ProviderModuleBase.cs

https://github.com/NzbDrone/NzbDrone · C# · 226 lines · 171 code · 55 blank · 0 comment · 13 complexity · 892edfbad57bf12b52e0f06dfdaabad8 MD5 · raw file

  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using FluentValidation;
  4. using FluentValidation.Results;
  5. using Nancy;
  6. using NzbDrone.Common.Reflection;
  7. using NzbDrone.Core.ThingiProvider;
  8. using NzbDrone.Core.Validation;
  9. using NzbDrone.Common.Serializer;
  10. using Sonarr.Http;
  11. using Sonarr.Http.ClientSchema;
  12. namespace NzbDrone.Api
  13. {
  14. public abstract class ProviderModuleBase<TProviderResource, TProvider, TProviderDefinition> : SonarrRestModule<TProviderResource>
  15. where TProviderDefinition : ProviderDefinition, new()
  16. where TProvider : IProvider
  17. where TProviderResource : ProviderResource, new()
  18. {
  19. private readonly IProviderFactory<TProvider, TProviderDefinition> _providerFactory;
  20. protected ProviderModuleBase(IProviderFactory<TProvider, TProviderDefinition> providerFactory, string resource)
  21. : base(resource)
  22. {
  23. _providerFactory = providerFactory;
  24. Get("schema", x => GetTemplates());
  25. Post("test", x => Test(ReadResourceFromRequest(true)));
  26. Post("action/{action}", x => RequestAction(x.action, ReadResourceFromRequest(true)));
  27. GetResourceAll = GetAll;
  28. GetResourceById = GetProviderById;
  29. CreateResource = CreateProvider;
  30. UpdateResource = UpdateProvider;
  31. DeleteResource = DeleteProvider;
  32. SharedValidator.RuleFor(c => c.Name).NotEmpty();
  33. SharedValidator.RuleFor(c => c.Name).Must((v,c) => !_providerFactory.All().Any(p => p.Name == c && p.Id != v.Id)).WithMessage("Should be unique");
  34. SharedValidator.RuleFor(c => c.Implementation).NotEmpty();
  35. SharedValidator.RuleFor(c => c.ConfigContract).NotEmpty();
  36. PostValidator.RuleFor(c => c.Fields).NotNull();
  37. }
  38. private TProviderResource GetProviderById(int id)
  39. {
  40. var definition = _providerFactory.Get(id);
  41. _providerFactory.SetProviderCharacteristics(definition);
  42. var resource = new TProviderResource();
  43. MapToResource(resource, definition);
  44. return resource;
  45. }
  46. private List<TProviderResource> GetAll()
  47. {
  48. var providerDefinitions = _providerFactory.All().OrderBy(p => p.ImplementationName);
  49. var result = new List<TProviderResource>(providerDefinitions.Count());
  50. foreach (var definition in providerDefinitions)
  51. {
  52. _providerFactory.SetProviderCharacteristics(definition);
  53. var providerResource = new TProviderResource();
  54. MapToResource(providerResource, definition);
  55. result.Add(providerResource);
  56. }
  57. return result.OrderBy(p => p.Name).ToList();
  58. }
  59. private int CreateProvider(TProviderResource providerResource)
  60. {
  61. var providerDefinition = GetDefinition(providerResource, true, false, false);
  62. if (providerDefinition.Enable)
  63. {
  64. Test(providerDefinition, false);
  65. }
  66. providerDefinition = _providerFactory.Create(providerDefinition);
  67. return providerDefinition.Id;
  68. }
  69. private void UpdateProvider(TProviderResource providerResource)
  70. {
  71. var providerDefinition = GetDefinition(providerResource, true, false, false);
  72. _providerFactory.Update(providerDefinition);
  73. }
  74. private TProviderDefinition GetDefinition(TProviderResource providerResource, bool validate, bool includeWarnings, bool forceValidate)
  75. {
  76. var definition = new TProviderDefinition();
  77. MapToModel(definition, providerResource);
  78. if (validate && (definition.Enable || forceValidate))
  79. {
  80. Validate(definition, includeWarnings);
  81. }
  82. return definition;
  83. }
  84. protected virtual void MapToResource(TProviderResource resource, TProviderDefinition definition)
  85. {
  86. resource.Id = definition.Id;
  87. resource.Name = definition.Name;
  88. resource.ImplementationName = definition.ImplementationName;
  89. resource.Implementation = definition.Implementation;
  90. resource.ConfigContract = definition.ConfigContract;
  91. resource.Message = definition.Message;
  92. resource.Fields = SchemaBuilder.ToSchema(definition.Settings);
  93. resource.InfoLink = $"https://wiki.servarr.com/sonarr/supported#{definition.Implementation.ToLower()}";
  94. }
  95. protected virtual void MapToModel(TProviderDefinition definition, TProviderResource resource)
  96. {
  97. definition.Id = resource.Id;
  98. definition.Name = resource.Name;
  99. definition.ImplementationName = resource.ImplementationName;
  100. definition.Implementation = resource.Implementation;
  101. definition.ConfigContract = resource.ConfigContract;
  102. definition.Message = resource.Message;
  103. var configContract = ReflectionExtensions.CoreAssembly.FindTypeByName(definition.ConfigContract);
  104. definition.Settings = (IProviderConfig)SchemaBuilder.ReadFromSchema(resource.Fields, configContract);
  105. }
  106. private void DeleteProvider(int id)
  107. {
  108. _providerFactory.Delete(id);
  109. }
  110. private object GetTemplates()
  111. {
  112. var defaultDefinitions = _providerFactory.GetDefaultDefinitions().OrderBy(p => p.ImplementationName).ToList();
  113. var result = new List<TProviderResource>(defaultDefinitions.Count());
  114. foreach (var providerDefinition in defaultDefinitions)
  115. {
  116. var providerResource = new TProviderResource();
  117. MapToResource(providerResource, providerDefinition);
  118. var presetDefinitions = _providerFactory.GetPresetDefinitions(providerDefinition);
  119. providerResource.Presets = presetDefinitions.Select(v =>
  120. {
  121. var presetResource = new TProviderResource();
  122. MapToResource(presetResource, v);
  123. return presetResource as ProviderResource;
  124. }).ToList();
  125. result.Add(providerResource);
  126. }
  127. return result;
  128. }
  129. private object Test(TProviderResource providerResource)
  130. {
  131. var providerDefinition = GetDefinition(providerResource, true, true, true);
  132. Test(providerDefinition, true);
  133. return "{}";
  134. }
  135. private object RequestAction(string action, TProviderResource providerResource)
  136. {
  137. var providerDefinition = GetDefinition(providerResource, false, false, false);
  138. var query = ((IDictionary<string, object>)Request.Query.ToDictionary()).ToDictionary(k => k.Key, k => k.Value.ToString());
  139. var data = _providerFactory.RequestAction(providerDefinition, action, query);
  140. Response resp = data.ToJson();
  141. resp.ContentType = "application/json";
  142. return resp;
  143. }
  144. private void Validate(TProviderDefinition definition, bool includeWarnings)
  145. {
  146. var validationResult = definition.Settings.Validate();
  147. VerifyValidationResult(validationResult, includeWarnings);
  148. }
  149. protected virtual void Test(TProviderDefinition definition, bool includeWarnings)
  150. {
  151. var validationResult = _providerFactory.Test(definition);
  152. VerifyValidationResult(validationResult, includeWarnings);
  153. }
  154. protected void VerifyValidationResult(ValidationResult validationResult, bool includeWarnings)
  155. {
  156. var result = validationResult as NzbDroneValidationResult;
  157. if (result == null)
  158. {
  159. result = new NzbDroneValidationResult(validationResult.Errors);
  160. }
  161. if (includeWarnings && (!result.IsValid || result.HasWarnings))
  162. {
  163. throw new ValidationException(result.Failures);
  164. }
  165. if (!result.IsValid)
  166. {
  167. throw new ValidationException(result.Errors);
  168. }
  169. }
  170. }
  171. }