Skip to content

Commit 0b1038d

Browse files
committed
Add DI extensions to Lambda Cloud service
1 parent 7106f90 commit 0b1038d

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
3+
using Microsoft;
4+
using Microsoft.Extensions;
5+
using Microsoft.Extensions.DependencyInjection;
6+
7+
namespace Lambda.Cloud
8+
{
9+
/// <summary>
10+
/// Represents the options when registering an instance of the <see cref="LambdaCloudService"/>
11+
/// class with an <see cref="IServiceCollection"/>.
12+
/// </summary>
13+
public sealed class LambdaCloudServiceOptions
14+
{
15+
/// <summary>
16+
/// Gets or sets the base URL to use.
17+
/// </summary>
18+
public Uri BaseUrl { get; set; } =
19+
LambdaCloudService.DefaultBaseUrl;
20+
21+
/// <summary>
22+
/// Gets or sets the API key to use.
23+
/// </summary>
24+
public string ApiKey { get; set; }
25+
}
26+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.Net;
3+
using System.Net.Http;
4+
5+
using Microsoft;
6+
using Microsoft.Extensions;
7+
using Microsoft.Extensions.DependencyInjection;
8+
9+
namespace Lambda.Cloud
10+
{
11+
/// <summary>
12+
/// Contains extension methods for
13+
/// <see cref="IServiceCollection"/>.
14+
/// </summary>
15+
public static class ServiceCollectionExtensions
16+
{
17+
/// <summary>
18+
/// The name to use for the named client.
19+
/// </summary>
20+
private const string LambdaCloudHttpClientName =
21+
"lambda-cloud";
22+
23+
/// <summary>
24+
/// Registers an instance of the <see cref="LambdaCloudService"/>
25+
/// class with an <see cref="IServiceCollection"/>
26+
/// using a named <see cref="HttpClient"/>.
27+
/// </summary>
28+
/// <param name="services">The service collection to register with.</param>
29+
/// <param name="optionsCallback">The callback to configure the options for the instance with.</param>
30+
/// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the named client.</returns>
31+
public static IHttpClientBuilder AddLambdaCloudHttpClient(
32+
this IServiceCollection services,
33+
Action<LambdaCloudServiceOptions> optionsCallback = default)
34+
{
35+
Guard.NotNull(
36+
services,
37+
nameof(services));
38+
39+
var options =
40+
new LambdaCloudServiceOptions();
41+
42+
optionsCallback?
43+
.Invoke(
44+
options);
45+
46+
return services
47+
.AddTransient<LambdaCloudDelegatingHandler>()
48+
.AddHttpClient<ILambdaCloudService, LambdaCloudService>(
49+
LambdaCloudHttpClientName,
50+
(httpClient) =>
51+
{
52+
// Configure the base URL for the client.
53+
httpClient.BaseAddress =
54+
options.BaseUrl;
55+
56+
// Configure the default request headers for the client.
57+
httpClient.DefaultRequestHeaders.Add(
58+
$"Authorization",
59+
$"Bearer {options.ApiKey}");
60+
})
61+
.AddHttpMessageHandler<LambdaCloudDelegatingHandler>();
62+
}
63+
}
64+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
3+
namespace Lambda
4+
{
5+
/// <summary>
6+
/// Contains guard clauses for throwing exceptions.
7+
/// </summary>
8+
internal static class Guard
9+
{
10+
/// <summary>
11+
/// Throws an exception if the specified argument is null.
12+
/// </summary>
13+
/// <param name="argument">The argument specified.</param>
14+
/// <param name="argumentName">The name of the argument specified.</param>
15+
/// <exception cref="ArgumentNullException">
16+
/// Thrown if the specified argument is null.
17+
/// </exception>
18+
public static void NotNull(
19+
object argument,
20+
string argumentName)
21+
{
22+
if (argument == null)
23+
{
24+
throw new ArgumentNullException(
25+
argumentName);
26+
}
27+
}
28+
29+
/// <summary>
30+
/// Throws an exception if the specified argument is null or empty.
31+
/// </summary>
32+
/// <param name="argument">The argument specified.</param>
33+
/// <param name="argumentName">The name of the argument specified.</param>
34+
/// <exception cref="ArgumentNullException">
35+
/// Thrown if the specified argument is null.
36+
/// </exception>
37+
/// <exception cref="ArgumentException">
38+
/// Thrown if the specified argument is empty.
39+
/// </exception>
40+
public static void NotNullOrEmpty(
41+
string argument,
42+
string argumentName)
43+
{
44+
if (argument == null)
45+
{
46+
throw new ArgumentNullException(
47+
argumentName);
48+
}
49+
50+
if (String.IsNullOrEmpty(argument))
51+
{
52+
throw new ArgumentException(
53+
argumentName);
54+
}
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)