1
0
Fork 0
mirror of https://github.com/lidarr/Lidarr synced 2024-12-24 16:51:58 +00:00
Lidarr/NzbDrone.Api/Extensions/ReqResExtensions.cs

57 lines
1.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using Nancy;
using Nancy.Responses;
2013-05-12 15:18:17 +00:00
using NzbDrone.Common.Serializer;
2013-02-23 20:35:26 +00:00
namespace NzbDrone.Api.Extensions
{
public static class JsonExtensions
{
2013-05-13 02:52:55 +00:00
private static readonly NancyJsonSerializer NancySerializer = new NancyJsonSerializer();
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 03:20:29 +00: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 03:20:29 +00:00
return Json.Deserialize(value, type);
}
public static JsonResponse<TModel> AsResponse<TModel>(this TModel model, HttpStatusCode statusCode = HttpStatusCode.OK)
{
return new JsonResponse<TModel>(model, NancySerializer) { StatusCode = statusCode };
}
public static IDictionary<string, string> DisableCache(this IDictionary<string, string> headers)
{
headers.Add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.Add("Pragma", "no-cache");
headers.Add("Expires", "0");
return headers;
}
public static IDictionary<string, string> EnableCache(this IDictionary<string, string> headers)
{
headers.Add("Cache-Control", "max-age=31536000 , public");
headers.Add("Expires", "Sat, 29 Jun 2020 00:00:00 GMT");
headers.Add("Last-Modified", "Sat, 29 Jun 2000 00:00:00 GMT");
headers.Add("Age", "193266");
return headers;
}
}
}