-
Notifications
You must be signed in to change notification settings - Fork 20
/
ClientBase.cs
114 lines (100 loc) · 5.29 KB
/
ClientBase.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
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
*/
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Net.Http;
using Oci.Common.Auth;
using Oci.Common.Http;
using Oci.Common.Http.Signing;
namespace Oci.Common
{
/// <summary>An abstract class for a generic service client.</summary>
public abstract class ClientBase
{
private readonly Dictionary<SigningStrategy, RequestSigner> availableRequestSigners;
private readonly RequestSigner requestSigner;
protected static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
protected RestClientHandler clientHandler;
protected readonly RestClient restClient;
protected Service service;
protected string userAgent = null;
/// <summary> Constructor of a service client.</summary>
/// <param name="authProvider">The authentication details provider.</param>
public ClientBase(IBasicAuthenticationDetailsProvider authProvider) : this(authProvider,
new ClientConfiguration(), new DefaultRequestSigner(authProvider))
{ }
/// <summary> Constructor of a service client.</summary>
/// <param name="authProvider">The authentication details provider.</param>
/// <param name="requestSigner">A request signer that will be used to sign requests.</param>
public ClientBase(IBasicAuthenticationDetailsProvider authProvider, RequestSigner requestSigner) :
this(authProvider, new ClientConfiguration(), requestSigner)
{ }
/// <summary> Constructor of a service client.</summary>
/// <param name="authProvider">The authentication details provider.</param>
/// <param name="clientConfiguration">A client configuration to customize client.</param>
public ClientBase(IBasicAuthenticationDetailsProvider authProvider, ClientConfiguration clientConfiguration) :
this(authProvider, new ClientConfiguration(), new DefaultRequestSigner(authProvider))
{ }
/// <summary> Constructor of a service client.</summary>
/// <param name="authProvider">The authentication details provider.</param>
/// <param name="clientConfiguration">A client configuration to customize client.</param>
/// <param name="requestSigner">A request signer that will be used to sign requests.</param>
public ClientBase(IBasicAuthenticationDetailsProvider authProvider, ClientConfiguration clientConfiguration, RequestSigner requestSigner)
{
this.clientHandler = new RestClientHandler(RequestReceptor);
this.restClient = new RestClient(clientHandler, clientConfiguration);
this.requestSigner = requestSigner;
this.availableRequestSigners = GetAvailableRequestSigners(authProvider);
this.restClient.SetDefaultUserAgent(GetUserAgent(clientConfiguration.ClientUserAgent));
}
/// <summary>Disposes the rest client.</summary>
public void Dispose()
{
this.restClient.Dispose();
}
/// <summary>Sets the endpoint in the rest client.</summary>
public void SetEndpoint(string endpoint)
{
logger.Info($"Setting endpoint to {endpoint}");
this.restClient.SetEndpoint(endpoint);
}
/// <summary>Retrieves the endpoint Uri.</summary>
public Uri GetEndpoint()
{
return this.restClient.GetEndpoint();
}
private string GetUserAgent(string clientUserAgent)
{
// User agent string will only be configured once and cannot be modified after that.
if (userAgent == null)
{
var additionalUserAgent = String.IsNullOrEmpty(clientUserAgent) ? "" : $" {clientUserAgent}";
OperatingSystem os = Environment.OSVersion;
userAgent = $"Oracle-DotNetSDK/{Version.GetVersion()} ({os.Platform}/{os.Version}; {RuntimeInformation.FrameworkDescription}{additionalUserAgent})";
}
return userAgent;
}
internal void RequestReceptor(HttpRequestMessage requestMessage)
{
RequestSigner requestSignerToUse = requestSigner;
if (requestMessage.Properties.TryGetValue(SigningStrategy.SIGNING_STRATEGY_PROPERTY_NAME_KEY, out var signingStrategy))
{
requestSignerToUse = availableRequestSigners.TryGetValue((SigningStrategy)signingStrategy, out var desiredSigner) ?
desiredSigner : requestSignerToUse;
}
requestSignerToUse.SignRequest(requestMessage);
}
private Dictionary<SigningStrategy, RequestSigner> GetAvailableRequestSigners(IBasicAuthenticationDetailsProvider authProvider)
{
var signers = new Dictionary<SigningStrategy, RequestSigner>();
foreach (SigningStrategy strategy in SigningStrategy.Values)
{
signers.Add(strategy, new DefaultRequestSigner(authProvider, strategy));
}
return signers;
}
}
}