RpcManualRouteProvider.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using EdjCase.JsonRpc.Router.Abstractions;
  2. using Microsoft.Extensions.Options;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace EdjCase.JsonRpc.Router.RouteProviders
  9. {
  10. public class RpcManualRouteProvider : IRpcRouteProvider
  11. {
  12. private IOptions<RpcManualRoutingOptions> Options { get; }
  13. public RpcManualRouteProvider(IOptions<RpcManualRoutingOptions> options)
  14. {
  15. this.Options = options ?? throw new ArgumentNullException(nameof(options));
  16. }
  17. public RpcPath BaseRequestPath => this.Options.Value.BaseRequestPath;
  18. /// <summary>
  19. /// Gets all the routes from all the cofiguration
  20. /// </summary>
  21. /// <returns>All the available routes</returns>
  22. public HashSet<RpcPath> GetRoutes()
  23. {
  24. if(this.Options.Value.Routes?.Keys != null)
  25. {
  26. return new HashSet<RpcPath>(this.Options.Value.Routes.Keys);
  27. }
  28. return new HashSet<RpcPath>();
  29. }
  30. /// <summary>
  31. /// Gets all the method providers for the specified path
  32. /// </summary>
  33. /// <param name="path">Path to the methods</param>
  34. /// <returns>All method providers for the specified path</returns>
  35. public List<IRpcMethodProvider> GetMethodsByPath(RpcPath path)
  36. {
  37. if(this.Options.Value.Routes == null
  38. || !this.Options.Value.Routes.TryGetValue(path, out List<IRpcMethodProvider> methods))
  39. {
  40. return new List<IRpcMethodProvider>();
  41. }
  42. return methods;
  43. }
  44. }
  45. }