protected override void SetHost <T>(SimpleWebRequest <T> request) { if (request != null) { var dics = new Dictionary <string, string>(); dics.Add("x-ca-accesskeyid", _accessKeyId); dics.Add("x-ca-version", _version); dics.Add("x-ca-timestamp", string.Format("{0}", (long)(DateTime.Now.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds))); dics.Add("x-ca-signaturenonce", Guid.NewGuid().ToString("N")); dics.Add("requestmethod", "POST"); foreach (var dic in dics) { request.SetHeader(dic.Key, dic.Value); } var properties = request.GetType().GetProperties(); foreach (var p in properties) { var ca = p.GetCustomAttribute(typeof(ParaSignAttribute)); if (ca != null) { var _ca = ca as ParaSignAttribute; var value = p.GetValue(request); if (p.PropertyType == typeof(System.String)) { if (value != null && !string.IsNullOrWhiteSpace(value.ToString())) { dics.Add(_ca.Name, string.Format("{0}", value)); } } else if (p.PropertyType == typeof(int?)) { var v = value as int?; if (v != null) { dics.Add(_ca.Name, string.Format("{0}", value)); } } else if (p.PropertyType == typeof(double?)) { var v = value as double?; if (v != null) { dics.Add(_ca.Name, string.Format("{0}", value)); } } } } request.SetHeader("x-ca-signature", Signnature(dics)); } base.SetHost <T>(request); }
public override async Task <T> ExecuteAsync <T>(SimpleWebRequest <T> request) { if (request == null) { throw new ArgumentNullException(nameof(request)); } var objType = request.GetType(); if (objType == typeof(Entity.AuthenticationRequest)) { return(await base.ExecuteAsync <T>(request)); } await ConnectAsync(); //Append Authorization token in the Http Headers //if (request.Headers.ContainsKey("Authorization")) // request.Headers["Authorization"] = _token; //else // request.Headers.Add("Authorization", _token); var tokenPro = objType.GetProperty("AuthorizationToken"); if (tokenPro != null && tokenPro.CanWrite) { tokenPro.SetValue(request, _token); } //Get Response var response = await base.ExecuteAsync(request); if (response != null) { var _apiResponse = response.GetType().GetProperty("Response"); if (_apiResponse != null) { var responseBody = _apiResponse.GetValue(response); var pro = responseBody.GetType().GetProperty("ErrorId"); var errorId = pro.GetValue(responseBody); if (responseBody != null && pro != null && errorId != null && errorId.ToString() == "NOAUTH") { //re-auth await ReAuthAsync(); return(await this.ExecuteAsync(request)); } } } return(response); }
public override async Task <T> ExecuteDownloadAsync <T>(SimpleWebRequest <T> request, string fileSaveFullPath) { await ConnectAsync(); var tokenPro = request.GetType().GetProperty("AuthorizationToken"); if (tokenPro != null && tokenPro.CanWrite) { tokenPro.SetValue(request, _token); } var t = await base.ExecuteDownloadAsync <T>(request, fileSaveFullPath); if (t.StatusCode == System.Net.HttpStatusCode.Unauthorized) { ReAuth(); return(await this.ExecuteDownloadAsync(request, fileSaveFullPath)); } return(t); }