1 Star 0 Fork 7

fnet/QcloudSharp

forked from JoyMoe/QcloudSharp
暂停
 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
QcloudClient.cs 5.83 KB
一键复制 编辑 原始数据 按行查看 历史
kinosang 提交于 2016-11-02 15:22 . Rewrite override of Submit()
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Net.Http;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
namespace QcloudSharp
{
public class QcloudClient : DynamicObject
{
private const string Uri = "/v2/index.php";
private List<KeyValuePair<string, string>> _patameters;
public string SecretId { get; set; }
public string SecretKey { get; set; }
public Enum.Region Region { get; set; }
public Enum.Endpoint Endpoint { get; set; }
public QcloudClient()
{
}
public QcloudClient(string secretId, string secretKey)
{
SecretId = secretId;
SecretKey = secretKey;
}
private static int CompareKeyValuePair(KeyValuePair<string, string> a, KeyValuePair<string, string> b)
{
return string.Compare(a.Key, b.Key, StringComparison.Ordinal);
}
private string GetSignature(string endpoint, List<KeyValuePair<string, string>> data)
{
if (data == null) throw new ArgumentNullException(nameof(data));
data.Sort(CompareKeyValuePair);
using (var content = new FormUrlEncodedContent(data))
{
var queryString = WebUtility.UrlDecode(content.ReadAsStringAsync().Result);
var plainString = $"GET{endpoint}{Uri}?{queryString}";
using (var hmac = new HMACSHA1(Encoding.UTF8.GetBytes(SecretKey)))
{
return Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(plainString)));
}
}
}
private string Send(string endpoint, List<KeyValuePair<string, string>> data)
{
if (data == null) throw new ArgumentNullException(nameof(data));
data.Add(new KeyValuePair<string, string>("Signature", GetSignature(endpoint, data)));
using (var content = new FormUrlEncodedContent(data))
{
using (var client = new HttpClient())
{
var message = client.GetAsync($"https://{endpoint}{Uri}?{content.ReadAsStringAsync().Result}").Result;
return message.Content.ReadAsStringAsync().Result;
}
}
}
private long ToUnixTimeSeconds(DateTimeOffset dateTimeOffset)
{
var unixStart = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
var unixTimeStampInTicks = (dateTimeOffset.ToUniversalTime() - unixStart).Ticks;
return unixTimeStampInTicks / TimeSpan.TicksPerSecond;
}
public void AddParameter(KeyValuePair<string, string> parameter)
{
if (_patameters == null) _patameters = new List<KeyValuePair<string, string>>();
_patameters.Add(parameter);
}
public void AddParameter(IEnumerable<KeyValuePair<string, string>> parameters)
{
if (parameters == null) throw new ArgumentNullException(nameof(parameters));
if (_patameters == null) _patameters = new List<KeyValuePair<string, string>>();
_patameters.AddRange(parameters.ToList());
}
public void ClearParameter()
{
_patameters = new List<KeyValuePair<string, string>>();
}
public string Submit(Enum.Endpoint endpoint, Enum.Region region, string action)
{
var patameters = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Action", action),
new KeyValuePair<string, string>("Region", Enum.ToRegion(region)),
new KeyValuePair<string, string>("Timestamp", ToUnixTimeSeconds(DateTimeOffset.Now).ToString()),
new KeyValuePair<string, string>("Nonce", new Random().Next().ToString()),
new KeyValuePair<string, string>("SecretId", SecretId)
};
if (_patameters != null) patameters.AddRange(_patameters);
return Send(Enum.ToEndpoint(endpoint), patameters);
}
public string Submit(Enum.Endpoint endpoint, string action)
{
return Submit(endpoint, Region, action);
}
public string Submit(string action)
{
return Submit(Endpoint, Region, action);
}
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
if (args.Length < 2)
throw new ArgumentException("Endpoint and Region must be specified.");
if (!(args[0] is Enum.Endpoint))
// ReSharper disable once NotResolvedInText
throw new ArgumentNullException("Endpoint");
Endpoint = (Enum.Endpoint)args[0];
if (!(args[1] is Enum.Region))
// ReSharper disable once NotResolvedInText
throw new ArgumentNullException("Region");
Region = (Enum.Region)args[1];
if (args.Length >= 3)
{
ClearParameter();
if (args[2] is IEnumerable<KeyValuePair<string, string>>)
{
AddParameter((IEnumerable<KeyValuePair<string, string>>)args[2]);
}
else
{
args = args.Skip(2).ToArray();
foreach (var arg in args)
{
if (!(arg is KeyValuePair<string, string>))
throw new ArgumentException("Parameters required to be KeyValuePair<string, string>");
AddParameter((KeyValuePair<string, string>)arg);
}
}
}
result = Submit(Endpoint, Region, binder.Name);
return true;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/fnet/QcloudSharp.git
git@gitee.com:fnet/QcloudSharp.git
fnet
QcloudSharp
QcloudSharp
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385