-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAliyunConnectionStringBuilder.cs
More file actions
81 lines (72 loc) · 3.1 KB
/
AliyunConnectionStringBuilder.cs
File metadata and controls
81 lines (72 loc) · 3.1 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
using System;
using System.Linq;
namespace Foundatio;
public abstract class AliyunConnectionStringBuilder
{
public string Endpoint { get; set; } = string.Empty;
public string AccessKey { get; set; } = string.Empty;
public string SecretKey { get; set; } = string.Empty;
protected AliyunConnectionStringBuilder() { }
protected AliyunConnectionStringBuilder(string connectionString)
{
if (String.IsNullOrEmpty(connectionString))
throw new ArgumentNullException(nameof(connectionString));
Parse(connectionString);
}
private void Parse(string connectionString)
{
foreach (string[] option in connectionString
.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.Where(kvp => kvp.Contains('='))
.Select(kvp => kvp.Split(new[] { '=' }, 2)))
{
string optionKey = option[0].Trim();
string optionValue = option[1].Trim();
if (!ParseItem(optionKey, optionValue))
{
throw new ArgumentException($"The option '{optionKey}' cannot be recognized in connection string.", nameof(connectionString));
}
}
}
protected virtual bool ParseItem(string key, string value)
{
if (String.Equals(key, "AccessKey", StringComparison.OrdinalIgnoreCase) ||
String.Equals(key, "Access Key", StringComparison.OrdinalIgnoreCase) ||
String.Equals(key, "AccessKeyId", StringComparison.OrdinalIgnoreCase) ||
String.Equals(key, "Access Key Id", StringComparison.OrdinalIgnoreCase) ||
String.Equals(key, "Id", StringComparison.OrdinalIgnoreCase))
{
AccessKey = value;
return true;
}
if (String.Equals(key, "SecretKey", StringComparison.OrdinalIgnoreCase) ||
String.Equals(key, "Secret Key", StringComparison.OrdinalIgnoreCase) ||
String.Equals(key, "SecretAccessKey", StringComparison.OrdinalIgnoreCase) ||
String.Equals(key, "Secret Access Key", StringComparison.OrdinalIgnoreCase) ||
String.Equals(key, "AccessKeySecret", StringComparison.OrdinalIgnoreCase) ||
String.Equals(key, "Access Key Secret", StringComparison.OrdinalIgnoreCase) ||
String.Equals(key, "Secret", StringComparison.OrdinalIgnoreCase))
{
SecretKey = value;
return true;
}
if (String.Equals(key, "EndPoint", StringComparison.OrdinalIgnoreCase) ||
String.Equals(key, "End Point", StringComparison.OrdinalIgnoreCase))
{
Endpoint = value;
return true;
}
return false;
}
public override string ToString()
{
string connectionString = String.Empty;
if (!String.IsNullOrEmpty(AccessKey))
connectionString += "AccessKey=" + AccessKey + ";";
if (!String.IsNullOrEmpty(SecretKey))
connectionString += "SecretKey=" + SecretKey + ";";
if (!String.IsNullOrEmpty(Endpoint))
connectionString += "EndPoint=" + Endpoint + ";";
return connectionString;
}
}