|
|
|
|
using RadarrSharp.Proxies;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace RadarrSharp.Services
|
|
|
|
|
{
|
|
|
|
|
public abstract class BaseRadarrService
|
|
|
|
|
{
|
|
|
|
|
private IRadarrProxy _proxy;
|
|
|
|
|
private string _ip, _port, _apiKey;
|
|
|
|
|
protected BaseRadarrService(string ip, string port, string apiKey)
|
|
|
|
|
{
|
|
|
|
|
_proxy = new RadarrProxy();
|
|
|
|
|
this._ip = ip;
|
|
|
|
|
this._port = port;
|
|
|
|
|
this._apiKey = apiKey;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<T> GetAsync<T>(string endpoint, IEnumerable<Tuple<string, string>> qParams = null)
|
|
|
|
|
{
|
|
|
|
|
var resp = await _proxy.GetAsync<T>(GenerateApiUrl(endpoint, qParams));
|
|
|
|
|
return resp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<T> PostAsync<T,U>(string endpoint, U data, IEnumerable<Tuple<string, string>> qParams = null)
|
|
|
|
|
{
|
|
|
|
|
var resp = await _proxy.PostAsync<T, U>(GenerateApiUrl(endpoint, qParams), data);
|
|
|
|
|
return resp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<T> PutAsync<T, U>(string endpoint, U data, IEnumerable<Tuple<string, string>> qParams = null)
|
|
|
|
|
{
|
|
|
|
|
var resp = await _proxy.PutAsync<T, U>(GenerateApiUrl(endpoint, qParams), data);
|
|
|
|
|
return resp;
|
|
|
|
|
}
|
|
|
|
|
public async Task<HttpResponseMessage> DeleteAsync(string endpoint, IEnumerable<Tuple<string, string>> qParams = null)
|
|
|
|
|
{
|
|
|
|
|
var resp = await _proxy.DeleteAsync(GenerateApiUrl(endpoint, qParams));
|
|
|
|
|
return resp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected string GenerateApiUrl(string endpoint, IEnumerable<Tuple<string,string>> qParams = null)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder url = new StringBuilder($"{_ip}:{_port}/api/v3/{endpoint}?apikey={_apiKey}");
|
|
|
|
|
if (qParams != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var param in qParams)
|
|
|
|
|
{
|
|
|
|
|
url.Append($"&{param.Item1}={param.Item2}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return url.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|