Skip to content

Commit ef8d19b

Browse files
authored
Den 429 Add upload and download to dendrite (#73)
1 parent 644e2a7 commit ef8d19b

File tree

5 files changed

+65
-8
lines changed

5 files changed

+65
-8
lines changed

dendrite/async_api/_core/dendrite_browser.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from abc import ABC, abstractmethod
2+
import pathlib
23
import re
3-
from typing import Any, List, Literal, Optional, Union
4+
from typing import Any, List, Literal, Optional, Sequence, Union
45
from uuid import uuid4
56
import os
67
from loguru import logger
@@ -11,6 +12,7 @@
1112
FileChooser,
1213
Download,
1314
Error,
15+
FilePayload,
1416
)
1517

1618
from dendrite.async_api._api.dto.authenticate_dto import AuthenticateDTO
@@ -374,7 +376,7 @@ async def _get_active_page_manager(self) -> PageManager:
374376

375377
return self._active_page_manager
376378

377-
async def _get_download(self, pw_page: PlaywrightPage, timeout: float) -> Download:
379+
async def get_download(self, pw_page: PlaywrightPage, timeout: float) -> Download:
378380
"""
379381
Retrieves the download event from the browser.
380382
@@ -386,6 +388,33 @@ async def _get_download(self, pw_page: PlaywrightPage, timeout: float) -> Downlo
386388
"""
387389
return await self._impl.get_download(self, pw_page, timeout)
388390

391+
async def upload_files(
392+
self,
393+
files: Union[
394+
str,
395+
pathlib.Path,
396+
FilePayload,
397+
Sequence[Union[str, pathlib.Path]],
398+
Sequence[FilePayload],
399+
],
400+
timeout: float = 30000,
401+
) -> None:
402+
"""
403+
Uploads files to the active page using a file chooser.
404+
405+
Args:
406+
files (Union[str, pathlib.Path, FilePayload, Sequence[Union[str, pathlib.Path]], Sequence[FilePayload]]): The file(s) to be uploaded.
407+
This can be a file path, a `FilePayload` object, or a sequence of file paths or `FilePayload` objects.
408+
timeout (float, optional): The maximum amount of time (in milliseconds) to wait for the file chooser to be ready. Defaults to 30.
409+
410+
Returns:
411+
None
412+
"""
413+
page = await self.get_active_page()
414+
415+
file_chooser = await self._get_filechooser(page.playwright_page, timeout)
416+
await file_chooser.set_files(files)
417+
389418
async def _get_filechooser(
390419
self, pw_page: PlaywrightPage, timeout: float = 30000
391420
) -> FileChooser:

dendrite/async_api/_core/dendrite_page.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ async def get_download(self, timeout: float = 30000) -> Download:
153153
Returns:
154154
The downloaded file data.
155155
"""
156-
return await self.dendrite_browser._get_download(self.playwright_page, timeout)
156+
return await self.dendrite_browser.get_download(self.playwright_page, timeout)
157157

158158
def _get_context(self, element: Any) -> Union[PlaywrightPage, FrameLocator]:
159159
"""

dendrite/sync_api/_core/dendrite_browser.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from abc import ABC, abstractmethod
2+
import pathlib
23
import re
3-
from typing import Any, List, Literal, Optional, Union
4+
from typing import Any, List, Literal, Optional, Sequence, Union
45
from uuid import uuid4
56
import os
67
from loguru import logger
@@ -11,6 +12,7 @@
1112
FileChooser,
1213
Download,
1314
Error,
15+
FilePayload,
1416
)
1517
from dendrite.sync_api._api.dto.authenticate_dto import AuthenticateDTO
1618
from dendrite.sync_api._api.dto.upload_auth_session_dto import UploadAuthSessionDTO
@@ -333,11 +335,11 @@ def _get_active_page_manager(self) -> PageManager:
333335
Exception: If there is an issue launching the browser or retrieving the PageManager.
334336
"""
335337
if not self._active_page_manager:
336-
_, _, active_page_manager = self._launch()
338+
(_, _, active_page_manager) = self._launch()
337339
return active_page_manager
338340
return self._active_page_manager
339341

340-
def _get_download(self, pw_page: PlaywrightPage, timeout: float) -> Download:
342+
def get_download(self, pw_page: PlaywrightPage, timeout: float) -> Download:
341343
"""
342344
Retrieves the download event from the browser.
343345
@@ -349,6 +351,32 @@ def _get_download(self, pw_page: PlaywrightPage, timeout: float) -> Download:
349351
"""
350352
return self._impl.get_download(self, pw_page, timeout)
351353

354+
def upload_files(
355+
self,
356+
files: Union[
357+
str,
358+
pathlib.Path,
359+
FilePayload,
360+
Sequence[Union[str, pathlib.Path]],
361+
Sequence[FilePayload],
362+
],
363+
timeout: float = 30000,
364+
) -> None:
365+
"""
366+
Uploads files to the active page using a file chooser.
367+
368+
Args:
369+
files (Union[str, pathlib.Path, FilePayload, Sequence[Union[str, pathlib.Path]], Sequence[FilePayload]]): The file(s) to be uploaded.
370+
This can be a file path, a `FilePayload` object, or a sequence of file paths or `FilePayload` objects.
371+
timeout (float, optional): The maximum amount of time (in milliseconds) to wait for the file chooser to be ready. Defaults to 30.
372+
373+
Returns:
374+
None
375+
"""
376+
page = self.get_active_page()
377+
file_chooser = self._get_filechooser(page.playwright_page, timeout)
378+
file_chooser.set_files(files)
379+
352380
def _get_filechooser(
353381
self, pw_page: PlaywrightPage, timeout: float = 30000
354382
) -> FileChooser:

dendrite/sync_api/_core/dendrite_page.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def get_download(self, timeout: float = 30000) -> Download:
123123
Returns:
124124
The downloaded file data.
125125
"""
126-
return self.dendrite_browser._get_download(self.playwright_page, timeout)
126+
return self.dendrite_browser.get_download(self.playwright_page, timeout)
127127

128128
def _get_context(self, element: Any) -> Union[PlaywrightPage, FrameLocator]:
129129
"""

dendrite/sync_api/_dom/util/mild_strip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def _mild_strip(soup: BeautifulSoup, keep_d_id: bool = True) -> None:
2626
continue
2727
tag.attrs = {
2828
attr: value[:100] if isinstance(value, str) else value
29-
for attr, value in tag.attrs.items()
29+
for (attr, value) in tag.attrs.items()
3030
}
3131
if keep_d_id == False:
3232
del tag["d-id"]

0 commit comments

Comments
 (0)