-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathODataHelpers.cs
111 lines (96 loc) · 4.01 KB
/
ODataHelpers.cs
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using Microsoft.OData.Core.UriParser;
using System;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Web;
using System.Web.Http;
using System.Web.Http.Routing;
using System.Web.OData;
using System.Web.OData.Extensions;
using System.Web.OData.Routing;
namespace AirVinyl.API.Helpers
{
/// <summary>
/// OData Helper methods - slightly adjusted from OData helpers provided by Microsoft
/// </summary>
public static class ODataHelpers
{
public static bool HasProperty(this object instance, string propertyName)
{
var propertyInfo = instance.GetType().GetProperty(propertyName);
return (propertyInfo != null);
}
public static object GetValue(this object instance, string propertyName)
{
var propertyInfo = instance.GetType().GetProperty(propertyName);
if (propertyInfo == null)
{
throw new HttpException("Can't find property with name " + propertyName);
}
var propertyValue = propertyInfo.GetValue(instance, new object[] { });
return propertyValue;
}
public static IHttpActionResult CreateOKHttpActionResult(this ODataController controller, object propertyValue)
{
var okMethod = default(MethodInfo);
// find the ok method on the current controller
var methods = controller.GetType().GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);
foreach (var method in methods)
{
if (method.Name == "Ok" && method.GetParameters().Length == 1)
{
okMethod = method;
break;
}
}
// invoke the method, passing in the propertyValue
okMethod = okMethod.MakeGenericMethod(propertyValue.GetType());
var returnValue = okMethod.Invoke(controller, new object[] { propertyValue });
return (IHttpActionResult)returnValue;
}
/// <summary>
/// Helper method to get the odata path for an arbitrary odata uri.
/// </summary>
/// <param name="request">The request instance in current context</param>
/// <param name="uri">OData uri</param>
/// <returns>The parsed odata path</returns>
public static ODataPath CreateODataPath(this HttpRequestMessage request, Uri uri)
{
if (uri == null)
{
throw new ArgumentNullException("uri");
}
var newRequest = new HttpRequestMessage(HttpMethod.Get, uri);
var route = request.GetRouteData().Route;
var newRoute = new HttpRoute(
route.RouteTemplate,
new HttpRouteValueDictionary(route.Defaults),
new HttpRouteValueDictionary(route.Constraints),
new HttpRouteValueDictionary(route.DataTokens),
route.Handler);
var routeData = newRoute.GetRouteData(request.GetConfiguration().VirtualPathRoot, newRequest);
if (routeData == null)
{
throw new InvalidOperationException("This link is not a valid OData link.");
}
return newRequest.ODataProperties().Path;
}
public static TKey GetKeyValue<TKey>(this HttpRequestMessage request, Uri uri)
{
if (uri == null)
{
throw new ArgumentNullException("uri");
}
//get the odata path Ex: ~/entityset/key/$links/navigation
var odataPath = request.CreateODataPath(uri);
var keySegment = odataPath.Segments.OfType<KeyValuePathSegment>().LastOrDefault();
if (keySegment == null)
{
throw new InvalidOperationException("This link does not contain a key.");
}
var value = ODataUriUtils.ConvertFromUriLiteral(keySegment.Value, Microsoft.OData.Core.ODataVersion.V4);
return (TKey)value;
}
}
}