private static (AliyunSmsSender, StubHttpClient) CreateSmsSender()
        {
            var options = new AliyunSmsOptions()
            {
                AccessKeySecret        = "dummy-secret",
                AccountKeyId           = "dummy-key",
                SmsServiceSignName     = "dotnetclub.net",
                SmsServiceTemplateCode = "SA873dK"
            };
            var optionsMock = new Mock <IOptions <AliyunSmsOptions> >();

            optionsMock.SetupGet(o => o.Value).Returns(options);

            var client = Services.StubHttpClient.Create().When(req =>
            {
                var response = new AliyunSmsResponse()
                {
                    RequestId = Guid.NewGuid().ToString(),
                    Code      = "OK",
                    Message   = "Sms Sent",
                    BizId     = Guid.NewGuid().ToString()
                };
                return(JsonResponse(response));
            });

            var smsSender = new AliyunSmsSender(optionsMock.Object, client, new SystemClock());

            return(smsSender, client);
        }
Exemple #2
0
 public AliyunSmsException(Exception exception,
                           string response, AliyunSmsResponse responseObject)
     : base("Failed to request api with action SendSms with response "
            + (response ?? string.Empty), exception)
 {
     this.ResponseContent = response;
     this.ResponseObject  = responseObject;
 }
Exemple #3
0
        async Task <AliyunSmsResponse> InvokeAliyunSmsApi(string signedParameters)
        {
            string            responseContent = null;
            AliyunSmsResponse smsResponse     = null;

            try
            {
                var request = new HttpRequestMessage(HttpMethod.Post, AliSmsHost)
                {
                    Content = new StringContent(signedParameters, Encoding.ASCII, "application/x-www-form-urlencoded")
                };
                request.Headers.UserAgent.TryParseAdd("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36");

                var response = await _httpInvoker.SendAsync(request, CancellationToken.None);

                responseContent = await response.Content.ReadAsStringAsync();

                smsResponse = JsonConvert.DeserializeObject <AliyunSmsResponse>(responseContent);
                return(smsResponse);
            }
            catch (HttpRequestException ex)
            {
                throw new AliyunSmsException(ex, responseContent, smsResponse);
            }
            catch (WebException ex)
            {
                try
                {
                    string resp;
                    using (var sr = new StreamReader(ex.Response.GetResponseStream()))
                    {
                        resp = sr.ReadToEnd();
                    }

                    throw new AliyunSmsException(ex, resp, null);
                }
                catch
                {
                    throw new AliyunSmsException(ex, null, null);
                }
            }
            catch (JsonException jsonException)
            {
                throw new AliyunSmsException(jsonException, responseContent, null);
            }
        }