2013-04-19 04:46:18 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using FluentAssertions;
|
|
|
|
|
using NLog;
|
2013-09-10 05:39:18 +00:00
|
|
|
|
using NzbDrone.Api;
|
2013-04-22 03:18:08 +00:00
|
|
|
|
using NzbDrone.Api.REST;
|
2013-05-12 15:18:17 +00:00
|
|
|
|
using NzbDrone.Common.Serializer;
|
2013-04-19 04:46:18 +00:00
|
|
|
|
using RestSharp;
|
2013-07-25 21:57:11 +00:00
|
|
|
|
using System.Linq;
|
2013-04-19 04:46:18 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Integration.Test.Client
|
|
|
|
|
{
|
2013-04-22 03:18:08 +00:00
|
|
|
|
public class ClientBase<TResource> where TResource : RestResource, new()
|
2013-04-19 04:46:18 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly IRestClient _restClient;
|
|
|
|
|
private readonly string _resource;
|
2013-09-21 02:07:42 +00:00
|
|
|
|
private readonly string _apiKey;
|
2013-04-19 04:46:18 +00:00
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
2013-09-21 02:07:42 +00:00
|
|
|
|
public ClientBase(IRestClient restClient, string apiKey, string resource = null)
|
2013-04-19 04:46:18 +00:00
|
|
|
|
{
|
2013-04-22 03:18:08 +00:00
|
|
|
|
if (resource == null)
|
|
|
|
|
{
|
|
|
|
|
resource = new TResource().ResourceName;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-19 04:46:18 +00:00
|
|
|
|
_restClient = restClient;
|
|
|
|
|
_resource = resource;
|
2013-09-21 02:07:42 +00:00
|
|
|
|
_apiKey = apiKey;
|
2013-04-28 19:46:13 +00:00
|
|
|
|
|
2013-04-19 04:46:18 +00:00
|
|
|
|
_logger = LogManager.GetLogger("REST");
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-22 03:18:08 +00:00
|
|
|
|
public List<TResource> All()
|
2013-04-19 04:46:18 +00:00
|
|
|
|
{
|
|
|
|
|
var request = BuildRequest();
|
|
|
|
|
return Get<List<TResource>>(request);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-10 05:39:18 +00:00
|
|
|
|
public PagingResource<TResource> 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<PagingResource<TResource>>(request);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-19 04:46:18 +00:00
|
|
|
|
public TResource Post(TResource body)
|
|
|
|
|
{
|
|
|
|
|
var request = BuildRequest();
|
|
|
|
|
request.AddBody(body);
|
|
|
|
|
return Post<TResource>(request);
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-27 03:20:03 +00:00
|
|
|
|
public TResource Put(TResource body)
|
|
|
|
|
{
|
|
|
|
|
var request = BuildRequest();
|
|
|
|
|
request.AddBody(body);
|
|
|
|
|
return Put<TResource>(request);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-04 02:42:11 +00:00
|
|
|
|
public TResource Get(int id, HttpStatusCode statusCode = HttpStatusCode.OK)
|
|
|
|
|
{
|
|
|
|
|
var request = BuildRequest(id.ToString());
|
|
|
|
|
return Get<TResource>(request, statusCode);
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-28 00:16:24 +00:00
|
|
|
|
public TResource GetSingle(HttpStatusCode statusCode = HttpStatusCode.OK)
|
|
|
|
|
{
|
|
|
|
|
var request = BuildRequest();
|
|
|
|
|
return Get<TResource>(request, statusCode);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-22 03:18:08 +00:00
|
|
|
|
public void Delete(int id)
|
|
|
|
|
{
|
|
|
|
|
var request = BuildRequest(id.ToString());
|
|
|
|
|
Delete(request);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-29 05:20:36 +00:00
|
|
|
|
public List<dynamic> InvalidPost(TResource body)
|
2013-04-20 22:14:41 +00:00
|
|
|
|
{
|
|
|
|
|
var request = BuildRequest();
|
|
|
|
|
request.AddBody(body);
|
2013-04-29 05:20:36 +00:00
|
|
|
|
return Post<List<dynamic>>(request, HttpStatusCode.BadRequest);
|
2013-04-20 22:14:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-10 07:01:27 +00:00
|
|
|
|
public RestRequest BuildRequest(string command = "")
|
2013-04-19 04:46:18 +00:00
|
|
|
|
{
|
2013-09-21 02:07:42 +00:00
|
|
|
|
var request = new RestRequest(_resource + "/" + command.Trim('/'))
|
2013-04-19 04:46:18 +00:00
|
|
|
|
{
|
2013-09-21 02:07:42 +00:00
|
|
|
|
RequestFormat = DataFormat.Json,
|
2013-04-19 04:46:18 +00:00
|
|
|
|
};
|
2013-09-21 02:07:42 +00:00
|
|
|
|
|
|
|
|
|
request.AddHeader("Authorization", _apiKey);
|
|
|
|
|
|
|
|
|
|
return request;
|
2013-04-19 04:46:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-10 07:01:27 +00:00
|
|
|
|
public T Get<T>(IRestRequest request, HttpStatusCode statusCode = HttpStatusCode.OK) where T : class, new()
|
2013-04-19 04:46:18 +00:00
|
|
|
|
{
|
|
|
|
|
request.Method = Method.GET;
|
|
|
|
|
return Execute<T>(request, statusCode);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-28 19:46:13 +00:00
|
|
|
|
public T Post<T>(IRestRequest request, HttpStatusCode statusCode = HttpStatusCode.Created) where T : class, new()
|
2013-04-19 04:46:18 +00:00
|
|
|
|
{
|
|
|
|
|
request.Method = Method.POST;
|
|
|
|
|
return Execute<T>(request, statusCode);
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-27 03:20:03 +00:00
|
|
|
|
public T Put<T>(IRestRequest request, HttpStatusCode statusCode = HttpStatusCode.Accepted) where T : class, new()
|
|
|
|
|
{
|
|
|
|
|
request.Method = Method.PUT;
|
|
|
|
|
return Execute<T>(request, statusCode);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-22 03:18:08 +00:00
|
|
|
|
public void Delete(IRestRequest request, HttpStatusCode statusCode = HttpStatusCode.OK)
|
|
|
|
|
{
|
|
|
|
|
request.Method = Method.DELETE;
|
|
|
|
|
Execute<object>(request, statusCode);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-28 19:46:13 +00:00
|
|
|
|
private T Execute<T>(IRestRequest request, HttpStatusCode statusCode) where T : class, new()
|
2013-04-19 04:46:18 +00:00
|
|
|
|
{
|
|
|
|
|
_logger.Info("{0}: {1}", request.Method, _restClient.BuildUri(request));
|
|
|
|
|
|
2013-04-28 19:46:13 +00:00
|
|
|
|
var response = _restClient.Execute(request);
|
2013-04-19 04:46:18 +00:00
|
|
|
|
_logger.Info("Response: {0}", response.Content);
|
|
|
|
|
|
|
|
|
|
if (response.ErrorException != null)
|
|
|
|
|
{
|
|
|
|
|
throw response.ErrorException;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-25 21:57:11 +00:00
|
|
|
|
AssertDisableCache(response.Headers);
|
|
|
|
|
|
2013-04-19 04:46:18 +00:00
|
|
|
|
response.ErrorMessage.Should().BeBlank();
|
|
|
|
|
|
2013-05-22 05:32:25 +00:00
|
|
|
|
response.StatusCode.Should().Be(statusCode);
|
|
|
|
|
|
2013-05-13 02:52:55 +00:00
|
|
|
|
return Json.Deserialize<T>(response.Content);
|
2013-04-19 04:46:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-25 21:57:11 +00:00
|
|
|
|
private static void AssertDisableCache(IList<Parameter> 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");
|
|
|
|
|
}
|
2013-04-19 04:46:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|