Skip to content

Instantly share code, notes, and snippets.

@Kashkovsky
Last active June 9, 2016 16:06
Show Gist options
  • Save Kashkovsky/731a5ebd0d15b58a2bb16376a1388e75 to your computer and use it in GitHub Desktop.
Save Kashkovsky/731a5ebd0d15b58a2bb16376a1388e75 to your computer and use it in GitHub Desktop.
Web Api HttpResponseResult
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Web.Http.ModelBinding;
using Microsoft.AspNet.Identity;
namespace Framework.Utility
{
public class HttpResponseResult<TResult> where TResult : class
{
public static Task<HttpResponseMessage> CreateAsync(
HttpRequestMessage request,
Expression<Func<Task<TResult>>> method,
ModelStateDictionary modelState = null, object response = null)
{
var args = GetTaskArguments(method);
if (modelState != null && !modelState.IsValid)
return Task.Factory.StartNew(() =>
request.CreateResponse(
HttpStatusCode.BadRequest,
CreateErrorResult(method.Compile().GetMethodInfo(), args, null, response)));
return Task.Factory.StartNew(method.Compile())
.ContinueWith((result) =>
GenerateResponseMessage(request, result.Result, method.Compile(), args, response));
}
public static HttpResponseMessage Create(
HttpRequestMessage request, Expression<Func<TResult>> method,
ModelStateDictionary modelState = null, object message = null)
{
var args = GetMethodArguments(method);
if (modelState != null && !modelState.IsValid)
return request.CreateResponse(
HttpStatusCode.BadRequest,
CreateErrorResult(method.Compile().GetMethodInfo(), args, null, message));
return GenerateResponseMessage(request, method.Compile(), args, message);
}
private static HttpResponseMessage GenerateResponseMessage(
HttpRequestMessage request,
Task<TResult> task,
Func<Task<TResult>> method,
Dictionary<string, object> args,
object message)
{
var isSuccess = false;
var result = task.Result;
if (result is IdentityResult)
isSuccess = (result as IdentityResult).Succeeded;
if (typeof(TResult).IsAssignableFrom(typeof(ServiceResponse)))
{
var response = result as ServiceResponse;
if (response != null) isSuccess = response.IsSuccess;
}
return isSuccess
? request.CreateResponse(HttpStatusCode.OK)
: request.CreateResponse(HttpStatusCode.BadRequest,
CreateErrorResult(method.GetMethodInfo(), args, result, message));
}
private static HttpResponseMessage GenerateResponseMessage(
HttpRequestMessage request,
Func<TResult> method,
Dictionary<string, object> args,
object message)
{
var isSuccess = false;
var result = method();
if (result is IdentityResult)
isSuccess = (result as IdentityResult).Succeeded;
if (typeof(TResult).IsAssignableFrom(typeof(ServiceResponse)))
{
var response = result as ServiceResponse;
if (response != null) isSuccess = response.IsSuccess;
}
return isSuccess
? request.CreateResponse(HttpStatusCode.OK, message)
: request.CreateResponse(HttpStatusCode.BadRequest,
CreateErrorResult(method.GetMethodInfo(), args, result, message));
}
private static ErrorResult CreateErrorResult(
MethodInfo methodInfo,
Dictionary<string, object> args,
TResult result, object response)
{
return new ErrorResult
{
Process = methodInfo.Name,
Params = args,
ResultObject = result,
Response = response
};
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static string GetCurrentMethod()
{
StackTrace st = new StackTrace();
StackFrame sf = st.GetFrame(1);
return sf.GetMethod().Name;
}
private static Dictionary<string, object> GetTaskArguments(
Expression<Func<Task<TResult>>> taskExpression)
{
var taskCall = taskExpression?.Body as MethodCallExpression;
return GetArguments(taskCall);
}
private static Dictionary<string, object> GetMethodArguments(
Expression<Func<TResult>> methodExpression)
{
var taskCall = methodExpression?.Body as MethodCallExpression;
return GetArguments(taskCall);
}
private static Dictionary<string, object> GetArguments(MethodCallExpression exp)
{
var result = new Dictionary<string, object>();
var argsExpression = exp?.Arguments;
if (argsExpression != null)
{
for (var i = 0; i < argsExpression.Count; i++)
{
var lambda = Expression.Lambda<Func<object>>(exp.Arguments[i]);
var value = lambda.Compile()();
var arg = exp.Arguments[i] as MemberExpression;
if (arg != null)
{
result.Add(arg.Member.Name, value);
}
}
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment