This commit is contained in:
skidoodle 2024-03-13 00:33:46 +01:00
commit e124a47765
19374 changed files with 9806149 additions and 0 deletions

View file

@ -0,0 +1,34 @@
using System.Configuration;
namespace Kreta.Client.CoreApi.Configuration
{
public class CoreApiClientConfiguration : ConfigurationSection, ICoreApiClientConfiguration
{
[ConfigurationProperty(nameof(IDPUrl), IsRequired = true)]
public string IDPUrl => (string)base[nameof(IDPUrl)];
[ConfigurationProperty(nameof(DKTUrl), IsRequired = true)]
public string DKTUrl => (string)base[nameof(DKTUrl)];
[ConfigurationProperty(nameof(PrivateClientId), IsRequired = true)]
public string PrivateClientId => (string)base[nameof(PrivateClientId)];
[ConfigurationProperty(nameof(PrivateClientSecret), IsRequired = true)]
public string PrivateClientSecret => (string)base[nameof(PrivateClientSecret)];
[ConfigurationProperty(nameof(Scope), IsRequired = true)]
public string Scope => (string)base[nameof(Scope)];
[ConfigurationProperty(nameof(CoreApiIDPUrl), IsRequired = true)]
public string CoreApiIDPUrl => (string)base[nameof(CoreApiIDPUrl)];
[ConfigurationProperty(nameof(CoreApiUrl), IsRequired = true)]
public string CoreApiUrl => (string)base[nameof(CoreApiUrl)];
[ConfigurationProperty(nameof(CoreApiPrivateClientId), IsRequired = true)]
public string CoreApiPrivateClientId => (string)base[nameof(CoreApiPrivateClientId)];
[ConfigurationProperty(nameof(CoreApiPrivateClientSecret), IsRequired = true)]
public string CoreApiPrivateClientSecret => (string)base[nameof(CoreApiPrivateClientSecret)];
}
}

View file

@ -0,0 +1,15 @@
namespace Kreta.Client.CoreApi.Configuration
{
public interface ICoreApiClientConfiguration
{
string IDPUrl { get; }
string DKTUrl { get; }
string PrivateClientId { get; }
string PrivateClientSecret { get; }
string Scope { get; }
string CoreApiIDPUrl { get; }
string CoreApiUrl { get; }
string CoreApiPrivateClientId { get; }
string CoreApiPrivateClientSecret { get; }
}
}

View file

@ -0,0 +1,7 @@
namespace Kreta.Client.CoreApi.Constant
{
internal static class GrantType
{
public const string ClientCredentials = "client_credentials";
}
}

View file

@ -0,0 +1,7 @@
namespace Kreta.Client.CoreApi.Constant
{
internal static class Scope
{
public const string Private = "kreta-core-webapi.public";
}
}

View file

@ -0,0 +1,10 @@
namespace Kreta.Client.CoreApi.Constant
{
internal static class TokenRequest
{
public const string GrantType = "grant_type";
public const string ClientId = "client_id";
public const string ClientSecret = "client_secret";
public const string Scope = "scope";
}
}

View file

@ -0,0 +1,366 @@
using System;
using System.Collections.Generic;
using System.Net;
using Kreta.Client.ClientBase;
using Kreta.Client.ClientBase.Interface;
using Kreta.Client.CoreApi.Configuration;
using Kreta.Client.CoreApi.Extension;
using Kreta.Client.CoreApi.Request;
using Kreta.Client.CoreApi.Response;
using Kreta.Framework;
using Kreta.Framework.Exceptions;
using Newtonsoft.Json;
namespace Kreta.Client.CoreApi
{
internal class CoreApiClient : RestSharpClientBase, ICoreApiClient
{
private readonly ICoreApiClientConfiguration coreApiClientConfiguration;
public CoreApiClient(ICoreApiClientConfiguration coreApiClientConfiguration)
{
this.coreApiClientConfiguration = coreApiClientConfiguration ?? throw new ArgumentNullException(nameof(coreApiClientConfiguration));
}
public DKTFeladatInsertResponse DKTFeladatInsert(DKTFeladatInsertRequest request, int alkalmazottId)
{
GetTokenResponse token = GetToken();
if (token.IsSuccess)
{
BaseUrl = coreApiClientConfiguration.DKTUrl;
string relativeUri = $"/kreta/intezmenyek/alkalmazottak/{alkalmazottId}/orak/hazifeladatok";
Dictionary<string, string> headers = token.AccessToken.GetAuthorizationHeaderWithJson(request.IntezmenyGuid, request.TanevSorszam);
IRestResult response = Post(relativeUri, headers, body: request);
if (response.StatusCode == HttpStatusCode.OK)
{
return new DKTFeladatInsertResponse
{
NewHazifeladatId = int.Parse(response.Result)
};
}
try
{
return JsonConvert.DeserializeObject<DKTFeladatInsertResponse>(response.Result);
}
catch (Exception e)
{
var ex = new Exception(response.Result, e);
SDAServer.Instance.Logger.ExceptionThrown(ex);
return new DKTFeladatInsertResponse
{
Exception = new DKTExceptionItem
{
Message = "Kapcsolódási probléma, kérjük a mentést próbálja meg újra!"
}
};
}
}
return new DKTFeladatInsertResponse
{
Exception = new DKTExceptionItem
{
Message = "Nem sikerült a kapcsolat felépítése DKT-val!"
}
};
}
public DKTFeladatResponse DKTFeladatUpdate(DKTFeladatUpdateRequest request, int alkalmazottId)
{
GetTokenResponse token = GetToken();
if (token.IsSuccess)
{
BaseUrl = coreApiClientConfiguration.DKTUrl;
string relativeUri = $"/kreta/intezmenyek/alkalmazottak/{alkalmazottId}/orak/hazifeladatok/{request.Id}";
Dictionary<string, string> headers = token.AccessToken.GetAuthorizationHeaderWithJson(request.IntezmenyGuid, request.TanevSorszam);
IRestResult response = Put(relativeUri, headers, body: request);
if (response.StatusCode == HttpStatusCode.OK)
{
return new DKTFeladatResponse();
}
try
{
return JsonConvert.DeserializeObject<DKTFeladatResponse>(response.Result);
}
catch (Exception e)
{
var ex = new Exception(response.Result, e);
SDAServer.Instance.Logger.ExceptionThrown(ex);
return new DKTFeladatResponse
{
Exception = new DKTExceptionItem
{
Message = "Kapcsolódási probléma, kérjük a mentést próbálja meg újra!"
}
};
}
}
return new DKTFeladatResponse
{
Exception = new DKTExceptionItem
{
Message = "Nem sikerült a kapcsolat felépítése DKT-val!"
}
};
}
public DKTFeladatResponse DKTFeladatDelete(DKTFeladatDeleteRequest request)
{
GetTokenResponse token = GetToken();
if (token.IsSuccess)
{
BaseUrl = coreApiClientConfiguration.DKTUrl;
string relativeUri = $"/kreta/intezmenyek/alkalmazottak/{request.AlkalmazottId}/orak/hazifeladatok/{request.Id}";
Dictionary<string, string> headers = token.AccessToken.GetAuthorizationHeaderWithJson(request.IntezmenyGuid, request.TanevSorszam);
IRestResult response = Delete(relativeUri, headers);
if (response.StatusCode == HttpStatusCode.OK)
{
return new DKTFeladatResponse();
}
try
{
return JsonConvert.DeserializeObject<DKTFeladatResponse>(response.Result);
}
catch (Exception e)
{
var ex = new Exception(response.Result, e);
SDAServer.Instance.Logger.ExceptionThrown(ex);
return new DKTFeladatResponse
{
Exception = new DKTExceptionItem
{
Message = "Kapcsolódási probléma, kérjük a törlést próbálja meg újra!"
}
};
}
}
return new DKTFeladatResponse
{
Exception = new DKTExceptionItem
{
Message = "Nem sikerült a kapcsolat felépítése DKT-val!"
}
};
}
public EszkozIgenylesCreateResponse EszkozIgenylesInsert(EszkozIgenylesCreateDto request, string intezmenyGuid, int tanevSorszam)
{
GetTokenResponse token = GetCoreApiToken();
if (token.IsSuccess)
{
BaseUrl = coreApiClientConfiguration.CoreApiUrl;
string relativeUri = $"/intezmenyek/EszkozIgenyles";
Dictionary<string, string> headers = token.AccessToken.GetAuthorizationHeaderWithJson(intezmenyGuid, tanevSorszam);
headers.Add("setting", "true");
IRestResult response = Post(relativeUri, headers, body: request);
try
{
if (response.StatusCode == HttpStatusCode.OK)
{
return new EszkozIgenylesCreateResponse
{
NewId = int.Parse(response.Result)
};
}
try
{
return JsonConvert.DeserializeObject<EszkozIgenylesCreateResponse>(response.Result);
}
catch (Exception e)
{
var ex = new Exception(response.Result, e);
SDAServer.Instance.Logger.ExceptionThrown(ex);
return new EszkozIgenylesCreateResponse
{
Exception = new CoreApiExceptionItem
{
Message = "Nem megfelelő response model!"
}
};
}
}
catch (Exception e)
{
var ex = new Exception(response.Result, e);
SDAServer.Instance.Logger.ExceptionThrown(ex);
throw new Exception(e.Message);
}
}
throw new Exception("IDP nem elérhető!");
}
public CoreApiResponse EszkozIgenylesUpdate(EszkozIgenylesUpdateDto request, string intezmenyGuid, int tanevSorszam)
{
GetTokenResponse token = GetCoreApiToken();
if (token.IsSuccess)
{
BaseUrl = coreApiClientConfiguration.CoreApiUrl;
string relativeUri = $"/intezmenyek/EszkozIgenyles/" + request.Id.ToString();
Dictionary<string, string> headers = token.AccessToken.GetAuthorizationHeaderWithJson(intezmenyGuid, tanevSorszam);
headers.Add("setting", "true");
IRestResult response = Put(relativeUri, headers, body: request);
try
{
if (response.StatusCode == HttpStatusCode.OK)
{
return new CoreApiResponse();
}
else
{
try
{
return JsonConvert.DeserializeObject<CoreApiResponse>(response.Result);
}
catch (Exception e)
{
var ex = new Exception(response.Result, e);
SDAServer.Instance.Logger.ExceptionThrown(ex);
return new CoreApiResponse
{
Exception = new CoreApiExceptionItem
{
Message = "Nem megfelelő response model!"
}
};
}
}
}
catch (Exception e)
{
var ex = new Exception(response.Result, e);
SDAServer.Instance.Logger.ExceptionThrown(ex);
throw new Exception(e.Message);
}
}
throw new Exception("IDP nem elérhető!");
}
public List<EszkozIgenylesDto> EszkozIgenylesGet(string intezmenyGuid, int tanevSorszam)
{
GetTokenResponse token = GetCoreApiToken();
if (token.IsSuccess)
{
BaseUrl = coreApiClientConfiguration.CoreApiUrl;
string relativeUri = $"/intezmenyek/EszkozIgenyles";
Dictionary<string, string> headers = token.AccessToken.GetAuthorizationHeaderWithJson(intezmenyGuid, tanevSorszam);
IRestResult response = Get(relativeUri, headers);
if (response.StatusCode == HttpStatusCode.OK)
{
try
{
return JsonConvert.DeserializeObject<List<EszkozIgenylesDto>>(response.Result,
new JsonSerializerSettings() { MissingMemberHandling = MissingMemberHandling.Ignore });
}
catch (Exception e)
{
var ex = new Exception(response.Result, e);
SDAServer.Instance.Logger.ExceptionThrown(ex);
throw new Exception(e.Message);
}
}
else
{
throw new Exception("Szolgáltatás nem elérhető!");
}
}
throw new Exception("IDP nem elérhető!");
}
public GetTokenResponse GetToken()
{
GetTokenResponse result = GetToken(coreApiClientConfiguration.GetPrivateTokenRequestParameters());
return result;
}
private GetTokenResponse GetToken(Dictionary<string, string> tokenRequestParameters)
{
return ExceptionLogger(() =>
{
BaseUrl = coreApiClientConfiguration.IDPUrl;
string relativeUri = "/connect/token";
Dictionary<string, string> headers = HeaderExtension.GetFormUrlEncodedHeader();
IRestResult response = Post(relativeUri, headers, tokenRequestParameters);
if (response.StatusCode == HttpStatusCode.OK)
{
GetTokenSuccessResponse successResponse = JsonConvert.DeserializeObject<GetTokenSuccessResponse>(response.Result);
return new GetTokenResponse(successResponse.AccessToken, successResponse.ExpiresIn);
}
GetTokenFailureResponse failureResponse = JsonConvert.DeserializeObject<GetTokenFailureResponse>(response.Result);
return new GetTokenResponse(failureResponse.Error);
});
}
public GetTokenResponse GetCoreApiToken()
{
GetTokenResponse result = GetCoreApiToken(coreApiClientConfiguration.GetCoreApiPrivateTokenRequestParameters());
return result;
}
private GetTokenResponse GetCoreApiToken(Dictionary<string, string> tokenRequestParameters)
{
return ExceptionLogger(() =>
{
BaseUrl = coreApiClientConfiguration.CoreApiIDPUrl;
string relativeUri = "/connect/token";
Dictionary<string, string> headers = HeaderExtension.GetFormUrlEncodedHeader();
IRestResult response = Post(relativeUri, headers, tokenRequestParameters);
if (response.StatusCode == HttpStatusCode.OK)
{
GetTokenSuccessResponse successResponse = JsonConvert.DeserializeObject<GetTokenSuccessResponse>(response.Result);
return new GetTokenResponse(successResponse.AccessToken, successResponse.ExpiresIn);
}
GetTokenFailureResponse failureResponse = JsonConvert.DeserializeObject<GetTokenFailureResponse>(response.Result);
return new GetTokenResponse(failureResponse.Error);
});
}
private T ExceptionLogger<T>(Func<T> action) where T : IResponse, new()
{
try
{
return action();
}
catch (Exception ex)
{
SDAServer.Instance.Logger.ExceptionThrown(ex);
return (T)Activator.CreateInstance(typeof(T), ExceptionUtil.ExceptionToString(ex));
}
}
}
}

View file

@ -0,0 +1,30 @@
using System.Collections.Generic;
using Kreta.Client.CoreApi.Configuration;
using Kreta.Client.CoreApi.Constant;
namespace Kreta.Client.CoreApi.Extension
{
internal static class CoreApiClientConfigurationExtension
{
public static Dictionary<string, string> GetPrivateTokenRequestParameters(this ICoreApiClientConfiguration coreApiClientConfiguration)
{
return new Dictionary<string, string>
{
{ TokenRequest.GrantType, GrantType.ClientCredentials },
{ TokenRequest.ClientId, coreApiClientConfiguration.PrivateClientId },
{ TokenRequest.ClientSecret, coreApiClientConfiguration.PrivateClientSecret },
{ TokenRequest.Scope, coreApiClientConfiguration.Scope },
};
}
public static Dictionary<string, string> GetCoreApiPrivateTokenRequestParameters(this ICoreApiClientConfiguration coreApiClientConfiguration)
{
return new Dictionary<string, string>
{
{ TokenRequest.GrantType, GrantType.ClientCredentials },
{ TokenRequest.ClientId, coreApiClientConfiguration.CoreApiPrivateClientId },
{ TokenRequest.ClientSecret, coreApiClientConfiguration.CoreApiPrivateClientSecret }
};
}
}
}

View file

@ -0,0 +1,29 @@
using System.Collections.Generic;
namespace Kreta.Client.CoreApi.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, string intezmenyGuid, int tanevId)
=> new Dictionary<string, string>
{
{ "Content-Type", "application/json" },
{ "Authorization", $"Bearer {token}" },
{ "IntezmenyId", $"{intezmenyGuid}" },
{ "TanevId", $"{tanevId}" },
};
public static Dictionary<string, string> GetFormUrlEncodedHeader()
=> new Dictionary<string, string>
{
{ "Content-Type", "application/x-www-form-urlencoded" },
};
}
}

View file

@ -0,0 +1,16 @@
using System.Collections.Generic;
using Kreta.Client.CoreApi.Request;
using Kreta.Client.CoreApi.Response;
namespace Kreta.Client.CoreApi
{
public interface ICoreApiClient
{
DKTFeladatInsertResponse DKTFeladatInsert(DKTFeladatInsertRequest dktFeladatDeleteRequest, int alkalmazottId);
DKTFeladatResponse DKTFeladatUpdate(DKTFeladatUpdateRequest dktFeladatDeleteRequest, int alkalmazottId);
DKTFeladatResponse DKTFeladatDelete(DKTFeladatDeleteRequest dktFeladatDeleteRequest);
EszkozIgenylesCreateResponse EszkozIgenylesInsert(EszkozIgenylesCreateDto request, string intezmenyGuid, int tanevSorszam);
CoreApiResponse EszkozIgenylesUpdate(EszkozIgenylesUpdateDto request, string intezmenyGuid, int tanevSorszam);
List<EszkozIgenylesDto> EszkozIgenylesGet(string intezmenyGuid, int tanevSorszam);
}
}

View file

@ -0,0 +1,13 @@
namespace Kreta.Client.CoreApi.Request
{
public class DKTFeladatDeleteRequest
{
public string IntezmenyGuid { get; set; }
public int TanevSorszam { get; set; }
public int AlkalmazottId { get; set; }
public int Id { get; set; }
}
}

View file

@ -0,0 +1,29 @@
using System;
namespace Kreta.Client.CoreApi.Request
{
public class DKTFeladatInsertRequest
{
public string IntezmenyGuid { get; set; }
public int TanevSorszam { get; set; }
public int AlkalmazottId { get; set; }
public int TantargyId { get; set; }
public int? OsztalyId { get; set; }
public int? CsoportId { get; set; }
public DateTime OraDatum { get; set; }
public int? Oraszam { get; set; }
public DateTime? OraIdopont { get; set; }
public DateTime BeadasHatarido { get; set; }
public string Szoveg { get; set; }
}
}

View file

@ -0,0 +1,19 @@
using System;
namespace Kreta.Client.CoreApi.Request
{
public class DKTFeladatUpdateRequest
{
public string IntezmenyGuid { get; set; }
public int TanevSorszam { get; set; }
public int AlkalmazottId { get; set; }
public int Id { get; set; }
public DateTime BeadasHatarido { get; set; }
public string Szoveg { get; set; }
}
}

View file

@ -0,0 +1,34 @@
using System.Collections.Generic;
namespace Kreta.Client.CoreApi.Response
{
public class CoreApiResponse
{
public CoreApiExceptionItem Exception { get; set; }
public CoreApiValidationResult ValidationResult { get; set; }
public string CorrelationId { get; set; }
}
public class CoreApiValidationResult
{
public List<CoreApiValidationItem> Errors { get; set; } = new List<CoreApiValidationItem>();
public List<CoreApiValidationItem> Warnings { get; set; } = new List<CoreApiValidationItem>();
public List<CoreApiValidationItem> Successes { get; set; } = new List<CoreApiValidationItem>();
}
public class CoreApiExceptionItem
{
public string Type { get; set; }
public string Message { get; set; }
public string StackTrace { get; set; }
}
public class CoreApiValidationItem
{
public string Message { get; set; }
public string PropertyName { get; set; }
public string Value { get; set; }
public string TextValue { get; set; }
}
}

View file

@ -0,0 +1,7 @@
namespace Kreta.Client.CoreApi.Response
{
public class DKTFeladatInsertResponse : DKTFeladatResponse
{
public int NewHazifeladatId { get; set; }
}
}

View file

@ -0,0 +1,35 @@
using System.Collections.Generic;
namespace Kreta.Client.CoreApi.Response
{
public class DKTFeladatResponse
{
public string CorrelationId { get; set; }
public DKTExceptionItem Exception { get; set; }
public DKTValidationResult ValidationResult { get; set; }
public int Status { get; set; }
public Dictionary<string, string[]> Errors { get; set; }
public string Instance { get; set; }
}
public class DKTValidationResult
{
public List<DKTValidationItem> Tiltasok { get; set; } = new List<DKTValidationItem>();
public List<DKTValidationItem> Figyelmeztetesek { get; set; } = new List<DKTValidationItem>();
public List<DKTValidationItem> Engedelyezettek { get; set; } = new List<DKTValidationItem>();
}
public class DKTExceptionItem
{
public string Type { get; set; }
public string Message { get; set; }
public string StackTrace { get; set; }
}
public class DKTValidationItem
{
public string Id { get; set; }
public string Nev { get; set; }
public string Uzenet { get; set; }
}
}

View file

@ -0,0 +1,19 @@
using System;
namespace Kreta.Client.CoreApi.Response
{
public class EszkozIgenylesCreateDto
{
public int? AlkalmazottId { get; set; }
public int? TanuloId { get; set; }
public int? GondviseloId { get; set; }
public bool EszkozKiosztva { get; set; }
public bool ElfogadottAszf { get; set; }
public DateTime? AszfElfogadasIdeje { get; set; }
}
}

View file

@ -0,0 +1,7 @@
namespace Kreta.Client.CoreApi.Response
{
public class EszkozIgenylesCreateResponse : CoreApiResponse
{
public int NewId { get; set; }
}
}

View file

@ -0,0 +1,19 @@
using System;
namespace Kreta.Client.CoreApi.Response
{
public class EszkozIgenylesDto
{
public int? AlkalmazottId { get; set; }
public int? TanuloId { get; set; }
public int? GondviseloId { get; set; }
public bool EszkozKiosztva { get; set; }
public bool ElfogadottAszf { get; set; }
public DateTime? AszfElfogadasIdeje { get; set; }
}
}

View file

@ -0,0 +1,21 @@
using System;
namespace Kreta.Client.CoreApi.Response
{
public class EszkozIgenylesUpdateDto
{
public int Id { get; set; }
public int? AlkalmazottId { get; set; }
public int? TanuloId { get; set; }
public int? GondviseloId { get; set; }
public bool EszkozKiosztva { get; set; }
public bool ElfogadottAszf { get; set; }
public DateTime? AszfElfogadasIdeje { get; set; }
}
}

View file

@ -0,0 +1,10 @@
using Newtonsoft.Json;
namespace Kreta.Client.CoreApi.Response
{
internal class GetTokenFailureResponse
{
[JsonProperty("error")]
public string Error { get; set; }
}
}

View file

@ -0,0 +1,46 @@
using System;
namespace Kreta.Client.CoreApi.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; }
}
}

View file

@ -0,0 +1,16 @@
using Newtonsoft.Json;
namespace Kreta.Client.CoreApi.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; }
}
}

View file

@ -0,0 +1,9 @@
namespace Kreta.Client.CoreApi.Response
{
internal interface IResponse
{
bool IsSuccess { get; }
string Error { get; }
}
}