31
31
requests_json_is_function = callable (requests .Response .json )
32
32
33
33
API_VERSTRING = "v1/"
34
+ API_STRING = "/api"
34
35
35
36
class CountingBackoff :
36
37
def __init__ (
@@ -381,9 +382,6 @@ def __init__(self, email: Optional[str] = None, api_key: Optional[str] = None, c
381
382
else :
382
383
raise MissingURLError ("Missing Zulip server URL; specify via --site or ~/.zuliprc." )
383
384
384
- if not self .base_url .endswith ("/api" ):
385
- self .base_url += "/api"
386
- self .base_url += "/"
387
385
self .retry_on_errors = retry_on_errors
388
386
self .client_name = client
389
387
@@ -467,7 +465,8 @@ def get_user_agent(self) -> str:
467
465
)
468
466
469
467
def do_api_query (self , orig_request : Mapping [str , Any ], url : str , method : str = "POST" ,
470
- longpolling : bool = False , files : Optional [List [IO [Any ]]] = None , timeout : Optional [float ] = None ) -> Dict [str , Any ]:
468
+ longpolling : bool = False , files : Optional [List [IO [Any ]]] = None , timeout : Optional [float ] = None ,
469
+ accesswithAPI : bool = True ) -> Dict [str , Any ]:
471
470
if files is None :
472
471
files = []
473
472
@@ -524,6 +523,14 @@ def end_error_retry(succeeded: bool) -> None:
524
523
else :
525
524
print ("Failed!" )
526
525
526
+ api_url = self .base_url
527
+ if accesswithAPI :
528
+ if not api_url .endswith (API_STRING ):
529
+ api_url += API_STRING
530
+ api_url += "/"
531
+ else :
532
+ if api_url .endswith (API_STRING ):
533
+ api_url = api_url [:- len (API_STRING )]
527
534
while True :
528
535
try :
529
536
if method == "GET" :
@@ -539,7 +546,7 @@ def end_error_retry(succeeded: bool) -> None:
539
546
# Actually make the request!
540
547
res = self .session .request (
541
548
method ,
542
- urllib .parse .urljoin (self . base_url , url ),
549
+ urllib .parse .urljoin (api_url , url ),
543
550
timeout = request_timeout ,
544
551
** kwargs )
545
552
@@ -573,7 +580,7 @@ def end_error_retry(succeeded: bool) -> None:
573
580
# go into retry logic, because the most likely scenario here is
574
581
# that somebody just hasn't started their server, or they passed
575
582
# in an invalid site.
576
- raise UnrecoverableNetworkError ('cannot connect to server ' + self . base_url )
583
+ raise UnrecoverableNetworkError ('cannot connect to server ' + api_url )
577
584
578
585
if error_retry ("" ):
579
586
continue
@@ -601,16 +608,21 @@ def end_error_retry(succeeded: bool) -> None:
601
608
"status_code" : res .status_code }
602
609
603
610
def call_endpoint (self , url : Optional [str ] = None , method : str = "POST" , request : Optional [Dict [str , Any ]] = None ,
604
- longpolling : bool = False , files : Optional [List [IO [Any ]]] = None , timeout : Optional [float ] = None ) -> Dict [str , Any ]:
611
+ longpolling : bool = False , files : Optional [List [IO [Any ]]] = None , timeout : Optional [float ] = None ,
612
+ accessVersionedAPI : bool = True ) -> Dict [str , Any ]:
605
613
if request is None :
606
614
request = dict ()
607
615
marshalled_request = {}
608
616
for (k , v ) in request .items ():
609
617
if v is not None :
610
618
marshalled_request [k ] = v
611
- versioned_url = API_VERSTRING + (url if url is not None else "" )
619
+ effective_url = (url if url is not None else "" )
620
+ accesswithAPI = False
621
+ if accessVersionedAPI :
622
+ effective_url = API_VERSTRING + effective_url
623
+ accesswithAPI = True
612
624
return self .do_api_query (marshalled_request , versioned_url , method = method ,
613
- longpolling = longpolling , files = files , timeout = timeout )
625
+ longpolling = longpolling , files = files , timeout = timeout , accesswithAPI = accesswithAPI )
614
626
615
627
def call_on_each_event (
616
628
self ,
@@ -738,6 +750,17 @@ def upload_file(self, file: IO[Any]) -> Dict[str, Any]:
738
750
files = [file ]
739
751
)
740
752
753
+ def fetch_upload (self ) -> Dict [str , Any ]:
754
+ '''
755
+ See examples/upload-file for example usage.
756
+ '''
757
+
758
+ return self .call_endpoint (
759
+ url = 'user_uploads' ,
760
+ method = 'GET' ,
761
+ accessVersionedAPI = False
762
+ )
763
+
741
764
def get_attachments (self ) -> Dict [str , Any ]:
742
765
'''
743
766
Example usage:
0 commit comments