2
0

SwaggerService.cs 7.4 KB

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