From a650a780b47f5586ddfd61760a70d439c3766281 Mon Sep 17 00:00:00 2001 From: Dinis Cruz Date: Sat, 5 Oct 2024 16:33:24 +0100 Subject: [PATCH] improved pdf and screenshot endpoint to allow the viewing of the file on the browser (or swagger) added more tests to Prefect api --- .../fast_api/routes/Routes__Browser.py | 43 ++++++++++++++----- .../prefect/Prefect__Cloud_API.py | 10 ++--- .../prefect/Prefect__Rest_API.py | 12 +++++- .../prefect/test_Prefect__Cloud_API.py | 15 +++++-- .../prefect/test_Prefect__Rest_API.py | 9 +--- 5 files changed, 58 insertions(+), 31 deletions(-) diff --git a/osbot_serverless_flows/fast_api/routes/Routes__Browser.py b/osbot_serverless_flows/fast_api/routes/Routes__Browser.py index 946da3d..bea54ed 100644 --- a/osbot_serverless_flows/fast_api/routes/Routes__Browser.py +++ b/osbot_serverless_flows/fast_api/routes/Routes__Browser.py @@ -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 @@ -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 ) diff --git a/osbot_serverless_flows/observability/prefect/Prefect__Cloud_API.py b/osbot_serverless_flows/observability/prefect/Prefect__Cloud_API.py index 6c34cd5..71d6bae 100644 --- a/osbot_serverless_flows/observability/prefect/Prefect__Cloud_API.py +++ b/osbot_serverless_flows/observability/prefect/Prefect__Cloud_API.py @@ -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) \ No newline at end of file + + def flows(self, limit=5): + return self.prefect_rest_api.filter(target='flows', limit=limit) \ No newline at end of file diff --git a/osbot_serverless_flows/observability/prefect/Prefect__Rest_API.py b/osbot_serverless_flows/observability/prefect/Prefect__Rest_API.py index b1e2d9e..bbb4533 100644 --- a/osbot_serverless_flows/observability/prefect/Prefect__Rest_API.py +++ b/osbot_serverless_flows/observability/prefect/Prefect__Rest_API.py @@ -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) @@ -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 \ No newline at end of file + 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) \ No newline at end of file diff --git a/tests/integration/observability/prefect/test_Prefect__Cloud_API.py b/tests/integration/observability/prefect/test_Prefect__Cloud_API.py index ea4302a..194b906 100644 --- a/tests/integration/observability/prefect/test_Prefect__Cloud_API.py +++ b/tests/integration/observability/prefect/test_Prefect__Cloud_API.py @@ -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 @@ -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'] diff --git a/tests/integration/observability/prefect/test_Prefect__Rest_API.py b/tests/integration/observability/prefect/test_Prefect__Rest_API.py index f3531ae..32071e0 100644 --- a/tests/integration/observability/prefect/test_Prefect__Rest_API.py +++ b/tests/integration/observability/prefect/test_Prefect__Rest_API.py @@ -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) \ No newline at end of file + assert self.prefect_rest_api.prefect_api_url() == expected_url \ No newline at end of file