44import httpx
55from httpx import Response
66
7+ from foxops_client .exceptions import (
8+ AuthenticationError ,
9+ FoxopsApiError ,
10+ IncarnationDoesNotExistError ,
11+ )
712from foxops_client .retries import default_retry
813from foxops_client .types import Incarnation , IncarnationWithDetails
914
1015
11- class AuthenticationError (Exception ):
12- pass
13-
14-
15- class FoxOpsApiError (Exception ):
16- def __init__ (self , message : str ):
17- super ().__init__ ()
18- self .message = message
19-
20-
21- class IncarnationDoesNotExistError (FoxOpsApiError ):
22- pass
23-
24-
25- class FoxOpsClient :
16+ class AsyncFoxopsClient :
2617 """
2718 This class can be used to call the FoxOps API.
2819
@@ -34,10 +25,9 @@ class FoxOpsClient:
3425
3526 def __init__ (self , base_url : str , token : str ):
3627 self .retry_function = default_retry ()
37-
3828 self .log : logging .Logger = logging .getLogger (self .__class__ .__name__ )
3929
40- self .client = httpx .Client (
30+ self .client = httpx .AsyncClient (
4131 base_url = base_url ,
4232 headers = {"Authorization" : f"Bearer { token } " },
4333 verify = True ,
@@ -55,16 +45,16 @@ def _handle_unexpected_response(self, resp: Response):
5545
5646 resp .raise_for_status ()
5747
58- def verify_token (self ):
59- response = self .retry_function (self .client .get )("/auth/test" )
48+ async def verify_token (self ):
49+ resp = await self .retry_function (self .client .get )("/auth/test" )
6050
61- if response .status_code == httpx .codes .OK :
51+ if resp .status_code == httpx .codes .OK :
6252 return
6353
64- self ._handle_unexpected_response (response )
54+ self ._handle_unexpected_response (resp )
6555 raise ValueError ("unexpected response" )
6656
67- def list_incarnations (
57+ async def list_incarnations (
6858 self , incarnation_repository : str | None = None , target_directory : str | None = None
6959 ) -> list [Incarnation ]:
7060 params = {}
@@ -73,7 +63,7 @@ def list_incarnations(
7363 if target_directory is not None :
7464 params ["target_directory" ] = target_directory
7565
76- resp = self .retry_function (self .client .get )("/api/incarnations" , params = params )
66+ resp = await self .retry_function (self .client .get )("/api/incarnations" , params = params )
7767
7868 match resp .status_code :
7969 case httpx .codes .OK :
@@ -84,8 +74,8 @@ def list_incarnations(
8474 self ._handle_unexpected_response (resp )
8575 raise ValueError ("unexpected response" )
8676
87- def get_incarnation (self , incarnation_id : int ) -> IncarnationWithDetails :
88- resp = self .retry_function (self .client .get )(f"/api/incarnations/{ incarnation_id } " )
77+ async def get_incarnation (self , incarnation_id : int ) -> IncarnationWithDetails :
78+ resp = await self .retry_function (self .client .get )(f"/api/incarnations/{ incarnation_id } " )
8979
9080 match resp .status_code :
9181 case httpx .codes .OK :
@@ -96,8 +86,8 @@ def get_incarnation(self, incarnation_id: int) -> IncarnationWithDetails:
9686 self ._handle_unexpected_response (resp )
9787 raise ValueError ("unexpected response" )
9888
99- def delete_incarnation (self , incarnation_id : int ):
100- resp = self .retry_function (self .client .delete )(f"/api/incarnations/{ incarnation_id } " )
89+ async def delete_incarnation (self , incarnation_id : int ):
90+ resp = await self .retry_function (self .client .delete )(f"/api/incarnations/{ incarnation_id } " )
10191
10292 match resp .status_code :
10393 case httpx .codes .NO_CONTENT :
@@ -108,7 +98,7 @@ def delete_incarnation(self, incarnation_id: int):
10898 self ._handle_unexpected_response (resp )
10999 raise ValueError ("unexpected response" )
110100
111- def update_incarnation (
101+ async def update_incarnation (
112102 self ,
113103 incarnation_id : int ,
114104 automerge : bool ,
@@ -123,7 +113,7 @@ def update_incarnation(
123113 if template_data is not None :
124114 data ["template_data" ] = template_data
125115
126- resp = self .retry_function (self .client .put )(f"/api/incarnations/{ incarnation_id } " , json = data )
116+ resp = await self .retry_function (self .client .put )(f"/api/incarnations/{ incarnation_id } " , json = data )
127117
128118 match resp .status_code :
129119 case httpx .codes .OK :
@@ -132,12 +122,12 @@ def update_incarnation(
132122 raise IncarnationDoesNotExistError (resp .json ()["message" ])
133123 case httpx .codes .BAD_REQUEST | httpx .codes .CONFLICT :
134124 self .log .error (f"received error from FoxOps API: { resp .status_code } { resp .headers } { resp .text } " )
135- raise FoxOpsApiError (resp .json ()["message" ])
125+ raise FoxopsApiError (resp .json ()["message" ])
136126
137127 self ._handle_unexpected_response (resp )
138128 raise ValueError ("unexpected response" )
139129
140- def create_incarnation (
130+ async def create_incarnation (
141131 self ,
142132 incarnation_repository : str ,
143133 template_repository : str ,
@@ -169,7 +159,7 @@ def create_incarnation(
169159 if allow_import is not None :
170160 params ["allow_import" ] = allow_import
171161
172- resp = self .retry_function (self .client .post )("/api/incarnations" , params = params , json = data )
162+ resp = await self .retry_function (self .client .post )("/api/incarnations" , params = params , json = data )
173163
174164 match resp .status_code :
175165 case httpx .codes .OK :
@@ -178,7 +168,7 @@ def create_incarnation(
178168 return False , IncarnationWithDetails (** resp .json ())
179169 case httpx .codes .BAD_REQUEST :
180170 self .log .error (f"received error from FoxOps API: { resp .status_code } { resp .headers } { resp .text } " )
181- raise FoxOpsApiError (resp .json ()["message" ])
171+ raise FoxopsApiError (resp .json ()["message" ])
182172
183173 self ._handle_unexpected_response (resp )
184174 raise ValueError ("unexpected response" )
0 commit comments