Skip to content

Commit 8464f92

Browse files
authored
Merge pull request #34 from 0xrohitgarg/main
Add Docstrings
2 parents 1ab1e40 + 125f8ec commit 8464f92

File tree

10 files changed

+473
-18
lines changed

10 files changed

+473
-18
lines changed

videodb/_constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Constants used in the videodb package."""
2+
23
from typing import Union
34
from dataclasses import dataclass
45

videodb/audio.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@
44

55

66
class Audio:
7-
def __init__(self, _connection, id: str, collection_id: str, **kwargs) -> None:
7+
"""Audio class to interact with the Audio
8+
9+
:ivar str id: Unique identifier for the audio
10+
:ivar str collection_id: ID of the collection this audio belongs to
11+
:ivar str name: Name of the audio file
12+
:ivar float length: Duration of the audio in seconds
13+
"""
14+
15+
def __init__(
16+
self, _connection, id: str, collection_id: str, **kwargs
17+
) -> None:
818
self._connection = _connection
919
self.id = id
1020
self.collection_id = collection_id
@@ -21,11 +31,23 @@ def __repr__(self) -> str:
2131
)
2232

2333
def generate_url(self) -> str:
34+
"""Generate the signed url of the audio.
35+
36+
:raises InvalidRequestError: If the get_url fails
37+
:return: The signed url of the audio
38+
:rtype: str
39+
"""
2440
url_data = self._connection.post(
2541
path=f"{ApiPath.audio}/{self.id}/{ApiPath.generate_url}",
2642
params={"collection_id": self.collection_id},
2743
)
2844
return url_data.get("signed_url", None)
2945

3046
def delete(self) -> None:
47+
"""Delete the audio.
48+
49+
:raises InvalidRequestError: If the delete fails
50+
:return: None if the delete is successful
51+
:rtype: None
52+
"""
3153
self._connection.delete(f"{ApiPath.audio}/{self.id}")

videodb/client.py

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,32 @@
2424

2525

2626
class Connection(HttpClient):
27-
def __init__(self, api_key: str, base_url: str) -> None:
27+
"""Connection class to interact with the VideoDB"""
28+
29+
def __init__(self, api_key: str, base_url: str) -> "Connection":
30+
"""Initializes a new instance of the Connection class with specified API credentials.
31+
32+
Note: Users should not initialize this class directly.
33+
Instead use :meth:`videodb.connect() <videodb.connect>`
34+
35+
:param str api_key: API key for authentication
36+
:param str base_url: Base URL of the VideoDB API
37+
:raise ValueError: If the API key is not provided
38+
:return: :class:`Connection <Connection>` object, to interact with the VideoDB
39+
:rtype: :class:`videodb.client.Connection`
40+
"""
2841
self.api_key = api_key
2942
self.base_url = base_url
3043
self.collection_id = "default"
3144
super().__init__(api_key=api_key, base_url=base_url, version=__version__)
3245

3346
def get_collection(self, collection_id: Optional[str] = "default") -> Collection:
47+
"""Get a collection object by its ID.
48+
49+
:param str collection_id: ID of the collection (optional, default: "default")
50+
:return: :class:`Collection <Collection>` object
51+
:rtype: :class:`videodb.collection.Collection`
52+
"""
3453
collection_data = self.get(path=f"{ApiPath.collection}/{collection_id}")
3554
self.collection_id = collection_data.get("id", "default")
3655
return Collection(
@@ -42,6 +61,11 @@ def get_collection(self, collection_id: Optional[str] = "default") -> Collection
4261
)
4362

4463
def get_collections(self) -> List[Collection]:
64+
"""Get a list of all collections.
65+
66+
:return: List of :class:`Collection <Collection>` objects
67+
:rtype: list[:class:`videodb.collection.Collection`]
68+
"""
4569
collections_data = self.get(path=ApiPath.collection)
4670
return [
4771
Collection(
@@ -57,6 +81,14 @@ def get_collections(self) -> List[Collection]:
5781
def create_collection(
5882
self, name: str, description: str, is_public: bool = False
5983
) -> Collection:
84+
"""Create a new collection.
85+
86+
:param str name: Name of the collection
87+
:param str description: Description of the collection
88+
:param bool is_public: Make collection public (optional, default: False)
89+
:return: :class:`Collection <Collection>` object
90+
:rtype: :class:`videodb.collection.Collection`
91+
"""
6092
collection_data = self.post(
6193
path=ApiPath.collection,
6294
data={
@@ -75,6 +107,14 @@ def create_collection(
75107
)
76108

77109
def update_collection(self, id: str, name: str, description: str) -> Collection:
110+
"""Update an existing collection.
111+
112+
:param str id: ID of the collection
113+
:param str name: Name of the collection
114+
:param str description: Description of the collection
115+
:return: :class:`Collection <Collection>` object
116+
:rtype: :class:`videodb.collection.Collection`
117+
"""
78118
collection_data = self.patch(
79119
path=f"{ApiPath.collection}/{id}",
80120
data={
@@ -92,12 +132,29 @@ def update_collection(self, id: str, name: str, description: str) -> Collection:
92132
)
93133

94134
def check_usage(self) -> dict:
135+
"""Check the usage.
136+
137+
:return: Usage data
138+
:rtype: dict
139+
"""
95140
return self.get(path=f"{ApiPath.billing}/{ApiPath.usage}")
96141

97142
def get_invoices(self) -> List[dict]:
143+
"""Get a list of all invoices.
144+
145+
:return: List of invoices
146+
:rtype: list[dict]
147+
"""
98148
return self.get(path=f"{ApiPath.billing}/{ApiPath.invoices}")
99149

100150
def download(self, stream_link: str, name: str) -> dict:
151+
"""Download a file from a stream link.
152+
153+
:param stream_link: URL of the stream to download
154+
:param name: Name to save the downloaded file as
155+
:return: Download response data
156+
:rtype: dict
157+
"""
101158
return self.post(
102159
path=f"{ApiPath.download}",
103160
data={
@@ -115,6 +172,17 @@ def upload(
115172
description: Optional[str] = None,
116173
callback_url: Optional[str] = None,
117174
) -> Union[Video, Audio, Image, None]:
175+
"""Upload a file.
176+
177+
:param str file_path: Path to the file to upload (optional)
178+
:param str url: URL of the file to upload (optional)
179+
:param MediaType media_type: MediaType object (optional)
180+
:param str name: Name of the file (optional)
181+
:param str description: Description of the file (optional)
182+
:param str callback_url: URL to receive the callback (optional)
183+
:return: :class:`Video <Video>`, or :class:`Audio <Audio>`, or :class:`Image <Image>` object
184+
:rtype: Union[ :class:`videodb.video.Video`, :class:`videodb.audio.Audio`, :class:`videodb.image.Image`]
185+
"""
118186
upload_data = upload(
119187
self,
120188
file_path,

videodb/collection.py

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424

2525

2626
class Collection:
27+
"""Collection class to interact with the Collection.
28+
29+
Note: Users should not initialize this class directly.
30+
Instead use :meth:`Connection.get_collection() <videodb.client.Connection.get_collection>`
31+
"""
32+
2733
def __init__(
2834
self,
2935
_connection,
@@ -57,20 +63,31 @@ def delete(self) -> None:
5763
self._connection.delete(path=f"{ApiPath.collection}/{self.id}")
5864

5965
def get_videos(self) -> List[Video]:
66+
"""Get all the videos in the collection.
67+
68+
:return: List of :class:`Video <Video>` objects
69+
:rtype: List[:class:`videodb.video.Video`]
70+
"""
6071
videos_data = self._connection.get(
6172
path=f"{ApiPath.video}",
6273
params={"collection_id": self.id},
6374
)
6475
return [Video(self._connection, **video) for video in videos_data.get("videos")]
6576

6677
def get_video(self, video_id: str) -> Video:
78+
"""Get a video by its ID.
79+
80+
:param str video_id: ID of the video
81+
:return: :class:`Video <Video>` object
82+
:rtype: :class:`videodb.video.Video`
83+
"""
6784
video_data = self._connection.get(
6885
path=f"{ApiPath.video}/{video_id}", params={"collection_id": self.id}
6986
)
7087
return Video(self._connection, **video_data)
7188

7289
def delete_video(self, video_id: str) -> None:
73-
"""Delete the video
90+
"""Delete the video.
7491
7592
:param str video_id: The id of the video to be deleted
7693
:raises InvalidRequestError: If the delete fails
@@ -82,37 +99,73 @@ def delete_video(self, video_id: str) -> None:
8299
)
83100

84101
def get_audios(self) -> List[Audio]:
102+
"""Get all the audios in the collection.
103+
104+
:return: List of :class:`Audio <Audio>` objects
105+
:rtype: List[:class:`videodb.audio.Audio`]
106+
"""
85107
audios_data = self._connection.get(
86108
path=f"{ApiPath.audio}",
87109
params={"collection_id": self.id},
88110
)
89111
return [Audio(self._connection, **audio) for audio in audios_data.get("audios")]
90112

91113
def get_audio(self, audio_id: str) -> Audio:
114+
"""Get an audio by its ID.
115+
116+
:param str audio_id: ID of the audio
117+
:return: :class:`Audio <Audio>` object
118+
:rtype: :class:`videodb.audio.Audio`
119+
"""
92120
audio_data = self._connection.get(
93121
path=f"{ApiPath.audio}/{audio_id}", params={"collection_id": self.id}
94122
)
95123
return Audio(self._connection, **audio_data)
96124

97125
def delete_audio(self, audio_id: str) -> None:
126+
"""Delete the audio.
127+
128+
:param str audio_id: The id of the audio to be deleted
129+
:raises InvalidRequestError: If the delete fails
130+
:return: None if the delete is successful
131+
:rtype: None
132+
"""
98133
return self._connection.delete(
99134
path=f"{ApiPath.audio}/{audio_id}", params={"collection_id": self.id}
100135
)
101136

102137
def get_images(self) -> List[Image]:
138+
"""Get all the images in the collection.
139+
140+
:return: List of :class:`Image <Image>` objects
141+
:rtype: List[:class:`videodb.image.Image`]
142+
"""
103143
images_data = self._connection.get(
104144
path=f"{ApiPath.image}",
105145
params={"collection_id": self.id},
106146
)
107147
return [Image(self._connection, **image) for image in images_data.get("images")]
108148

109149
def get_image(self, image_id: str) -> Image:
150+
"""Get an image by its ID.
151+
152+
:param str image_id: ID of the image
153+
:return: :class:`Image <Image>` object
154+
:rtype: :class:`videodb.image.Image`
155+
"""
110156
image_data = self._connection.get(
111157
path=f"{ApiPath.image}/{image_id}", params={"collection_id": self.id}
112158
)
113159
return Image(self._connection, **image_data)
114160

115161
def delete_image(self, image_id: str) -> None:
162+
"""Delete the image.
163+
164+
:param str image_id: The id of the image to be deleted
165+
:raises InvalidRequestError: If the delete fails
166+
:return: None if the delete is successful
167+
:rtype: None
168+
"""
116169
return self._connection.delete(
117170
path=f"{ApiPath.image}/{image_id}", params={"collection_id": self.id}
118171
)
@@ -127,6 +180,18 @@ def search(
127180
dynamic_score_percentage: Optional[float] = None,
128181
filter: List[Dict[str, Any]] = [],
129182
) -> SearchResult:
183+
"""Search for a query in the collection.
184+
185+
:param str query: Query to search for
186+
:param SearchType search_type: Type of search to perform (optional)
187+
:param IndexType index_type: Type of index to search (optional)
188+
:param int result_threshold: Number of results to return (optional)
189+
:param float score_threshold: Threshold score for the search (optional)
190+
:param float dynamic_score_percentage: Percentage of dynamic score to consider (optional)
191+
:raise SearchError: If the search fails
192+
:return: :class:`SearchResult <SearchResult>` object
193+
:rtype: :class:`videodb.search.SearchResult`
194+
"""
130195
search = SearchFactory(self._connection).get_search(search_type)
131196
return search.search_inside_collection(
132197
collection_id=self.id,
@@ -161,6 +226,17 @@ def upload(
161226
description: Optional[str] = None,
162227
callback_url: Optional[str] = None,
163228
) -> Union[Video, Audio, Image, None]:
229+
"""Upload a file to the collection.
230+
231+
:param str file_path: Path to the file to be uploaded
232+
:param str url: URL of the file to be uploaded
233+
:param MediaType media_type: MediaType object (optional)
234+
:param str name: Name of the file (optional)
235+
:param str description: Description of the file (optional)
236+
:param str callback_url: URL to receive the callback (optional)
237+
:return: :class:`Video <Video>`, or :class:`Audio <Audio>`, or :class:`Image <Image>` object
238+
:rtype: Union[ :class:`videodb.video.Video`, :class:`videodb.audio.Audio`, :class:`videodb.image.Image`]
239+
"""
164240
upload_data = upload(
165241
self._connection,
166242
file_path,
@@ -179,12 +255,22 @@ def upload(
179255
return Image(self._connection, **upload_data)
180256

181257
def make_public(self):
258+
"""Make the collection public.
259+
260+
:return: None
261+
:rtype: None
262+
"""
182263
self._connection.patch(
183264
path=f"{ApiPath.collection}/{self.id}", data={"is_public": True}
184265
)
185266
self.is_public = True
186267

187268
def make_private(self):
269+
"""Make the collection private.
270+
271+
:return: None
272+
:rtype: None
273+
"""
188274
self._connection.patch(
189275
path=f"{ApiPath.collection}/{self.id}", data={"is_public": False}
190276
)

0 commit comments

Comments
 (0)