1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-05 02:22:31 +01:00
Radarr/NzbDrone.Api/Extensions/ReqResExtensions.cs

64 lines
2.0 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using Nancy;
using Nancy.Responses;
using NzbDrone.Common.EnvironmentInfo;
2013-05-12 17:18:17 +02:00
using NzbDrone.Common.Serializer;
2013-02-23 21:35:26 +01:00
namespace NzbDrone.Api.Extensions
{
public static class ReqResExtensions
{
2013-05-13 04:52:55 +02:00
private static readonly NancyJsonSerializer NancySerializer = new NancyJsonSerializer();
public static readonly string LastModified = BuildInfo.BuildDateTime.ToString("r");
public static T FromJson<T>(this Stream body) where T : class, new()
{
return FromJson<T>(body, typeof(T));
}
public static T FromJson<T>(this Stream body, Type type)
2013-05-21 05:20:29 +02:00
{
return (T)FromJson(body, type);
}
public static object FromJson(this Stream body, Type type)
{
var reader = new StreamReader(body, true);
body.Position = 0;
var value = reader.ReadToEnd();
2013-05-21 05:20:29 +02:00
return Json.Deserialize(value, type);
}
public static JsonResponse<TModel> AsResponse<TModel>(this TModel model, HttpStatusCode statusCode = HttpStatusCode.OK)
{
2013-07-25 23:57:11 +02:00
var response = new JsonResponse<TModel>(model, NancySerializer) { StatusCode = statusCode };
response.Headers.DisableCache();
return response;
}
public static IDictionary<string, string> DisableCache(this IDictionary<string, string> headers)
{
2013-07-25 23:57:11 +02:00
headers["Cache-Control"] = "no-cache, no-store, must-revalidate";
headers["Pragma"] = "no-cache";
headers["Expires"] = "0";
return headers;
}
public static IDictionary<string, string> EnableCache(this IDictionary<string, string> headers)
{
2013-07-25 23:57:11 +02:00
headers["Cache-Control"] = "max-age=31536000 , public";
headers["Expires"] = "Sat, 29 Jun 2020 00:00:00 GMT";
headers["Last-Modified"] = LastModified;
2013-07-25 23:57:11 +02:00
headers["Age"] = "193266";
return headers;
}
}
}