-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSession.cs
121 lines (93 loc) · 5.44 KB
/
Session.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
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type. - disabled because of it's not possible for this case
#pragma warning disable CS8603 // Possible null reference return. - disabled because of it's not possible for this case
namespace LocalStack.Client;
public class Session : ISession
{
private readonly IConfig _config;
private readonly ISessionOptions _sessionOptions;
private readonly ISessionReflection _sessionReflection;
public Session(ISessionOptions sessionOptions, IConfig config, ISessionReflection sessionReflection)
{
_sessionOptions = sessionOptions;
_config = config;
_sessionReflection = sessionReflection;
}
public TClient CreateClientByImplementation<TClient>(bool useServiceUrl = false) where TClient : AmazonServiceClient
{
Type clientType = typeof(TClient);
return (TClient)CreateClientByImplementation(clientType, useServiceUrl);
}
public AmazonServiceClient CreateClientByImplementation(Type implType, bool useServiceUrl = false)
{
if (!useServiceUrl && string.IsNullOrWhiteSpace(_sessionOptions.RegionName))
{
throw new MisconfiguredClientException($"{nameof(_sessionOptions.RegionName)} must be set if {nameof(useServiceUrl)} is false.");
}
IServiceMetadata serviceMetadata = _sessionReflection.ExtractServiceMetadata(implType);
AwsServiceEndpoint awsServiceEndpoint = _config.GetAwsServiceEndpoint(serviceMetadata.ServiceId) ??
throw new NotSupportedClientException($"{serviceMetadata.ServiceId} is not supported by this mock session.");
AWSCredentials awsCredentials = new SessionAWSCredentials(_sessionOptions.AwsAccessKeyId, _sessionOptions.AwsAccessKey, _sessionOptions.AwsSessionToken);
ClientConfig clientConfig = _sessionReflection.CreateClientConfig(implType);
clientConfig.UseHttp = !_config.GetConfigOptions().UseSsl;
_sessionReflection.SetForcePathStyle(clientConfig);
clientConfig.ProxyHost = awsServiceEndpoint.Host;
clientConfig.ProxyPort = awsServiceEndpoint.Port;
if (useServiceUrl)
{
clientConfig.ServiceURL = awsServiceEndpoint.ServiceUrl.AbsoluteUri;
}
else if (!string.IsNullOrWhiteSpace(_sessionOptions.RegionName))
{
clientConfig.RegionEndpoint = RegionEndpoint.GetBySystemName(_sessionOptions.RegionName);
}
var clientInstance = (AmazonServiceClient)Activator.CreateInstance(implType, awsCredentials, clientConfig);
return clientInstance;
}
public AmazonServiceClient CreateClientByInterface<TClient>(bool useServiceUrl = false) where TClient : IAmazonService
{
Type serviceInterfaceType = typeof(TClient);
return CreateClientByInterface(serviceInterfaceType, useServiceUrl);
}
public AmazonServiceClient CreateClientByInterface(Type serviceInterfaceType, bool useServiceUrl = false)
{
if (serviceInterfaceType == null)
{
throw new ArgumentNullException(nameof(serviceInterfaceType));
}
var clientTypeName = $"{serviceInterfaceType.Namespace}.{serviceInterfaceType.Name.Substring(1)}Client";
Type clientType = serviceInterfaceType.GetTypeInfo().Assembly.GetType(clientTypeName);
if (clientType == null)
{
throw new AmazonClientException($"Failed to find service client {clientTypeName} which implements {serviceInterfaceType.FullName}.");
}
if (!useServiceUrl && string.IsNullOrWhiteSpace(_sessionOptions.RegionName))
{
throw new MisconfiguredClientException($"{nameof(_sessionOptions.RegionName)} must be set if {nameof(useServiceUrl)} is false.");
}
IServiceMetadata serviceMetadata = _sessionReflection.ExtractServiceMetadata(clientType);
AwsServiceEndpoint awsServiceEndpoint = _config.GetAwsServiceEndpoint(serviceMetadata.ServiceId) ??
throw new NotSupportedClientException($"{serviceMetadata.ServiceId} is not supported by this mock session.");
AWSCredentials awsCredentials = new SessionAWSCredentials(_sessionOptions.AwsAccessKeyId, _sessionOptions.AwsAccessKey, _sessionOptions.AwsSessionToken);
ClientConfig clientConfig = _sessionReflection.CreateClientConfig(clientType);
clientConfig.UseHttp = !_config.GetConfigOptions().UseSsl;
_sessionReflection.SetForcePathStyle(clientConfig);
clientConfig.ProxyHost = awsServiceEndpoint.Host;
clientConfig.ProxyPort = awsServiceEndpoint.Port;
if (useServiceUrl)
{
clientConfig.ServiceURL = awsServiceEndpoint.ServiceUrl.AbsoluteUri;
}
else if (!string.IsNullOrWhiteSpace(_sessionOptions.RegionName))
{
clientConfig.RegionEndpoint = RegionEndpoint.GetBySystemName(_sessionOptions.RegionName);
}
ConstructorInfo? constructor = clientType.GetConstructor(new[] { typeof(AWSCredentials), clientConfig.GetType() });
if (constructor == null)
{
var message = $"Service client {clientTypeName} missing a constructor with parameters AWSCredentials and {clientConfig.GetType().FullName}.";
throw new AmazonClientException(message);
}
var client = (AmazonServiceClient)constructor.Invoke(new object[] { awsCredentials, clientConfig });
return client;
}
}