/
CloudantDbConnectionInfo.cs
37 lines (32 loc) · 1.3 KB
/
CloudantDbConnectionInfo.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
using System;
using System.Linq;
using MyCouch.Net;
namespace MyCouch.CloudantIAM
{
public class CloudantDbConnectionInfo : DbConnectionInfo
{
public ApikeyAuth ApikeyAuth { get; set; }
public CookieAuth CookieAuth { get; set; }
public CloudantDbConnectionInfo(string address, string dbName) : this(new Uri(address), dbName) { }
public CloudantDbConnectionInfo(Uri address, string dbName) : base(RemoveUserInfoFrom(address), dbName)
{
if (!string.IsNullOrWhiteSpace(address.UserInfo))
{
var userInfoParts = ExtractUserInfoPartsFrom(address);
if (userInfoParts.Length == 2) CookieAuth = new CookieAuth(userInfoParts[0], userInfoParts[1]);
else ApikeyAuth = new ApikeyAuth(userInfoParts[0]);
}
}
private static Uri RemoveUserInfoFrom(Uri address)
{
return new Uri(address.GetComponents(UriComponents.AbsoluteUri & ~UriComponents.UserInfo, UriFormat.UriEscaped));
}
private static string[] ExtractUserInfoPartsFrom(Uri address)
{
return address.UserInfo
.Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries)
.Select(Uri.UnescapeDataString)
.ToArray();
}
}
}