-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathTypedRequestExtensions.RequestUri.cs
142 lines (127 loc) · 5.17 KB
/
TypedRequestExtensions.RequestUri.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
namespace KubeClient.Http
{
using Utilities;
/// <summary>
/// <see cref="HttpRequest{TContext}"/> / <see cref="IHttpRequest{TContext}"/> extension methods for request URIs.
/// </summary>
public static partial class TypedRequestExtensions
{
/// <summary>
/// Create a copy of the request with the specified base URI.
/// </summary>
/// <typeparam name="TContext">
/// The type of object used as a context for resolving deferred parameters.
/// </typeparam>
/// <param name="request">
/// The request.
/// </param>
/// <param name="baseUri">
/// The request base URI (must be absolute).
/// </param>
/// <returns>
/// The new <see cref="HttpRequest{TContext}"/>.
/// </returns>
/// <exception cref="InvalidOperationException">
/// The request already has an absolute URI.
/// </exception>
public static HttpRequest<TContext> WithBaseUri<TContext>(this HttpRequest<TContext> request, Uri baseUri)
{
if (request == null)
throw new ArgumentNullException(nameof(request));
if (baseUri == null)
throw new ArgumentNullException(nameof(baseUri));
if (!baseUri.IsAbsoluteUri)
throw new ArgumentException("The supplied base URI is not an absolute URI.", nameof(baseUri));
if (request.Uri.IsAbsoluteUri)
throw new InvalidOperationException("The request already has an absolute URI.");
return request.Clone(properties =>
{
properties.SetUri(
baseUri.AppendRelativeUri(request.Uri)
);
});
}
/// <summary>
/// Create a copy of the request with the specified request URI.
/// </summary>
/// <typeparam name="TContext">
/// The type of object used as a context for resolving deferred parameters.
/// </typeparam>
/// <param name="request">
/// The request.
/// </param>
/// <param name="requestUri">
/// The new request URI.
///
/// Must be an absolute URI (otherwise, use <see cref="WithRelativeUri{TContext}(HttpRequest{TContext}, Uri)"/>).
/// </param>
/// <returns>
/// The new <see cref="HttpRequest{TContext}"/>.
/// </returns>
public static HttpRequest<TContext> WithUri<TContext>(this HttpRequest<TContext> request, Uri requestUri)
{
if (request == null)
throw new ArgumentNullException(nameof(request));
if (requestUri == null)
throw new ArgumentNullException(nameof(requestUri));
return request.Clone(properties =>
{
properties.SetUri(requestUri);
});
}
/// <summary>
/// Create a copy of the request with the specified request URI appended to its existing URI.
/// </summary>
/// <typeparam name="TContext">
/// The type of object used as a context for resolving deferred parameters.
/// </typeparam>
/// <param name="request">
/// The request.
/// </param>
/// <param name="relativeUri">
/// The relative request URI.
/// </param>
/// <returns>
/// The new <see cref="HttpRequest{TContext}"/>.
/// </returns>
public static HttpRequest<TContext> WithRelativeUri<TContext>(this HttpRequest<TContext> request, string relativeUri)
{
if (request == null)
throw new ArgumentNullException(nameof(request));
if (String.IsNullOrWhiteSpace(relativeUri))
throw new ArgumentException("Argument cannot be null, empty, or composed entirely of whitespace: 'relativeUri'.", nameof(relativeUri));
return request.WithRelativeUri(
new Uri(relativeUri, UriKind.Relative)
);
}
/// <summary>
/// Create a copy of the request with the specified request URI appended to its existing URI.
/// </summary>
/// <typeparam name="TContext">
/// The type of object used as a context for resolving deferred parameters.
/// </typeparam>
/// <param name="request">
/// The request.
/// </param>
/// <param name="relativeUri">
/// The relative request URI.
/// </param>
/// <returns>
/// The new <see cref="HttpRequest{TContext}"/>.
/// </returns>
public static HttpRequest<TContext> WithRelativeUri<TContext>(this HttpRequest<TContext> request, Uri relativeUri)
{
if (relativeUri == null)
throw new ArgumentNullException(nameof(relativeUri));
if (relativeUri.IsAbsoluteUri)
throw new ArgumentException("The specified URI is not a relative URI.", nameof(relativeUri));
return request.Clone(properties =>
{
properties.SetUri(
request.Uri.AppendRelativeUri(relativeUri)
);
});
}
}
}