首页 > 解决方案 > 通过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")

标签: pythonsdk

解决方案


推荐阅读