138 lines
6 KiB
C#
138 lines
6 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using Kreta.Core.Client.ClientProxy;
|
|
using Kreta.Core.Client.Exceptions;
|
|
using Kreta.Core.Client.MessageInvoker;
|
|
using Kreta.Naplo.Domain;
|
|
using Kreta.Naplo.Infrastructure;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace Kreta.Naplo.BusinessLogic
|
|
{
|
|
/// <summary>
|
|
/// Service base
|
|
/// </summary>
|
|
abstract class Service : HttpClientProxy, IService
|
|
{
|
|
/// <summary>
|
|
/// Api key
|
|
/// </summary>
|
|
const string ApiKey = nameof(ApiKey);
|
|
|
|
/// <summary>
|
|
/// The request default error message
|
|
/// </summary>
|
|
const string RequestDefaultErrorMessage = "Proxy error requesting data from eKreta (for details see innerexception and request\response property).";
|
|
|
|
/// <summary>
|
|
/// The error during response deserialization
|
|
/// </summary>
|
|
const string ErrorDuringResponseDeserialization = "Proxy error during response deserialization when requesting data from eKreta web api (for details see innerexception and request\response property).";
|
|
|
|
/// <summary>
|
|
/// The error during response deserialization
|
|
/// </summary>
|
|
const string ErrorDuringRequestSerialization = "Proxy error during request serialization when requesting data from eKreta web api (for details see innerexception and request\response property)";
|
|
|
|
/// <summary>
|
|
/// Relative url
|
|
/// </summary>
|
|
protected string RelativeUrl { get; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the context.
|
|
/// </summary>
|
|
/// <value>
|
|
/// The context.
|
|
/// </value>
|
|
public ServiceContext Context { get; set; }
|
|
|
|
/// <summary>
|
|
/// Handles the HTTP request error.
|
|
/// </summary>
|
|
/// <param name="request">The request.</param>
|
|
/// <param name="response">The response.</param>
|
|
/// <param name="requestUrl">RequestUrl</param>
|
|
/// <param name="exception">The exception.</param>
|
|
/// <exception cref="Kreta.Mobile.Infrastructure.Exceptions.ApiServiceCallException">null</exception>
|
|
protected override void HandleHttpRequestError(HttpRequestMessage request, HttpResponseMessage response, Exception exception)
|
|
{
|
|
if (request != null && request.Headers.Contains(ApiKey))
|
|
{
|
|
request.Headers.Remove(nameof(ApiKey));
|
|
request.Headers.Add((nameof(ApiKey)), Infrastructure.Constants.SecretLogValue);
|
|
}
|
|
|
|
throw new HttpClientProxyException(RequestDefaultErrorMessage, exception, request, response);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handles the deserialization error.
|
|
/// </summary>
|
|
/// <param name="response">The response.</param>
|
|
/// <param name="exception">The exception.</param>
|
|
/// <exception cref="Kreta.Mobile.Infrastructure.Exceptions.ApiServiceCallException"></exception>
|
|
protected override void HandleDeserializationError(HttpRequestMessage request, HttpResponseMessage response, JsonException exception)
|
|
{
|
|
throw new HttpClientProxyException(ErrorDuringResponseDeserialization, exception, request, response);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handles the serialization error.
|
|
/// </summary>
|
|
/// <param name="request">The request.</param>
|
|
/// <param name="exception">The exception.</param>
|
|
/// <exception cref="Kreta.Mobile.Infrastructure.Exceptions.ApiServiceCallException"></exception>
|
|
protected override void HandleSerializationError(HttpRequestMessage request, JsonException exception)
|
|
{
|
|
throw new HttpClientProxyException(ErrorDuringRequestSerialization, exception, request, null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates the request.
|
|
/// </summary>
|
|
/// <param name="httpMethod">The HTTP method.</param>
|
|
/// <param name="relativeUrl">The relative URI.</param>
|
|
/// <param name="parameters">The parameters.</param>
|
|
/// <param name="data">The data.</param>
|
|
/// <returns></returns>
|
|
protected override HttpRequestMessage CreateRequest(HttpMethod httpMethod, string relativeUrl, HttpClientProxyParameterCollection parameters, object data)
|
|
{
|
|
//add parameters
|
|
parameters.Add(new HttpClientProxyParameter(nameof(this.Context.UserId), this.Context.UserId));
|
|
|
|
var requestMessage = base.CreateRequest(httpMethod, relativeUrl, parameters, data);
|
|
|
|
//add header values
|
|
requestMessage.Headers.Add(ApiKey, this.Context.ApiKey);
|
|
requestMessage.Headers.Add(nameof(this.Context.InstituteCode), this.Context.InstituteCode);
|
|
requestMessage.Headers.Add(nameof(this.Context.SchoolYearId), this.Context.SchoolYearId.ToString());
|
|
requestMessage.Headers.Add(nameof(this.Context.UserId), this.Context.UserId.ToString());
|
|
|
|
requestMessage.Headers.Add(nameof(this.Context.UserRoles), this.Context.UserRoles != null ? string.Join(",", this.Context.UserRoles) : string.Empty);
|
|
|
|
string userAgentHeaderKey = requestMessage.Headers.FirstOrDefault(header => header.Key?.Equals(Constants.Http.Headers.UserAgent, StringComparison.OrdinalIgnoreCase) ?? false).Key;
|
|
|
|
if (userAgentHeaderKey != null)
|
|
{
|
|
requestMessage.Headers.Remove(userAgentHeaderKey);
|
|
}
|
|
|
|
requestMessage.Headers.TryAddWithoutValidation(Constants.Http.Headers.UserAgent, this.Context.UserAgent ?? string.Empty);
|
|
|
|
return requestMessage;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="Service"/> class.
|
|
/// </summary>
|
|
/// <param name="invoker">Message invoker</param>
|
|
/// <param name="relativeUrl">The relative URL.</param>
|
|
public Service(IHttpMessageInvokerFactory invokerFactory, string relativeUrl) :
|
|
base(invokerFactory, string.Empty)
|
|
{
|
|
this.RelativeUrl = relativeUrl ?? throw new ArgumentNullException(nameof(relativeUrl));
|
|
}
|
|
}
|
|
}
|