# Conflicts: # BeWo/BeWo.csproj # BeWo/BeWoApp.xaml.cs # BeWo/ServiceProxy/GeneratedAiEnhancedService.cs # BeWo/Services/BeWoControlFactory.cs # BeWo/Services/BeWoWindowFactory.cs # BeWo/Services/BeWoWindowService.cs # BeWo/Services/IControlFactory.cs # BeWo/Services/IWindowFactory.cs # BeWo/Services/IWindowService.cs # BeWo/View/BeWoFileView.xaml # BeWo/View/Detail/Report/AbwesenheitsView.xaml # BeWo/View/Windows/AnimatedBeWoWindow.xaml.cs # BeWo/app.config # BeWoAllReports.sln # BeWoPlanerMobil.sln # BeWoPlanerMobil/.vs/BeWoPlanerMobil.csproj.dtbcache.json # BeWoTests.sln # Dakota/DakotaUtils.cs # Dakota/Logic/DakotaInfoCreator.cs # Dakota/Models/DakotaFile.cs # Data/Access/GenericDAO.cs # Data/Access/SearchDAO.cs # Data/Data.csproj # Host/Web.config # Server/Components/ServerUtils/ApiFacade/WebClientFacade.cs # Service/BeWoServiceEnums.cs # Service/Core/ServiceHelper.cs # Service/Core/ServiceValidator.cs # Service/Extensions/ServiceEnumExtension.cs # Service/Service.csproj # Service/ServiceContracts/Enhanced/IAiEnhancedService.cs # Service/ServiceContracts/Enhanced/IOperationsEnhancedService.cs # Service/ServiceImplementations/EmployeeServiceImp.cs # Service/ServiceImplementations/Enhanced/AiEnhancedServiceImp.cs # Service/ServiceImplementations/Enhanced/OperationsEnhancedServiceImp.cs # Service/ServiceProxy/OpenWebUIFacade.cs # Service/ServiceProxy/ServiceFacade.cs # Service/ServiceUtils/DistanceCalculator/GoogleDistanceMatrixAPI.cs # Shared/BeWoEntityEnums.cs # Shared/Core/AppError.cs # Shared/Core/Facade/WebClientFacade.cs # Shared/Core/WebClientFacade.cs # Shared/Shared.csproj
219 lines
5.0 KiB
C#
219 lines
5.0 KiB
C#
using BS.Shared.Core;
|
|
using DevExpress.XtraRichEdit.Import.Html;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BS.Shared.DataContracts
|
|
{
|
|
public class ApiResponse<T>
|
|
{
|
|
public T Data { get; set; }
|
|
|
|
public List<AppError> Errors { get; set; }
|
|
|
|
public List<string> ErrorStrings { get; set; }
|
|
|
|
public List<string> Messages { get; set; }
|
|
|
|
public Dictionary<string, object> Meta { get; set; }
|
|
|
|
public string TraceId { get; set; }
|
|
|
|
public DateTime Timestamp { get; set; } = DateTime.Now;
|
|
|
|
public int StatusCode { get; set; } = 200;
|
|
|
|
public bool Success => StatusCode == 200 && (Errors?.Count ?? 0) == 0 && (ErrorStrings?.Count ?? 0) == 0;
|
|
|
|
public ApiResponse()
|
|
{
|
|
|
|
}
|
|
|
|
public static void CopyTo<Tnew>(ApiResponse<T> from, ApiResponse<Tnew> to)
|
|
{
|
|
to.Errors = from.Errors;
|
|
to.ErrorStrings = from.ErrorStrings;
|
|
to.Messages = from.Messages;
|
|
to.Meta = from.Meta;
|
|
to.TraceId = from.TraceId;
|
|
to.Timestamp = from.Timestamp;
|
|
to.StatusCode = from.StatusCode;
|
|
}
|
|
|
|
//Factory Methods
|
|
public static ApiResponse<T> SuccessResponse(T data, List<string> messages = null, Dictionary<string, object> meta = null)
|
|
{
|
|
return new ApiResponse<T>
|
|
{
|
|
Data = data,
|
|
Messages = messages ?? new List<string>(),
|
|
Meta = meta ?? new Dictionary<string, object>(),
|
|
};
|
|
}
|
|
|
|
public static ApiResponse<T> FailureResponse(List<AppError> errors, int statusCode = 400, string traceId = null)
|
|
{
|
|
return new ApiResponse<T>
|
|
{
|
|
Errors = errors,
|
|
StatusCode = statusCode,
|
|
TraceId = traceId
|
|
};
|
|
}
|
|
|
|
public static ApiResponse<T> FailureResponse(AppError error, int statusCode = 400, string traceId = null)
|
|
{
|
|
return new ApiResponse<T>
|
|
{
|
|
Errors = new List<AppError> { error },
|
|
StatusCode = statusCode,
|
|
TraceId = traceId
|
|
};
|
|
}
|
|
|
|
public static ApiResponse<T> FailureResponse(AppError error, string error2, int statusCode = 400, string traceId = null)
|
|
{
|
|
return new ApiResponse<T>
|
|
{
|
|
Errors = new List<AppError> { error },
|
|
ErrorStrings = new List<string> { error2 },
|
|
StatusCode = statusCode,
|
|
TraceId = traceId
|
|
};
|
|
}
|
|
|
|
public static ApiResponse<T> FailureResponse(AppError error, List<string> errors, int statusCode = 400, string traceId = null)
|
|
{
|
|
return new ApiResponse<T>
|
|
{
|
|
Errors = new List<AppError> { error },
|
|
ErrorStrings = errors,
|
|
StatusCode = statusCode,
|
|
TraceId = traceId
|
|
};
|
|
}
|
|
|
|
public static ApiResponse<T> FailureResponse(List<string> errors, int statusCode = 400, string traceId = null)
|
|
{
|
|
return new ApiResponse<T>
|
|
{
|
|
ErrorStrings = errors,
|
|
StatusCode = statusCode,
|
|
TraceId = traceId
|
|
};
|
|
}
|
|
|
|
public static ApiResponse<T> FailureResponse(string error, int statusCode = 400, string traceId = null)
|
|
{
|
|
return new ApiResponse<T>
|
|
{
|
|
ErrorStrings = new List<string> { error },
|
|
StatusCode = statusCode,
|
|
TraceId = traceId
|
|
};
|
|
}
|
|
|
|
//Parse Methods
|
|
public static ApiResponse<T> Parse(object obj)
|
|
{
|
|
var response = new ApiResponse<T>();
|
|
|
|
if (typeof(T) == typeof(byte[]))
|
|
{
|
|
response.Data = (T)obj;
|
|
|
|
var response_string = Encoding.UTF8.GetString(obj as byte[]);
|
|
|
|
if (response_string.StartsWith("%"))
|
|
return response;
|
|
|
|
var kv = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(response_string);
|
|
|
|
if (kv is object && kv.TryGetValue("errors", out var errors))
|
|
{
|
|
response.ErrorStrings = errors;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
throw new NotImplementedException("typeof(T) not implemented!");
|
|
}
|
|
|
|
return response;
|
|
}
|
|
|
|
//public static ApiResponse<T> Parse(HttpResponseMessage http_response, IApiResponseParser parser, IApiErrorExtractor extractor)
|
|
//{
|
|
// var responseBytes = http_response.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult();
|
|
|
|
// var response = Parse(responseBytes);
|
|
|
|
// response.Data = (T)(object)responseBytes;
|
|
// response.StatusCode = (int)http_response.StatusCode;
|
|
|
|
|
|
// if (!http_response.IsSuccessStatusCode)
|
|
// {
|
|
|
|
// TryParseErrors(response, responseBytes);
|
|
// }
|
|
|
|
// return response;
|
|
//}
|
|
|
|
//public static bool TryParseErrors(ApiResponse<T> response, byte[] responseBytes)
|
|
//{
|
|
// try
|
|
// {
|
|
// //var responseBytes
|
|
// //var errors = JsonConvert.DeserializeObject<Dictionary<string, string>>()
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// return false;
|
|
// }
|
|
|
|
// return true;
|
|
//}
|
|
|
|
public ApiResponse<Tnew> Copy<Tnew>()
|
|
{
|
|
var response = new ApiResponse<Tnew>();
|
|
|
|
CopyTo(this, response);
|
|
|
|
return response;
|
|
}
|
|
|
|
public string ToErrorString()
|
|
{
|
|
var sb = new StringBuilder();
|
|
|
|
if (Errors is object && Errors.Any())
|
|
{
|
|
foreach (var appError in Errors)
|
|
{
|
|
sb.AppendLine(appError.Displayname);
|
|
}
|
|
}
|
|
|
|
if (ErrorStrings is object && ErrorStrings.Any())
|
|
{
|
|
foreach (var error in ErrorStrings)
|
|
{
|
|
sb.AppendLine(error);
|
|
}
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
}
|