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