zhaoqingang
2024-11-20 67626f1c71d76c5e3d1646259024cb4a452c2890
app/service/basic.py
@@ -10,21 +10,27 @@
        if response.status_code not in [200, 201]:
            raise Exception(f"Failed to fetch data from API: {response.text}")
        response_data = response.json()
        status_code = response_data.get("status_code", 0)
        if status_code != 200:
            raise Exception(f"Failed to fetch data from API: {response.text}")
        return response_data.get("data", {})
        return response_data
    async def download_from_url(self, url: str, params: dict):
    async def download_from_url(self, url, params=None):
        async with httpx.AsyncClient() as client:
            response = await client.get(url, params=params, stream=True)
            if response.status_code == 200:
                content_disposition = response.headers.get('Content-Disposition')
                filename = content_disposition.split('filename=')[-1].strip(
                    '"') if content_disposition else 'unknown_filename'
                return response.content, filename, response.headers.get('Content-Type')
            else:
                return None, None, None
            async with client.stream('GET', url, params=params) as response:
                if response.status_code == 200:
                    # 获取文件名
                    content_disposition = response.headers.get('Content-Disposition')
                    if content_disposition:
                        filename = content_disposition.split('filename=')[1].strip('"')
                    else:
                        filename = 'unknown_filename'
                    # 获取内容类型
                    content_type = response.headers.get('Content-Type')
                    # 读取文件内容
                    content = await response.aread()
                    return content, filename, content_type
                else:
                    raise Exception(f"Failed to download: {response.status_code}")
    async def excel_talk_image_download(self, file_id: str):
        url = f"{self.base_url}/exceltalk/download/image"
@@ -47,7 +53,7 @@
                files=files,
                params=params
            )
            return await self._check_response(response)
            return self._check_response(response)
    async def excel_talk(self, question: str, chat_id: str):
        url = f"{self.base_url}/exceltalk/talk"
@@ -65,4 +71,4 @@
                        print(e)
                        return
                else:
                    yield f"Error: {response.status_code}"
                    yield f"Error: {response.status_code}"