Skip to content

Commit cdd2b80

Browse files
author
fengyikai
committed
星曜裸金属服务器(epc):星海支持智启
1 parent 8e9a3fe commit cdd2b80

3 files changed

Lines changed: 69 additions & 9 deletions

File tree

ksyun/common/abstract_client.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,9 @@ def _get_endpoint(self):
229229

230230

231231
def call(self, action, params, options=None):
232-
req = RequestInternal(self._get_endpoint(), self.profile.httpProfile.reqMethod, self._requestPath)
232+
# Use custom path from HttpProfile if available, otherwise use default _requestPath
233+
request_path = self.profile.httpProfile.path if self.profile.httpProfile.path else self._requestPath
234+
req = RequestInternal(self._get_endpoint(), self.profile.httpProfile.reqMethod, request_path)
233235
self._build_req_inter(action, params, req, options)
234236
resp_inter = self.request.send_request(req)
235237
self._check_status(resp_inter)
@@ -252,9 +254,11 @@ def call_octet_stream(self, action, headers, body):
252254
if self.profile.httpProfile.reqMethod != "POST":
253255
raise SDKError("ClientError", "Invalid request method.")
254256

257+
# Use custom path from HttpProfile if available, otherwise use default _requestPath
258+
request_path = self.profile.httpProfile.path if self.profile.httpProfile.path else self._requestPath
255259
req = RequestInternal(self._get_endpoint(),
256260
self.profile.httpProfile.reqMethod,
257-
self._requestPath)
261+
request_path)
258262
for key in headers:
259263
req.header[key] = headers[key]
260264
req.data = body

ksyun/common/http/request.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,36 @@ def __init__(self, host, req_timeout=60, debug=False, proxy=None, is_http=False,
7272
self.request_size = 0
7373
self.response_size = 0
7474

75+
def _build_url(self, base_url, path=None, query_params=None):
76+
"""Build complete URL from base_url, path and query parameters.
77+
78+
Ensures no double slashes between domain and path.
79+
80+
:param base_url: Base URL (scheme + domain)
81+
:type base_url: str
82+
:param path: URL path
83+
:type path: str
84+
:param query_params: Query parameters
85+
:type query_params: str
86+
:return: Complete URL
87+
:rtype: str
88+
"""
89+
# Start with base URL, remove trailing slash if present
90+
url = base_url.rstrip('/')
91+
92+
# Add path if provided
93+
if path:
94+
# Ensure path starts with /
95+
if not path.startswith('/'):
96+
path = '/' + path
97+
url += path
98+
99+
# Add query parameters if provided
100+
if query_params:
101+
url += '?' + query_params
102+
103+
return url
104+
75105
def set_req_timeout(self, req_timeout):
76106
self.req_timeout = req_timeout
77107

@@ -89,14 +119,14 @@ def _request(self, req_inter):
89119
req_inter.header["Connection"] = "Keep-Alive"
90120
if self.debug:
91121
logger.debug("SendRequest %s" % req_inter)
122+
92123
if req_inter.method == 'GET':
93-
req_inter_url = '%s?%s' % (self.host, req_inter.data)
124+
# For GET requests, parameters are in the query string (req_inter.data)
125+
req_inter_url = self._build_url(self.host, req_inter.uri, req_inter.data)
94126
return self.conn.request(req_inter.method, req_inter_url, None, req_inter.header, req_inter.auth)
95127
elif req_inter.method == 'POST' or req_inter.method == 'PUT' or req_inter.method == 'DELETE':
96-
if req_inter.uri_params:
97-
req_inter_url = '%s?%s' % (self.host, req_inter.uri_params)
98-
else:
99-
req_inter_url = self.host
128+
# For POST/PUT/DELETE, use uri_params for query string if present
129+
req_inter_url = self._build_url(self.host, req_inter.uri, req_inter.uri_params if req_inter.uri_params else None)
100130
return self.conn.request(req_inter.method, req_inter_url, req_inter.data, req_inter.header, req_inter.auth)
101131
else:
102132
raise KsyunSDKException("ClientParamsError", 'Method only support (GET, POST, PUT, DELETE)')
@@ -115,7 +145,7 @@ def send_request(self, req_inter):
115145

116146

117147
class RequestInternal(object):
118-
def __init__(self, host="", method="", uri="", header=None, data="",auth=None):
148+
def __init__(self, host="", method="", uri="", header=None, data="", auth=None):
119149
if header is None:
120150
header = {}
121151
self.host = host
@@ -124,6 +154,7 @@ def __init__(self, host="", method="", uri="", header=None, data="",auth=None):
124154
self.header = header
125155
self.data = data
126156
self.auth = auth
157+
self.uri_params = None # Query parameters for POST/PUT/DELETE requests
127158

128159
def __str__(self):
129160
headers = "\n".join("%s: %s" % (k, v) for k, v in self.header.items())

ksyun/common/profile/http_profile.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class HttpProfile(object):
1717
scheme = "https"
1818

1919
def __init__(self, protocol=None, endpoint=None, reqMethod="POST", reqTimeout=60,
20-
keepAlive=False, proxy=None, rootDomain=None, certification=None):
20+
keepAlive=False, proxy=None, rootDomain=None, certification=None, path=None):
2121
"""HTTP profile.
2222
:param protocol: http or https, default is https.
2323
:type protocol: str
@@ -29,6 +29,8 @@ def __init__(self, protocol=None, endpoint=None, reqMethod="POST", reqTimeout=60
2929
:type reqTimeout: int
3030
:param rootDomain: The root domain to access, like: api.ksyun.com.
3131
:type rootDomain: str
32+
:param path: Custom URL path like /api/xxx/xxx. Query parameters will be automatically removed.
33+
:type path: str
3234
"""
3335
self.endpoint = endpoint
3436
self.reqTimeout = 60 if reqTimeout is None else reqTimeout
@@ -40,3 +42,26 @@ def __init__(self, protocol=None, endpoint=None, reqMethod="POST", reqTimeout=60
4042
self.proxy = proxy
4143
self.rootDomain = "api.ksyun.com" if rootDomain is None else rootDomain
4244
self.certification = certification
45+
# Clean path: remove query parameters if present
46+
self.path = self._clean_path(path) if path else None
47+
48+
def _clean_path(self, path):
49+
"""Clean path by removing query parameters and normalizing slashes.
50+
51+
:param path: The path to clean
52+
:type path: str
53+
:return: Cleaned path
54+
:rtype: str
55+
"""
56+
if not path:
57+
return None
58+
59+
# Remove query parameters (everything after ?)
60+
if '?' in path:
61+
path = path.split('?')[0]
62+
63+
# Ensure path starts with /
64+
if not path.startswith('/'):
65+
path = '/' + path
66+
67+
return path

0 commit comments

Comments
 (0)