python - 通过rest api调用编写python SDK并且需要知道request.session是否应该关闭
问题描述
我正在为我的 rest api 项目开发一个 python sdk。因此,当我初始化客户端时,会创建 request.session 来管理所有进一步请求的标头。我没有在任何需要关闭会话的地方关闭这个会话?
class Api(object):
def __init__(self, client_id, client_secret, url=server):
self.api_url = url
self.api_client_id = client_id
self.api_client_secret = client_secret
self.baseHeaders = {
"Accept": "application/json",
"Content-Type": "application/json",
"X-API-Key": self.api_client_id,
"X-API-Client": self.api_client_secret,
}
default_session = requests.Session()
default_session.headers = self.baseHeaders
self.session = default_session
def _make_request(
self, method=None, url=None, data=None, headers=None, params=None, files=None
):
url = urljoin(self.api_url, url)
try:
response = self.session.request(
method=method,
url=url,
data=data,
headers=headers,
params=params,
files=files,
)
except Exception as e:
pass
content = response.content
return json_body
用法
from client import APIClient
from app import CreateUser
api_client = APIClient("fake_user", "fake_password")
sign_resource = CreateUser(api_client, "test")
解决方案
推荐阅读
- react-native - react-native 不使用 startInLoadingState={true} 在 WebView 中呈现远程 PDF
- javascript - 从 Cloud Firebase 获取价值并显示元素取决于 if/else 语句
- dns - google.com 的 DNS 请求在不同的时间返回不同的 a_record,即使名称服务器每次都相同
- python-3.x - 如何在函数中使用 calendar.leapdays() 来返回给定列表中有多少闰年?
- linux - 如何启动将启动另一个 bash 文件的 bash 脚本?
- google-apps-script - 检查范围内的空白单元格
- ruby-on-rails - Ruby on Rails 关联对象
- c++ - 快速的 2D 线条绘制
- forms - 羊驼表单添加按钮到文本字段
- php - 来自两个数组的对象数组