/// <summary> /// Sets the auth handler for a specific method, and sets the EnableHandlers bit flag appropriately /// </summary> /// <param name="method">Name of the method. Must only contain alpha-numeric characters.</param> /// <param name="authHandler">Auth method handler.</param> /// <returns>This instance.</returns> public DynamicHandler AuthMethod(string method, Func <IAuthRequest, Task> authHandler) { if (!ResService.IsValidPart(method)) { throw new ArgumentException("Invalid method name: " + method); } if (authHandler == null) { if (authMethods != null) { authMethods.Remove(method); if (authMethods.Count == 0) { authMethods = null; } } } else { if (authMethods == null) { authMethods = new Dictionary <string, Func <IAuthRequest, Task> >(); } else if (authMethods.ContainsKey(method)) { throw new InvalidOperationException(String.Format("Auth method {0} already set.", method)); } authMethods[method] = authHandler; } toggleHandlers(HandlerTypes.Auth, this.authHandler != null || authMethods != null); return(this); }
/// <summary> /// Sends a custom event on the resource with payload. /// Throws an exception if the event is one of the pre-defined or reserved events, /// "change", "delete", "add", "remove", "patch", "reaccess", "unsubscribe", or "query". /// For pre-defined events, the matching method, ChangeEvent, AddEvent, /// RemoveEvent, or ReaccessEvent should be used instead. /// </summary> /// <remarks> /// See the protocol specification for more information: /// https://github.com/resgateio/resgate/blob/master/docs/res-service-protocol.md#custom-event /// </remarks> /// <param name="eventName">Name of the event.</param> /// <param name="payload">JSON serializable payload. May be null.</param> public async Task EventAsync(string eventName, object payload) { switch (eventName) { case "change": throw new ArgumentException("Use ChangeEvent to send change events"); case "delete": throw new ArgumentException("Use DeleteEvent to send delete events"); case "add": throw new ArgumentException("Use AddEvent to send add events"); case "remove": throw new ArgumentException("Use RemoveEvent to send remove events"); case "patch": throw new ArgumentException("Reserved event name: \"patch\""); case "reaccess": throw new ArgumentException("Use ReaccessEvent to send reaccess events"); case "unsubscribe": throw new ArgumentException("Reserved event name: \"unsubscribe\""); case "query": throw new ArgumentException("Reserved event name: \"query\""); } if (!ResService.IsValidPart(eventName)) { throw new ArgumentException("Invalid event name: " + eventName); } var ev = new CustomEventArgs(eventName, payload); await Handler.Apply(this, ev); sendEvent(eventName, payload); eventHandler?.Invoke(this, ev); }