init
This commit is contained in:
commit
e124a47765
19374 changed files with 9806149 additions and 0 deletions
104
Kreta.Client/GlobalApi/GlobalApiClient.cs
Normal file
104
Kreta.Client/GlobalApi/GlobalApiClient.cs
Normal file
|
@ -0,0 +1,104 @@
|
|||
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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue