using EdjCase.JsonRpc.Router.Abstractions;
using EdjCase.JsonRpc.Router.Defaults;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace EdjCase.JsonRpc.Router
{
public abstract class RpcController
{
///
/// Helper for returning a successful rpc response
///
/// Object to return in response
/// Success result for rpc response
public virtual RpcMethodSuccessResult Ok(object obj = null)
{
return new RpcMethodSuccessResult(obj);
}
///
/// Helper for returning an error rpc response
///
/// JSON-RPC custom error code
/// (Optional)Error message
/// (Optional)Error data
///
public virtual RpcMethodErrorResult Error(int errorCode, string message = null, object data = null)
{
return new RpcMethodErrorResult(errorCode, message, data);
}
}
public abstract class RpcErrorFilterAttribute : Attribute
{
public abstract OnExceptionResult OnException(RpcRouteInfo routeInfo, Exception ex);
}
public class OnExceptionResult
{
public bool ThrowException { get; }
public object ResponseObject { get; }
private OnExceptionResult(bool throwException, object responseObject)
{
this.ThrowException = throwException;
this.ResponseObject = responseObject;
}
public static OnExceptionResult UseObjectResponse(object responseObject)
{
return new OnExceptionResult(false, responseObject);
}
public static OnExceptionResult UseMethodResultResponse(IRpcMethodResult result)
{
return new OnExceptionResult(false, result);
}
public static OnExceptionResult UseExceptionResponse(Exception ex)
{
return new OnExceptionResult(true, ex);
}
public static OnExceptionResult DontHandle()
{
return new OnExceptionResult(true, null);
}
}
#if !NETSTANDARD1_3
///
/// Attribute to decorate a derived class
///
public class RpcRouteAttribute : Attribute
{
///
/// Name of the route to be used in the router. If unspecified, will use controller name.
///
public string RouteName { get; }
///
///
///
/// (Optional) Name of the route to be used in the router. If unspecified, will use controller name.
/// (Optional) Name of the group the route is in to allow route filtering per request.
public RpcRouteAttribute(string routeName = null)
{
this.RouteName = routeName?.Trim();
}
}
#endif
}