You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.0 KiB

3 years ago
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();
}
}
}