-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathFlurlExtensions.cs
More file actions
147 lines (135 loc) · 5.28 KB
/
FlurlExtensions.cs
File metadata and controls
147 lines (135 loc) · 5.28 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
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
143
144
145
146
147
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Flurl.Http;
using OpenStack;
using OpenStack.Authentication;
// ReSharper disable once CheckNamespace
namespace Flurl.Extensions
{
/// <summary>
/// Useful Flurl extension methods for custom implementations.
/// </summary>
/// <exclude/>
public static class FlurlExtensions
{
/// <summary>
/// Converts a <see cref="Url"/> to a <see cref="Uri"/>.
/// </summary>
/// <param name="url">The URL.</param>
public static Uri ToUri(this Url url)
{
return new Uri(url.ToString());
}
/// <summary>
/// Removes any query parameters which have a null or empty value.
/// </summary>
/// <param name="url">The URL.</param>
public static Url RemoveNullOrEmptyQueryParams(this string url)
{
return new Url(url).RemoveNullOrEmptyQueryParams();
}
/// <summary>
/// Removes any query parameters which have a null or empty value.
/// </summary>
/// <param name="url">The URL.</param>
public static Url RemoveNullOrEmptyQueryParams(this Url url)
{
foreach (KeyValuePair<string, object> queryParam in url.QueryParams.ToList())
{
if (queryParam.Value == null || queryParam.Value.ToString() == string.Empty)
url.QueryParams.Remove(queryParam);
}
return url;
}
/// <summary>
/// Applies OpenStack authentication to a request.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="authenticationProvider">The authentication provider.</param>
/// <returns>
/// An authenticated request.
/// </returns>
public static PreparedRequest Authenticate(this string url, IAuthenticationProvider authenticationProvider)
{
return new Url(url).Authenticate(authenticationProvider);
}
/// <summary>
/// Applies OpenStack authentication to a request.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="authenticationProvider">The authentication provider.</param>
/// <returns>
/// An authenticated request.
/// </returns>
public static PreparedRequest Authenticate(this Url url, IAuthenticationProvider authenticationProvider)
{
return url.PrepareRequest().Authenticate(authenticationProvider);
}
/// <summary>
/// Builds a prepared Flurl request which can be executed at a later time.
/// </summary>
/// <param name="url">The URL.</param>
public static PreparedRequest PrepareRequest(this string url)
{
return new Url(url).PrepareRequest();
}
/// <summary>
/// Builds a prepared Flurl request which can be executed at a later time.
/// </summary>
/// <param name="url">The URL.</param>
public static PreparedRequest PrepareRequest(this Url url)
{
return new PreparedRequest(url, autoDispose: true)
{
Settings = OpenStackNet.Configuration.FlurlHttpSettings
};
}
/// <summary>
/// Applies OpenStack authentication to a request.
/// </summary>
/// <param name="request">The request.</param>
/// <param name="authenticationProvider">The authentication provider.</param>
/// <returns>
/// An authenticated request.
/// </returns>
public static PreparedRequest Authenticate(this PreparedRequest request, IAuthenticationProvider authenticationProvider)
{
var authenticatedMessageHandler = request.HttpMessageHandler as AuthenticatedMessageHandler;
if (authenticatedMessageHandler != null)
{
authenticatedMessageHandler.AuthenticationProvider = authenticationProvider;
}
return request;
}
/// <inheritdoc cref="ClientConfigExtensions.WithHeader(FlurlClient,string,object)" />
public static PreparedRequest WithHeader(this PreparedRequest request, string key, object value)
{
((FlurlClient)request).WithHeader(key, value);
return request;
}
/// <summary>
/// Sends the <see cref="PreparedRequest"/>.
/// </summary>
/// <param name="requestTask">A task which returns the request.</param>
/// <returns>The HTTP response message.</returns>
public static async Task<HttpResponseMessage> SendAsync(this Task<PreparedRequest> requestTask)
{
PreparedRequest request = await requestTask.ConfigureAwait(false);
return await request.SendAsync().ConfigureAwait(false);
}
/// <summary>
/// Sends the <see cref="HttpResponseHeaders"/>.
/// </summary>
/// <param name="responseTask">A task with returns the response</param>
/// <returns></returns>
public static async Task<HttpResponseHeaders> ReceiveHeaders(this Task<HttpResponseMessage> responseTask)
{
return await Task.FromResult(responseTask.Result.Headers);
}
}
}