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

261 lines
7.7 KiB
C#
Raw Normal View History

2017-09-07 18:17:18 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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; }
public string[] schemes { get; set; }
public SwaggerInfo info { get; set; }
public string host { get; set; }
public string basePath { get; set; }
public SwaggerTag[] tags { get; set; }
2017-09-11 19:25:13 +00:00
public IDictionary<string, Dictionary<string, SwaggerMethod>> paths { get; set; }
2017-09-07 18:17:18 +00:00
public Dictionary<string, SwaggerDefinition> definitions { get; set; }
2017-09-16 04:14:10 +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; }
public string type { get; set; }
public string @in { get; set; }
2017-09-07 18:17:18 +00:00
}
public class SwaggerInfo
{
public string description { get; set; }
public string version { get; set; }
public string title { get; set; }
2017-09-09 18:20:48 +00:00
public string termsOfService { get; set; }
2017-09-07 18:17:18 +00:00
public SwaggerConcactInfo contact { get; set; }
}
public class SwaggerConcactInfo
{
public string email { get; set; }
2017-09-16 04:14:10 +00:00
public string name { get; set; }
public string url { get; set; }
2017-09-07 18:17:18 +00:00
}
public class SwaggerTag
{
public string description { get; set; }
public string name { get; set; }
}
public class SwaggerMethod
{
public string summary { get; set; }
public string description { get; set; }
public string[] tags { get; set; }
public string operationId { get; set; }
public string[] consumes { get; set; }
public string[] produces { get; set; }
public SwaggerParam[] parameters { get; set; }
public Dictionary<string, SwaggerResponse> responses { get; set; }
2017-09-16 04:14:10 +00:00
public Dictionary<string, string[]>[] security { get; set; }
2017-09-07 18:17:18 +00:00
}
public class SwaggerParam
{
public string @in { get; set; }
public string name { get; set; }
public string description { get; set; }
public bool required { get; set; }
public string type { get; set; }
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; }
public Dictionary<string, SwaggerProperty> properties { get; set; }
}
public class SwaggerProperty
{
public string type { get; set; }
public string format { get; set; }
public string description { get; set; }
public string[] @enum { get; set; }
public string @default { get; set; }
}
2017-09-09 18:20:48 +00:00
public class SwaggerService : IService, IRequiresRequest
2017-09-07 18:17:18 +00:00
{
private SwaggerSpec _spec;
2017-09-09 18:20:48 +00:00
public IRequest Request { get; set; }
2017-09-07 18:17:18 +00:00
public object Get(GetSwaggerSpec request)
{
return _spec ?? (_spec = GetSpec());
}
private SwaggerSpec GetSpec()
{
2017-09-09 18:20:48 +00:00
string host = null;
Uri uri;
if (Uri.TryCreate(Request.RawUrl, UriKind.Absolute, out uri))
{
host = uri.Host;
}
2017-09-16 04:14:10 +00:00
var securitySchemes = new Dictionary<string, SwaggerSecurityScheme>();
securitySchemes["api_key"] = new SwaggerSecurityScheme
{
name = "api_key",
type = "apiKey",
@in = "query"
};
2017-09-07 18:17:18 +00:00
var spec = new SwaggerSpec
{
schemes = new[] { "http" },
tags = GetTags(),
swagger = "2.0",
info = new SwaggerInfo
{
title = "Emby Server API",
2017-09-09 18:20:48 +00:00
version = "1.0.0",
2017-09-07 18:17:18 +00:00
description = "Explore the Emby Server API",
contact = new SwaggerConcactInfo
{
2017-09-16 04:14:10 +00:00
name = "Emby Developer Community",
url = "https://emby.media/community/index.php?/forum/47-developer-api"
2017-09-09 18:20:48 +00:00
},
termsOfService = "https://emby.media/terms"
2017-09-07 18:17:18 +00:00
},
paths = GetPaths(),
2017-09-09 18:20:48 +00:00
definitions = GetDefinitions(),
basePath = "/emby",
2017-09-16 04:14:10 +00:00
host = host,
components = new SwaggerComponents
{
securitySchemes = securitySchemes
}
2017-09-07 18:17:18 +00:00
};
return spec;
}
private SwaggerTag[] GetTags()
{
return new SwaggerTag[] { };
}
private Dictionary<string, SwaggerDefinition> GetDefinitions()
{
return new Dictionary<string, SwaggerDefinition>();
}
2017-09-11 19:25:13 +00:00
private IDictionary<string, Dictionary<string, SwaggerMethod>> GetPaths()
2017-09-07 18:17:18 +00:00
{
2017-09-11 19:25:13 +00:00
var paths = new SortedDictionary<string, Dictionary<string, SwaggerMethod>>();
2017-09-07 18:17:18 +00:00
2017-09-11 19:25:13 +00:00
var all = ServiceController.Instance.RestPathMap.OrderBy(i => i.Key, StringComparer.OrdinalIgnoreCase).ToList();
2017-09-07 18:17:18 +00:00
foreach (var current in all)
{
foreach (var info in current.Value)
{
2017-09-11 19:25:13 +00:00
if (info.IsHidden)
{
continue;
}
2017-09-09 18:20:48 +00:00
if (info.Path.StartsWith("/mediabrowser", StringComparison.OrdinalIgnoreCase))
{
continue;
}
if (info.Path.StartsWith("/emby", StringComparison.OrdinalIgnoreCase))
{
continue;
}
2017-09-07 18:17:18 +00:00
paths[info.Path] = GetPathInfo(info);
}
}
return paths;
}
private Dictionary<string, SwaggerMethod> GetPathInfo(RestPath info)
{
var result = new Dictionary<string, SwaggerMethod>();
2017-09-09 18:20:48 +00:00
2017-09-07 18:17:18 +00:00
foreach (var verb in info.Verbs)
{
2017-09-09 18:20:48 +00:00
var responses = new Dictionary<string, SwaggerResponse>
{
};
responses["200"] = new SwaggerResponse
{
description = "OK"
};
2017-09-16 04:14:10 +00:00
var security = new List<Dictionary<string, string[]>>();
var apiKeySecurity = new Dictionary<string, string[]>();
apiKeySecurity["api_key"] = new string[] { };
security.Add(apiKeySecurity);
2017-09-09 18:20:48 +00:00
result[verb.ToLower()] = new SwaggerMethod
2017-09-07 18:17:18 +00:00
{
summary = info.Summary,
2017-09-11 19:25:13 +00:00
description = info.Description,
2017-09-07 18:17:18 +00:00
produces = new[]
{
2017-09-10 03:18:23 +00:00
"application/json"
2017-09-07 18:17:18 +00:00
},
consumes = new[]
{
2017-09-10 03:18:23 +00:00
"application/json"
2017-09-07 18:17:18 +00:00
},
operationId = info.RequestType.Name,
tags = new string[] { },
2017-09-09 18:20:48 +00:00
parameters = new SwaggerParam[] { },
2017-09-16 04:14:10 +00:00
responses = responses,
security = security.ToArray()
2017-09-07 18:17:18 +00:00
};
}
return result;
}
}
}