forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpResultExtensions.cs
More file actions
80 lines (71 loc) · 2.81 KB
/
HttpResultExtensions.cs
File metadata and controls
80 lines (71 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using ServiceStack.Common.Utils;
using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface.ServiceModel;
namespace ServiceStack.Common.Web
{
public static class HttpResultExtensions
{
/// <summary>
/// Shortcut to get the ResponseDTO whether it's bare or inside a IHttpResult
/// </summary>
/// <param name="response"></param>
/// <returns></returns>
public static object ToDto(this object response)
{
if (response == null) return null;
var httpResult = response as IHttpResult;
return httpResult != null ? httpResult.Response : response;
}
/// <summary>
/// Alias of ToDto
/// </summary>
public static object ToResponseDto(this object response)
{
return ToDto(response);
}
/// <summary>
/// Shortcut to get the ResponseDTO whether it's bare or inside a IHttpResult
/// </summary>
/// <param name="response"></param>
/// <returns>TResponse if found; otherwise null</returns>
public static TResponse ToDto<TResponse>(this object response) where TResponse : class
{
if (response == null) return default(TResponse);
var httpResult = response as IHttpResult;
return (httpResult != null ? httpResult.Response : response) as TResponse;
}
/// <summary>
/// Alias of ToDto
/// </summary>
public static TResponse ToResponseDto<TResponse>(this object response) where TResponse : class
{
return ToDto<TResponse>(response);
}
/// <summary>
/// Shortcut to get the ResponseStatus whether it's bare or inside a IHttpResult
/// </summary>
/// <param name="response"></param>
/// <returns></returns>
public static ResponseStatus ToResponseStatus(this object response)
{
if (response == null) return null;
var hasResponseStatus = response as IHasResponseStatus;
if (hasResponseStatus != null)
return hasResponseStatus.ResponseStatus;
var propertyInfo = response.GetType().GetProperty("ResponseStatus");
if (propertyInfo == null)
return null;
return ReflectionUtils.GetProperty(response, propertyInfo) as ResponseStatus;
}
/// <summary>
/// Whether the response is an IHttpError or Exception
/// </summary>
/// <param name="response"></param>
/// <returns></returns>
public static bool IsErrorResponse(this object response)
{
return response != null && (response is IHttpError || response is Exception);
}
}
}