public static void ExecuteRequest(Action executeRequestDelegate, RestExceptionHandler restExceptionHandler) { try { executeRequestDelegate.Invoke(); } catch (Exception ex) { if (ex is RestException && restExceptionHandler != null) { restExceptionHandler.Invoke((RestException)ex); } else { throw ex; } } }
public static T ExecuteRequest <T>(Func <T> executeRequestDelegate, RestExceptionHandler restExceptionHandler) where T : new() { T retVal = default(T); try { retVal = executeRequestDelegate.Invoke(); } catch (Exception ex) { if (ex is RestException && restExceptionHandler != null) { restExceptionHandler.Invoke((RestException)ex); } else { throw ex; } } return(retVal); }
public static async void ExecuteRequestAsync(Func <Task> executeRequestDelegate, RestExceptionHandler restExceptionHandler) { try { await executeRequestDelegate.Invoke(); } catch (Exception ex) { if (ex is RestException && restExceptionHandler != null) { restExceptionHandler.Invoke((RestException)ex); } else { throw ex; } } }
public void Init(Assembly runningAssembly, string modulesAssembly) { Type[] typelist = Tools.GetTypesInNamespace(runningAssembly, modulesAssembly); for (int i = 0; i < typelist.Length; i++) { Type tClass = typelist[i]; // Search for REST Attribute Attribute t = tClass.GetCustomAttribute(typeof(REST)); if (t != null) { Logger.Log("RestProcessor", $"Found REST class {tClass.Name}"); REST trest = (REST)t; proxies.Add(tClass.Name, new RestProxy(tClass, injectables)); MethodInfo[] methods = tClass.GetMethods(); foreach (var methodInfo in methods) { foreach (Type rt in RestTypes) { Attribute rta = methodInfo.GetCustomAttribute(rt); if (rta != null) { RestCall restCall = new RestCall(); try { restCall.className = tClass.Name; restCall.methodName = methodInfo.Name; restCall.method = (IHttpMethod)rta; restCall.baseRest = trest; Logger.Log("RestProcessor", $"Registering method {methodInfo.Name} for {restCall.method.Method} {trest.Path}{restCall.method.Path}"); AddEndpoint(restCall); } catch (DuplicateRestMethodException) { Logger.Log("RestProcessor", $"DuplicateRestMethodException: There is already a {restCall.method.Method} {trest.Path}{restCall.method.Path} registered."); } } } } } // Search for RestExceptionHandler Attribute t = tClass.GetCustomAttribute(typeof(RestExceptionHandler)); if (t != null) { Logger.Log("RestProcessor", $"Found a RestExceptionHandler {tClass.Name}"); if (typeof(IRestExceptionHandler).IsAssignableFrom(tClass)) { RestExceptionHandler reh = (RestExceptionHandler)t; if (typeof(Exception).IsAssignableFrom(reh.exceptionType)) { IRestExceptionHandler handler = (IRestExceptionHandler)Activator.CreateInstance(tClass); exceptionHandlers.Add(reh.ExceptionType.Name, handler); Logger.Log("RestProcessor", $" Registered a custom exception handler for exception \"{reh.ExceptionType.Name}\" for class {tClass.Name}"); } else { Logger.Log("RestProcessor", $" Class {tClass.Name} contains the \"RestExceptionHandler\" attribute the passed type does not inherit Exception class. Skipping it."); } } else { Logger.Log("RestProcessor", $" Class {tClass.Name} contains the \"RestExceptionHandler\" attribute but does not implement IRestExceptionHandler. Skipping it."); } } } if (proxies.Count != 0 || endpoints.Keys.Count != 0 || exceptionHandlers.Keys.Count != 0) { Logger.Log("RestProcessor", $"Initialized {proxies.Count} REST proxies."); Logger.Log("RestProcessor", $"Initialized {endpoints.Keys.Count} REST endpoints."); Logger.Log("RestProcessor", $"Initialized {exceptionHandlers.Keys.Count} Custom Exception Handlers"); } }