366 lines
14 KiB
C#
366 lines
14 KiB
C#
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));
|
|
}
|
|
}
|
|
}
|
|
}
|