Skip to content

Commit fc93235

Browse files
committed
support python 3,7
APPx: Feature or Bugfix:
1 parent fe9ead3 commit fc93235

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+3386
-69
lines changed
-1 Bytes
Binary file not shown.
File renamed without changes.

python27/Push-CA-Root.pem

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh
3+
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
4+
d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD
5+
QTAeFw0wNjExMTAwMDAwMDBaFw0zMTExMTAwMDAwMDBaMGExCzAJBgNVBAYTAlVT
6+
MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j
7+
b20xIDAeBgNVBAMTF0RpZ2lDZXJ0IEdsb2JhbCBSb290IENBMIIBIjANBgkqhkiG
8+
9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4jvhEXLeqKTTo1eqUKKPC3eQyaKl7hLOllsB
9+
CSDMAZOnTjC3U/dDxGkAV53ijSLdhwZAAIEJzs4bg7/fzTtxRuLWZscFs3YnFo97
10+
nh6Vfe63SKMI2tavegw5BmV/Sl0fvBf4q77uKNd0f3p4mVmFaG5cIzJLv07A6Fpt
11+
43C/dxC//AH2hdmoRBBYMql1GNXRor5H4idq9Joz+EkIYIvUX7Q6hL+hqkpMfT7P
12+
T19sdl6gSzeRntwi5m3OFBqOasv+zbMUZBfHWymeMr/y7vrTC0LUq7dBMtoM1O/4
13+
gdW7jVg/tRvoSSiicNoxBN33shbyTApOB6jtSj1etX+jkMOvJwIDAQABo2MwYTAO
14+
BgNVHQ8BAf8EBAMCAYYw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71uL0gRgykmm
33+
KPZpO/bLyCiR5Z2KYVc3rHQU3HTgOu5yLy6c+9C7v/U9AOEGM+iCK65TpjoWc4zd
34+
QQ4gOsC0p6Hpsk+QLjJg6VfLuQSSaGjlOCZgdbKfd/+RFO+uIEn8rUAVSNECMWEZ
35+
XriX7613t2Saer9fwRPvm2L7DWzgVGkWqQPabumDk3F2xmmFghcCAwEAAaNCMEAw
36+
DgYDVR0PAQH/BAQDAgEGMA8GA1UdEwEB/wQFMAMBAf8wHQYDVR0OBBYEFI/wS3+o
37+
LkUkrk1Q+mOai97i3Ru8MA0GCSqGSIb3DQEBCwUAA4IBAQBLQNvAUKr+yAzv95ZU
38+
RUm7lgAJQayzE4aGKAczymvmdLm6AC2upArT9fHxD4q/c2dKg8dEe3jgr25sbwMp
39+
jjM5RcOO5LlXbKr8EpbsU8Yt5CRsuZRj+9xTaGdWPoO4zzUhw8lo/s7awlOqzJCK
40+
6fBdRoyV3XpYKBovHd7NADdBj+1EbddTKJd+82cEHhXXipa0095MJ6RMG3NzdvQX
41+
mcIfeg7jLQitChws/zyrVQ4PkX4268NXSb7hLi18YIvDQVETI53O9zJrlAGomecs
42+
Mx86OyXShkDOOyyGeMlhLxS67ttVb9+E7gUJTb0o2HLO02JQZR7rkpeDMdmztcpH
43+
WD9f
44+
-----END CERTIFICATE-----
45+

README.md renamed to python27/README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* [Configuration ](#configuration )
99
* [Supported Environments](#supported-environments)
1010
* [Sample Code](#sample-code)
11+
* [Libraries](#Libraries)
1112
* [License](#license)
1213

1314

@@ -30,7 +31,7 @@ python setup.py install
3031
```
3132

3233
## Supported Environments
33-
For pushkit-python-sample, We currently support Python 2.7 and JetBrains PyCharm are recommended.
34+
For pushkit-python-sample, We currently support Python 2.7/3.7 and JetBrains PyCharm are recommended.
3435

3536

3637
## Configuration
@@ -86,6 +87,11 @@ Code location: examples/send_apns_message.py
8687
8) Send a test message.
8788
Code location: examples/send_test_message.py
8889

90+
## Libraries
91+
| Library | Site
92+
| ----------------- | --------------------------------------------------- |
93+
| requests | https://requests.readthedocs.io/en/master/ |
94+
| six | https://six.readthedocs.io/ |
8995
## License
9096

9197
pushkit Python sample is licensed under the [Apache License, version 2.0](http://www.apache.org/licenses/LICENSE-2.0).
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
requests >= 2.20.1
2-
six == 1.1.3.0
2+
six == 1.13.0

setup.py renamed to python27/setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
license='Apache License 2.0',
4040
keywords='huawei cloud development',
4141
install_requires=install_requires,
42-
packages=['push_admin'],
42+
packages=['src/push_admin'],
4343
python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*',
4444
classifiers=[
4545
'Development Status :: 5 - Production/Stable',

python27/src/__init__.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# -*-coding:utf-8-*-
2+
#
3+
# Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.

src/push_admin/__init__.py renamed to python27/src/push_admin/__init__.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@
2424
_DEFAULT_APP_NAME = 'DEFAULT'
2525

2626

27-
def initialize_app(appid, appsecret, token_server='https://oauth-login.cloud.huawei.com/oauth2/v2/token',
27+
def initialize_app(appid, appsecret, appid_push=None, token_server='https://oauth-login.cloud.huawei.com/oauth2/v2/token',
2828
push_open_url='https://push-api.cloud.huawei.com'):
2929
"""
3030
Initializes and returns a new App instance.
31-
:param appid: appid parameters obtained by developer alliance applying for Push service
32-
:param appsecret: appsecret parameters obtained by developer alliance applying for Push service
31+
:param appid_at: appid parameters obtained by developer alliance applying for Push service
32+
:param appsecret_at: appsecret parameters obtained by developer alliance applying for Push service
33+
:param appid_push: the application Id in the URL
3334
:param token_server: Oauth server URL
3435
:param push_open_url: push open API URL
3536
"""
36-
app = _app.App(appid, appsecret, token_server=token_server, push_open_url=push_open_url)
37+
app = _app.App(appid, appsecret, appid_push, token_server=token_server, push_open_url=push_open_url)
3738

3839
with _apps_lock:
3940
if appid not in _apps:

src/push_admin/_app.py renamed to python27/src/push_admin/_app.py

+30-19
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ class App(object):
2727
JSON_ENCODER = _message_serializer.MessageSerializer()
2828

2929
@classmethod
30-
def _send_to_server(cls, headers, body, url):
30+
def _send_to_server(cls, headers, body, url, verify_peer=False):
3131
try:
3232
msg_body = json.dumps(body)
33-
response = _http.post(url, msg_body, headers)
33+
response = _http.post(url, msg_body, headers, verify_peer)
3434

3535
if response.status_code is not 200:
3636
raise ApiCallError('http status code is {0} in send.'.format(response.status_code))
@@ -42,11 +42,15 @@ def _send_to_server(cls, headers, body, url):
4242
except Exception as e:
4343
raise ApiCallError('caught exception when send. {0}'.format(e))
4444

45-
def __init__(self, app_id, app_secret, token_server='https://oauth-login.cloud.huawei.com/oauth2/v2/token',
45+
def __init__(self, appid_at, app_secret, appid_push, token_server='https://oauth-login.cloud.huawei.com/oauth2/v2/token',
4646
push_open_url='https://push-api.cloud.huawei.com'):
4747
"""class init"""
48-
self.app_id = app_id
49-
self.app_secret = app_secret
48+
self.appid_at = appid_at
49+
self.app_secret_at = app_secret
50+
if appid_push is None:
51+
self.appid_push = appid_at
52+
else:
53+
self.appid_push = appid_push
5054
self.token_expired_time = 0
5155
self.access_token = None
5256
self.token_server = token_server
@@ -56,20 +60,24 @@ def __init__(self, app_id, app_secret, token_server='https://oauth-login.cloud.h
5660
self.hw_push_topic_unsub_server = self.push_open_url + "/v1/{0}/topic:unsubscribe"
5761
self.hw_push_topic_query_server = self.push_open_url + "/v1/{0}/topic:list"
5862

59-
def _refresh_token(self):
60-
"""refresh access token"""
63+
def _refresh_token(self, verify_peer=False):
64+
"""refresh access token
65+
:param verify_peer: (optional) Either a boolean, in which case it controls whether we verify
66+
the server's TLS certificate, or a string, in which case it must be a path
67+
to a CA bundle to use. Defaults to ``True``.
68+
"""
6169
headers = dict()
6270
headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'
6371

6472
params = dict()
6573
params['grant_type'] = 'client_credentials'
66-
params['client_secret'] = self.app_secret
67-
params['client_id'] = self.app_id
74+
params['client_secret'] = self.app_secret_at
75+
params['client_id'] = self.appid_at
6876

6977
msg_body = urllib.urlencode(params)
7078

7179
try:
72-
response = _http.post(self.token_server, msg_body, headers)
80+
response = _http.post(self.token_server, msg_body, headers, verify_peer=verify_peer)
7381

7482
if response.status_code is not 200:
7583
return False, 'http status code is {0} in get access token'.format(response.status_code)
@@ -91,9 +99,9 @@ def _is_token_expired(self):
9199
return True
92100
return long(round(time.time() * 1000)) >= self.token_expired_time
93101

94-
def _update_token(self):
102+
def _update_token(self, verify_peer=False):
95103
if self._is_token_expired() is True:
96-
result, reason = self._refresh_token()
104+
result, reason = self._refresh_token(verify_peer)
97105
if result is False:
98106
raise ApiCallError(reason)
99107

@@ -103,24 +111,27 @@ def _create_header(self):
103111
headers['Authorization'] = 'Bearer {0}'.format(self.access_token)
104112
return headers
105113

106-
def send(self, message, validate_only):
114+
def send(self, message, validate_only, **kwargs):
107115
"""
108116
Sends the given message Huawei Cloud Messaging (HCM)
109117
:param message:
110118
:param validate_only:
119+
:param kwargs:
120+
verify_peer: HTTPS server identity verification, use library 'certifi'
111121
:return:
112122
response dict: response body dict
113123
:raise:
114124
ApiCallError: failure reason
115125
"""
116-
self._update_token()
126+
verify_peer = kwargs['verify_peer']
127+
self._update_token(verify_peer)
117128
headers = self._create_header()
118-
url = self.hw_push_server.format(self.app_id)
129+
url = self.hw_push_server.format(self.appid_push)
119130
msg_body_dict = dict()
120131
msg_body_dict['validate_only'] = validate_only
121132
msg_body_dict['message'] = App.JSON_ENCODER.default(message)
122133

123-
return App._send_to_server(headers, msg_body_dict, url)
134+
return App._send_to_server(headers, msg_body_dict, url, verify_peer)
124135

125136
def subscribe_topic(self, topic, token_list):
126137
"""
@@ -130,7 +141,7 @@ def subscribe_topic(self, topic, token_list):
130141
"""
131142
self._update_token()
132143
headers = self._create_header()
133-
url = self.hw_push_topic_sub_server.format(self.app_id)
144+
url = self.hw_push_topic_sub_server.format(self.appid_push)
134145
msg_body_dict = {'topic': topic, 'tokenArray': token_list}
135146
return App._send_to_server(headers, msg_body_dict, url)
136147

@@ -143,7 +154,7 @@ def unsubscribe_topic(self, topic, token_list):
143154
"""
144155
self._update_token()
145156
headers = self._create_header()
146-
url = self.hw_push_topic_unsub_server.format(self.app_id)
157+
url = self.hw_push_topic_unsub_server.format(self.appid_push)
147158
msg_body_dict = {'topic': topic, 'tokenArray': token_list}
148159
return App._send_to_server(headers, msg_body_dict, url)
149160

@@ -154,7 +165,7 @@ def query_subscribe_list(self, token):
154165
"""
155166
self._update_token()
156167
headers = self._create_header()
157-
url = self.hw_push_topic_query_server.format(self.app_id)
168+
url = self.hw_push_topic_query_server.format(self.appid_push)
158169
msg_body_dict = {'token': token}
159170
return App._send_to_server(headers, msg_body_dict, url)
160171

python27/src/push_admin/_http.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import requests
18+
19+
20+
def post(url, req_body, headers=None, verify_peer=False):
21+
""" post http request to slb service
22+
:param url: url path
23+
:param req_body: http request body
24+
:param headers: http headers
25+
:param verify_peer: (optional) Either a boolean, in which case it controls whether we verify
26+
the server's TLS certificate, or a string, in which case it must be a path
27+
to a CA bundle to use. Defaults to ``True``.
28+
:return:
29+
success return response
30+
fali return None
31+
"""
32+
try:
33+
response = requests.post(url, data=req_body, headers=headers, timeout=10, verify=verify_peer)
34+
return response
35+
36+
except Exception as e:
37+
raise ValueError('caught exception when post {0}. {1}'.format(url, e))
38+
39+
40+
def _format_http_text(method, url, headers, body):
41+
"""
42+
print http head and body for request or response
43+
44+
For examples: _format_http_text('', title, response.headers, response.text)
45+
"""
46+
result = method + ' ' + url + '\n'
47+
48+
if headers is not None:
49+
for key, value in headers.items():
50+
result = result + key + ': ' + value + '\n'
51+
52+
result = result + body
53+
return result
54+
55+
File renamed without changes.

src/push_admin/messaging.py renamed to python27/src/push_admin/messaging.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,21 @@
5050
ApiCallError = _app.ApiCallError
5151

5252

53-
def send_message(message, validate_only=False, app_id=None):
53+
def send_message(message, validate_only=False, app_id=None, verify_peer=False):
5454
"""
5555
Sends the given message Huawei Cloud Messaging (HCM)
5656
:param message: An instance of ``messaging.Message``.
5757
:param validate_only: A boolean indicating whether to run the operation in dry run mode (optional).
5858
:param app_id: app id parameters obtained by developer alliance applying for Push service (optional).
59+
:param verify_peer: (optional) Either a boolean, in which case it controls whether we verify
60+
the server's TLS certificate, or a string, in which case it must be a path
61+
to a CA bundle to use. Defaults to ``True``.
5962
:return: SendResponse
6063
Raises:
6164
ApiCallError: If an error occurs while sending the message to the HCM service.
6265
"""
6366
try:
64-
response = push_admin.get_app(app_id).send(message, validate_only)
67+
response = push_admin.get_app(app_id).send(message, validate_only, verify_peer=verify_peer)
6568
return SendResponse(response)
6669
except Exception as e:
6770
raise ApiCallError(repr(e))
@@ -112,8 +115,8 @@ class SendResponse(object):
112115
"""
113116
def __init__(self, response=None):
114117
try:
115-
self._code = response['code']
116118
self._msg = response['msg']
119+
self._code = response['code']
117120
self._requestId = response['requestId']
118121
except Exception as e:
119122
raise ValueError(format(repr(e)))

python27/test/__init__.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# -*-coding:utf-8-*-
2+
#
3+
# Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
File renamed without changes.

0 commit comments

Comments
 (0)