235 lines
9.7 KiB
C#
235 lines
9.7 KiB
C#
using System.Collections.Generic;
|
|
using System.Net.Cache;
|
|
using Kreta.Client.ClientBase.Interface;
|
|
using Kreta.Client.Jira.Model.Request;
|
|
using RestSharp;
|
|
using RestSharp.Authenticators;
|
|
|
|
namespace Kreta.Client.ClientBase
|
|
{
|
|
internal abstract class RestSharpClientBase : IClientBase
|
|
{
|
|
private IAuthenticator Authenticator { get; set; }
|
|
internal string BaseUrl { get; set; }
|
|
|
|
private IRestRequest CreateRestRequest(
|
|
Method method,
|
|
string relativeUri,
|
|
Dictionary<string, string> headers = null,
|
|
Dictionary<string, string> parameters = null,
|
|
Dictionary<string, string> urlSegments = null,
|
|
Dictionary<string, string> queryStrings = null,
|
|
object body = null,
|
|
List<File> fileList = null,
|
|
int? requestTimeout = null)
|
|
{
|
|
var restRequest = new RestRequest(relativeUri, method);
|
|
|
|
if (method == Method.POST || method == Method.PUT || method == Method.PATCH) /*az alap serializáló post esetén nem tudja a property neveket másnak megfeleltetni*/
|
|
{
|
|
restRequest.RequestFormat = DataFormat.Json;
|
|
restRequest.JsonSerializer = new RestSharp.Newtonsoft.Json.NewtonsoftJsonSerializer();
|
|
/*frissebb RestSharp esetében lehetne ezt a nuget-et használni: RestSharp.Serializers.NewtonsoftJson. Ezzel viszont már a RestSharp-os kliensek lehet beállítani a NewtonsoftJson serializert.*/
|
|
}
|
|
|
|
if (headers != null)
|
|
{
|
|
foreach (var header in headers)
|
|
{
|
|
restRequest.AddHeader(header.Key, header.Value);
|
|
}
|
|
}
|
|
|
|
if (parameters != null)
|
|
{
|
|
foreach (var parameter in parameters)
|
|
{
|
|
restRequest.AddParameter(parameter.Key, parameter.Value);
|
|
}
|
|
}
|
|
|
|
if (urlSegments != null)
|
|
{
|
|
foreach (var urlSegment in urlSegments)
|
|
{
|
|
restRequest.AddUrlSegment(urlSegment.Key, urlSegment.Value);
|
|
}
|
|
}
|
|
|
|
if (queryStrings != null)
|
|
{
|
|
foreach (var queryString in queryStrings)
|
|
{
|
|
restRequest.AddQueryParameter(queryString.Key, queryString.Value);
|
|
}
|
|
}
|
|
|
|
if (body != null)
|
|
{
|
|
restRequest.AddJsonBody(body);
|
|
}
|
|
|
|
if (fileList != null && fileList.Count > 0)
|
|
{
|
|
fileList.ForEach(file =>
|
|
{
|
|
restRequest.AddFileBytes(file.Name, file.Content, file.FileName, file.ContentType);
|
|
});
|
|
}
|
|
|
|
if (requestTimeout.HasValue)
|
|
{
|
|
restRequest.Timeout = requestTimeout.Value;
|
|
}
|
|
|
|
return restRequest;
|
|
}
|
|
|
|
private IRestClient CreateRestClient(string baseUrl, RequestCacheLevel cachePolicyLevel)
|
|
{
|
|
var restClient = new RestClient(string.Format(baseUrl))
|
|
{
|
|
Authenticator = Authenticator,
|
|
CachePolicy = new RequestCachePolicy(cachePolicyLevel)
|
|
};
|
|
|
|
return restClient;
|
|
}
|
|
|
|
public void CreateBasicAuthenticator(string username, string password)
|
|
{
|
|
if (Authenticator == null && !string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(password))
|
|
{
|
|
Authenticator = new HttpBasicAuthenticator(username, password);
|
|
}
|
|
}
|
|
|
|
public IRestResult Post(
|
|
string relativeUri,
|
|
Dictionary<string, string> headers = null,
|
|
Dictionary<string, string> parameters = null,
|
|
Dictionary<string, string> urlSegments = null,
|
|
Dictionary<string, string> queryStrings = null,
|
|
object body = null,
|
|
List<File> fileList = null,
|
|
RequestCacheLevel cachePolicyLevel = RequestCacheLevel.NoCacheNoStore)
|
|
{
|
|
IRestClient client = CreateRestClient(BaseUrl, cachePolicyLevel);
|
|
IRestRequest request = CreateRestRequest(Method.POST, relativeUri, headers, parameters, urlSegments, queryStrings, body, fileList);
|
|
|
|
return RestResult.Convert(client.Execute(request));
|
|
}
|
|
|
|
public IRestResult<TResponse> Post<TResponse>(
|
|
string relativeUri,
|
|
Dictionary<string, string> headers = null,
|
|
Dictionary<string, string> parameters = null,
|
|
Dictionary<string, string> urlSegments = null,
|
|
Dictionary<string, string> queryStrings = null,
|
|
object body = null,
|
|
List<File> fileList = null,
|
|
RequestCacheLevel cachePolicyLevel = RequestCacheLevel.NoCacheNoStore)
|
|
where TResponse : new()
|
|
{
|
|
IRestClient client = CreateRestClient(BaseUrl, cachePolicyLevel);
|
|
IRestRequest request = CreateRestRequest(Method.POST, relativeUri, headers, parameters, urlSegments, queryStrings, body, fileList);
|
|
|
|
return RestResult<TResponse>.Convert(client.Execute<TResponse>(request));
|
|
}
|
|
|
|
public IRestResult Put(
|
|
string relativeUri,
|
|
Dictionary<string, string> headers = null,
|
|
Dictionary<string, string> parameters = null,
|
|
Dictionary<string, string> urlSegments = null,
|
|
Dictionary<string, string> queryStrings = null,
|
|
object body = null,
|
|
List<File> fileList = null,
|
|
RequestCacheLevel cachePolicyLevel = RequestCacheLevel.NoCacheNoStore)
|
|
{
|
|
IRestClient client = CreateRestClient(BaseUrl, cachePolicyLevel);
|
|
IRestRequest request = CreateRestRequest(Method.PUT, relativeUri, headers, parameters, urlSegments, queryStrings, body, fileList);
|
|
|
|
return RestResult.Convert(client.Execute(request));
|
|
}
|
|
|
|
public IRestResult<TResponse> Put<TResponse>(
|
|
string relativeUri,
|
|
Dictionary<string, string> headers = null,
|
|
Dictionary<string, string> parameters = null,
|
|
Dictionary<string, string> urlSegments = null,
|
|
Dictionary<string, string> queryStrings = null,
|
|
object body = null,
|
|
List<File> fileList = null,
|
|
RequestCacheLevel cachePolicyLevel = RequestCacheLevel.NoCacheNoStore)
|
|
where TResponse : new()
|
|
{
|
|
IRestClient client = CreateRestClient(BaseUrl, cachePolicyLevel);
|
|
IRestRequest request = CreateRestRequest(Method.PUT, relativeUri, headers, parameters, urlSegments, queryStrings, body, fileList);
|
|
|
|
return RestResult<TResponse>.Convert(client.Execute<TResponse>(request));
|
|
}
|
|
|
|
public IRestResult<TResponse> Get<TResponse>(
|
|
string relativeUri,
|
|
Dictionary<string, string> headers = null,
|
|
Dictionary<string, string> parameters = null,
|
|
Dictionary<string, string> urlSegments = null,
|
|
Dictionary<string, string> queryStrings = null,
|
|
RequestCacheLevel cachePolicyLevel = RequestCacheLevel.NoCacheNoStore,
|
|
int? requestTimeout = null)
|
|
where TResponse : new()
|
|
{
|
|
IRestClient client = CreateRestClient(BaseUrl, cachePolicyLevel);
|
|
IRestRequest request = CreateRestRequest(Method.GET, relativeUri, headers, parameters, urlSegments, queryStrings, requestTimeout: requestTimeout);
|
|
|
|
return RestResult<TResponse>.Convert(client.Execute<TResponse>(request));
|
|
}
|
|
|
|
public IRestResult Get(
|
|
string relativeUri,
|
|
Dictionary<string, string> headers = null,
|
|
Dictionary<string, string> parameters = null,
|
|
Dictionary<string, string> urlSegments = null,
|
|
Dictionary<string, string> queryStrings = null,
|
|
RequestCacheLevel cachePolicyLevel = RequestCacheLevel.NoCacheNoStore,
|
|
int? requestTimeout = null)
|
|
{
|
|
IRestClient client = CreateRestClient(BaseUrl, cachePolicyLevel);
|
|
IRestRequest request = CreateRestRequest(Method.GET, relativeUri, headers, parameters, urlSegments, queryStrings, requestTimeout: requestTimeout);
|
|
|
|
return RestResult.Convert(client.Execute(request));
|
|
}
|
|
|
|
public IRestResult Patch(
|
|
string relativeUri,
|
|
Dictionary<string, string> headers = null,
|
|
Dictionary<string, string> parameters = null,
|
|
Dictionary<string, string> urlSegments = null,
|
|
Dictionary<string, string> queryStrings = null,
|
|
object body = null,
|
|
RequestCacheLevel cachePolicyLevel = RequestCacheLevel.NoCacheNoStore)
|
|
{
|
|
IRestClient client = CreateRestClient(BaseUrl, cachePolicyLevel);
|
|
IRestRequest request = CreateRestRequest(Method.PATCH, relativeUri, headers, parameters, urlSegments, queryStrings, body);
|
|
|
|
return RestResult.Convert(client.Execute(request));
|
|
}
|
|
|
|
public IRestResult Delete(
|
|
string relativeUri,
|
|
Dictionary<string, string> headers = null,
|
|
Dictionary<string, string> parameters = null,
|
|
Dictionary<string, string> urlSegments = null,
|
|
Dictionary<string, string> queryStrings = null,
|
|
object body = null,
|
|
List<File> fileList = null,
|
|
RequestCacheLevel cachePolicyLevel = RequestCacheLevel.NoCacheNoStore)
|
|
{
|
|
IRestClient client = CreateRestClient(BaseUrl, cachePolicyLevel);
|
|
IRestRequest request = CreateRestRequest(Method.DELETE, relativeUri, headers, parameters, urlSegments, queryStrings, body, fileList);
|
|
|
|
return RestResult.Convert(client.Execute(request));
|
|
}
|
|
}
|
|
}
|