Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: default use http session #293

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion aliyun/log/logclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ def __init__(self, endpoint, accessKeyId=None, accessKey=None, securityToken=Non
self._auth = make_auth(StaticCredentialsProvider(accessKeyId, accessKey, securityToken),
auth_version, region)
self._get_logs_v2_enabled = True
self._session = requests.Session()
self._use_long_live_conn = True

def _replace_credentials(self):
delta = time.time() - self._last_refresh
Expand Down Expand Up @@ -247,10 +249,16 @@ def _loadJson(resp_status, resp_header, resp_body, requestId):
'Bad json format:\n"%s"' % resp_body + '\n' + repr(ex),
requestId, resp_status, resp_header, resp_body)

def _get_http_sender(self, method):
if self._use_long_live_conn:
return getattr(self._session, method.lower())
return getattr(requests, method.lower())

def _getHttpResponse(self, method, url, params, body, headers): # ensure method, url, body is str
try:
headers['User-Agent'] = self._user_agent
r = getattr(requests, method.lower())(url, params=params, data=body, headers=headers, timeout=self._timeout)
http_sender = self._get_http_sender(method)
r = http_sender(url, params=params, data=body, headers=headers, timeout=self._timeout)
return r.status_code, r.content, r.headers
except Exception as ex:
raise LogException('LogRequestError', str(ex))
Expand Down
2 changes: 1 addition & 1 deletion aliyun/log/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.9.6'
__version__ = '0.9.7'

import sys
OS_VERSION = str(sys.platform)
Expand Down