public void Should_return_null_in_json_format() { var nullResponse = formatter.AsJson <Person>(null); using (var stream = new MemoryStream()) { nullResponse.Contents(stream); Encoding.UTF8.GetString(stream.ToArray()).ShouldEqual("null"); } }
public static Response JsonError(this IResponseFormatter responseFormatter, string message, HttpStatusCode statusCode) { return(responseFormatter.AsJson(message == null ? null : new { error = message }, statusCode)); }
public static Response CreateResponse(this Exception e, ResponseFormat format, IResponseFormatter formatter) { // dumb ugliness b/c MSFT's xml serializer can't handle anonymous objects var exception = new FormattedException { name = e.GetType().Name, message = e.Message, callStack = e.StackTrace }; Response r; switch (format) { case ResponseFormat.Json: r = formatter.AsJson(exception, HttpStatusCode.InternalServerError); break; case ResponseFormat.Xml: r = formatter.AsXml(exception); break; default: r = null; break; } if (r != null) { r.StatusCode = HttpStatusCode.InternalServerError; } return r; }
public static Response AsOData <T>(this IResponseFormatter formatter, IEnumerable <T> modelItems, HttpStatusCode code = HttpStatusCode.OK) { // BCR only supports json, no need to supply and check the $format every time.... int totalCount = 0; return(formatter.AsJson(formatter.Context.ApplyODataUriFilter(modelItems, ref totalCount), code)); }
public static Response DeleteUser(IResponseFormatter responseFormatter, UserIdentifier user) { UserIdentifier storedUser = Global.NapackStorageManager.GetUser(user.Email); IEnumerable <string> authorizedPackages = Global.NapackStorageManager.GetAuthorizedPackages(storedUser.Email); List <string> orphanedPackages = new List <string>(); foreach (string authorizedPackage in authorizedPackages) { NapackMetadata metadata = Global.NapackStorageManager.GetPackageMetadata(authorizedPackage, true); metadata.AuthorizedUserIds.Remove(storedUser.Email); if (metadata.AuthorizedUserIds.Any()) { orphanedPackages.Add(authorizedPackage); } Global.NapackStorageManager.UpdatePackageMetadata(metadata); } Global.NapackStorageManager.RemoveUser(user); return(responseFormatter.AsJson(new { NapacksDeauthenticatedFrom = authorizedPackages, OrphanedPackages = orphanedPackages, }, HttpStatusCode.Gone)); }
private static Response FormatResponse(this IResponseFormatter formatter, string responseCode, string errorMessage) { return(formatter.AsJson(new FailedResponse { Code = responseCode, Message = errorMessage })); }
/// <summary> /// Takes a user object and encodes it as a new JWT session token. /// </summary> public static Response WithSessionToken(this IResponseFormatter response, User user, int expirationDays = 30) { var token = new SessionToken(user, expirationDays); // Never return the token's ShopifyAccessToken to the client token.ShopifyAccessToken = null; return(response.AsJson(token).WithCookie("Rustwrench_Auth", token.token)); }
public Response Generate(IResponseFormatter response, ErrorCode code) { var error = Errors[code]; return response.AsJson(new { ErrorCode = (int)code, ErrorMessage = error.Message }) .WithStatusCode(error.StatusCode); }
public static Response WithContent <T>(this IResponseFormatter formatter, IEnumerable <Tuple <string, decimal> > acceptHeaders, T content) { var xmlWeight = CalculateWeightForContentType(acceptHeaders, "xml"); var jsonWeight = CalculateWeightForContentType(acceptHeaders, "json"); if (jsonWeight > xmlWeight) { return(formatter.AsJson(content)); } else { return(formatter.AsXml(content)); } }
public static Response AsOData <T>(this IResponseFormatter formatter, IEnumerable <T> modelItems, HttpStatusCode code = HttpStatusCode.OK) { bool isJson = formatter.Context.Request.Headers.Accept.Select(x => x.Item1).Any(x => x.StartsWith("application/json", StringComparison.InvariantCultureIgnoreCase)); var nv = ParseUriOptions(formatter.Context); string value = nv.Get("$format"); if ("json".Equals(value, StringComparison.OrdinalIgnoreCase)) { isJson = true; } if (isJson) { return(formatter.AsJson(formatter.Context.ApplyODataUriFilter(modelItems), code)); } throw new NotImplementedException("Atom feeds not implemented"); }
public static Response AutomapJson <T>(this IResponseFormatter response, object model) { return(response.AsJson(Mapper.Map <T>(model))); }
/// <summary> /// AsError /// </summary> /// <param name="response"></param> /// <param name="message"></param> /// <returns></returns> public static Response AsErrorJson(this IResponseFormatter response, string message) { var serverResponse = ResponseProvider.Error(message); return(response.AsJson(serverResponse)); }
public Response OnBrokenRequest(NancyContext context, Request request, IResponseFormatter formatter, ICollection<JsonSchemaMessage> violations) { return formatter.AsJson(new { }, HttpStatusCode.BadRequest); }
public static Response AsBadRequest(this IResponseFormatter response, string errormsg) { return(response.AsJson(errormsg).WithStatusCode(HttpStatusCode.BadRequest)); }
/// <summary> /// AsSuccess /// </summary> /// <typeparam name="T"></typeparam> /// <param name="response"></param> /// <param name="data"></param> /// <returns></returns> public static Response AsSuccessJson <T>(this IResponseFormatter response, T data) where T : class { var serverResponse = ResponseProvider.Success(data); return(response.AsJson(serverResponse)); }
/// <summary> /// AsError /// </summary> /// <typeparam name="T"></typeparam> /// <param name="response"></param> /// <param name="data"></param> /// <param name="message"></param> /// <returns></returns> public static Response AsErrorJson <T>(this IResponseFormatter response, T data, string message) where T : class { var serverResponse = ResponseProvider.Error(data, message); return(response.AsJson(serverResponse)); }
public static Response AsErrorResponse(this IResponseFormatter source, ErrorResponse error, string redirectUri = null) { return(string.IsNullOrEmpty(redirectUri) ? source.AsJson(error, HttpStatusCode.BadRequest) : source.AsRedirect(string.Concat(redirectUri, error.AsQueryString()))); }
/// <summary> /// Takes a Nancy response + error message and turns it into a JSON error message. /// </summary> public static Response AsJsonError(this IResponseFormatter response, string message, HttpStatusCode statusCode, object details = null) { return(response.AsJson(new { message = message, details }, statusCode)); }