SwaggerService.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Emby.Server.Implementations.HttpServer;
  6. using MediaBrowser.Controller.Net;
  7. using MediaBrowser.Model.Services;
  8. namespace Emby.Server.Implementations.Services
  9. {
  10. [Route("/swagger", "GET", Summary = "Gets the swagger specifications")]
  11. [Route("/swagger.json", "GET", Summary = "Gets the swagger specifications")]
  12. public class GetSwaggerSpec : IReturn<SwaggerSpec>
  13. {
  14. }
  15. public class SwaggerSpec
  16. {
  17. public string swagger { get; set; }
  18. public string[] schemes { get; set; }
  19. public SwaggerInfo info { get; set; }
  20. public string host { get; set; }
  21. public string basePath { get; set; }
  22. public SwaggerTag[] tags { get; set; }
  23. public IDictionary<string, Dictionary<string, SwaggerMethod>> paths { get; set; }
  24. public Dictionary<string, SwaggerDefinition> definitions { get; set; }
  25. public SwaggerComponents components { get; set; }
  26. }
  27. public class SwaggerComponents
  28. {
  29. public Dictionary<string, SwaggerSecurityScheme> securitySchemes { get; set; }
  30. }
  31. public class SwaggerSecurityScheme
  32. {
  33. public string name { get; set; }
  34. public string type { get; set; }
  35. public string @in { get; set; }
  36. }
  37. public class SwaggerInfo
  38. {
  39. public string description { get; set; }
  40. public string version { get; set; }
  41. public string title { get; set; }
  42. public string termsOfService { get; set; }
  43. public SwaggerConcactInfo contact { get; set; }
  44. }
  45. public class SwaggerConcactInfo
  46. {
  47. public string email { get; set; }
  48. public string name { get; set; }
  49. public string url { get; set; }
  50. }
  51. public class SwaggerTag
  52. {
  53. public string description { get; set; }
  54. public string name { get; set; }
  55. }
  56. public class SwaggerMethod
  57. {
  58. public string summary { get; set; }
  59. public string description { get; set; }
  60. public string[] tags { get; set; }
  61. public string operationId { get; set; }
  62. public string[] consumes { get; set; }
  63. public string[] produces { get; set; }
  64. public SwaggerParam[] parameters { get; set; }
  65. public Dictionary<string, SwaggerResponse> responses { get; set; }
  66. public Dictionary<string, string[]>[] security { get; set; }
  67. }
  68. public class SwaggerParam
  69. {
  70. public string @in { get; set; }
  71. public string name { get; set; }
  72. public string description { get; set; }
  73. public bool required { get; set; }
  74. public string type { get; set; }
  75. public string collectionFormat { get; set; }
  76. }
  77. public class SwaggerResponse
  78. {
  79. public string description { get; set; }
  80. // ex. "$ref":"#/definitions/Pet"
  81. public Dictionary<string, string> schema { get; set; }
  82. }
  83. public class SwaggerDefinition
  84. {
  85. public string type { get; set; }
  86. public Dictionary<string, SwaggerProperty> properties { get; set; }
  87. }
  88. public class SwaggerProperty
  89. {
  90. public string type { get; set; }
  91. public string format { get; set; }
  92. public string description { get; set; }
  93. public string[] @enum { get; set; }
  94. public string @default { get; set; }
  95. }
  96. public class SwaggerService : IService, IRequiresRequest
  97. {
  98. private readonly IHttpServer _httpServer;
  99. private SwaggerSpec _spec;
  100. public IRequest Request { get; set; }
  101. public SwaggerService(IHttpServer httpServer)
  102. {
  103. _httpServer = httpServer;
  104. }
  105. public object Get(GetSwaggerSpec request)
  106. {
  107. return _spec ?? (_spec = GetSpec());
  108. }
  109. private SwaggerSpec GetSpec()
  110. {
  111. string host = null;
  112. Uri uri;
  113. if (Uri.TryCreate(Request.RawUrl, UriKind.Absolute, out uri))
  114. {
  115. host = uri.Host;
  116. }
  117. var securitySchemes = new Dictionary<string, SwaggerSecurityScheme>();
  118. securitySchemes["api_key"] = new SwaggerSecurityScheme
  119. {
  120. name = "api_key",
  121. type = "apiKey",
  122. @in = "query"
  123. };
  124. var spec = new SwaggerSpec
  125. {
  126. schemes = new[] { "http" },
  127. tags = GetTags(),
  128. swagger = "2.0",
  129. info = new SwaggerInfo
  130. {
  131. title = "Jellyfin Server API",
  132. version = "1.0.0",
  133. description = "Explore the Jellyfin Server API",
  134. contact = new SwaggerConcactInfo
  135. {
  136. name = "Jellyfin Community",
  137. url = "https://jellyfin.readthedocs.io/en/latest/user-docs/getting-help/"
  138. }
  139. },
  140. paths = GetPaths(),
  141. definitions = GetDefinitions(),
  142. basePath = "/jellyfin",
  143. host = host,
  144. components = new SwaggerComponents
  145. {
  146. securitySchemes = securitySchemes
  147. }
  148. };
  149. return spec;
  150. }
  151. private SwaggerTag[] GetTags()
  152. {
  153. return Array.Empty<SwaggerTag>();
  154. }
  155. private Dictionary<string, SwaggerDefinition> GetDefinitions()
  156. {
  157. return new Dictionary<string, SwaggerDefinition>();
  158. }
  159. private IDictionary<string, Dictionary<string, SwaggerMethod>> GetPaths()
  160. {
  161. var paths = new SortedDictionary<string, Dictionary<string, SwaggerMethod>>();
  162. // REVIEW: this can be done better
  163. var all = ((HttpListenerHost)_httpServer).ServiceController.RestPathMap.OrderBy(i => i.Key, StringComparer.OrdinalIgnoreCase).ToList();
  164. foreach (var current in all)
  165. {
  166. foreach (var info in current.Value)
  167. {
  168. if (info.IsHidden)
  169. {
  170. continue;
  171. }
  172. if (info.Path.StartsWith("/mediabrowser", StringComparison.OrdinalIgnoreCase)
  173. || info.Path.StartsWith("/jellyfin", StringComparison.OrdinalIgnoreCase))
  174. {
  175. continue;
  176. }
  177. paths[info.Path] = GetPathInfo(info);
  178. }
  179. }
  180. return paths;
  181. }
  182. private Dictionary<string, SwaggerMethod> GetPathInfo(RestPath info)
  183. {
  184. var result = new Dictionary<string, SwaggerMethod>();
  185. foreach (var verb in info.Verbs)
  186. {
  187. var responses = new Dictionary<string, SwaggerResponse>
  188. {
  189. { "200", new SwaggerResponse { description = "OK" } }
  190. };
  191. var apiKeySecurity = new Dictionary<string, string[]>
  192. {
  193. { "api_key", Array.Empty<string>() }
  194. };
  195. result[verb.ToLowerInvariant()] = new SwaggerMethod
  196. {
  197. summary = info.Summary,
  198. description = info.Description,
  199. produces = new[] { "application/json" },
  200. consumes = new[] { "application/json" },
  201. operationId = info.RequestType.Name,
  202. tags = Array.Empty<string>(),
  203. parameters = Array.Empty<SwaggerParam>(),
  204. responses = responses,
  205. security = new[] { apiKeySecurity }
  206. };
  207. }
  208. return result;
  209. }
  210. }
  211. }