1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00
Sonarr/NzbDrone.Integration.Test/Client/ClientBase.cs

111 lines
3.3 KiB
C#
Raw Normal View History

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