SwaggerService.cs 6.3 KB

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