SwaggerService.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Model.Services;
  7. namespace Emby.Server.Implementations.Services
  8. {
  9. [Route("/swagger", "GET", Summary = "Gets the swagger specifications")]
  10. [Route("/swagger.json", "GET", Summary = "Gets the swagger specifications")]
  11. public class GetSwaggerSpec : IReturn<SwaggerSpec>
  12. {
  13. }
  14. public class SwaggerSpec
  15. {
  16. public string swagger { get; set; }
  17. public string[] schemes { get; set; }
  18. public SwaggerInfo info { get; set; }
  19. public string host { get; set; }
  20. public string basePath { get; set; }
  21. public SwaggerTag[] tags { get; set; }
  22. public Dictionary<string, Dictionary<string, SwaggerMethod>> paths { get; set; }
  23. public Dictionary<string, SwaggerDefinition> definitions { get; set; }
  24. }
  25. public class SwaggerInfo
  26. {
  27. public string description { get; set; }
  28. public string version { get; set; }
  29. public string title { get; set; }
  30. public SwaggerConcactInfo contact { get; set; }
  31. }
  32. public class SwaggerConcactInfo
  33. {
  34. public string email { get; set; }
  35. }
  36. public class SwaggerTag
  37. {
  38. public string description { get; set; }
  39. public string name { get; set; }
  40. }
  41. public class SwaggerMethod
  42. {
  43. public string summary { get; set; }
  44. public string description { get; set; }
  45. public string[] tags { get; set; }
  46. public string operationId { get; set; }
  47. public string[] consumes { get; set; }
  48. public string[] produces { get; set; }
  49. public SwaggerParam[] parameters { get; set; }
  50. public Dictionary<string, SwaggerResponse> responses { get; set; }
  51. }
  52. public class SwaggerParam
  53. {
  54. public string @in { get; set; }
  55. public string name { get; set; }
  56. public string description { get; set; }
  57. public bool required { get; set; }
  58. public string type { get; set; }
  59. public string collectionFormat { get; set; }
  60. }
  61. public class SwaggerResponse
  62. {
  63. public string description { get; set; }
  64. // ex. "$ref":"#/definitions/Pet"
  65. public Dictionary<string, string> schema { get; set; }
  66. }
  67. public class SwaggerDefinition
  68. {
  69. public string type { get; set; }
  70. public Dictionary<string, SwaggerProperty> properties { get; set; }
  71. }
  72. public class SwaggerProperty
  73. {
  74. public string type { get; set; }
  75. public string format { get; set; }
  76. public string description { get; set; }
  77. public string[] @enum { get; set; }
  78. public string @default { get; set; }
  79. }
  80. public class SwaggerService : IService
  81. {
  82. private SwaggerSpec _spec;
  83. public object Get(GetSwaggerSpec request)
  84. {
  85. return _spec ?? (_spec = GetSpec());
  86. }
  87. private SwaggerSpec GetSpec()
  88. {
  89. var spec = new SwaggerSpec
  90. {
  91. schemes = new[] { "http" },
  92. tags = GetTags(),
  93. swagger = "2.0",
  94. info = new SwaggerInfo
  95. {
  96. title = "Emby Server API",
  97. version = "1",
  98. description = "Explore the Emby Server API",
  99. contact = new SwaggerConcactInfo
  100. {
  101. email = "api@emby.media"
  102. }
  103. },
  104. paths = GetPaths(),
  105. definitions = GetDefinitions()
  106. };
  107. return spec;
  108. }
  109. private SwaggerTag[] GetTags()
  110. {
  111. return new SwaggerTag[] { };
  112. }
  113. private Dictionary<string, SwaggerDefinition> GetDefinitions()
  114. {
  115. return new Dictionary<string, SwaggerDefinition>();
  116. }
  117. private Dictionary<string, Dictionary<string, SwaggerMethod>> GetPaths()
  118. {
  119. var paths = new Dictionary<string, Dictionary<string, SwaggerMethod>>();
  120. var all = ServiceController.Instance.RestPathMap.ToList();
  121. foreach (var current in all)
  122. {
  123. foreach (var info in current.Value)
  124. {
  125. paths[info.Path] = GetPathInfo(info);
  126. }
  127. }
  128. return paths;
  129. }
  130. private Dictionary<string, SwaggerMethod> GetPathInfo(RestPath info)
  131. {
  132. var result = new Dictionary<string, SwaggerMethod>();
  133. foreach (var verb in info.Verbs)
  134. {
  135. result[verb] = new SwaggerMethod
  136. {
  137. summary = info.Summary,
  138. produces = new[]
  139. {
  140. "application/json",
  141. "application/xml"
  142. },
  143. consumes = new[]
  144. {
  145. "application/json",
  146. "application/xml"
  147. },
  148. operationId = info.RequestType.Name,
  149. tags = new string[] { },
  150. parameters = new SwaggerParam[] { }
  151. };
  152. }
  153. return result;
  154. }
  155. }
  156. }