@@ -78,3 +78,71 @@ def get_device(self, app_id: AppId, user_id: UserId, device_id: DeviceId) -> Dev
7878 except JSONDecodeError :
7979 raise ApiError (status_code = _response .status_code , body = _response .text )
8080 raise ApiError (status_code = _response .status_code , body = _response_json )
81+
82+
83+ class AsyncDeviceClient :
84+ def __init__ (self , * , environment : RavenApiEnvironment , auth_key : str ):
85+ self ._environment = environment
86+ self .auth_key = auth_key
87+
88+ async def add (self , app_id : AppId , user_id : UserId , * , request : Device ) -> Device :
89+ async with httpx .AsyncClient () as _client :
90+ _response = await _client .request (
91+ "POST" ,
92+ urllib .parse .urljoin (f"{ self ._environment } /" , f"v1/apps/{ app_id } /users/{ user_id } /devices" ),
93+ json = jsonable_encoder (request ),
94+ headers = remove_none_from_headers ({"Authorization" : self .auth_key }),
95+ )
96+ if 200 <= _response .status_code < 300 :
97+ return pydantic .parse_obj_as (Device , _response .json ()) # type: ignore
98+ try :
99+ _response_json = _response .json ()
100+ except JSONDecodeError :
101+ raise ApiError (status_code = _response .status_code , body = _response .text )
102+ raise ApiError (status_code = _response .status_code , body = _response_json )
103+
104+ async def update (self , app_id : AppId , user_id : UserId , device_id : DeviceId , * , request : Device ) -> Device :
105+ async with httpx .AsyncClient () as _client :
106+ _response = await _client .request (
107+ "PUT" ,
108+ urllib .parse .urljoin (f"{ self ._environment } /" , f"v1/apps/{ app_id } /users/{ user_id } /devices/{ device_id } " ),
109+ json = jsonable_encoder (request ),
110+ headers = remove_none_from_headers ({"Authorization" : self .auth_key }),
111+ )
112+ if 200 <= _response .status_code < 300 :
113+ return pydantic .parse_obj_as (Device , _response .json ()) # type: ignore
114+ try :
115+ _response_json = _response .json ()
116+ except JSONDecodeError :
117+ raise ApiError (status_code = _response .status_code , body = _response .text )
118+ raise ApiError (status_code = _response .status_code , body = _response_json )
119+
120+ async def delete (self , app_id : AppId , user_id : UserId , device_id : DeviceId ) -> None :
121+ async with httpx .AsyncClient () as _client :
122+ _response = await _client .request (
123+ "DELETE" ,
124+ urllib .parse .urljoin (f"{ self ._environment } /" , f"v1/apps/{ app_id } /users/{ user_id } /devices/{ device_id } " ),
125+ headers = remove_none_from_headers ({"Authorization" : self .auth_key }),
126+ )
127+ if 200 <= _response .status_code < 300 :
128+ return
129+ try :
130+ _response_json = _response .json ()
131+ except JSONDecodeError :
132+ raise ApiError (status_code = _response .status_code , body = _response .text )
133+ raise ApiError (status_code = _response .status_code , body = _response_json )
134+
135+ async def get_device (self , app_id : AppId , user_id : UserId , device_id : DeviceId ) -> Device :
136+ async with httpx .AsyncClient () as _client :
137+ _response = await _client .request (
138+ "GET" ,
139+ urllib .parse .urljoin (f"{ self ._environment } /" , f"v1/apps/{ app_id } /users/{ user_id } /devices/{ device_id } " ),
140+ headers = remove_none_from_headers ({"Authorization" : self .auth_key }),
141+ )
142+ if 200 <= _response .status_code < 300 :
143+ return pydantic .parse_obj_as (Device , _response .json ()) # type: ignore
144+ try :
145+ _response_json = _response .json ()
146+ except JSONDecodeError :
147+ raise ApiError (status_code = _response .status_code , body = _response .text )
148+ raise ApiError (status_code = _response .status_code , body = _response_json )
0 commit comments