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 GetAsync(string endpoint, IEnumerable> qParams = null) { var resp = await _proxy.GetAsync(GenerateApiUrl(endpoint, qParams)); return resp; } public async Task PostAsync(string endpoint, U data, IEnumerable> qParams = null) { var resp = await _proxy.PostAsync(GenerateApiUrl(endpoint, qParams), data); return resp; } public async Task PutAsync(string endpoint, U data, IEnumerable> qParams = null) { var resp = await _proxy.PutAsync(GenerateApiUrl(endpoint, qParams), data); return resp; } public async Task DeleteAsync(string endpoint, IEnumerable> qParams = null) { var resp = await _proxy.DeleteAsync(GenerateApiUrl(endpoint, qParams)); return resp; } protected string GenerateApiUrl(string endpoint, IEnumerable> 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(); } } }