using System.Collections.Generic; using System.Net; using FluentAssertions; using NLog; using NzbDrone.Api; using NzbDrone.Api.REST; using NzbDrone.Common.Serializer; using RestSharp; using System.Linq; namespace NzbDrone.Integration.Test.Client { public class ClientBase where TResource : RestResource, new() { private readonly IRestClient _restClient; private readonly string _resource; private readonly Logger _logger; public ClientBase(IRestClient restClient, string resource = null) { if (resource == null) { resource = new TResource().ResourceName; } _restClient = restClient; _resource = resource; _logger = LogManager.GetLogger("REST"); } public List All() { var request = BuildRequest(); return Get>(request); } public PagingResource GetPaged(int pageNumber, int pageSize, string sortKey, string sortDir) { var request = BuildRequest(); request.AddParameter("page", pageNumber); request.AddParameter("pageSize", pageSize); request.AddParameter("sortKey", sortKey); request.AddParameter("sortDir", sortDir); return Get>(request); } public TResource Post(TResource body) { var request = BuildRequest(); request.AddBody(body); return Post(request); } public TResource Put(TResource body) { var request = BuildRequest(); request.AddBody(body); return Put(request); } public TResource Get(int id, HttpStatusCode statusCode = HttpStatusCode.OK) { var request = BuildRequest(id.ToString()); return Get(request, statusCode); } public TResource GetSingle(HttpStatusCode statusCode = HttpStatusCode.OK) { var request = BuildRequest(); return Get(request, statusCode); } public void Delete(int id) { var request = BuildRequest(id.ToString()); Delete(request); } public List InvalidPost(TResource body) { var request = BuildRequest(); request.AddBody(body); return Post>(request, HttpStatusCode.BadRequest); } protected RestRequest BuildRequest(string command = "") { return new RestRequest(_resource + "/" + command.Trim('/')) { RequestFormat = DataFormat.Json }; } protected T Get(IRestRequest request, HttpStatusCode statusCode = HttpStatusCode.OK) where T : class, new() { request.Method = Method.GET; return Execute(request, statusCode); } public T Post(IRestRequest request, HttpStatusCode statusCode = HttpStatusCode.Created) where T : class, new() { request.Method = Method.POST; return Execute(request, statusCode); } public T Put(IRestRequest request, HttpStatusCode statusCode = HttpStatusCode.Accepted) where T : class, new() { request.Method = Method.PUT; return Execute(request, statusCode); } public void Delete(IRestRequest request, HttpStatusCode statusCode = HttpStatusCode.OK) { request.Method = Method.DELETE; Execute(request, statusCode); } private T Execute(IRestRequest request, HttpStatusCode statusCode) where T : class, new() { _logger.Info("{0}: {1}", request.Method, _restClient.BuildUri(request)); var response = _restClient.Execute(request); _logger.Info("Response: {0}", response.Content); if (response.ErrorException != null) { throw response.ErrorException; } AssertDisableCache(response.Headers); response.ErrorMessage.Should().BeBlank(); response.StatusCode.Should().Be(statusCode); return Json.Deserialize(response.Content); } private static void AssertDisableCache(IList headers) { headers.Single(c => c.Name == "Cache-Control").Value.Should().Be("no-cache, no-store, must-revalidate"); headers.Single(c => c.Name == "Pragma").Value.Should().Be("no-cache"); headers.Single(c => c.Name == "Expires").Value.Should().Be("0"); } } }