Skip to content

Commit

Permalink
2024年12月12日 优化应用请求地址拼写
Browse files Browse the repository at this point in the history
  • Loading branch information
ss1917 committed Dec 12, 2024
1 parent 4f1e948 commit 415370f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 20 deletions.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down
68 changes: 50 additions & 18 deletions websdk2/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 415370f

Please sign in to comment.