Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding fetch_upload method to client #678

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions zulip/zulip/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
requests_json_is_function = callable(requests.Response.json)

API_VERSTRING = "v1/"
API_STRING = "/api"

class CountingBackoff:
def __init__(
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 = []

Expand Down Expand Up @@ -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":
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 "")
return self.do_api_query(marshalled_request, versioned_url, method=method,
longpolling=longpolling, files=files, timeout=timeout)
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, effective_url, method=method,
longpolling=longpolling, files=files, timeout=timeout, accesswithAPI = accesswithAPI)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think accesswithAPI = accessVersionedAPI makes more sense, we don't need a separate variable accesswithAPI.


def call_on_each_event(
self,
Expand Down Expand Up @@ -738,6 +750,16 @@ 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:
Expand Down