2019-02-23 22:39:11 +00:00
|
|
|
using System;
|
2013-06-28 19:19:40 +00:00
|
|
|
using System.Collections.Generic;
|
2013-04-27 02:03:34 +00:00
|
|
|
using System.IO;
|
2013-01-19 01:27:30 +00:00
|
|
|
using Nancy;
|
|
|
|
using Nancy.Responses;
|
2013-08-10 19:58:30 +00:00
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2013-05-12 15:18:17 +00:00
|
|
|
using NzbDrone.Common.Serializer;
|
2013-01-19 01:27:30 +00:00
|
|
|
|
2017-09-04 02:20:56 +00:00
|
|
|
namespace Lidarr.Http.Extensions
|
2013-01-19 01:27:30 +00:00
|
|
|
{
|
2013-08-10 19:58:30 +00:00
|
|
|
public static class ReqResExtensions
|
2013-01-19 01:27:30 +00:00
|
|
|
{
|
2013-05-13 02:52:55 +00:00
|
|
|
private static readonly NancyJsonSerializer NancySerializer = new NancyJsonSerializer();
|
2021-03-07 22:49:41 +00:00
|
|
|
private static readonly string Expires = DateTime.UtcNow.AddYears(1).ToString("r");
|
2013-04-17 00:24:49 +00:00
|
|
|
|
2013-08-10 19:58:30 +00:00
|
|
|
public static readonly string LastModified = BuildInfo.BuildDateTime.ToString("r");
|
|
|
|
|
2020-01-03 12:49:24 +00:00
|
|
|
public static T FromJson<T>(this Stream body)
|
|
|
|
where T : class, new()
|
2013-04-27 02:03:34 +00:00
|
|
|
{
|
|
|
|
return FromJson<T>(body, typeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static T FromJson<T>(this Stream body, Type type)
|
2013-05-21 03:20:29 +00:00
|
|
|
{
|
|
|
|
return (T)FromJson(body, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static object FromJson(this Stream body, Type type)
|
2013-01-19 01:27:30 +00:00
|
|
|
{
|
|
|
|
var reader = new StreamReader(body, true);
|
|
|
|
body.Position = 0;
|
|
|
|
var value = reader.ReadToEnd();
|
2013-05-21 03:20:29 +00:00
|
|
|
return Json.Deserialize(value, type);
|
2013-01-19 01:27:30 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 20:32:51 +00:00
|
|
|
public static JsonResponse<TModel> AsResponse<TModel>(this TModel model, NancyContext context, HttpStatusCode statusCode = HttpStatusCode.OK)
|
2013-01-19 01:27:30 +00:00
|
|
|
{
|
2019-09-12 20:32:51 +00:00
|
|
|
var response = new JsonResponse<TModel>(model, NancySerializer, context.Environment) { StatusCode = statusCode };
|
2013-07-25 21:57:11 +00:00
|
|
|
response.Headers.DisableCache();
|
|
|
|
|
|
|
|
return response;
|
2013-01-19 01:27:30 +00:00
|
|
|
}
|
2013-06-28 19:19:40 +00:00
|
|
|
|
|
|
|
public static IDictionary<string, string> DisableCache(this IDictionary<string, string> headers)
|
|
|
|
{
|
2019-02-23 22:39:11 +00:00
|
|
|
headers["Cache-Control"] = "no-cache, no-store, must-revalidate, max-age=0";
|
2013-07-25 21:57:11 +00:00
|
|
|
headers["Pragma"] = "no-cache";
|
|
|
|
headers["Expires"] = "0";
|
2013-06-28 19:19:40 +00:00
|
|
|
|
|
|
|
return headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static IDictionary<string, string> EnableCache(this IDictionary<string, string> headers)
|
|
|
|
{
|
2021-03-07 22:49:41 +00:00
|
|
|
headers["Cache-Control"] = "max-age=31536000, public";
|
|
|
|
headers["Expires"] = Expires;
|
2013-08-10 19:58:30 +00:00
|
|
|
headers["Last-Modified"] = LastModified;
|
2013-07-25 21:57:11 +00:00
|
|
|
headers["Age"] = "193266";
|
2013-06-28 19:19:40 +00:00
|
|
|
|
|
|
|
return headers;
|
|
|
|
}
|
2013-01-19 01:27:30 +00:00
|
|
|
}
|
2019-02-23 22:39:11 +00:00
|
|
|
}
|