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.

54 lines
1.7 KiB

3 years ago
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace RadarrSharp.Proxies
{
public class RadarrProxy : IRadarrProxy
{
private HttpClient _client;
public RadarrProxy()
{
_client = new HttpClient();
}
public async Task<T> GetAsync<T>(string rootUrl)
{
var resp = await _client.GetAsync(rootUrl);
var content = await resp.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(content);
}
public async Task<T> PostAsync<T, U>(string rootUrl, U data)
{
var jsonPayload = JsonConvert.SerializeObject(data);
var response = await _client.PostAsync(rootUrl, PrepJsonForPost(jsonPayload));
var resp = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(resp);
}
public async Task<T> PutAsync<T, U>(string rootUrl, U data)
{
var jsonPayload = JsonConvert.SerializeObject(data);
var response = await _client.PutAsync(rootUrl, PrepJsonForPost(jsonPayload));
return JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync());
}
public async Task<HttpResponseMessage> DeleteAsync(string rootUrl)
{
var resp = await _client.DeleteAsync(rootUrl);
return resp;
}
private StringContent PrepJsonForPost(string jsonObj)
{
return new StringContent(jsonObj, Encoding.UTF8, "application/json");
}
}
}