269 lines
8.3 KiB
C#
269 lines
8.3 KiB
C#
using BS.Shared.Extensions;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Specialized;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Security.Policy;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BeWo.ServerUtils.ApiFacade
|
|
{
|
|
public class WebClientFacade
|
|
{
|
|
//
|
|
// GET
|
|
//
|
|
protected TRes SendGet<TRes>(string sub_url, string token = null)
|
|
=> Get<TRes>(sub_url, token);
|
|
protected Task<TRes> SendGetAsync<TRes>(string sub_url, string token = null)
|
|
=> GetAsync<TRes>(sub_url, token);
|
|
|
|
//
|
|
// POST
|
|
//
|
|
protected TRes SendPostUploadString<TReq, TRes>(string sub_url, TReq req, string token = null)
|
|
=> SendUploadString<TReq, TRes>(sub_url, "POST", req, token);
|
|
protected TRes SendPostUploadValues<TReq, TRes>(string sub_url, TReq req, string token = null)
|
|
=> SendUploadValues<TReq, TRes>(sub_url, "POST", req, token);
|
|
protected byte[] SendPostUploadValuesRaw<TReq>(string sub_url, TReq req, string token = null)
|
|
=> SendUploadValuesRaw(sub_url, "POST", req, token);
|
|
protected Task<TRes> SendPostUploadStringAsync<TReq, TRes>(string sub_url, TReq req, string token = null)
|
|
=> SendUploadStringAsync<TReq, TRes>(sub_url, "POST", req, token);
|
|
protected Task<TRes> SendPostUploadValuesAsync<TReq, TRes>(string sub_url, TReq req, string token = null)
|
|
=> SendUploadValuesAsync<TReq, TRes>(sub_url, "POST", req, token);
|
|
protected Task<byte[]> SendPostUploadValuesAsyncRaw<TReq, TRes>(string sub_url, TReq req, string token = null)
|
|
=> SendUploadValuesRawAsync<TReq, TRes>(sub_url, "POST", req, token);
|
|
|
|
#region private
|
|
//
|
|
// Requests
|
|
//
|
|
private TRes Get<TRes>(string sub_url, string token = null)
|
|
{
|
|
var url = getUrl(sub_url);
|
|
|
|
string response = Get(url, token);
|
|
|
|
return deserialize<TRes>(response);
|
|
}
|
|
private TRes SendUploadString<TReq, TRes>(string sub_url, string method, TReq req, string token = null)
|
|
{
|
|
var data = serializeJson(req);
|
|
var url = getUrl(sub_url);
|
|
|
|
string response = SendUploadString<TRes>(url, method, data, token);
|
|
|
|
return deserialize<TRes>(response);
|
|
}
|
|
private TRes SendUploadValues<TReq, TRes>(string sub_url, string method, TReq req, string token = null)
|
|
{
|
|
var data = serialize(req);
|
|
var url = getUrl(sub_url);
|
|
|
|
string response = SendUploadValues(url, method, data, token);
|
|
|
|
return deserialize<TRes>(response);
|
|
}
|
|
private byte[] SendUploadValuesRaw<TReq>(string sub_url, string method, TReq req, string token = null)
|
|
{
|
|
var data = serialize(req);
|
|
var url = getUrl(sub_url);
|
|
|
|
var response = SendUploadValuesRaww(url, method, data, token);
|
|
|
|
return response;
|
|
}
|
|
private async Task<TRes> GetAsync<TRes>(string sub_url, string token = null)
|
|
{
|
|
var url = getUrl(sub_url);
|
|
|
|
string response = await GetAsync(url, token);
|
|
|
|
return deserialize<TRes>(response);
|
|
}
|
|
private async Task<TRes> SendUploadStringAsync<TReq, TRes>(string sub_url, string method, TReq req, string token = null)
|
|
{
|
|
var data = serializeJson(req);
|
|
var url = getUrl(sub_url);
|
|
|
|
string response = await SendUploadStringAsync<TRes>(url, method, data, token);
|
|
|
|
return deserialize<TRes>(response);
|
|
}
|
|
private async Task<TRes> SendUploadValuesAsync<TReq, TRes>(string sub_url, string method, TReq req, string token = null)
|
|
{
|
|
var data = serialize(req);
|
|
var url = getUrl(sub_url);
|
|
|
|
string response = await SendUploadValuesAsync(url, method, data, token);
|
|
|
|
return deserialize<TRes>(response);
|
|
}
|
|
private async Task<byte[]> SendUploadValuesRawAsync<TReq, TRes>(string sub_url, string method, TReq req, string token = null)
|
|
{
|
|
var data = serialize(req);
|
|
var url = getUrl(sub_url);
|
|
|
|
var response = await SendUploadValuesRawAsync(url, method, data, token);
|
|
|
|
return response;
|
|
}
|
|
|
|
//
|
|
// String
|
|
//
|
|
private string Get(string url, string token = null)
|
|
=> exec(true, client => client.DownloadString(url), token);
|
|
private string SendUploadString<T>(string url, string method, string request, string token = null)
|
|
=> exec(false, client => client.UploadString(url, method, request), token);
|
|
private string SendUploadValues<T>(string url, string method, T request, string token = null)
|
|
=> exec(false, client => Encoding.UTF8.GetString(client.UploadValues(url, method, request?.ToNameValueCollection())), token);
|
|
private byte[] SendUploadValuesRaww<T>(string url, string method, T request, string token = null)
|
|
=> execraw(false, client => client.UploadValues(url, method, request?.ToNameValueCollection()), token);
|
|
private Task<string> GetAsync(string url, string token = null)
|
|
=> execasync(true, client => client.DownloadStringTaskAsync(url), token);
|
|
private Task<string> SendUploadStringAsync<T>(string url, string method, string request, string token = null)
|
|
=> execasync(false, client => client.UploadStringTaskAsync(url, method, request), token);
|
|
private Task<string> SendUploadValuesAsync<T>(string url, string method, T request, string token = null)
|
|
=> execasync(false, async client => Encoding.UTF8.GetString(await client.UploadValuesTaskAsync(url, method, request?.ToNameValueCollection()).ConfigureAwait(false)), token);
|
|
private Task<byte[]> SendUploadValuesRawAsync<T>(string url, string method, T request, string token = null)
|
|
=> execasyncraw(false, async client => await client.UploadValuesTaskAsync(url, method, request?.ToNameValueCollection()).ConfigureAwait(false), token);
|
|
|
|
//
|
|
// Base
|
|
//
|
|
private string exec(bool override_header, Func<WebClient, string> action, string token = null)
|
|
{
|
|
string response;
|
|
|
|
using (WebClient client = new WebClient())
|
|
{
|
|
//client.Encoding = Encoding.Default;
|
|
client.Encoding = Encoding.UTF8;
|
|
|
|
if (override_header)
|
|
client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
|
|
|
|
if (token != null)
|
|
client.Headers.Set(HttpRequestHeader.Authorization, "Bearer " + token);
|
|
|
|
response = action(client);
|
|
}
|
|
|
|
return response;
|
|
}
|
|
private byte[] execraw(bool override_header, Func<WebClient, byte[]> action, string token = null)
|
|
{
|
|
byte[] response;
|
|
|
|
using (WebClient client = new WebClient())
|
|
{
|
|
//client.Encoding = Encoding.Default;
|
|
client.Encoding = Encoding.UTF8;
|
|
|
|
if (override_header)
|
|
client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
|
|
|
|
if (token != null)
|
|
client.Headers.Set(HttpRequestHeader.Authorization, "Bearer " + token);
|
|
|
|
response = action(client);
|
|
}
|
|
|
|
return response;
|
|
}
|
|
private async Task<string> execasync(bool override_header, Func<WebClient, Task<string>> action, string token = null)
|
|
{
|
|
string response;
|
|
|
|
using (WebClient client = new WebClient())
|
|
{
|
|
//client.Encoding = Encoding.Default;
|
|
client.Encoding = Encoding.UTF8;
|
|
|
|
if (override_header)
|
|
client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
|
|
|
|
if (token != null)
|
|
client.Headers.Set(HttpRequestHeader.Authorization, "Bearer " + token);
|
|
|
|
response = await action(client);
|
|
}
|
|
|
|
return response;
|
|
}
|
|
private async Task<byte[]> execasyncraw(bool override_header, Func<WebClient, Task<byte[]>> action, string token = null)
|
|
{
|
|
byte[] response;
|
|
|
|
using (WebClient client = new WebClient())
|
|
{
|
|
//client.Encoding = Encoding.Default;
|
|
client.Encoding = Encoding.UTF8;
|
|
|
|
if (override_header)
|
|
client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
|
|
|
|
if (token != null)
|
|
client.Headers.Set(HttpRequestHeader.Authorization, "Bearer " + token);
|
|
|
|
response = await action(client);
|
|
}
|
|
|
|
return response;
|
|
}
|
|
|
|
private T deserialize<T>(string input)
|
|
{
|
|
if (string.IsNullOrEmpty(input))
|
|
return default;
|
|
|
|
if (typeof(T) == typeof(string))
|
|
return (T)(object)input;
|
|
|
|
if (typeof(T) == typeof(byte[]))
|
|
return (T)(object)Encoding.UTF8.GetBytes(input);
|
|
|
|
var t = JsonConvert.DeserializeObject<T>(input);
|
|
|
|
return t;
|
|
}
|
|
private IDictionary<string, string> serialize<T>(T input)
|
|
{
|
|
return input?.ToKeyValue();
|
|
}
|
|
private string serializeJson<T>(T input)
|
|
{
|
|
if (typeof(T) == typeof(string))
|
|
return input as string;
|
|
|
|
var t = JsonConvert.SerializeObject(input);
|
|
|
|
return t;
|
|
}
|
|
#endregion
|
|
|
|
protected virtual string getKey()
|
|
{
|
|
return null;
|
|
}
|
|
|
|
protected virtual string getUrl()
|
|
{
|
|
return null;
|
|
}
|
|
|
|
private string getUrl(string sub_url)
|
|
{
|
|
var root = getUrl();
|
|
var baseUri = new Uri(root);
|
|
var uri = new Uri(baseUri, sub_url);
|
|
var url = uri.ToString();
|
|
return url;
|
|
}
|
|
}
|
|
}
|