11from __future__ import annotations
22
33from typing import Any , Dict
4- from urllib .parse import quote , urlencode
54
65import httpx
76
7+ from pocketbase .errors import ClientResponseError
88from pocketbase .models import FileUpload
99from pocketbase .models .record import Record
1010from pocketbase .services .admin_service import AdminService
1111from pocketbase .services .backups_service import BackupsService
1212from pocketbase .services .collection_service import CollectionService
13+ from pocketbase .services .files_service import FileService
14+ from pocketbase .services .health_service import HealthService
1315from pocketbase .services .log_service import LogService
1416from pocketbase .services .realtime_service import RealtimeService
1517from pocketbase .services .record_service import RecordService
1618from pocketbase .services .settings_service import SettingsService
17- from pocketbase .stores .base_auth_store import BaseAuthStore
18- from pocketbase .utils import ClientResponseError
19+ from pocketbase .stores .base_auth_store import AuthStore , BaseAuthStore
1920
2021
2122class Client :
22- base_url : str
23- lang : str
24- auth_store : BaseAuthStore
25- settings : SettingsService
26- admins : AdminService
27- collections : CollectionService
28- records : RecordService
29- logs : LogService
30- realtime : RealtimeService
31- record_service : Dict [str , RecordService ]
32-
3323 def __init__ (
3424 self ,
3525 base_url : str = "/" ,
3626 lang : str = "en-US" ,
37- auth_store : BaseAuthStore | None = None ,
27+ auth_store : AuthStore | None = None ,
3828 timeout : float = 120 ,
3929 http_client : httpx .Client | None = None ,
4030 ) -> None :
@@ -47,16 +37,12 @@ def __init__(
4737 self .admins = AdminService (self )
4838 self .backups = BackupsService (self )
4939 self .collections = CollectionService (self )
40+ self .files = FileService (self )
41+ self .health = HealthService (self )
5042 self .logs = LogService (self )
5143 self .settings = SettingsService (self )
5244 self .realtime = RealtimeService (self )
53- self .record_service = {}
54-
55- def collection (self , id_or_name : str ) -> RecordService :
56- """Returns the RecordService associated to the specified collection."""
57- if id_or_name not in self .record_service :
58- self .record_service [id_or_name ] = RecordService (self , id_or_name )
59- return self .record_service [id_or_name ]
45+ self .record_service : Dict [str , RecordService ] = {}
6046
6147 def _send (self , path : str , req_config : dict [str , Any ]) -> httpx .Response :
6248 """Sends an api http request returning response object."""
@@ -74,9 +60,9 @@ def _send(self, path: str, req_config: dict[str, Any]) -> httpx.Response:
7460 method = config .get ("method" , "GET" )
7561 params = config .get ("params" , None )
7662 headers = config .get ("headers" , None )
77- body = config .get ("body" , None )
63+ body : dict [ str , Any ] | None = config .get ("body" , None )
7864 # handle requests including files as multipart:
79- data = {}
65+ data : dict [ str , Any ] | None = {}
8066 files = ()
8167 for k , v in (body if isinstance (body , dict ) else {}).items ():
8268 if isinstance (v , FileUpload ):
@@ -108,6 +94,12 @@ def _send(self, path: str, req_config: dict[str, Any]) -> httpx.Response:
10894 )
10995 return response
11096
97+ def collection (self , id_or_name : str ) -> RecordService :
98+ """Returns the RecordService associated to the specified collection."""
99+ if id_or_name not in self .record_service :
100+ self .record_service [id_or_name ] = RecordService (self , id_or_name )
101+ return self .record_service [id_or_name ]
102+
111103 def send_raw (self , path : str , req_config : dict [str , Any ]) -> bytes :
112104 """Sends an api http request returning raw bytes response."""
113105 response = self ._send (path , req_config )
@@ -123,35 +115,29 @@ def send(self, path: str, req_config: dict[str, Any]) -> Any:
123115 if response .status_code >= 400 :
124116 raise ClientResponseError (
125117 f"Response error. Status code:{ response .status_code } " ,
126- url = response .url ,
118+ url = str ( response .url ) ,
127119 status = response .status_code ,
128120 data = data ,
129121 )
130122 return data
131123
132- def get_file_url (self , record : Record , filename : str , query_params : dict ):
133- parts = [
134- "api" ,
135- "files" ,
136- quote (record .collection_id or record .collection_name ),
137- quote (record .id ),
138- quote (filename ),
139- ]
140- result = self .build_url ("/" .join (parts ))
141- if len (query_params ) != 0 :
142- params : str = urlencode (query_params )
143- result += "&" if "?" in result else "?"
144- result += params
145- return result
146-
147- def get_file_token (self ):
148- res = self .send ("/api/files/token" , req_config = {"method" : "POST" })
149- return res ["token" ]
150-
151124 def build_url (self , path : str ) -> str :
152125 url = self .base_url
153126 if not self .base_url .endswith ("/" ):
154127 url += "/"
155128 if path .startswith ("/" ):
156129 path = path [1 :]
157130 return url + path
131+
132+ # TODO: add deprecated decorator
133+ def get_file_url (
134+ self ,
135+ record : Record ,
136+ filename : str ,
137+ query_params : dict [str , Any ] | None = None ,
138+ ):
139+ return self .files .get_url (record , filename , query_params )
140+
141+ # TODO: add deprecated decorator
142+ def get_file_token (self ) -> str :
143+ return self .files .get_token ()
0 commit comments