init
This commit is contained in:
commit
e124a47765
19374 changed files with 9806149 additions and 0 deletions
27
Kreta.Client/ClientBase/Extension/HeaderExtension.cs
Normal file
27
Kreta.Client/ClientBase/Extension/HeaderExtension.cs
Normal file
|
@ -0,0 +1,27 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace Kreta.Client.ClientBase.Extension
|
||||
{
|
||||
internal static class HeaderExtension
|
||||
{
|
||||
public static Dictionary<string, string> GetAuthorizationHeaderWithMulitpartFrom(this string token)
|
||||
=> new Dictionary<string, string>
|
||||
{
|
||||
{ "Content-Type", "multipart/form-data" },
|
||||
{ "Authorization", $"Bearer {token}" },
|
||||
};
|
||||
|
||||
public static Dictionary<string, string> GetAuthorizationHeaderWithJson(this string token)
|
||||
=> new Dictionary<string, string>
|
||||
{
|
||||
{ "Content-Type", "application/json" },
|
||||
{ "Authorization", $"Bearer {token}" },
|
||||
};
|
||||
|
||||
public static Dictionary<string, string> GetFormUrlEncodedHeader()
|
||||
=> new Dictionary<string, string>
|
||||
{
|
||||
{ "Content-Type", "application/x-www-form-urlencoded" },
|
||||
};
|
||||
}
|
||||
}
|
70
Kreta.Client/ClientBase/Interface/IClientBase.cs
Normal file
70
Kreta.Client/ClientBase/Interface/IClientBase.cs
Normal file
|
@ -0,0 +1,70 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Net.Cache;
|
||||
using Kreta.Client.Jira.Model.Request;
|
||||
|
||||
namespace Kreta.Client.ClientBase.Interface
|
||||
{
|
||||
internal interface IClientBase
|
||||
{
|
||||
void CreateBasicAuthenticator(string username, string password);
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
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();
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
20
Kreta.Client/ClientBase/Interface/IRestResult.cs
Normal file
20
Kreta.Client/ClientBase/Interface/IRestResult.cs
Normal file
|
@ -0,0 +1,20 @@
|
|||
using System;
|
||||
using System.Net;
|
||||
|
||||
namespace Kreta.Client.ClientBase.Interface
|
||||
{
|
||||
internal interface IRestResult
|
||||
{
|
||||
string Result { get; set; }
|
||||
HttpStatusCode StatusCode { get; set; }
|
||||
string StatusDescription { get; set; }
|
||||
Exception Exception { get; set; }
|
||||
string ErrorMessage { get; set; }
|
||||
byte[] RawBytes { get; set; }
|
||||
}
|
||||
|
||||
internal interface IRestResult<TResponse> : IRestResult
|
||||
{
|
||||
TResponse Result { get; set; }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
using Newtonsoft.Json;
|
||||
|
||||
namespace Kreta.Client.ClientBase.Response
|
||||
{
|
||||
internal class GetTokenFailureResponse
|
||||
{
|
||||
[JsonProperty("error")]
|
||||
public string Error { get; set; }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
using System;
|
||||
|
||||
namespace Kreta.Client.ClientBase.Response
|
||||
{
|
||||
public class GetTokenResponse : IResponse
|
||||
{
|
||||
public GetTokenResponse() { }
|
||||
|
||||
public GetTokenResponse(string error)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(error))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(error));
|
||||
}
|
||||
|
||||
Error = error;
|
||||
}
|
||||
|
||||
public GetTokenResponse(string accessToken, int expiresIn)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(accessToken))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(accessToken));
|
||||
}
|
||||
|
||||
if (expiresIn < 1)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(expiresIn));
|
||||
}
|
||||
|
||||
IsSuccess = true;
|
||||
AccessToken = accessToken;
|
||||
ExpiresIn = expiresIn;
|
||||
}
|
||||
|
||||
public bool IsSuccess { get; }
|
||||
|
||||
public string AccessToken { get; }
|
||||
|
||||
public int ExpiresIn { get; }
|
||||
|
||||
public string Error { get; }
|
||||
|
||||
public bool TryAgain { get; }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
using Newtonsoft.Json;
|
||||
|
||||
namespace Kreta.Client.ClientBase.Response
|
||||
{
|
||||
internal class GetTokenSuccessResponse
|
||||
{
|
||||
[JsonProperty(PropertyName = "access_token")]
|
||||
public string AccessToken { get; set; }
|
||||
|
||||
[JsonProperty(PropertyName = "expires_in")]
|
||||
public int ExpiresIn { get; set; }
|
||||
|
||||
[JsonProperty(PropertyName = "token_type")]
|
||||
public string TokenType { get; set; }
|
||||
}
|
||||
}
|
11
Kreta.Client/ClientBase/Response/IResponse.cs
Normal file
11
Kreta.Client/ClientBase/Response/IResponse.cs
Normal file
|
@ -0,0 +1,11 @@
|
|||
namespace Kreta.Client.ClientBase.Response
|
||||
{
|
||||
internal interface IResponse
|
||||
{
|
||||
bool IsSuccess { get; }
|
||||
|
||||
string Error { get; }
|
||||
|
||||
bool TryAgain { get; }
|
||||
}
|
||||
}
|
42
Kreta.Client/ClientBase/RestResult.cs
Normal file
42
Kreta.Client/ClientBase/RestResult.cs
Normal file
|
@ -0,0 +1,42 @@
|
|||
using System;
|
||||
using System.Net;
|
||||
using Kreta.Client.ClientBase.Interface;
|
||||
using RestSharp;
|
||||
|
||||
namespace Kreta.Client.ClientBase
|
||||
{
|
||||
internal class RestResult : IRestResult
|
||||
{
|
||||
public string Result { get; set; }
|
||||
public HttpStatusCode StatusCode { get; set; }
|
||||
public string StatusDescription { get; set; }
|
||||
public Exception Exception { get; set; }
|
||||
public string ErrorMessage { get; set; }
|
||||
public byte[] RawBytes { get; set; }
|
||||
|
||||
public static IRestResult Convert(IRestResponse restResponse) => new RestResult
|
||||
{
|
||||
Result = restResponse.Content,
|
||||
StatusCode = restResponse.StatusCode,
|
||||
StatusDescription = restResponse.StatusDescription,
|
||||
Exception = restResponse.ErrorException,
|
||||
ErrorMessage = restResponse.ErrorMessage,
|
||||
RawBytes = restResponse.RawBytes
|
||||
};
|
||||
}
|
||||
|
||||
internal class RestResult<TResponse> : RestResult, IRestResult<TResponse>
|
||||
{
|
||||
public TResponse Result { get; set; }
|
||||
|
||||
public static IRestResult<TResponse> Convert(IRestResponse<TResponse> restResponse) => new RestResult<TResponse>
|
||||
{
|
||||
Result = restResponse.Data,
|
||||
StatusCode = restResponse.StatusCode,
|
||||
StatusDescription = restResponse.StatusDescription,
|
||||
Exception = restResponse.ErrorException,
|
||||
ErrorMessage = restResponse.ErrorMessage,
|
||||
RawBytes = restResponse.RawBytes
|
||||
};
|
||||
}
|
||||
}
|
235
Kreta.Client/ClientBase/RestSharpClientBase.cs
Normal file
235
Kreta.Client/ClientBase/RestSharpClientBase.cs
Normal file
|
@ -0,0 +1,235 @@
|
|||
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));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue