Files
spark/src/client/spark_excel/utils/APIDispatch.cs
T

134 lines
4.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ExcelDna.Integration;
using bts.api.core;
using bts.excel.extensions;
using Google.Protobuf;
using Google.Protobuf.Reflection;
namespace bts.excel
{
public class APIDispatch
{
public static Lazy<ServiceContext> service_context = new Lazy<ServiceContext>(() => new ServiceContext(), true);
static MethodInfo _locate_meth;
readonly static Dictionary<object, object> _services = new Dictionary<object, object>();
const string descriptor_property_name = "Descriptor";
static MethodInfo locate_method
{
get
{
if (_locate_meth == null)
{
_locate_meth = service_context.Value.GetType().find_method((_) =>
{
return _.Name == nameof(ServiceContext.locate) && _.IsGenericMethod;
})[0];
}
return _locate_meth;
}
}
static public object api_call_internal(Type service_type, string method_name, IMessage parameters)
{
object response = null;
if (service_type != null && method_name != null)
{
object service = locate_service(service_type);
response = invoke_service_call(service, method_name, parameters);
}
return response;
}
//static public object invoke_service_call(object service, Type request_type, IMessage request)
//{
// var sub_method = service.GetType().GetMethods().Where(meth =>
// {
// var par = meth.GetParameters().FirstOrDefault();
// return (par != null && par.ParameterType.IsAssignableFrom(request_type));
// }).FirstOrDefault();
// object subscription = null;
// if(sub_method != null)
// subscription = sub_method.Invoke(service, new[] { request });
// return subscription;
//}
static public object invoke_service_call(object service, string method_name, IMessage request)
{
var sub_method = service.GetType().GetMethod(method_name);
object subscription = null;
if (sub_method != null)
subscription = sub_method.Invoke(service, new[] { request });
return subscription;
}
public static Type find_request_type(Type service_type, string method_name)
{
return service_type?.GetMethod(method_name)?.GetParameters().FirstOrDefault()?.ParameterType;
}
static public IMessage generate_imessage(Type type, string json, bool null_for_empty = false)
{
IMessage request = null;
var descproperty = type?.get_static_field_or_property(descriptor_property_name);
if (descproperty != null)
{
MessageDescriptor desc = (MessageDescriptor)descproperty.get_value(null);
if (null_for_empty)
{
if (!string.IsNullOrEmpty(json) && json != "{ }" && json != "{}")
request = JsonParser.Default.Parse(json, desc);
else
request = null; //TODO this is a workaround for Eric's bug that requires 'empty' requests to pass null instead
}
else
{
if(string.IsNullOrEmpty(json))
json = "{}";
request = JsonParser.Default.Parse(json, desc);
}
}
return request;
}
static public IMessage generate_request(Type service_type, string method_name, string request_json)
{
Type request_type = find_request_type(service_type, method_name);
return generate_imessage(request_type, request_json, true);
}
static public object locate_service(Type service_type)
{
//object svc = null;
//if(service_type != null && !_services.TryGetValue(service_type, out svc))
//{
var method = locate_method?.MakeGenericMethod(service_type);
object svc = method?.Invoke(service_context.Value, new object[] { null });
if (svc != null)
_services[svc] = null;
return svc;
}
[ExcelFunction]
static public object spark_api_call(string service_type, string method_name, object parameters)
{
Type svc = Utilities.find_full_type_in_all_assemblies(service_type, ".*api.*");
var tok = Utilities.get_tokens(parameters);
string json = tok.FirstOrDefault();
IMessage request = generate_request(svc, method_name, json);
object resp = api_call_internal(svc, method_name, request);
if (resp != null)
return DataParser.scatter_data(resp);
else
return ExcelErrorUtil.ToComError(ExcelError.ExcelErrorNA);
}
}
}