20
20
UndefinedQuixWorkspaceId ,
21
21
)
22
22
23
+ __all__ = ("QuixKafkaConfigsBuilder" , "QuixApplicationConfig" , "DEFAULT_PORTAL_API_URL" )
24
+
23
25
logger = logging .getLogger (__name__ )
24
26
25
- __all__ = ("QuixKafkaConfigsBuilder" , "QuixApplicationConfig" )
26
27
28
+ DEFAULT_PORTAL_API_URL = "https://portal-api.platform.quix.io/"
27
29
QUIX_CONNECTIONS_MAX_IDLE_MS = 3 * 60 * 1000
28
30
QUIX_METADATA_MAX_AGE_MS = 3 * 60 * 1000
29
31
@@ -95,32 +97,21 @@ class QuixKafkaConfigsBuilder:
95
97
It also currently handles the app_auto_create_topics setting for Quix Applications.
96
98
"""
97
99
98
- # TODO: Consider a workspace class?
99
100
def __init__ (
100
101
self ,
101
- quix_sdk_token : Optional [ str ] = None ,
102
+ quix_portal_api_service : QuixPortalApiService ,
102
103
workspace_id : Optional [str ] = None ,
103
- quix_portal_api_service : Optional [QuixPortalApiService ] = None ,
104
104
timeout : float = 30 ,
105
105
topic_create_timeout : float = 60 ,
106
106
):
107
107
"""
108
- :param quix_portal_api_service: A QuixPortalApiService instance (else generated)
108
+ :param quix_portal_api_service: A QuixPortalApiService instance
109
109
:param workspace_id: A valid Quix Workspace ID (else searched for)
110
110
"""
111
- if quix_sdk_token :
112
- self .api = QuixPortalApiService (
113
- default_workspace_id = workspace_id , auth_token = quix_sdk_token
114
- )
115
- elif quix_portal_api_service :
116
- self .api = quix_portal_api_service
117
- else :
118
- raise ValueError (
119
- 'Either "quix_sdk_token" or "quix_portal_api_service" must be provided'
120
- )
121
111
112
+ self ._api = quix_portal_api_service
122
113
try :
123
- self ._workspace_id = workspace_id or self .api .default_workspace_id
114
+ self ._workspace_id = workspace_id or self ._api .default_workspace_id
124
115
except UndefinedQuixWorkspaceId :
125
116
self ._workspace_id = ""
126
117
logger .warning (
@@ -136,6 +127,30 @@ def __init__(
136
127
self ._timeout = timeout
137
128
self ._topic_create_timeout = topic_create_timeout
138
129
130
+ @classmethod
131
+ def from_credentials (
132
+ cls ,
133
+ quix_sdk_token : str ,
134
+ quix_portal_api : str = DEFAULT_PORTAL_API_URL ,
135
+ workspace_id : Optional [str ] = None ,
136
+ timeout : float = 30 ,
137
+ topic_create_timeout : float = 60 ,
138
+ ) -> "QuixKafkaConfigsBuilder" :
139
+ """
140
+ Initialize class using the quix_sdk_token and quix_portal_api params.
141
+ """
142
+ api_service = QuixPortalApiService (
143
+ default_workspace_id = workspace_id ,
144
+ auth_token = quix_sdk_token ,
145
+ portal_api = quix_portal_api ,
146
+ )
147
+ return cls (
148
+ quix_portal_api_service = api_service ,
149
+ workspace_id = workspace_id ,
150
+ timeout = timeout ,
151
+ topic_create_timeout = topic_create_timeout ,
152
+ )
153
+
139
154
@property
140
155
def workspace_id (self ) -> str :
141
156
if not self ._workspace_id :
@@ -250,12 +265,12 @@ def search_for_workspace(
250
265
if not workspace_name_or_id :
251
266
workspace_name_or_id = self ._workspace_id
252
267
try :
253
- return self .api .get_workspace (
268
+ return self ._api .get_workspace (
254
269
workspace_id = workspace_name_or_id , timeout = timeout
255
270
)
256
271
except HTTPError :
257
272
# check to see if they provided the workspace name instead
258
- ws_list = self .api .get_workspaces (timeout = timeout )
273
+ ws_list = self ._api .get_workspaces (timeout = timeout )
259
274
for ws in ws_list :
260
275
if ws ["name" ] == workspace_name_or_id :
261
276
return ws
@@ -315,7 +330,7 @@ def search_workspace_for_topic(
315
330
316
331
:return: the workspace_id if success, else None
317
332
"""
318
- topics = self .api .get_topics (
333
+ topics = self ._api .get_topics (
319
334
workspace_id = workspace_id ,
320
335
timeout = timeout if timeout is not None else self ._timeout ,
321
336
)
@@ -338,7 +353,7 @@ def search_for_topic_workspace(
338
353
339
354
:return: workspace data dict if topic search success, else None
340
355
"""
341
- ws_list = self .api .get_workspaces (
356
+ ws_list = self ._api .get_workspaces (
342
357
timeout = timeout if timeout is not None else self ._timeout
343
358
)
344
359
if len (ws_list ) == 1 :
@@ -376,7 +391,7 @@ def create_topic(self, topic: Topic, timeout: Optional[float] = None) -> dict:
376
391
f'Creating topic "{ topic .name } " '
377
392
f'with a config: "{ topic .create_config .as_dict () if topic .create_config is not None else {} } "'
378
393
)
379
- resp = self .api .post_topic (
394
+ resp = self ._api .post_topic (
380
395
topic_name = topic .name ,
381
396
workspace_id = self .workspace_id ,
382
397
topic_partitions = cfg .num_partitions ,
@@ -473,14 +488,14 @@ def get_topic(self, topic: Topic, timeout: Optional[float] = None) -> dict:
473
488
:return: response dict of the topic info if topic found, else None
474
489
:raises QuixApiRequestFailure: when topic does not exist
475
490
"""
476
- return self .api .get_topic (
491
+ return self ._api .get_topic (
477
492
topic_name = topic .name ,
478
493
workspace_id = self .workspace_id ,
479
494
timeout = timeout if timeout is not None else self ._timeout ,
480
495
)
481
496
482
497
def get_topics (self , timeout : Optional [float ] = None ) -> List [dict ]:
483
- return self .api .get_topics (
498
+ return self ._api .get_topics (
484
499
workspace_id = self .workspace_id ,
485
500
timeout = timeout if timeout is not None else self ._timeout ,
486
501
)
@@ -490,7 +505,7 @@ def _get_librdkafka_connection_config(self) -> ConnectionConfig:
490
505
Get the full client config required to authenticate a confluent-kafka
491
506
client to a Quix platform broker/workspace as a ConnectionConfig
492
507
"""
493
- librdkafka_dict = self .api .get_librdkafka_connection_config (self .workspace_id )
508
+ librdkafka_dict = self ._api .get_librdkafka_connection_config (self .workspace_id )
494
509
if (cert := librdkafka_dict .pop ("ssl.ca.cert" , None )) is not None :
495
510
librdkafka_dict ["ssl.ca.pem" ] = base64 .b64decode (cert )
496
511
return ConnectionConfig .from_librdkafka_dict (librdkafka_dict )
0 commit comments