Files
duplicati/Duplicati/Server/WebServer/RESTMethods/Help.cs
T

101 lines
3.5 KiB
C#
Raw Normal View History

2015-08-05 13:09:01 +02:00
// Copyright (C) 2015, The Duplicati Team
2015-07-04 13:42:52 +02:00
// http://www.duplicati.com, info@duplicati.com
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2018-05-27 13:27:24 -07:00
using System;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
2015-07-04 13:42:52 +02:00
namespace Duplicati.Server.WebServer.RESTMethods
{
public class Help : IRESTMethodGET
2018-05-27 13:27:24 -07:00
{
public void GET(string key, RequestInfo info)
{
var sb = new StringBuilder();
if (string.IsNullOrWhiteSpace(key))
{
foreach(var m in RESTHandler.Modules.Keys.OrderBy(x => x))
{
var mod = RESTHandler.Modules[m];
if (mod == this)
continue;
var desc = mod.GetType().Name;
2019-09-29 20:16:28 -07:00
if (mod is IRESTMethodDocumented documented)
desc = documented.Description;
2018-05-27 13:27:24 -07:00
sb.AppendFormat(ITEM_TEMPLATE, RESTHandler.API_URI_PATH, m, mod.GetType().Name, desc);
}
2018-05-27 13:17:30 -07:00
var data = Encoding.UTF8.GetBytes(string.Format(TEMPLATE, "API Information", "", sb));
2018-05-27 13:27:24 -07:00
info.Response.ContentType = "text/html";
info.Response.ContentLength = data.Length;
info.Response.Body.Write(data, 0, data.Length);
info.Response.Send();
}
else
{
IRESTMethod m;
RESTHandler.Modules.TryGetValue(key, out m);
if (m == null)
{
info.Response.Status = System.Net.HttpStatusCode.NotFound;
info.Response.Reason = "Module not found";
}
else
{
var desc = "";
2019-09-29 20:16:28 -07:00
if (m is IRESTMethodDocumented doc)
2018-05-27 13:27:24 -07:00
{
desc = doc.Description;
foreach(var t in doc.Types)
sb.AppendFormat(METHOD_TEMPLATE, t.Key, JsonConvert.SerializeObject(t.Value)); //TODO: Format the type
}
2018-05-27 13:17:30 -07:00
var data = Encoding.UTF8.GetBytes(string.Format(TEMPLATE, m.GetType().Name, desc, sb));
2018-05-27 13:27:24 -07:00
info.Response.ContentType = "text/html";
info.Response.ContentLength = data.Length;
info.Response.Body.Write(data, 0, data.Length);
info.Response.Send();
}
}
}
private const string TEMPLATE = @"
<html><head><title>{0}</title></head>
<body>
<h1>{0}</h1>
<p>{1}</p>
<ul>
{2}
</ul>
</body>
";
private const string ITEM_TEMPLATE = @"
<li><a href=""{0}/help/{1}"">{2}</a>: {3}</li>
";
private const string METHOD_TEMPLATE = @"
<b>{0}:</b><br><code>{1}</code><br>
";
}
2015-07-04 13:42:52 +02:00
}