kreta/Kreta.Client/KozpontiKreta/KozpontiClient.cs
2024-03-13 00:33:46 +01:00

84 lines
3 KiB
C#

using System;
using System.Collections.Generic;
using Kreta.Client.KozpontiKreta.Model;
using Kreta.Core.KozpontiModels.MdszModels;
using Newtonsoft.Json;
using RestSharp;
namespace Kreta.Client.KozpontiKreta
{
public class KozpontiClient
{
private readonly string baseUrl;
private readonly string apiKey;
public KozpontiClient(string baseUrl, string apiKey)
{
this.baseUrl = baseUrl;
this.apiKey = apiKey;
}
private IRestResponse Http(Method method, string relativeUri, Dictionary<string, string> parameters = null, Object body = null)
{
var restRequest = new RestRequest(relativeUri, method);
restRequest.AddHeader("ApiKey", apiKey);
if (parameters != null && parameters.Count > 0)
{
foreach (var parameter in parameters)
{
restRequest.AddParameter(parameter.Key, parameter.Value);
}
}
if (method == Method.POST && body != null)
{
restRequest.AddJsonBody(body);
}
var result = new RestClient(string.Format(baseUrl)).Execute(restRequest);
return result;
}
private TResponse Http<TResponse>(Method method, string relativeUri, Dictionary<string, string> parameters = null, Object body = null)
{
var result = Http(method, relativeUri, parameters, body);
var response = JsonConvert.DeserializeObject<TResponse>(result.Content);
return response;
}
private TResponse GetAsync<TResponse>(string relativeUri, Dictionary<string, string> parameters = null)
{
return Http<TResponse>(Method.GET, relativeUri, parameters);
}
private TResponse PostAsync<TResponse>(string relativeUri, Dictionary<string, string> parameters = null, Object body = null)
{
return Http<TResponse>(Method.POST, relativeUri, parameters, body);
}
private void Post(string relativeUri, Dictionary<string, string> parameters = null, Object body = null)
{
Http(Method.POST, relativeUri, parameters, body);
}
public List<MdszResponseModel> GetDiakOlimpiaList(MdszRequestModel model)
{
var response = PostAsync<List<MdszResponseModel>>("/api/Mdsz/external/get", null, model);
return response;
}
public KozpontiNebuloResponseModel NebuloJelenlet(KozpontiNebuloRequestModel model)
{
var response = PostAsync<KozpontiNebuloResponseModel>("/api/nebulo/external/megjelent", null, model);
return response;
}
public KozpontiNebuloEngedelyezettResponseModel GetBeiratkozasLetszamList(KozpontiNebuloEngedelyezettRequestModel model)
{
var response = PostAsync<KozpontiNebuloEngedelyezettResponseModel>("/api/Approves/external/engedelyezett", null, model);
return response;
}
}
}