-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
HttpItemsExtensions.cs
97 lines (79 loc) · 3.81 KB
/
HttpItemsExtensions.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
using Ocelot.Configuration;
using Ocelot.DownstreamRouteFinder.UrlMatcher;
using Ocelot.Errors;
using Ocelot.Request.Middleware;
namespace Ocelot.Middleware
{
public static class HttpItemsExtensions
{
public static void UpsertDownstreamRequest(this IDictionary<object, object> input, DownstreamRequest downstreamRequest)
{
input.Upsert("DownstreamRequest", downstreamRequest);
}
public static void UpsertDownstreamResponse(this IDictionary<object, object> input, DownstreamResponse downstreamResponse)
{
input.Upsert("DownstreamResponse", downstreamResponse);
}
public static void UpsertDownstreamRoute(this IDictionary<object, object> input, DownstreamRoute downstreamRoute)
{
input.Upsert("DownstreamRoute", downstreamRoute);
}
public static void UpsertTemplatePlaceholderNameAndValues(this IDictionary<object, object> input, List<PlaceholderNameAndValue> tPNV)
{
input.Upsert("TemplatePlaceholderNameAndValues", tPNV);
}
public static void UpsertDownstreamRoute(this IDictionary<object, object> input, DownstreamRouteFinder.DownstreamRouteHolder downstreamRoute)
{
input.Upsert("DownstreamRouteHolder", downstreamRoute);
}
public static void UpsertErrors(this IDictionary<object, object> input, List<Error> errors)
{
input.Upsert("Errors", errors);
}
public static void SetError(this IDictionary<object, object> input, Error error)
{
var errors = new List<Error> { error };
input.Upsert("Errors", errors);
}
public static void SetIInternalConfiguration(this IDictionary<object, object> input, IInternalConfiguration config)
{
input.Upsert("IInternalConfiguration", config);
}
public static IInternalConfiguration IInternalConfiguration(this IDictionary<object, object> input)
{
return input.Get<IInternalConfiguration>("IInternalConfiguration");
}
public static List<Error> Errors(this IDictionary<object, object> input)
{
var errors = input.Get<List<Error>>("Errors");
return errors ?? new List<Error>();
}
public static DownstreamRouteFinder.DownstreamRouteHolder
DownstreamRouteHolder(this IDictionary<object, object> input) =>
input.Get<DownstreamRouteFinder.DownstreamRouteHolder>("DownstreamRouteHolder");
public static List<PlaceholderNameAndValue>
TemplatePlaceholderNameAndValues(this IDictionary<object, object> input) =>
input.Get<List<PlaceholderNameAndValue>>("TemplatePlaceholderNameAndValues");
public static DownstreamRequest DownstreamRequest(this IDictionary<object, object> input) =>
input.Get<DownstreamRequest>("DownstreamRequest");
public static DownstreamResponse DownstreamResponse(this IDictionary<object, object> input) =>
input.Get<DownstreamResponse>("DownstreamResponse");
public static DownstreamRoute DownstreamRoute(this IDictionary<object, object> input) =>
input.Get<DownstreamRoute>("DownstreamRoute");
private static T Get<T>(this IDictionary<object, object> input, string key) =>
input.TryGetValue(key, out var value) ? (T)value : default;
private static void Upsert<T>(this IDictionary<object, object> input, string key, T value)
{
if (input.DoesntExist(key))
{
input.Add(key, value);
}
else
{
input.Remove(key);
input.Add(key, value);
}
}
private static bool DoesntExist(this IDictionary<object, object> input, string key) => !input.ContainsKey(key);
}
}