diff --git a/setup.py b/setup.py index 77958c7..6aa8610 100644 --- a/setup.py +++ b/setup.py @@ -2,14 +2,14 @@ # -*-coding:utf-8-*- """" author : shenshuo -date : 2023年3月8日 +date : 2024年12月12日 desc : CODO SDK """ import sys from distutils.core import setup -VERSION = '1.0.7' +VERSION = '1.0.8' if sys.version_info < (2, 7) or (3, 0) <= sys.version_info < (3, 6): print('This program requires at least Python 2.7 or 3.6 to run.') diff --git a/websdk2/client.py b/websdk2/client.py index 24c4987..bf4ccc7 100644 --- a/websdk2/client.py +++ b/websdk2/client.py @@ -10,6 +10,7 @@ import json import requests from urllib.parse import urlencode +from typing import Union, Optional import logging from .consts import const from .configs import configs @@ -98,32 +99,63 @@ async def _implementation_of_do_action(self, **kwargs): # timeout=self.request_timeout) as response: # return await response.read() - def with_params_data_url(self, **kwargs): - # 重新组装URL - url = "{}{}".format(self.endpoint, kwargs['url']) - kwargs['url'] = url + # def with_params_data_url(self, **kwargs): + # # 重新组装URL + # url = "{}{}".format(self.endpoint, kwargs['url']) + # kwargs['url'] = url + # + # if not kwargs['method']: kwargs['method'] = 'GET' + # + # # logging.debug(f"with_params_data_url {kwargs}") + # body = kwargs.get('body', {}) + # req_json = kwargs.get('json') + # + # if kwargs['method'] in ['POST', 'post', 'PATCH', 'patch', 'PUT', 'put']: + # if not (body or req_json): + # raise TypeError('method {}, body can not be empty'.format(kwargs['method'])) + # else: + # if not isinstance(body, dict): + # json.loads(body) + # + # if body and isinstance(body, dict): kwargs['body'] = json.dumps(body) + # + # params = kwargs.get('params') + # if params: kwargs['url'] = "{}?{}".format(url, urlencode(params)) + # + # if not self.headers: self.headers = kwargs.get('headers', {}) + # + # if kwargs['method'] not in ['GET', 'get']: self.headers['Content-Type'] = 'application/json' + # + # return kwargs - if not kwargs['method']: kwargs['method'] = 'GET' + def with_params_data_url(self, **kwargs) -> dict: + endpoint = self.endpoint.strip("'").strip('"') + kwargs['url'] = f"{endpoint}{kwargs.get('url', '')}" + kwargs['method'] = kwargs.get('method', 'GET').upper() - # logging.debug(f"with_params_data_url {kwargs}") - body = kwargs.get('body', {}) - req_json = kwargs.get('json') + body: Union[dict, str] = kwargs.get('body', {}) + req_json: Optional[dict] = kwargs.get('json') - if kwargs['method'] in ['POST', 'post', 'PATCH', 'patch', 'PUT', 'put']: + if kwargs['method'] in {'POST', 'PATCH', 'PUT'}: if not (body or req_json): - raise TypeError('method {}, body can not be empty'.format(kwargs['method'])) - else: - if not isinstance(body, dict): - json.loads(body) + raise TypeError(f"Method {kwargs['method']} requires a non-empty body or JSON payload.") + if body and not isinstance(body, dict): + try: + body = json.loads(body) + except json.JSONDecodeError as e: + raise TypeError(f"Invalid JSON body: {e}") - if body and isinstance(body, dict): kwargs['body'] = json.dumps(body) + if body and isinstance(body, dict): + kwargs['body'] = json.dumps(body) - params = kwargs.get('params') - if params: kwargs['url'] = "{}?{}".format(url, urlencode(params)) + params: Optional[dict] = kwargs.get('params') + if params: + kwargs['url'] = f"{kwargs['url']}?{urlencode(params)}" - if not self.headers: self.headers = kwargs.get('headers', {}) + kwargs['headers'] = kwargs.get('headers', self.headers or {}) - if kwargs['method'] not in ['GET', 'get']: self.headers['Content-Type'] = 'application/json' + if kwargs['method'] != 'GET': + kwargs['headers'].setdefault('Content-Type', 'application/json') return kwargs