jellyfin-server/Emby.Server.Implementations/Services/SwaggerService.cs

288 lines
7.7 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
2019-10-25 10:47:20 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
2020-03-24 15:12:06 +00:00
using Emby.Server.Implementations.HttpServer;
2019-03-26 18:20:40 +00:00
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.Services;
namespace Emby.Server.Implementations.Services
{
[Route("/swagger", "GET", Summary = "Gets the swagger specifications")]
[Route("/swagger.json", "GET", Summary = "Gets the swagger specifications")]
public class GetSwaggerSpec : IReturn<SwaggerSpec>
{
}
public class SwaggerSpec
{
public string swagger { get; set; }
2020-06-15 21:43:52 +00:00
public string[] schemes { get; set; }
2020-06-15 21:43:52 +00:00
public SwaggerInfo info { get; set; }
2020-06-15 21:43:52 +00:00
public string host { get; set; }
2020-06-15 21:43:52 +00:00
public string basePath { get; set; }
2020-06-15 21:43:52 +00:00
public SwaggerTag[] tags { get; set; }
2020-06-15 21:43:52 +00:00
public IDictionary<string, Dictionary<string, SwaggerMethod>> paths { get; set; }
2020-06-15 21:43:52 +00:00
public Dictionary<string, SwaggerDefinition> definitions { get; set; }
2020-06-15 21:43:52 +00:00
public SwaggerComponents components { get; set; }
}
public class SwaggerComponents
{
public Dictionary<string, SwaggerSecurityScheme> securitySchemes { get; set; }
}
public class SwaggerSecurityScheme
{
public string name { get; set; }
2020-06-15 21:43:52 +00:00
public string type { get; set; }
2020-06-15 21:43:52 +00:00
public string @in { get; set; }
}
public class SwaggerInfo
{
public string description { get; set; }
2020-06-15 21:43:52 +00:00
public string version { get; set; }
2020-06-15 21:43:52 +00:00
public string title { get; set; }
2020-06-15 21:43:52 +00:00
public string termsOfService { get; set; }
public SwaggerConcactInfo contact { get; set; }
}
public class SwaggerConcactInfo
{
public string email { get; set; }
2020-06-15 21:43:52 +00:00
public string name { get; set; }
2020-06-15 21:43:52 +00:00
public string url { get; set; }
}
public class SwaggerTag
{
public string description { get; set; }
2020-06-15 21:43:52 +00:00
public string name { get; set; }
}
public class SwaggerMethod
{
public string summary { get; set; }
2020-06-15 21:43:52 +00:00
public string description { get; set; }
2020-06-15 21:43:52 +00:00
public string[] tags { get; set; }
2020-06-15 21:43:52 +00:00
public string operationId { get; set; }
2020-06-15 21:43:52 +00:00
public string[] consumes { get; set; }
2020-06-15 21:43:52 +00:00
public string[] produces { get; set; }
2020-06-15 21:43:52 +00:00
public SwaggerParam[] parameters { get; set; }
2020-06-15 21:43:52 +00:00
public Dictionary<string, SwaggerResponse> responses { get; set; }
2020-06-15 21:43:52 +00:00
public Dictionary<string, string[]>[] security { get; set; }
}
public class SwaggerParam
{
public string @in { get; set; }
2020-06-15 21:43:52 +00:00
public string name { get; set; }
2020-06-15 21:43:52 +00:00
public string description { get; set; }
2020-06-15 21:43:52 +00:00
public bool required { get; set; }
2020-06-15 21:43:52 +00:00
public string type { get; set; }
2020-06-15 21:43:52 +00:00
public string collectionFormat { get; set; }
}
public class SwaggerResponse
{
public string description { get; set; }
// ex. "$ref":"#/definitions/Pet"
public Dictionary<string, string> schema { get; set; }
}
public class SwaggerDefinition
{
public string type { get; set; }
2020-06-15 21:43:52 +00:00
public Dictionary<string, SwaggerProperty> properties { get; set; }
}
public class SwaggerProperty
{
public string type { get; set; }
2020-06-15 21:43:52 +00:00
public string format { get; set; }
2020-06-15 21:43:52 +00:00
public string description { get; set; }
2020-06-15 21:43:52 +00:00
public string[] @enum { get; set; }
2020-06-15 21:43:52 +00:00
public string @default { get; set; }
}
public class SwaggerService : IService, IRequiresRequest
{
2019-03-26 18:20:40 +00:00
private readonly IHttpServer _httpServer;
private SwaggerSpec _spec;
public IRequest Request { get; set; }
2019-03-26 18:20:40 +00:00
public SwaggerService(IHttpServer httpServer)
{
_httpServer = httpServer;
}
public object Get(GetSwaggerSpec request)
{
return _spec ?? (_spec = GetSpec());
}
private SwaggerSpec GetSpec()
{
string host = null;
Uri uri;
if (Uri.TryCreate(Request.RawUrl, UriKind.Absolute, out uri))
{
host = uri.Host;
}
var securitySchemes = new Dictionary<string, SwaggerSecurityScheme>();
securitySchemes["api_key"] = new SwaggerSecurityScheme
{
name = "api_key",
type = "apiKey",
@in = "query"
};
var spec = new SwaggerSpec
{
schemes = new[] { "http" },
tags = GetTags(),
swagger = "2.0",
info = new SwaggerInfo
{
2019-01-18 05:55:33 +00:00
title = "Jellyfin Server API",
version = "1.0.0",
2019-01-18 05:55:33 +00:00
description = "Explore the Jellyfin Server API",
contact = new SwaggerConcactInfo
{
2019-01-18 05:55:33 +00:00
name = "Jellyfin Community",
url = "https://jellyfin.readthedocs.io/en/latest/user-docs/getting-help/"
}
},
paths = GetPaths(),
definitions = GetDefinitions(),
2019-01-18 05:55:33 +00:00
basePath = "/jellyfin",
host = host,
components = new SwaggerComponents
{
securitySchemes = securitySchemes
}
};
return spec;
}
private SwaggerTag[] GetTags()
{
2019-10-25 10:47:20 +00:00
return Array.Empty<SwaggerTag>();
}
private Dictionary<string, SwaggerDefinition> GetDefinitions()
{
return new Dictionary<string, SwaggerDefinition>();
}
private IDictionary<string, Dictionary<string, SwaggerMethod>> GetPaths()
{
var paths = new SortedDictionary<string, Dictionary<string, SwaggerMethod>>();
2019-03-26 18:20:40 +00:00
// REVIEW: this can be done better
var all = ((HttpListenerHost)_httpServer).ServiceController.RestPathMap.OrderBy(i => i.Key, StringComparer.OrdinalIgnoreCase).ToList();
foreach (var current in all)
{
foreach (var info in current.Value)
{
if (info.IsHidden)
{
continue;
}
2019-03-26 18:20:40 +00:00
if (info.Path.StartsWith("/mediabrowser", StringComparison.OrdinalIgnoreCase)
|| info.Path.StartsWith("/jellyfin", StringComparison.OrdinalIgnoreCase))
{
continue;
}
paths[info.Path] = GetPathInfo(info);
}
}
return paths;
}
private Dictionary<string, SwaggerMethod> GetPathInfo(RestPath info)
{
var result = new Dictionary<string, SwaggerMethod>();
foreach (var verb in info.Verbs)
{
var responses = new Dictionary<string, SwaggerResponse>
{
2019-01-27 11:03:43 +00:00
{ "200", new SwaggerResponse { description = "OK" } }
};
2019-01-27 11:03:43 +00:00
var apiKeySecurity = new Dictionary<string, string[]>
{
2019-01-27 11:03:43 +00:00
{ "api_key", Array.Empty<string>() }
};
2019-01-27 11:03:43 +00:00
result[verb.ToLowerInvariant()] = new SwaggerMethod
{
summary = info.Summary,
description = info.Description,
2019-01-27 11:03:43 +00:00
produces = new[] { "application/json" },
consumes = new[] { "application/json" },
operationId = info.RequestType.Name,
tags = Array.Empty<string>(),
2019-01-27 11:03:43 +00:00
parameters = Array.Empty<SwaggerParam>(),
responses = responses,
2020-03-24 15:12:06 +00:00
security = new[] { apiKeySecurity }
};
}
return result;
}
}
}