104 lines
3.4 KiB
C#
104 lines
3.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Net;
|
|
using Kreta.Client.ClientBase;
|
|
using Kreta.Client.ClientBase.Extension;
|
|
using Kreta.Client.ClientBase.Response;
|
|
using Kreta.Client.GlobalApi.Configuration;
|
|
using Kreta.Client.GlobalApi.Models;
|
|
using Kreta.Core;
|
|
using Kreta.Framework;
|
|
using Kreta.Framework.Exceptions;
|
|
using Kreta.Resources;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace Kreta.Client.GlobalApi
|
|
{
|
|
internal class GlobalApiClient : RestSharpClientBase, IGlobalApiClient
|
|
{
|
|
private readonly GlobalApiConfiguration configuration;
|
|
|
|
public GlobalApiClient()
|
|
{
|
|
configuration = (GlobalApiConfiguration)ConfigurationManager.GetSection(Constants.ConfigurationSectionNames.GlobalApiConfiguration);
|
|
}
|
|
|
|
public GetTokenResponse GetPrivateToken()
|
|
{
|
|
return GetToken(new Dictionary<string, string> {
|
|
{ "client_id", configuration.ClientId },
|
|
{ "client_secret", configuration.ClientSecret },
|
|
{ "grant_type", "client_credentials" }
|
|
});
|
|
}
|
|
|
|
public List<AdatbaziselerhetosegResponseModel> GetKretaAdatbaziselerhetosegek(string privateToken, SystemType systemType)
|
|
{
|
|
return ExceptionLogger(() =>
|
|
{
|
|
BaseUrl = configuration.GlobalApiUrl;
|
|
|
|
var relativeUri = $"/adatbaziselerhetosegek/kreta/{systemType}";
|
|
var header = privateToken.GetAuthorizationHeaderWithJson();
|
|
|
|
var response = Get(relativeUri, header);
|
|
|
|
if (response.StatusCode == HttpStatusCode.OK)
|
|
{
|
|
return JsonConvert.DeserializeObject<List<AdatbaziselerhetosegResponseModel>>(response.Result,
|
|
new JsonSerializerSettings() { MissingMemberHandling = MissingMemberHandling.Ignore });
|
|
}
|
|
else
|
|
{
|
|
throw new Exception(ErrorResource.HibasIdpKeres);
|
|
}
|
|
});
|
|
}
|
|
|
|
private GetTokenResponse GetToken(Dictionary<string, string> tokenRequestParameters)
|
|
{
|
|
return ExceptionLogger(() =>
|
|
{
|
|
BaseUrl = configuration.TokenUrl;
|
|
|
|
var relativeUri = "/connect/token";
|
|
|
|
var response = Post(relativeUri, HeaderExtension.GetFormUrlEncodedHeader(), tokenRequestParameters);
|
|
|
|
if (response.StatusCode == HttpStatusCode.OK)
|
|
{
|
|
var successResponse = JsonConvert.DeserializeObject<GetTokenSuccessResponse>(response.Result);
|
|
|
|
return new GetTokenResponse(successResponse.AccessToken, successResponse.ExpiresIn);
|
|
}
|
|
|
|
var failureResponse = JsonConvert.DeserializeObject<GetTokenFailureResponse>(response.Result);
|
|
|
|
return new GetTokenResponse(failureResponse.Error);
|
|
});
|
|
}
|
|
|
|
private T ExceptionLogger<T>(Func<T> action)
|
|
{
|
|
try
|
|
{
|
|
return action();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SDAServer.Instance.Logger.ExceptionThrown(ex);
|
|
|
|
return (T)Activator.CreateInstance(typeof(T), ExceptionUtil.ExceptionToString(ex));
|
|
}
|
|
}
|
|
}
|
|
|
|
public class GlobalApiClientFactory
|
|
{
|
|
public static IGlobalApiClient Build()
|
|
{
|
|
return new GlobalApiClient();
|
|
}
|
|
}
|
|
}
|