Skip to content

Commit

Permalink
release_3.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
coolli(李帅) committed Apr 24, 2018
0 parents commit 72841e9
Show file tree
Hide file tree
Showing 119 changed files with 32,952 additions and 0 deletions.
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 1999-2017 Tencent Ltd.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include README.rst
include LICENSE
recursive-include QcloudApi *
1 change: 1 addition & 0 deletions QcloudApi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = '2.0.12'
Empty file added QcloudApi/common/__init__.py
Empty file.
50 changes: 50 additions & 0 deletions QcloudApi/common/api_exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# -*- coding: utf-8 -*-


class ApiExceptionBase(Exception):
"""
@type message: string
@param message: error describe
"""
def __init__(self, message):
self.message = message

def get_info(self):
return 'Error Message: %s\n' % (self.message)

def __str__(self):
return "ApiExceptionBase %s" % (self.get_info())


class ApiClientParamException(ApiExceptionBase):
def __init__(self, message):
ApiExceptionBase.__init__(self, message)

def __str__(self):
return "ApiClientException %s" % (self.get_info())


class ApiClientNetworkException(ApiExceptionBase):
""" @note: client network exception
"""
def __init__(self, message):
ApiExceptionBase.__init__(self, message)

def __str__(self):
return "ApiClientNetworkException %s" % (self.get_info())


class ApiServerNetworkException(ApiExceptionBase):
""" @note: api server exception
"""
def __init__(self, status=200, header=None, data=""):
if header is None:
header = {}
self.status = status
self.header = header
self.data = data

def __str__(self):
headers = "\n".join("%s: %s" % (k, v) for k, v in self.header.items())
return ("ApiServerNetworkException Status: %s\nHeader: %s\nData: %s\n"
% (self.status, headers, self.data))
138 changes: 138 additions & 0 deletions QcloudApi/common/request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import socket
try:
from http.client import HTTPConnection, BadStatusLine, HTTPSConnection
from urllib.parse import urlparse
except ImportError:
from httplib import HTTPConnection, BadStatusLine, HTTPSConnection
from urlparse import urlparse

from .api_exception import ApiClientNetworkException, ApiClientParamException


class MyHTTPSConnection(HTTPSConnection):
def __init__(self, host, port=None):
self.has_proxy = False
self.request_host = host
https_proxy = (os.environ.get('https_proxy')
or os.environ.get('HTTPS_PROXY'))
if https_proxy:
url = urlparse(https_proxy)
if not url.hostname:
url = urlparse('https://' + https_proxy)
host = url.hostname
port = url.port
self.has_proxy = True
HTTPSConnection.__init__(self, host, port)
self.request_length = 0

def send(self, astr):
HTTPSConnection.send(self, astr)
self.request_length += len(astr)

def request(self, method, url, body=None, headers={}):
self.request_length = 0
if self.has_proxy:
self.set_tunnel(self.request_host, 443)
HTTPSConnection.request(self, method, url, body, headers)


class ApiRequest(object):
def __init__(self, host, req_timeout=90, debug=False):
self.conn = MyHTTPSConnection(host)
self.req_timeout = req_timeout
self.keep_alive = False
self.debug = debug
self.request_size = 0
self.response_size = 0

def set_req_timeout(self, req_timeout):
self.req_timeout = req_timeout

def is_keep_alive(self):
return self.keep_alive

def set_debug(self, debug):
self.debug = debug

def send_request(self, req_inter):
try:
if self.debug:
print("SendRequest %s" % req_inter)
if req_inter.method == 'GET':
req_inter_url = '%s?%s' % (req_inter.uri, req_inter.data)
self.conn.request(req_inter.method, req_inter_url,
None, req_inter.header)
elif req_inter.method == 'POST':
self.conn.request(req_inter.method, req_inter.uri,
req_inter.data, req_inter.header)
else:
raise ApiClientParamException(
'Method only support (GET, POST)')

self.conn.sock.settimeout(self.req_timeout)
self.conn.sock.setsockopt(socket.IPPROTO_TCP,
socket.TCP_NODELAY, 1)
try:
http_resp = self.conn.getresponse()
except BadStatusLine:
# open another connection when keep-alive timeout
# httplib will not handle keep-alive timeout,
# so we must handle it ourself
if self.debug:
print("keep-alive timeout, reopen connection")
self.conn.close()

self.conn.request(req_inter.method, req_inter.uri,
req_inter.data, req_inter.header)
self.conn.sock.settimeout(self.req_timeout)
self.conn.sock.setsockopt(socket.IPPROTO_TCP,
socket.TCP_NODELAY, 1)
http_resp = self.conn.getresponse()
headers = dict(http_resp.getheaders())
resp_inter = ResponseInternal(status=http_resp.status,
header=headers,
data=http_resp.read())
self.request_size = self.conn.request_length
self.response_size = len(resp_inter.data)
if not self.is_keep_alive():
self.conn.close()
if self.debug:
print(("GetResponse %s" % resp_inter))
return resp_inter
except Exception as e:
self.conn.close()
raise ApiClientNetworkException(str(e))


class RequestInternal(object):
def __init__(self, host="", method="", uri="", header=None, data=""):
if header is None:
header = {}
self.host = host
self.method = method
self.uri = uri
self.header = header
self.data = data

def __str__(self):
headers = "\n".join("%s: %s" % (k, v) for k, v in self.header.items())
return ("Host: %s\nMethod: %s\nUri: %s\nHeader: %s\nData: %s\n"
% (self.host, self.method, self.uri, headers, self.data))


class ResponseInternal(object):
def __init__(self, status=0, header=None, data=""):
if header is None:
header = {}
self.status = status
self.header = header
self.data = data

def __str__(self):
headers = "\n".join("%s: %s" % (k, v) for k, v in self.header.items())
return ("Status: %s\nHeader: %s\nData: %s\n"
% (self.status, headers, self.data))
42 changes: 42 additions & 0 deletions QcloudApi/common/sign.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-

import binascii
import hashlib
import hmac
import sys


class Sign(object):
def __init__(self, secretId, secretKey):
self.secretId = secretId
self.secretKey = secretKey
if sys.version_info[0] > 2:
self.Py2 = False
self.secretKey = bytes(self.secretKey, 'utf-8')
else:
self.Py2 = True

def make(self, requestHost, requestUri, params,
method='POST', sign_method='HmacSHA1'):
p = {}
for k in params:
if method == 'POST' and str(params[k])[0:1] == '@':
continue
p[k.replace('_', '.')] = params[k]
ps = '&'.join('%s=%s' % (k, p[k]) for k in sorted(p))

msg = '%s%s%s?%s' % (method.upper(), requestHost, requestUri, ps)
if not self.Py2:
msg = bytes(msg, 'utf-8')

if sign_method == 'HmacSHA256':
digestmod = hashlib.sha256
else:
digestmod = hashlib.sha1

hashed = hmac.new(self.secretKey, msg, digestmod)
base64 = binascii.b2a_base64(hashed.digest())[:-1]
if not self.Py2:
base64 = base64.decode()

return base64
Empty file added QcloudApi/modules/__init__.py
Empty file.
19 changes: 19 additions & 0 deletions QcloudApi/modules/account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 1999-2017 Tencent Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from QcloudApi.modules import base


class Account(base.Base):
requestHost = 'account.api.qcloud.com'
19 changes: 19 additions & 0 deletions QcloudApi/modules/apigateway.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 1999-2017 Tencent Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from QcloudApi.modules import base


class APIGateway(base.Base):
requestHost = 'apigateway.api.qcloud.com'
23 changes: 23 additions & 0 deletions QcloudApi/modules/athena.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 1999-2018 Tencent Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from QcloudApi.modules import base


class Athena(base.Base):
"""Financial Intelligent Customer Service.
document: https://cloud.tencent.com/document/product/671
"""
requestHost = 'athena.api.qcloud.com'
Loading

0 comments on commit 72841e9

Please sign in to comment.