SwaggerService.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 = "Emby Server API",
  126. version = "1.0.0",
  127. description = "Explore the Emby Server API",
  128. contact = new SwaggerConcactInfo
  129. {
  130. name = "Emby Developer Community",
  131. url = "https://emby.media/community/index.php?/forum/47-developer-api"
  132. },
  133. termsOfService = "https://emby.media/terms"
  134. },
  135. paths = GetPaths(),
  136. definitions = GetDefinitions(),
  137. basePath = "/emby",
  138. host = host,
  139. components = new SwaggerComponents
  140. {
  141. securitySchemes = securitySchemes
  142. }
  143. };
  144. return spec;
  145. }
  146. private SwaggerTag[] GetTags()
  147. {
  148. return new SwaggerTag[] { };
  149. }
  150. private Dictionary<string, SwaggerDefinition> GetDefinitions()
  151. {
  152. return new Dictionary<string, SwaggerDefinition>();
  153. }
  154. private IDictionary<string, Dictionary<string, SwaggerMethod>> GetPaths()
  155. {
  156. var paths = new SortedDictionary<string, Dictionary<string, SwaggerMethod>>();
  157. var all = ServiceController.Instance.RestPathMap.OrderBy(i => i.Key, StringComparer.OrdinalIgnoreCase).ToList();
  158. foreach (var current in all)
  159. {
  160. foreach (var info in current.Value)
  161. {
  162. if (info.IsHidden)
  163. {
  164. continue;
  165. }
  166. if (info.Path.StartsWith("/mediabrowser", StringComparison.OrdinalIgnoreCase))
  167. {
  168. continue;
  169. }
  170. if (info.Path.StartsWith("/emby", StringComparison.OrdinalIgnoreCase))
  171. {
  172. continue;
  173. }
  174. paths[info.Path] = GetPathInfo(info);
  175. }
  176. }
  177. return paths;
  178. }
  179. private Dictionary<string, SwaggerMethod> GetPathInfo(RestPath info)
  180. {
  181. var result = new Dictionary<string, SwaggerMethod>();
  182. foreach (var verb in info.Verbs)
  183. {
  184. var responses = new Dictionary<string, SwaggerResponse>
  185. {
  186. };
  187. responses["200"] = new SwaggerResponse
  188. {
  189. description = "OK"
  190. };
  191. var security = new List<Dictionary<string, string[]>>();
  192. var apiKeySecurity = new Dictionary<string, string[]>();
  193. apiKeySecurity["api_key"] = new string[] { };
  194. security.Add(apiKeySecurity);
  195. result[verb.ToLower()] = new SwaggerMethod
  196. {
  197. summary = info.Summary,
  198. description = info.Description,
  199. produces = new[]
  200. {
  201. "application/json"
  202. },
  203. consumes = new[]
  204. {
  205. "application/json"
  206. },
  207. operationId = info.RequestType.Name,
  208. tags = new string[] { },
  209. parameters = new SwaggerParam[] { },
  210. responses = responses,
  211. security = security.ToArray()
  212. };
  213. }
  214. return result;
  215. }
  216. }
  217. }