Skip to content

Commit

Permalink
improved pdf and screenshot endpoint to allow the viewing of the file…
Browse files Browse the repository at this point in the history
… on the browser (or swagger)

added more tests to Prefect api
  • Loading branch information
DinisCruz committed Oct 5, 2024
1 parent cb0c081 commit a650a78
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 31 deletions.
43 changes: 33 additions & 10 deletions osbot_serverless_flows/fast_api/routes/Routes__Browser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import io

from osbot_fast_api.api.Fast_API_Routes import Fast_API_Routes
from starlette.responses import StreamingResponse

from osbot_utils.decorators.methods.cache_on_self import cache_on_self

from osbot_serverless_flows.flows.browser_based.Flow__Playwright__Get_Page_Html import Flow__Playwright__Get_Page_Html
Expand Down Expand Up @@ -28,21 +32,40 @@ def url_html(self, url="https://httpbin.org/get"):
result = _.run()
return result

def url_pdf(self, url="https://httpbin.org/get"):
self.install_browser() # todo: BUG: for now, put the check there to make sure the browser is installed
def url_pdf(self, url="https://httpbin.org/get", return_file:bool=False): # todo: refactor with url_screenshot
self.install_browser() # todo: BUG: for now, put the check there to make sure the browser is installed
with Flow__Playwright__Get_Page_Pdf() as _:
_.url = url
screenshot_base64 = _.run().get('pdf_base64')
result = {'pdf_base64': screenshot_base64}
return result
run_data =_.run()
pdf_bytes = run_data.get('pdf_bytes' )
pdf_base64 = run_data.get('pdf_base64')

if return_file is True:
pdf_stream = io.BytesIO(pdf_bytes)
response = StreamingResponse( pdf_stream,
media_type = "application/pdf",
headers = {"Content-Disposition": "attachment; filename=document.pdf"})
else:
response = {'pdf_base64': pdf_base64}

def url_screenshot(self, url="https://httpbin.org/get"):
self.install_browser() # todo: BUG: for now, put the check there to make sure the browser is installed
return response

def url_screenshot(self, url="https://httpbin.org/get", return_file:bool=False):
self.install_browser() # todo: BUG: for now, put the check there to make sure the browser is installed
with Flow__Playwright__Get_Page_Screenshot() as _:
_.url = url
screenshot_base64 = _.run().get('screenshot_base64')
result = {'screenshot_base64': screenshot_base64}
return result
run_data = _.run()
screenshot_base64 = run_data.get('screenshot_base64')
screenshot_bytes = run_data.get('screenshot_bytes')
if return_file:
screenshot_stream = io.BytesIO(screenshot_bytes)
response = StreamingResponse(screenshot_stream,
media_type = "image/png",
headers = {"Content-Disposition": "attachment; filename=screenshot.png"})
else:
response = {'screenshot_base64': screenshot_base64}

return response

def setup_routes(self):
self.add_route_get(self.url_html )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
class Prefect__Cloud_API(Type_Safe):
prefect_rest_api = Prefect__Rest_API()

def make_request(self):
path = '/flows/filter'
data = {
"sort": "CREATED_DESC",
"limit": 5,
}
return self.prefect_rest_api.requests_post(path, data)

def flows(self, limit=5):
return self.prefect_rest_api.filter(target='flows', limit=limit)
12 changes: 10 additions & 2 deletions osbot_serverless_flows/observability/prefect/Prefect__Rest_API.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

class Prefect__Rest_API(Type_Safe):

# raw request methods
def api_key(self):
return get_env(ENV_NAME__PREFECT_CLOUD__API_KEY)

Expand All @@ -22,8 +23,15 @@ def prefect_api_url(self):
return f"https://api.prefect.cloud/api/accounts/{self.account_id()}/workspaces/{self.workspace_id()}"

def requests_post(self, path, data):

headers = {"Authorization": f"Bearer {self.api_key()}"}
endpoint = f"{self.prefect_api_url()}{path}"
response = requests.post(endpoint, headers=headers, json=data)
return response
return response

# request helpers

def filter(self, target, limit=5):
path = f'/{target}/filter'
data = { "sort" : "CREATED_DESC",
"limit": limit }
return self.requests_post(path, data)
15 changes: 11 additions & 4 deletions tests/integration/observability/prefect/test_Prefect__Cloud_API.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from unittest import TestCase

from osbot_utils.utils.Misc import list_set

from osbot_utils.utils.Dev import pprint

from osbot_serverless_flows.observability.prefect.Prefect__Cloud_API import Prefect__Cloud_API
from osbot_utils.utils.Env import load_dotenv

Expand All @@ -11,7 +15,10 @@ def setUpClass(cls) -> None:
load_dotenv()
cls.prefect_cloud_api = Prefect__Cloud_API()

def test_make_request(self):
response = self.prefect_cloud_api.make_request()
from osbot_utils.utils.Dev import pprint
assert response.status_code == 200
def test_flows(self):
response = self.prefect_cloud_api.flows()
status_code = response.status_code
flows = response.json()
assert status_code == 200
for flow in flows:
assert list_set(flow) == ['created', 'id', 'labels', 'name', 'tags', 'updated']
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,4 @@ def test_prefect_api_url(self):
account_id = self.prefect_rest_api.account_id()
workspace_id = self.prefect_rest_api.workspace_id()
expected_url = f"https://api.prefect.cloud/api/accounts/{account_id}/workspaces/{workspace_id}"
assert self.prefect_rest_api.prefect_api_url() == expected_url

# def test_make_request(self):
# response = self.prefect_rest_api.make_request()
# assert response.status_code == 200
# pprint(response.json())
# for artifact in response.json():
# print(artifact)
assert self.prefect_rest_api.prefect_api_url() == expected_url

0 comments on commit a650a78

Please sign in to comment.