dotX12 / fastapi-gateway

⚙️ FastAPI gateway for microservices.
MIT License
111 stars 27 forks source link

route throw error when it deserialize a StreamingResponse/FileResponse (Fastapi) #5

Open BrandeeDev opened 2 years ago

BrandeeDev commented 2 years ago

Hello. I'm having trouble downloading PDF files from the microservice. I suppose that the maqueRequest method only decodes JSON responses. In my example, the gateway throws an exception since the microservice returns a StreamingResponse.

with async_timeout.timeout(delay=timeout):
        async with aiohttp.ClientSession(headers=headers) as session:
            async with session.request(
                    method=method, url=url, params=query, data=data) as response:
                response_json = await response.json()
                decoded_json = decode_json(data=response_json)
                return decoded_json, response.status, response.headers

My method signature

@pmrush_billing_information_router.get("/api/billing/invoices/download/{invoice_number}", response_model=Any,
                                           tags=["billing_information"])
    @pm_logger
    async def download_invoice(self, invoice_number: str, auth_user: UserInfoResponse = Depends(get_current_user)):

       ......

        invoice_pdf = requests.get(download_link, stream=True)
        output = BytesIO(invoice_pdf.content)
        output.seek(0)
        headers = {
            'Content-Disposition': 'attachment; filename="invoice.pdf"'
        }
        return StreamingResponse(output, headers=headers)

Api gateway signature:

@staticmethod
    @route(
        request_method=api_gateway_billing_information_router.get,
        service_url=SERVICE_URL,
        gateway_path='/api/billing/invoices/download/{invoice_number}',
        service_path='/api/billing/invoices/download/{invoice_number}',
        query_params=["invoice_number"],
        body_params=[],
        status_code=status.HTTP_200_OK,
        tags=['billing_information'],
        response_model=Any
    )
    @pm_logger
    async def download_invoice(invoice_number: str, request: Request, response: Response,
                               token: str = Depends(reuseable_oauth)):
        pass
BrandeeDev commented 2 years ago

Hello Any update for this feature ?

dotX12 commented 2 years ago

Hello Any update for this feature ?

Hello, I'll take a look the other day, I left the city :(

xitowzys commented 1 year ago

@BrandeeDev Hi, I was somehow able to add support for SteamingResponse. Look in the Pull request #7

ricciarellif commented 1 year ago

Hallo, this feature is CRITICAL in many scenarios... a lot of people need to download - through the API gateway - a binary resource... can you please pay attention to our requests?