1111# License for the specific language governing permissions and limitations
1212# under the License.
1313
14- from typing import Optional , List
14+ from typing import Optional
1515
1616import requests
17- from openfga_sdk import ClientConfiguration
18- from openfga_sdk .credentials import Credentials , CredentialConfiguration
19- from openfga_sdk .sync import OpenFgaClient
17+
2018from testcontainers .core .container import DockerContainer
2119from testcontainers .core .waiting_utils import wait_container_is_ready
2220
21+ no_client = False
22+ try :
23+ from openfga_sdk import ClientConfiguration
24+ from openfga_sdk .credentials import CredentialConfiguration , Credentials
25+ from openfga_sdk .sync import OpenFgaClient
26+ except ImportError :
27+ no_client = True
28+
29+ class OpenFgaClient :
30+ pass
31+
32+
2333_DEFAULT_RUN_COMMAND = "run"
2434
2535
@@ -41,13 +51,13 @@ class OpenFGAContainer(DockerContainer):
4151
4252 # pylint: disable=too-many-arguments
4353 def __init__ (
44- self ,
45- image : str = "openfga/openfga:latest" ,
46- preshared_keys : Optional [List [str ]] = None ,
47- playground_port : int = 3000 ,
48- http_port : int = 8080 ,
49- grpc_port : int = 8081 ,
50- cmd : str = _DEFAULT_RUN_COMMAND ,
54+ self ,
55+ image : str = "openfga/openfga:latest" ,
56+ preshared_keys : Optional [list [str ]] = None ,
57+ playground_port : int = 3000 ,
58+ http_port : int = 8080 ,
59+ grpc_port : int = 8081 ,
60+ cmd : str = _DEFAULT_RUN_COMMAND ,
5161 ) -> None :
5262 super ().__init__ (image = image )
5363 self .preshared_keys = preshared_keys
@@ -60,7 +70,7 @@ def __init__(
6070 def _configure (self ) -> None :
6171 if self .preshared_keys :
6272 self .cmd += " --authn-method=preshared"
63- self .cmd += f" --authn-preshared-keys=\" { ',' .join (self .preshared_keys )} \" "
73+ self .cmd += f' --authn-preshared-keys=" { "," .join (self .preshared_keys )} "'
6474 self .with_command (self .cmd )
6575
6676 def get_api_url (self ) -> str :
@@ -77,14 +87,17 @@ def start(self) -> "OpenFGAContainer":
7787 self ._readiness_probe ()
7888 return self
7989
80- def get_preshared_keys (self ) -> Optional [List [str ]]:
90+ def get_preshared_keys (self ) -> Optional [list [str ]]:
8191 return self .preshared_keys
8292
83- def get_client (self ) -> OpenFgaClient :
93+ def get_client (self ) -> "OpenFgaClient" :
94+ if no_client :
95+ raise NotImplementedError ("failed to import openfga_sdk: is python < 3.10?" )
96+
8497 credentials = None
8598 if preshared_keys := self .get_preshared_keys ():
8699 credentials = Credentials (
87- method = ' api_token' ,
100+ method = " api_token" ,
88101 configuration = CredentialConfiguration (
89102 api_token = preshared_keys [0 ],
90103 ),
0 commit comments