Skip to content

Commit 7cf1000

Browse files
committed
Adding fetch_upload method to client
1 parent 175972c commit 7cf1000

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

zulip/zulip/__init__.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
requests_json_is_function = callable(requests.Response.json)
3232

3333
API_VERSTRING = "v1/"
34+
API_STRING = "/api"
3435

3536
class CountingBackoff:
3637
def __init__(
@@ -381,9 +382,6 @@ def __init__(self, email: Optional[str] = None, api_key: Optional[str] = None, c
381382
else:
382383
raise MissingURLError("Missing Zulip server URL; specify via --site or ~/.zuliprc.")
383384

384-
if not self.base_url.endswith("/api"):
385-
self.base_url += "/api"
386-
self.base_url += "/"
387385
self.retry_on_errors = retry_on_errors
388386
self.client_name = client
389387

@@ -467,7 +465,8 @@ def get_user_agent(self) -> str:
467465
)
468466

469467
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]:
471470
if files is None:
472471
files = []
473472

@@ -524,6 +523,14 @@ def end_error_retry(succeeded: bool) -> None:
524523
else:
525524
print("Failed!")
526525

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)]
527534
while True:
528535
try:
529536
if method == "GET":
@@ -539,7 +546,7 @@ def end_error_retry(succeeded: bool) -> None:
539546
# Actually make the request!
540547
res = self.session.request(
541548
method,
542-
urllib.parse.urljoin(self.base_url, url),
549+
urllib.parse.urljoin(api_url, url),
543550
timeout=request_timeout,
544551
**kwargs)
545552

@@ -573,7 +580,7 @@ def end_error_retry(succeeded: bool) -> None:
573580
# go into retry logic, because the most likely scenario here is
574581
# that somebody just hasn't started their server, or they passed
575582
# in an invalid site.
576-
raise UnrecoverableNetworkError('cannot connect to server ' + self.base_url)
583+
raise UnrecoverableNetworkError('cannot connect to server ' + api_url)
577584

578585
if error_retry(""):
579586
continue
@@ -601,16 +608,21 @@ def end_error_retry(succeeded: bool) -> None:
601608
"status_code": res.status_code}
602609

603610
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]:
605613
if request is None:
606614
request = dict()
607615
marshalled_request = {}
608616
for (k, v) in request.items():
609617
if v is not None:
610618
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
612624
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)
614626

615627
def call_on_each_event(
616628
self,
@@ -738,6 +750,17 @@ def upload_file(self, file: IO[Any]) -> Dict[str, Any]:
738750
files=[file]
739751
)
740752

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+
741764
def get_attachments(self) -> Dict[str, Any]:
742765
'''
743766
Example usage:

0 commit comments

Comments
 (0)