From 7cf10009356c01b5b489ed74dadff21cb2f7d199 Mon Sep 17 00:00:00 2001 From: Ponni-M Date: Sun, 18 Apr 2021 19:06:04 +0530 Subject: [PATCH 1/2] Adding fetch_upload method to client --- zulip/zulip/__init__.py | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/zulip/zulip/__init__.py b/zulip/zulip/__init__.py index 18f523a56..65773c5fe 100644 --- a/zulip/zulip/__init__.py +++ b/zulip/zulip/__init__.py @@ -31,6 +31,7 @@ requests_json_is_function = callable(requests.Response.json) API_VERSTRING = "v1/" +API_STRING = "/api" class CountingBackoff: def __init__( @@ -381,9 +382,6 @@ def __init__(self, email: Optional[str] = None, api_key: Optional[str] = None, c else: raise MissingURLError("Missing Zulip server URL; specify via --site or ~/.zuliprc.") - if not self.base_url.endswith("/api"): - self.base_url += "/api" - self.base_url += "/" self.retry_on_errors = retry_on_errors self.client_name = client @@ -467,7 +465,8 @@ def get_user_agent(self) -> str: ) def do_api_query(self, orig_request: Mapping[str, Any], url: str, method: str = "POST", - longpolling: bool = False, files: Optional[List[IO[Any]]] = None, timeout: Optional[float] = None) -> Dict[str, Any]: + longpolling: bool = False, files: Optional[List[IO[Any]]] = None, timeout: Optional[float] = None, + accesswithAPI: bool = True) -> Dict[str, Any]: if files is None: files = [] @@ -524,6 +523,14 @@ def end_error_retry(succeeded: bool) -> None: else: print("Failed!") + api_url = self.base_url + if accesswithAPI: + if not api_url.endswith(API_STRING): + api_url += API_STRING + api_url += "/" + else: + if api_url.endswith(API_STRING): + api_url = api_url[:-len(API_STRING)] while True: try: if method == "GET": @@ -539,7 +546,7 @@ def end_error_retry(succeeded: bool) -> None: # Actually make the request! res = self.session.request( method, - urllib.parse.urljoin(self.base_url, url), + urllib.parse.urljoin(api_url, url), timeout=request_timeout, **kwargs) @@ -573,7 +580,7 @@ def end_error_retry(succeeded: bool) -> None: # go into retry logic, because the most likely scenario here is # that somebody just hasn't started their server, or they passed # in an invalid site. - raise UnrecoverableNetworkError('cannot connect to server ' + self.base_url) + raise UnrecoverableNetworkError('cannot connect to server ' + api_url) if error_retry(""): continue @@ -601,16 +608,21 @@ def end_error_retry(succeeded: bool) -> None: "status_code": res.status_code} def call_endpoint(self, url: Optional[str] = None, method: str = "POST", request: Optional[Dict[str, Any]] = None, - longpolling: bool = False, files: Optional[List[IO[Any]]] = None, timeout: Optional[float] = None) -> Dict[str, Any]: + longpolling: bool = False, files: Optional[List[IO[Any]]] = None, timeout: Optional[float] = None, + accessVersionedAPI: bool = True) -> Dict[str, Any]: if request is None: request = dict() marshalled_request = {} for (k, v) in request.items(): if v is not None: marshalled_request[k] = v - versioned_url = API_VERSTRING + (url if url is not None else "") + effective_url = (url if url is not None else "") + accesswithAPI = False + if accessVersionedAPI: + effective_url = API_VERSTRING + effective_url + accesswithAPI = True return self.do_api_query(marshalled_request, versioned_url, method=method, - longpolling=longpolling, files=files, timeout=timeout) + longpolling=longpolling, files=files, timeout=timeout, accesswithAPI = accesswithAPI) def call_on_each_event( self, @@ -738,6 +750,17 @@ def upload_file(self, file: IO[Any]) -> Dict[str, Any]: files=[file] ) + def fetch_upload(self) -> Dict[str, Any]: + ''' + See examples/upload-file for example usage. + ''' + + return self.call_endpoint( + url='user_uploads', + method = 'GET', + accessVersionedAPI = False + ) + def get_attachments(self) -> Dict[str, Any]: ''' Example usage: From a3b5d9569987bc69cdd8cf80586bd3796ae5502d Mon Sep 17 00:00:00 2001 From: Ponni-M Date: Sun, 18 Apr 2021 20:35:32 +0530 Subject: [PATCH 2/2] Fixing static analysis in fetch_upload --- zulip/zulip/__init__.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/zulip/zulip/__init__.py b/zulip/zulip/__init__.py index 65773c5fe..69b7cb8b8 100644 --- a/zulip/zulip/__init__.py +++ b/zulip/zulip/__init__.py @@ -465,7 +465,7 @@ def get_user_agent(self) -> str: ) def do_api_query(self, orig_request: Mapping[str, Any], url: str, method: str = "POST", - longpolling: bool = False, files: Optional[List[IO[Any]]] = None, timeout: Optional[float] = None, + longpolling: bool = False, files: Optional[List[IO[Any]]] = None, timeout: Optional[float] = None, accesswithAPI: bool = True) -> Dict[str, Any]: if files is None: files = [] @@ -527,7 +527,7 @@ def end_error_retry(succeeded: bool) -> None: if accesswithAPI: if not api_url.endswith(API_STRING): api_url += API_STRING - api_url += "/" + api_url += "/" else: if api_url.endswith(API_STRING): api_url = api_url[:-len(API_STRING)] @@ -608,7 +608,7 @@ def end_error_retry(succeeded: bool) -> None: "status_code": res.status_code} def call_endpoint(self, url: Optional[str] = None, method: str = "POST", request: Optional[Dict[str, Any]] = None, - longpolling: bool = False, files: Optional[List[IO[Any]]] = None, timeout: Optional[float] = None, + longpolling: bool = False, files: Optional[List[IO[Any]]] = None, timeout: Optional[float] = None, accessVersionedAPI: bool = True) -> Dict[str, Any]: if request is None: request = dict() @@ -621,7 +621,7 @@ def call_endpoint(self, url: Optional[str] = None, method: str = "POST", request if accessVersionedAPI: effective_url = API_VERSTRING + effective_url accesswithAPI = True - return self.do_api_query(marshalled_request, versioned_url, method=method, + return self.do_api_query(marshalled_request, effective_url, method=method, longpolling=longpolling, files=files, timeout=timeout, accesswithAPI = accesswithAPI) def call_on_each_event( @@ -754,10 +754,9 @@ def fetch_upload(self) -> Dict[str, Any]: ''' See examples/upload-file for example usage. ''' - return self.call_endpoint( url='user_uploads', - method = 'GET', + method = 'GET', accessVersionedAPI = False )