public SWEventArgs(string req, string resp)
 {
     RequestRaw   = req;
     ResponseRaw  = resp;
     RequestJson  = JsonConvert.DeserializeObject <JObject>(req);
     ResponseJson = JsonConvert.DeserializeObject <JObject>(resp);
     Request      = JsonConvert.DeserializeObject <SWRequest>(req, new SWRequestConverter());
     Response     = JsonConvert.DeserializeObject <SWResponse>(resp, new SWResponseConverter());
 }
Example #2
0
        private string GenerateQueryString <TResponse>(SWRequest <TResponse> request) where TResponse : SWResponse
        {
            var parameters = new List <string>();
            var properties = request.GetType().GetProperties();

            foreach (var property in properties)
            {
                var value = property.GetValue(request);
                if (value != null)
                {
                    string queryName = property.Name;
                    if (property.GetCustomAttributes().FirstOrDefault(a => a.GetType() == typeof(QueryStringAliasAttribute)) is QueryStringAliasAttribute aliasAttribute)
                    {
                        queryName = aliasAttribute.Name;
                    }

                    parameters.Add($"{queryName}={value}");
                }
            }

            return(string.Join("&", parameters));
        }
Example #3
0
        public async Task <TResponse> Execute <TResponse>(SWRequest <TResponse> request, string token = "") where TResponse : SWResponse
        {
            using (var client = _httpClientFactory.CreateClient())
            {
                try
                {
                    string url = request.GetApiUrl() + "?";
                    if (!string.IsNullOrEmpty(token))
                    {
                        url += $"access_token={token}&";
                    }

                    string responseJson;
                    if (request.Method() == HttpMethod.Get)
                    {
                        url += GenerateQueryString(request);
                        //string url = $"{request.GetApiUrl()}?{GenerateQueryString(request)}";
                        responseJson = await client.GetStringAsync(url);
                    }
                    else
                    {
                        var content = new StringContent(request.ToJson(), Encoding.UTF8);
                        var resp    = await client.PostAsync(url, content);

                        responseJson = await resp.Content.ReadAsStringAsync();
                    }

                    return(responseJson.JsonDeserialize <TResponse>());
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, $"DefaultRequest:Execute:{request.GetApiUrl()} Error");
                    throw new SmartWeChatException(ex);
                }
            }
        }
 public async Task <TResponse> Execute <TResponse>(SWRequest <TResponse> request) where TResponse : SWResponse
 {
     return(await SmartWeChatConfig.DefaultRequest.Execute(request,
                                                           await SmartWeChatConfig.TokenManager.GetTokenAsync()));
 }