Skip to content

Commit 3b51970

Browse files
committed
Merge branch 'AntoniaBK-upload_capture'
2 parents 89cc3fb + 4920233 commit 3b51970

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

pylookyloo/api.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
import os
56
import base64
67
import warnings
78

@@ -484,3 +485,64 @@ def send_mail(self, tree_uuid: str, email: str = '', comment: str | None = None)
484485
to_send['comment'] = comment
485486
r = self.session.post(urljoin(self.root_url, str(PurePosixPath('json', tree_uuid, 'report'))), json=to_send)
486487
return r.json()
488+
489+
def upload_capture(self, *, quiet: bool = False,
490+
listing: bool = False,
491+
full_capture: Path | BytesIO | str | None = None,
492+
har: Path | BytesIO | str | None = None,
493+
html: Path | BytesIO | str | None = None,
494+
last_redirected_url: str | None = None,
495+
screenshot: Path | BytesIO | str | None = None) -> str | tuple[str, dict[str, str]]:
496+
'''Upload a capture via har-file and others
497+
498+
:param quiet: Returns the UUID only, instead of the the UUID and the potential error / warning messages
499+
:param listing: if true the capture should be public, else private - overwritten if the full_capture is given and it contains no_index
500+
:param full_capture: path to the capture made by another instance
501+
:param har: Harfile of the capture
502+
:param html: rendered HTML of the capture
503+
:param last_redirected_url: The landing page of the capture
504+
:param screenshot: Screenshot of the capture
505+
'''
506+
def encode_document(document: Path | BytesIO | str) -> str:
507+
if isinstance(document, str):
508+
if not os.path.exists(document):
509+
raise FileNotFoundError(f'{document} does not exist')
510+
document = Path(document)
511+
if isinstance(document, Path):
512+
with document.open('rb') as f:
513+
document = BytesIO(f.read())
514+
return base64.b64encode(document.getvalue()).decode()
515+
516+
to_send: dict[str, Any] = {'listing': listing}
517+
518+
if full_capture:
519+
b64_full_capture = encode_document(full_capture)
520+
to_send['full_capture'] = b64_full_capture
521+
elif har:
522+
b64_har = encode_document(har)
523+
to_send['har_file'] = b64_har
524+
525+
if html:
526+
b64_html = encode_document(html)
527+
to_send['html_file'] = b64_html
528+
529+
if last_redirected_url:
530+
to_send['landing_page'] = last_redirected_url
531+
532+
if screenshot:
533+
b64_screenshot = encode_document(screenshot)
534+
to_send['screenshot_file'] = b64_screenshot
535+
else:
536+
raise PyLookylooError("Full capture or at least har-file are required")
537+
538+
r = self.session.post(urljoin(self.root_url, str(PurePosixPath('json', 'upload'))), json=to_send)
539+
r.raise_for_status()
540+
json_response = r.json()
541+
uuid = json_response['uuid']
542+
messages = json_response['messages']
543+
544+
if not uuid:
545+
raise PyLookylooError('Unable to get UUID from lookyloo instance.')
546+
if quiet:
547+
return uuid
548+
return uuid, messages

0 commit comments

Comments
 (0)