forked from FISCO-BCOS/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.py
More file actions
35 lines (25 loc) · 943 Bytes
/
request.py
File metadata and controls
35 lines (25 loc) · 943 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import requests
import lru
from utils.caching import (
generate_cache_key,
)
def _remove_session(key, session):
session.close()
_session_cache = lru.LRU(8, callback=_remove_session)
def _get_session(*args, **kwargs):
cache_key = generate_cache_key((args, kwargs))
if cache_key not in _session_cache:
_session_cache[cache_key] = requests.Session()
return _session_cache[cache_key]
def make_post_request(endpoint_uri, method, params, data, *args, **kwargs):
kwargs.setdefault('timeout', 10)
session = _get_session(endpoint_uri, method, params, kwargs)
response = session.post(endpoint_uri, data=data, *args, **kwargs)
response.raise_for_status()
#增加一些兼容性逻辑
if hasattr(response,"cb_context"):
return response.cb_context
if hasattr(response, "context"):
return response.context
if hasattr(response, "content"):
return response.content