11from __future__ import annotations
22
3- from typing import Any
3+ from typing import Any , Dict
4+ from urllib .parse import quote , urlencode
45
56import httpx
67
7- from pocketbase .services .admins import Admins
8- from pocketbase .services .collections import Collections
9- from pocketbase .services .logs import Logs
10- from pocketbase .services .realtime import Realtime
11- from pocketbase .services .records import Records
12- from pocketbase .services .users import Users
13- from pocketbase .services .settings import Settings
8+ from pocketbase .utils import ClientResponseError
9+ from pocketbase .models .record import Record
10+ from pocketbase .services .admin_service import AdminService
11+ from pocketbase .services .collection_service import CollectionService
12+ from pocketbase .services .log_service import LogService
13+ from pocketbase .services .realtime_service import RealtimeService
14+ from pocketbase .services .record_service import RecordService
15+ from pocketbase .services .settings_service import SettingsService
1416from pocketbase .stores .base_auth_store import BaseAuthStore
1517
1618
17- class ClientResponseError (Exception ):
18- url : str = ""
19- status : int = 0
20- data : dict = {}
21- is_abort : bool = False
22- original_error : Any | None = None
23-
24- def __init__ (self , * args , ** kwargs ) -> None :
25- super ().__init__ (* args )
26- self .url = kwargs .get ("url" , "" )
27- self .status = kwargs .get ("status" , 0 )
28- self .data = kwargs .get ("data" , {})
29- self .is_abort = kwargs .get ("is_abort" , False )
30- self .original_error = kwargs .get ("original_error" , None )
31-
32-
3319class Client :
3420 base_url : str
3521 lang : str
3622 auth_store : BaseAuthStore
37- settings : Settings
38- admins : Admins
39- users : Users
40- collections : Collections
41- records : Records
42- logs : Logs
43- realtime : Realtime
23+ settings : SettingsService
24+ admins : AdminService
25+ records : Record
26+ collections : CollectionService
27+ records : RecordService
28+ logs : LogService
29+ realtime : RealtimeService
30+ record_service : Dict [str , RecordService ]
4431
4532 def __init__ (
4633 self ,
@@ -52,13 +39,18 @@ def __init__(
5239 self .lang = lang
5340 self .auth_store = auth_store or BaseAuthStore () # LocalAuthStore()
5441 # services
55- self .admins = Admins (self )
56- self .users = Users (self )
57- self .records = Records (self )
58- self .collections = Collections (self )
59- self .logs = Logs (self )
60- self .settings = Settings (self )
61- self .realtime = Realtime (self )
42+ self .admins = AdminService (self )
43+ self .collections = CollectionService (self )
44+ self .logs = LogService (self )
45+ self .settings = SettingsService (self )
46+ self .realtime = RealtimeService (self )
47+ self .record_service = {}
48+
49+ def collection (self , id_or_name : str ) -> RecordService :
50+ """Returns the RecordService associated to the specified collection."""
51+ if id_or_name not in self .record_service :
52+ self .record_service [id_or_name ] = RecordService (self , id_or_name )
53+ return self .record_service [id_or_name ]
6254
6355 def send (self , path : str , req_config : dict [str :Any ]) -> Any :
6456 """Sends an api http request."""
@@ -68,12 +60,9 @@ def send(self, path: str, req_config: dict[str:Any]) -> Any:
6860 if self .auth_store .token and (
6961 "headers" not in config or "Authorization" not in config ["headers" ]
7062 ):
71- auth_type = "Admin"
72- if hasattr (self .auth_store .model , "verified" ):
73- auth_type = "User"
7463 config ["headers" ] = config .get ("headers" , {})
7564 config ["headers" ].update (
76- {"Authorization" : f" { auth_type } { self .auth_store .token } " }
65+ {"Authorization" : self .auth_store .token }
7766 )
7867 # build url + path
7968 url = self .build_url (path )
@@ -109,6 +98,21 @@ def send(self, path: str, req_config: dict[str:Any]) -> Any:
10998 )
11099 return data
111100
101+ def get_file_url (self , record : Record , filename : str , query_params : dict ):
102+ parts = [
103+ 'api' ,
104+ 'files' ,
105+ quote (record .collection_id or record .collection_name ),
106+ quote (record .id ),
107+ quote (filename ),
108+ ]
109+ result = self .build_url ('/' .join (parts ))
110+ if len (query_params ) != 0 :
111+ params : str = urlencode (query_params )
112+ result += '&' if '?' in result else '?'
113+ result += params
114+ return result
115+
112116 def build_url (self , path : str ) -> str :
113117 url = self .base_url
114118 if not self .base_url .endswith ("/" ):
0 commit comments