DocSpace-buildtools/common/ASC.Api.Core/Middleware/CommonApiResponse.cs

112 lines
3.0 KiB
C#
Raw Normal View History

2019-05-30 09:28:21 +00:00
using System;
using System.Collections;
2019-06-13 12:12:21 +00:00
using System.Collections.Generic;
2019-08-15 12:04:42 +00:00
using System.Linq;
2019-05-30 09:28:21 +00:00
using System.Net;
using System.Runtime.Serialization;
namespace ASC.Api.Core.Middleware
{
[DataContract]
2019-05-30 14:57:15 +00:00
public abstract class CommonApiResponse
2019-05-30 09:28:21 +00:00
{
2019-05-30 15:21:30 +00:00
[DataMember(Order = 1)]
2019-05-30 09:28:21 +00:00
public int Status { get; set; }
2019-05-30 15:21:30 +00:00
[DataMember(Order = 2)]
2019-05-30 09:28:21 +00:00
public HttpStatusCode StatusCode { get; set; }
2019-05-30 14:57:15 +00:00
protected CommonApiResponse(HttpStatusCode statusCode)
{
StatusCode = statusCode;
}
2019-05-30 09:28:21 +00:00
2019-05-30 14:57:15 +00:00
public static SuccessApiResponse Create(HttpStatusCode statusCode, object response)
{
return new SuccessApiResponse(statusCode, response);
}
2019-05-30 09:28:21 +00:00
2019-05-30 14:57:15 +00:00
public static ErrorApiResponse CreateError(HttpStatusCode statusCode, Exception error)
2019-05-30 09:28:21 +00:00
{
2019-05-30 14:57:15 +00:00
return new ErrorApiResponse(statusCode, error);
2019-05-30 09:28:21 +00:00
}
2019-05-30 14:57:15 +00:00
}
2019-05-30 09:28:21 +00:00
2019-05-30 14:57:15 +00:00
[DataContract]
public class ErrorApiResponse : CommonApiResponse
{
2019-05-30 15:21:30 +00:00
[DataMember(EmitDefaultValue = false, Order = 3)]
2019-05-30 14:57:15 +00:00
public CommonApiError Error { get; set; }
protected internal ErrorApiResponse(HttpStatusCode statusCode, Exception error) : base(statusCode)
2019-05-30 09:28:21 +00:00
{
2019-05-30 14:57:15 +00:00
Status = 1;
Error = CommonApiError.FromException(error);
2019-05-30 09:28:21 +00:00
}
2019-05-30 14:57:15 +00:00
}
2019-05-30 09:28:21 +00:00
2019-05-30 14:57:15 +00:00
[DataContract]
public class SuccessApiResponse : CommonApiResponse
{
2019-05-30 15:21:30 +00:00
[DataMember(EmitDefaultValue = false, Order = 0)]
2019-05-30 14:57:15 +00:00
public int? Count { get; set; }
2019-07-29 10:51:14 +00:00
[DataMember(EmitDefaultValue = false, Order = 1)]
public long? Total { get; set; }
2019-05-30 15:21:30 +00:00
[DataMember(EmitDefaultValue = false, Order = 3)]
2019-05-30 14:57:15 +00:00
public object Response { get; set; }
2019-07-29 10:51:14 +00:00
protected internal SuccessApiResponse(HttpStatusCode statusCode, object response, long? total = null) : base(statusCode)
2019-05-30 09:28:21 +00:00
{
2019-05-30 14:57:15 +00:00
Status = 0;
Response = response;
2019-07-29 10:51:14 +00:00
Total = total;
2019-06-13 12:12:21 +00:00
if (response is IEnumerable<object> collection)
{
Count = collection.Count();
}
2019-08-15 12:04:42 +00:00
else if (response == null)
2019-06-13 12:12:21 +00:00
{
Count = 0;
}
else
{
Count = 1;
}
2019-05-30 09:28:21 +00:00
}
}
[DataContract]
public class CommonApiError
{
[DataMember]
public string Message { get; set; }
[DataMember]
public Type Type { get; set; }
[DataMember]
public string Stack { get; set; }
[DataMember]
public int Hresult { get; set; }
[DataMember]
public IDictionary Data { get; set; }
public static CommonApiError FromException(Exception exception)
{
return new CommonApiError()
{
Message = exception.Message,
Type = exception.GetType(),
Stack = exception.StackTrace,
Hresult = exception.HResult,
Data = exception.Data
};
}
}
}