Skip to content

Commit 795aa14

Browse files
committed
add workspace usage method
1 parent 45d4454 commit 795aa14

1 file changed

Lines changed: 60 additions & 4 deletions

File tree

mergin/client.py

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,24 @@ def workspace_service(self, workspace_id):
364364

365365
return response
366366

367+
def workspace_usage(self, workspace_id):
368+
"""
369+
This Requests information about a workspace usage from /workspace/{id}/usage endpoint,
370+
if such exists in self.url server.
371+
372+
Returns response from server as JSON dict or None if endpoint is not found
373+
"""
374+
375+
try:
376+
response = self.get(f"/v1/workspace/{workspace_id}/usage")
377+
except ClientError as e:
378+
self.log.debug(f"Unable to query for /workspace/{workspace_id}/usage endpoint")
379+
return
380+
381+
response = json.loads(response.read())
382+
383+
return response
384+
367385
def server_type(self):
368386
"""
369387
Returns the deployment type of the server
@@ -698,18 +716,30 @@ def project_versions(self, project_path, since=None, to=None):
698716
If neither 'since' nor 'to' is specified it will return all versions.
699717
700718
:param project_path: Project's full name (<namespace>/<name>)
701-
:type project_path: String
719+
:type project_path: String | Int
702720
:param since: Version to track project history from
703-
:type since: String
721+
:type since: String | Int
704722
:param to: Version to track project history to
705723
:type to: String
706724
707725
:rtype: List[Dict]
708726
"""
709727
versions = []
710728
per_page = 100 # server limit
711-
num_since = int_version(since) if since else 1
712-
num_to = int_version(to) if to else None # we may not know yet
729+
730+
if type(since) == str:
731+
num_since = int_version(since)
732+
elif since == None:
733+
num_since == 1
734+
else:
735+
#keep the since parameter as is
736+
num_since = since
737+
if type(to) == str:
738+
num_to = int_version(int)
739+
else:
740+
#keep the to parameter as is
741+
num_to = to
742+
713743
start_page = math.ceil(num_since / per_page)
714744
if not num_to:
715745
# let's get first page and count
@@ -720,6 +750,7 @@ def project_versions(self, project_path, since=None, to=None):
720750
num_to = resp_json["count"]
721751
latest_version = int_version(versions[-1]["name"])
722752
if latest_version < num_to:
753+
#add yield here
723754
versions += self.project_versions(project_path, f"v{latest_version+1}", f"v{num_to}")
724755
else:
725756
end_page = math.ceil(num_to / per_page)
@@ -731,6 +762,31 @@ def project_versions(self, project_path, since=None, to=None):
731762
# filter out versions not within range
732763
filtered_versions = list(filter(lambda v: (num_since <= int_version(v["name"]) <= num_to), versions))
733764
return filtered_versions
765+
766+
def project_versions_count(self, project_path):
767+
768+
#TODO ask tomas if we should return the total count another
769+
770+
"""
771+
Get the total count of project's versions history.
772+
773+
:param project_path: Project's full name (<namespace>/<name>)
774+
:type project_path: String
775+
:param since: Version to track project history from
776+
:type since: String
777+
:param to: Version to track project history to
778+
:type to: String
779+
780+
:rtype: Integer
781+
"""
782+
start_page = 1 #we don't care which page to get the count
783+
per_page = 100 # server limit
784+
params = {"page": start_page, "per_page": per_page, "descending": False}
785+
resp = self.get("/v1/project/versions/paginated/{}".format(project_path), params)
786+
resp_json = json.load(resp)
787+
788+
return resp_json["count"]
789+
734790

735791
def download_project(self, project_path, directory, version=None):
736792
"""

0 commit comments

Comments
 (0)