44from __future__ import annotations
55
66import typing
7+ from typing import Literal
78
8- import httpx
99import pydantic
1010
11- from beeai_sdk .platform .context import get_platform_client
11+ from beeai_sdk .platform .client import PlatformClient , get_platform_client
1212
1313
1414class Extraction (pydantic .BaseModel ):
@@ -39,13 +39,17 @@ async def create(
3939 filename : str ,
4040 content : typing .BinaryIO | bytes ,
4141 content_type : str = "application/octet-stream" ,
42- client : httpx .AsyncClient | None = None ,
42+ client : PlatformClient | None = None ,
43+ context_id : str | None | Literal ["auto" ] = "auto" ,
4344 ) -> File :
45+ platform_client = client or get_platform_client ()
46+ context_id = platform_client .context_id if context_id == "auto" else context_id
4447 return pydantic .TypeAdapter (File ).validate_python (
4548 (
46- await ( client or get_platform_client ()) .post (
49+ await platform_client .post (
4750 url = "/api/v1/files" ,
4851 files = {"file" : (filename , content , content_type )},
52+ params = context_id and {"context_id" : context_id },
4953 )
5054 )
5155 .raise_for_status ()
@@ -55,60 +59,96 @@ async def create(
5559 async def get (
5660 self : File | str ,
5761 * ,
58- client : httpx .AsyncClient | None = None ,
62+ client : PlatformClient | None = None ,
63+ context_id : str | None | Literal ["auto" ] = "auto" ,
5964 ) -> File :
6065 # `self` has a weird type so that you can call both `instance.get()` to update an instance, or `File.get("123")` to obtain a new instance
6166 file_id = self if isinstance (self , str ) else self .id
67+ platform_client = client or get_platform_client ()
68+ context_id = platform_client .context_id if context_id == "auto" else context_id
6269 return pydantic .TypeAdapter (File ).validate_python (
63- (await (client or get_platform_client ()).get (url = f"/api/v1/files/{ file_id } " )).raise_for_status ().json ()
70+ (
71+ await platform_client .get (
72+ url = f"/api/v1/files/{ file_id } " ,
73+ params = context_id and {"context_id" : context_id },
74+ )
75+ )
76+ .raise_for_status ()
77+ .json ()
6478 )
6579
6680 async def delete (
6781 self : File | str ,
6882 * ,
69- client : httpx .AsyncClient | None = None ,
83+ client : PlatformClient | None = None ,
84+ context_id : str | None | Literal ["auto" ] = "auto" ,
7085 ) -> None :
7186 # `self` has a weird type so that you can call both `instance.delete()` or `File.delete("123")`
7287 file_id = self if isinstance (self , str ) else self .id
73- _ = (await (client or get_platform_client ()).delete (url = f"/api/v1/files/{ file_id } " )).raise_for_status ()
88+ platform_client = client or get_platform_client ()
89+ context_id = platform_client .context_id if context_id == "auto" else context_id
90+ _ = (
91+ await platform_client .delete (
92+ url = f"/api/v1/files/{ file_id } " , params = context_id and {"context_id" : context_id }
93+ )
94+ ).raise_for_status ()
7495
7596 async def content (
7697 self : File | str ,
7798 * ,
78- client : httpx .AsyncClient | None = None ,
99+ client : PlatformClient | None = None ,
100+ context_id : str | None | Literal ["auto" ] = "auto" ,
79101 ) -> str :
80102 # `self` has a weird type so that you can call both `instance.content()` to get content of an instance, or `File.content("123")`
81103 file_id = self if isinstance (self , str ) else self .id
104+ platform_client = client or get_platform_client ()
105+ context_id = platform_client .context_id if context_id == "auto" else context_id
82106 return (
83- (await (client or get_platform_client ()).get (url = f"/api/v1/files/{ file_id } /content" ))
107+ (
108+ await platform_client .get (
109+ url = f"/api/v1/files/{ file_id } /content" , params = context_id and {"context_id" : context_id }
110+ )
111+ )
84112 .raise_for_status ()
85113 .text
86114 )
87115
88116 async def text_content (
89117 self : File | str ,
90118 * ,
91- client : httpx .AsyncClient | None = None ,
119+ client : PlatformClient | None = None ,
120+ context_id : str | None | Literal ["auto" ] = "auto" ,
92121 ) -> str :
93122 # `self` has a weird type so that you can call both `instance.text_content()` to get text content of an instance, or `File.text_content("123")`
94123 file_id = self if isinstance (self , str ) else self .id
124+ platform_client = client or get_platform_client ()
125+ context_id = platform_client .context_id if context_id == "auto" else context_id
95126 return (
96- (await (client or get_platform_client ()).get (url = f"/api/v1/files/{ file_id } /text_content" ))
127+ (
128+ await platform_client .get (
129+ url = f"/api/v1/files/{ file_id } /text_content" ,
130+ params = context_id and {"context_id" : context_id },
131+ )
132+ )
97133 .raise_for_status ()
98134 .text
99135 )
100136
101137 async def create_extraction (
102138 self : File | str ,
103139 * ,
104- client : httpx .AsyncClient | None = None ,
140+ client : PlatformClient | None = None ,
141+ context_id : str | None | Literal ["auto" ] = "auto" ,
105142 ) -> Extraction :
106143 # `self` has a weird type so that you can call both `instance.create_extraction()` to create an extraction for an instance, or `File.create_extraction("123")`
107144 file_id = self if isinstance (self , str ) else self .id
145+ platform_client = client or get_platform_client ()
146+ context_id = platform_client .context_id if context_id == "auto" else context_id
108147 return pydantic .TypeAdapter (Extraction ).validate_python (
109148 (
110- await ( client or get_platform_client ()) .post (
149+ await platform_client .post (
111150 url = f"/api/v1/files/{ file_id } /extraction" ,
151+ params = context_id and {"context_id" : context_id },
112152 )
113153 )
114154 .raise_for_status ()
@@ -118,14 +158,18 @@ async def create_extraction(
118158 async def get_extraction (
119159 self : File | str ,
120160 * ,
121- client : httpx .AsyncClient | None = None ,
161+ client : PlatformClient | None = None ,
162+ context_id : str | None | Literal ["auto" ] = "auto" ,
122163 ) -> Extraction :
123164 # `self` has a weird type so that you can call both `instance.get_extraction()` to get an extraction of an instance, or `File.get_extraction("123", "456")`
124165 file_id = self if isinstance (self , str ) else self .id
166+ platform_client = client or get_platform_client ()
167+ context_id = platform_client .context_id if context_id == "auto" else context_id
125168 return pydantic .TypeAdapter (Extraction ).validate_python (
126169 (
127- await ( client or get_platform_client ()) .get (
170+ await platform_client .get (
128171 url = f"/api/v1/files/{ file_id } /extraction" ,
172+ params = context_id and {"context_id" : context_id },
129173 )
130174 )
131175 .raise_for_status ()
@@ -135,10 +179,16 @@ async def get_extraction(
135179 async def delete_extraction (
136180 self : File | str ,
137181 * ,
138- client : httpx .AsyncClient | None = None ,
182+ client : PlatformClient | None = None ,
183+ context_id : str | None | Literal ["auto" ] = "auto" ,
139184 ) -> None :
140185 # `self` has a weird type so that you can call both `instance.delete_extraction()` or `File.delete_extraction("123", "456")`
141186 file_id = self if isinstance (self , str ) else self .id
187+ platform_client = client or get_platform_client ()
188+ context_id = platform_client .context_id if context_id == "auto" else context_id
142189 _ = (
143- await (client or get_platform_client ()).delete (url = f"/api/v1/files/{ file_id } /extraction" )
190+ await platform_client .delete (
191+ url = f"/api/v1/files/{ file_id } /extraction" ,
192+ params = context_id and {"context_id" : context_id },
193+ )
144194 ).raise_for_status ()
0 commit comments