Example #1
0
async def send_single(to_address, subject, text, html):
    region = DEFAULT_REGION
    fixture = FIXTURE[region]
    alias = 'PIE'
    account = ''
    ak_id = ''
    ak_secret = ''

    params = {
        'Version': fixture['version'],
        'RegionId': region,
        'Action': 'SingleSendMail',
        'AccountName': account,
        'ReplyToAddress': True,
        'AddressType': 0,
        'ToAddress': to_address,
        'FromAlias': alias,
        'Subject': subject,
        'HtmlBody': html,
        'TextBody': text,
    }
    url = 'https://' + fixture['host'] + get_signed_url(
        params, ak_id, ak_secret, 'JSON', 'GET', {})
    async with aiohttp.ClientSession(raise_for_status=True) as session:
        async with session.get(url) as resp:
            return await resp.json()
Example #2
0
 def test_get_signed_url(self, mock_get_iso_8061_date, mock_get_uuid):
     mock_get_iso_8061_date.return_value = "2018-12-04T04:03:12Z"
     mock_get_uuid.return_value = "7e1c7d12-7551-4856-8abb-1938ccac6bcc"
     url = get_signed_url({}, 'access_key_id', 'access_key_secret', 'JSON',
                          'GET', {})
     mock_get_iso_8061_date.assert_called_once_with()
     mock_get_uuid.assert_called_once_with()
Example #3
0
 def get_url(self, region_id, ak, secret):
     sign_params = self._get_sign_params()
     if 'RegionId' not in iterkeys(sign_params):
         sign_params['RegionId'] = region_id
     url = rpc_signer.get_signed_url(sign_params, ak, secret,
                                     self.get_accept_format(),
                                     self.get_method(),
                                     self.get_body_params(), self._signer)
     return url
Example #4
0
 def get_url(self, region_id, access_key_id, access_key_secret):
     sign_params = self._get_sign_params()
     if 'RegionId' not in iterkeys(sign_params):
         sign_params['RegionId'] = region_id
     url, string_to_sign = rpc_signer.get_signed_url(
         sign_params, access_key_id, access_key_secret,
         self.get_accept_format(), self.get_method(),
         self.get_body_params(), self._signer)
     self.string_to_sign = string_to_sign
     return url
Example #5
0
async def send_ali_sms(phone: str, captcha: str):
    params = {
        # "AccessKeyId": config['sms']['ali']['AccessKeyId'],
        # "Timestamp": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ"),
        # "Format": "JSON",
        # "SignatureMethod": "HMAC-SHA1",
        # "SignatureVersion": "1.0",
        # "SignatureNonce": uuid4().hex,
        # "Signature": "",
        "Action": "SendSms",
        "Version": "2017-05-25",
        "RegionId": "cn-hangzhou",
        "PhoneNumbers": phone,
        "SignName": CONFIG['sms']['ali']['SignName'],
        "TemplateCode": CONFIG['sms']['ali']['TemplateCode'],
        "TemplateParam": {
            "code": captcha
        },
    }
    uri = CONFIG['sms']['ali']['uri'] + get_signed_url(
        params, CONFIG['sms']['ali']['AccessKeyId'],
        CONFIG['sms']['ali']['Secret'], 'JSON', 'GET', {})[0]
    try:
        async with ClientSession(timeout=ClientTimeout(total=3)) as session:
            async with session.get(uri) as resp:
                try:
                    data = await resp.json()
                except ContentTypeError:
                    data = loads(await resp.text())
    except ClientError:
        raise RuntimeError
    try:
        assert data['Code'] == 'OK'
    except KeyError:
        raise RuntimeError
    except AssertionError:
        if 'LIMIT' in data['Code']:
            raise SmsLimitException
        else:
            raise RuntimeError