@@ -110,6 +110,20 @@ def has_request_body(method: str):
110
110
return not (method == "GET" or method == "DELETE" or method == "HEAD" or method == "OPTIONS" )
111
111
112
112
113
+ def combine_with (base_url : str , relative_url : str ):
114
+ if not base_url :
115
+ return relative_url
116
+ if not relative_url :
117
+ return base_url
118
+ # We want to handle relative urls different by maintaining the base_url path at all times
119
+ temp_relative_url = relative_url
120
+ temp_base_url = base_url
121
+ if not temp_base_url .endswith ("/" ):
122
+ temp_base_url += "/"
123
+ if temp_relative_url .startswith ("/" ):
124
+ temp_relative_url = temp_relative_url [1 :]
125
+ return urljoin (temp_base_url , temp_relative_url )
126
+
113
127
@dataclass_json
114
128
@dataclass
115
129
class SendContext :
@@ -213,11 +227,11 @@ def __init__(self, base_url:str):
213
227
214
228
def set_base_path (self , base_path :str = '' ):
215
229
if not base_path :
216
- self .reply_base_url = urljoin (self .base_url , 'json/reply' ) + "/"
217
- self .oneway_base_url = urljoin (self .base_url , 'json/oneway' ) + "/"
230
+ self .reply_base_url = combine_with (self .base_url , 'json/reply' ) + "/"
231
+ self .oneway_base_url = combine_with (self .base_url , 'json/oneway' ) + "/"
218
232
else :
219
- self .reply_base_url = urljoin (self .base_url , base_path ) + "/"
220
- self .oneway_base_url = urljoin (self .base_url , base_path ) + "/"
233
+ self .reply_base_url = combine_with (self .base_url , base_path ) + "/"
234
+ self .oneway_base_url = combine_with (self .base_url , base_path ) + "/"
221
235
return self
222
236
223
237
def set_credentials (self , username :str , password :str ):
@@ -247,7 +261,7 @@ def refresh_token_cookie(self):
247
261
return self ._get_cookie_value (SS_REFRESH_TOKEN_COOKIE )
248
262
249
263
def create_url_from_dto (self , method : str , request : Any ):
250
- url = urljoin (self .reply_base_url , nameof (request ))
264
+ url = combine_with (self .reply_base_url , nameof (request ))
251
265
if not has_request_body (method ):
252
266
url = append_querystring (url , to_dict (request , key_case = clean_camelcase ))
253
267
return url
@@ -276,7 +290,7 @@ def head(self, request: IReturn[T], args: Dict[str, Any] = None) -> T:
276
290
def to_absolute_url (self , path_or_url : str ):
277
291
if path_or_url .startswith ("http://" ) or path_or_url .startswith ("https://" ):
278
292
return path_or_url
279
- return urljoin (self .base_url , path_or_url )
293
+ return combine_with (self .base_url , path_or_url )
280
294
281
295
def get_url (self , path : str , response_as : Type , args : Dict [str , Any ] = None ):
282
296
return self .send_url (path , "GET" , response_as , None , args )
@@ -356,7 +370,7 @@ def assert_valid_batch_request(request_dtos: list):
356
370
357
371
def send_all (self , request_dtos : List [IReturn [T ]]):
358
372
request , item_response_as = self .assert_valid_batch_request (request_dtos )
359
- url = urljoin (self .reply_base_url , nameof (request ) + "[]" )
373
+ url = combine_with (self .reply_base_url , nameof (request ) + "[]" )
360
374
361
375
return self .send_request (SendContext (
362
376
session = self ._session ,
@@ -371,7 +385,7 @@ def send_all(self, request_dtos: List[IReturn[T]]):
371
385
372
386
def send_all_oneway (self , request_dtos : list ):
373
387
request , item_response_as = self .assert_valid_batch_request (request_dtos )
374
- url = urljoin (self .oneway_base_url , nameof (request ) + "[]" )
388
+ url = combine_with (self .oneway_base_url , nameof (request ) + "[]" )
375
389
376
390
self .send_request (SendContext (
377
391
session = self ._session ,
@@ -482,7 +496,7 @@ def create_request(self, info: SendContext):
482
496
if not url :
483
497
body_not_request_dto = info .request and info .body
484
498
if body_not_request_dto :
485
- url = urljoin (self .reply_base_url , nameof (info .request ))
499
+ url = combine_with (self .reply_base_url , nameof (info .request ))
486
500
url = append_querystring (url , to_dict (info .request , key_case = clean_camelcase ))
487
501
else :
488
502
url = self .create_url_from_dto (info .method , body )
@@ -564,3 +578,4 @@ def send_request(self, info: SendContext):
564
578
return res_dto
565
579
566
580
return self ._handle_error (response , e )
581
+
0 commit comments