From f582d6463db7c154a273e881909a7237bdd0ed48 Mon Sep 17 00:00:00 2001 From: songfei9315 Date: Thu, 6 Jun 2019 22:08:01 +0800 Subject: [PATCH 1/2] add sms_ch --- CHANGELOG.md | 3 + examples/sms_test.py | 98 ++++++++++++++++ qiniu/__init__.py | 5 +- qiniu/auth.py | 6 +- qiniu/http.py | 46 +++++++- qiniu/services/sms/__init__.py | 0 qiniu/services/sms/sms.py | 207 +++++++++++++++++++++++++++++++++ 7 files changed, 356 insertions(+), 9 deletions(-) create mode 100644 examples/sms_test.py create mode 100644 qiniu/services/sms/__init__.py create mode 100644 qiniu/services/sms/sms.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ba182a3..4562d660 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +# 7.2.5 (2019-06-06) +* 添加sms + # 7.2.4 (2019-04-01) * 默认导入region类 diff --git a/examples/sms_test.py b/examples/sms_test.py new file mode 100644 index 00000000..05d5b957 --- /dev/null +++ b/examples/sms_test.py @@ -0,0 +1,98 @@ +# -*- coding: utf-8 -*- +# flake8: noqa + +from qiniu import QiniuMacAuth +from qiniu import Sms + + +access_key = 'bjtWBQXrcxgo7HWwlC_bgHg81j352_GhgBGZPeOW' +secret_key = 'pCav6rTslxP2SIFg0XJmAw53D9PjWEcuYWVdUqAf' + +# 初始化Auth状态 +q = QiniuMacAuth(access_key, secret_key) + +# 初始化Sms +sms = Sms(q) + +""" +#创建签名 +signature = 'abs' +source = 'website' +req, info = sms.createSignature(signature, source) +print(req,info) +""" + +""" +#查询签名 +audit_status = '' +page = 1 +page_size = 20 +req, info = sms.querySignature(audit_status, page, page_size) +print(req, info) +""" + +""" +编辑签名 +id = 1136530250662940672 +signature = 'sssss' +req, info = sms.updateSignature(id, signature) +print(req, info) +""" + +""" +#删除签名 +signature_id= 1136530250662940672 +req, info = sms.deleteSignature(signature_id) +print(req, info) +""" + +""" +#创建模版 +name = '06-062-test' +template = '${test}' +type = 'notification' +description = '就测试啊' +signature_id = '1131464448834277376' +req, info = sms.createTemplate(name, template, type, description, signature_id) +print(req, info) +""" + +""" +#查询模版 +audit_status = '' +page = 1 +page_size = 20 +req, info = sms.queryTemplate(audit_status, page, page_size) +print(req, info) +""" + +""" +#编辑模版 +template_id = '1136589777022226432' +name = '06-06-test' +template = 'hi,你好' +description = '就测试啊' +signature_id = '1131464448834277376' +req, info = sms.updateTemplate(template_id, name, template, description, signature_id) +print(info) +""" + +""" +#删除模版 +template_id = '1136589777022226432' +req, info = sms.deleteTemplate(template_id) +print(req, info) +""" + +""" +#发送短信 +""" +template_id = '' +mobiles = [] +parameters = {} +req, info = sms.sendMessage(template_id, mobiles, parameters) +print(req, info) + + + + diff --git a/qiniu/__init__.py b/qiniu/__init__.py index 4ff86c51..61854789 100644 --- a/qiniu/__init__.py +++ b/qiniu/__init__.py @@ -9,7 +9,7 @@ # flake8: noqa -__version__ = '7.2.4' +__version__ = '7.2.5' from .auth import Auth, QiniuMacAuth @@ -25,7 +25,6 @@ from .services.processing.cmd import build_op, pipe_cmd, op_save from .services.compute.app import AccountClient from .services.compute.qcos_api import QcosClient - +from .services.sms.sms import Sms from .services.pili.rtc_server_manager import RtcServer, get_room_token - from .utils import urlsafe_base64_encode, urlsafe_base64_decode, etag, entry diff --git a/qiniu/auth.py b/qiniu/auth.py index fe58f930..d8d8ddd0 100644 --- a/qiniu/auth.py +++ b/qiniu/auth.py @@ -3,9 +3,7 @@ import hmac import time from hashlib import sha1 - from requests.auth import AuthBase - from .compat import urlparse, json, b from .utils import urlsafe_base64_encode @@ -266,8 +264,8 @@ def token_of_request( data += "\n" if content_type and content_type != "application/octet-stream" and body: - data += body.decode(encoding='UTF-8') - + # data += body.decode(encoding='UTF-8') + data += body return '{0}:{1}'.format(self.__access_key, self.__token(data)) def qiniu_headers(self, headers): diff --git a/qiniu/http.py b/qiniu/http.py index 14a422f4..ff0089e7 100644 --- a/qiniu/http.py +++ b/qiniu/http.py @@ -9,6 +9,7 @@ import qiniu.auth from . import __version__ + _sys_info = '{0}; {1}'.format(platform.system(), platform.machine()) _python_ver = platform.python_version() @@ -24,7 +25,7 @@ def __return_wrapper(resp): return None, ResponseInfo(resp) resp.encoding = 'utf-8' ret = resp.json(encoding='utf-8') if resp.text != '' else {} - if ret is None: # json null + if ret is None: # json null ret = {} return ret, ResponseInfo(resp) @@ -110,6 +111,10 @@ def _post_with_auth_and_headers(url, data, auth, headers): return _post(url, data, None, qiniu.auth.RequestsAuth(auth), headers) +def _post_with_qiniu_mac_and_headers(url, data, auth, headers): + return _post(url, data, None, qiniu.auth.QiniuMacRequestsAuth(auth), headers) + + def _put_with_auth(url, data, auth): return _put(url, data, None, qiniu.auth.RequestsAuth(auth)) @@ -118,11 +123,14 @@ def _put_with_auth_and_headers(url, data, auth, headers): return _put(url, data, None, qiniu.auth.RequestsAuth(auth), headers) +def _put_with_qiniu_mac_and_headers(url, data, auth, headers): + return _put(url, data, None, qiniu.auth.QiniuMacRequestsAuth(auth), headers) + + def _post_with_qiniu_mac(url, data, auth): qn_auth = qiniu.auth.QiniuMacRequestsAuth( auth) if auth is not None else None timeout = config.get_default('connection_timeout') - try: r = requests.post( url, @@ -148,6 +156,23 @@ def _get_with_qiniu_mac(url, params, auth): return __return_wrapper(r) +def _get_with_qiniu_mac_and_headers(url, params, auth, headers): + try: + post_headers = _headers.copy() + if headers is not None: + for k, v in headers.items(): + post_headers.update({k: v}) + r = requests.get( + url, + params=params, + auth=qiniu.auth.QiniuMacRequestsAuth(auth) if auth is not None else None, + timeout=config.get_default('connection_timeout'), + headers=post_headers) + except Exception as e: + return None, ResponseInfo(None, e) + return __return_wrapper(r) + + def _delete_with_qiniu_mac(url, params, auth): try: r = requests.delete( @@ -161,6 +186,23 @@ def _delete_with_qiniu_mac(url, params, auth): return __return_wrapper(r) +def _delete_with_qiniu_mac_and_headers(url, params, auth, headers): + try: + post_headers = _headers.copy() + if headers is not None: + for k, v in headers.items(): + post_headers.update({k: v}) + r = requests.delete( + url, + params=params, + auth=qiniu.auth.QiniuMacRequestsAuth(auth) if auth is not None else None, + timeout=config.get_default('connection_timeout'), + headers=post_headers) + except Exception as e: + return None, ResponseInfo(None, e) + return __return_wrapper(r) + + class ResponseInfo(object): """七牛HTTP请求返回信息类 diff --git a/qiniu/services/sms/__init__.py b/qiniu/services/sms/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/qiniu/services/sms/sms.py b/qiniu/services/sms/sms.py new file mode 100644 index 00000000..310c9fe7 --- /dev/null +++ b/qiniu/services/sms/sms.py @@ -0,0 +1,207 @@ +# -*- coding: utf-8 -*- + +from qiniu import http +import json + + +class Sms(object): + def __init__(self, auth): + self.auth = auth + self.server = 'https://sms.qiniuapi.com' + + def createSignature(self, signature, source, pics=None): + """ + *创建签名 + *signature: string类型,必填,【长度限制8个字符内】超过长度会报错 + *source: string类型,必填,申请签名时必须指定签名来源。取值范围为: + enterprises_and_institutions 企事业单位的全称或简称 + website 工信部备案网站的全称或简称 + app APP应用的全称或简称 + public_number_or_small_program 公众号或小程序的全称或简称 + store_name 电商平台店铺名的全称或简称 + trade_name 商标名的全称或简称 + *pics: 签名对应的资质证明图片进行 base64 编码格式转换后的字符串 + * @ return: 类型array + { + "signature_id": < signature_id > + } + """ + req = {} + req['signature'] = signature + req['source'] = source + if pics: + req['pics'] = pics + body = json.dumps(req) + url = '{0}/v1/signature'.format(self.server) + return self.__post(url, body) + + def querySignature(self, audit_status=None, page=1, page_size=20): + """ + 查询签名 + * audit_status: 审核状态 string 类型,可选,取值范围为: "passed"(通过), "rejected"(未通过), "reviewing"(审核中) + * page:页码 int 类型, + * page_size: 分页大小 int 类型,可选, 默认为20 + *@return: 类型array { + "items": [{ + "id": string, + "signature": string, + "source": string, + "audit_status": string, + "reject_reason": string, + "created_at": int64, + "updated_at": int64 + }...], + "total": int, + "page": int, + "page_size": int, + } + """ + url = '{}/v1/signature'.format(self.server) + if audit_status: + url = '{}?audit_status={}&page={}&page_size={}'.format(url, audit_status, page, page_size) + else: + url = '{}?page={}&page_size={}'.format(url, page, page_size) + return self.__get(url) + + def updateSignature(self, id, signature): + """ + 编辑签名 + * id 签名id : string 类型,必填, + * signature: string 类型,必填, + request 类型array { + "signature": string + } + :return: + """ + url = '{}/v1/signature/{}'.format(self.server, id) + req = {} + req['signature'] = signature + body = json.dumps(req) + return self.__put(url, body) + + def deleteSignature(self, id): + + """ + 删除辑签名 + * id 签名id : string 类型,必填, + * @retrun : 请求成功 HTTP 状态码为 200 + + """ + url = '{}/v1/signature/{}'.format(self.server, id) + return self.__delete(url) + + def createTemplate(self, name, template, type, description, signature_id): + """ + 创建模版 + :param name: 模板名称 string 类型 ,必填 + :param template: 模板内容 string 类型,必填 + :param type: 模板类型 string 类型,必填, + 取值范围为: notification (通知类短信), verification (验证码短信), marketing (营销类短信) + :param description: 申请理由简述 string 类型,必填 + :param signature_id: 已经审核通过的签名 string 类型,必填 + :return: 类型 array { + "template_id": string + } + """ + url = '{}/v1/template'.format(self.server) + req = {} + req['name'] = name + req['template'] = template + req['type'] = type + req['description'] = description + req['signature_id'] = signature_id + body = json.dumps(req) + return self.__post(url, body) + + def queryTemplate(self, audit_status, page=1, page_size=20): + """ + 查询模版 + :param audit_status: 审核状态, 取值范围为: passed (通过), rejected (未通过), reviewing (审核中) + :param page: 页码。默认为 1 + :param page_size: 分页大小。默认为 20 + :return:{ + "items": [{ + "id": string, + "name": string, + "template": string, + "audit_status": string, + "reject_reason": string, + "type": string, + "signature_id": string, // 模版绑定的签名ID + "signature_text": string, // 模版绑定的签名内容 + "created_at": int64, + "updated_at": int64 + }...], + "total": int, + "page": int, + "page_size": int + } + """ + url = '{}/v1/template'.format(self.server) + if audit_status: + url = '{}?audit_status={}&page={}&page_size={}'.format(url, audit_status, page, page_size) + else: + url = '{}?page={}&page_size={}'.format(url, page, page_size) + return self.__get(url) + + def updateTemplate(self, id, name, template, description, signature_id): + """ + 更新模版 + :param id: template_id + :param name: 模板名称 string 类型 ,必填 + :param template: 模板内容 string 类型,必填 + :param description: 申请理由简述 string 类型,必填 + :param signature_id: 已经审核通过的签名 string 类型,必填 + :return: 请求成功 HTTP 状态码为 200 + """ + url = '{}/v1/template/{}'.format(self.server, id) + req = {} + req['name'] = name + req['template'] = template + req['description'] = description + req['signature_id'] = signature_id + body = json.dumps(req) + return self.__put(url, body) + + def deleteTemplate(self, id): + """ + 删除模版 + :param id: template_id + :return: 请求成功 HTTP 状态码为 200 + """ + url = '{}/v1/template/{}'.format(self.server, id) + return self.__delete(url) + + def sendMessage(self, template_id, mobiles, parameters): + """ + 发送短信 + :param template_id: 模板 ID + :param mobiles: 手机号 + :param parameters: 自定义魔法变量,变量设置在创建模板时,参数template指定 + :return:{ + "job_id": string + } + """ + url = '{}/v1/message'.format(self.server) + req = {} + req['template_id'] = template_id + req['mobiles'] = mobiles + req['parameters'] = parameters + body = json.dumps(req) + return self.__post(url, body) + + def __post(self, url, data=None): + headers = {'Content-Type': 'application/json'} + return http._post_with_qiniu_mac_and_headers(url, data, self.auth, headers) + + def __get(self, url, params=None): + headers = {'Content-Type': 'application/x-www-form-urlencoded'} + return http._get_with_qiniu_mac_and_headers(url, params, self.auth, headers) + + def __put(self, url, data=None): + headers = {'Content-Type': 'application/json'} + return http._put_with_qiniu_mac_and_headers(url, data, self.auth, headers) + + def __delete(self, url, data=None): + headers = {'Content-Type': 'application/json'} + return http._delete_with_qiniu_mac_and_headers(url, data, self.auth, headers) From 2547fb75575d13912d8fc0cfe4118384259ffedd Mon Sep 17 00:00:00 2001 From: songfei9315 Date: Mon, 10 Jun 2019 09:48:27 +0800 Subject: [PATCH 2/2] add sms_ch,rm ak,sk --- examples/sms_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/sms_test.py b/examples/sms_test.py index 05d5b957..bb7c1a51 100644 --- a/examples/sms_test.py +++ b/examples/sms_test.py @@ -5,8 +5,8 @@ from qiniu import Sms -access_key = 'bjtWBQXrcxgo7HWwlC_bgHg81j352_GhgBGZPeOW' -secret_key = 'pCav6rTslxP2SIFg0XJmAw53D9PjWEcuYWVdUqAf' +access_key = '' +secret_key = '' # 初始化Auth状态 q = QiniuMacAuth(access_key, secret_key)