@@ -21,9 +21,63 @@ class CogStack(object):
2121 hosts : List[str]
2222 A list of Elasticsearch host URLs.
2323 """
24+ ES_TIMEOUT = 300
25+
2426 def __init__ (self , hosts : List [str ]):
2527 self .hosts = hosts
2628
29+ @classmethod
30+ def with_basic_auth (cls , hosts : List [str ], username : Optional [str ] = None , password : Optional [str ] = None ) -> 'CogStack' :
31+ """
32+ Create an instance of CogStack using basic authentication.
33+
34+ Parameters
35+ ----------
36+ hosts : List[str]
37+ A list of Elasticsearch host URLs.
38+ username : str, optional
39+ The username to use when connecting to Elasticsearch. If not provided, the user will be prompted to enter a username.
40+ password : str, optional
41+ The password to use when connecting to Elasticsearch. If not provided, the user will be prompted to enter a password.
42+ Returns
43+ -------
44+ CogStack: An instance of the CogStack class.
45+ """
46+ cs = cls (hosts )
47+ cs .use_basic_auth (username , password )
48+ return cs
49+
50+ @classmethod
51+ def with_api_key_auth (cls , hosts : List [str ], api_key : Optional [Dict ] = None ) -> 'CogStack' :
52+ """
53+ Create an instance of CogStack using API key authentication.
54+
55+ Parameters
56+ ----------
57+ hosts : List[str]
58+ A list of Elasticsearch host URLs.
59+ apiKey : Dict, optional
60+
61+ API key object with "id" and "api_key" or "encoded" strings as fields. Generated in Elasticseach or Kibana
62+ and provided by your CogStack administrator.
63+
64+ If not provided, the user will be prompted to enter API key "encoded" value.
65+
66+ Example:
67+ .. code-block:: json
68+ {
69+ "id": "API_KEY_ID",
70+ "api_key": "API_KEY",
71+ "encoded": "API_KEY_ENCODED_STRING"
72+ }
73+ Returns
74+ -------
75+ CogStack: An instance of the CogStack class.
76+ """
77+ cs = cls (hosts )
78+ cs .use_api_key_auth (api_key )
79+ return cs
80+
2781 def use_basic_auth (self , username : Optional [str ] = None , password :Optional [str ] = None ) -> 'CogStack' :
2882 """
2983 Create an instance of CogStack using basic authentication.
@@ -55,7 +109,7 @@ def use_api_key_auth(self, api_key: Optional[Dict] = None) -> 'CogStack':
55109 ----------
56110 apiKey : Dict, optional
57111
58- API key object with "id" and "api_key" or "encoded" strings as fields. Generated in Elasticseach or Kibana
112+ API key object with "id" and "api_key" or "encoded" strings as fields. Generated in Elasticsearch or Kibana
59113 and provided by your CogStack administrator.
60114
61115 If not provided, the user will be prompted to enter API key "encoded" value.
@@ -82,8 +136,14 @@ def use_api_key_auth(self, api_key: Optional[Dict] = None) -> 'CogStack':
82136 hasEncodedValue = True
83137 elif isinstance (api_key , Dict ):
84138 # If api_key is a dictionary, check for "encoded", "id" and "api_key" keys
85- encoded = api_key ["encoded" ] if "encoded" in api_key .keys () and api_key ["encoded" ] != '' else input ("Encoded API key: " )
86- hasEncodedValue = encoded is not None and encoded != ''
139+ if "id" in api_key .keys () and api_key ["id" ] != '' and "api_key" in api_key .keys () and api_key ["api_key" ] != '' :
140+ # If both "id" and "api_key" are present, use them
141+ encoded = None
142+ hasEncodedValue = False
143+ else :
144+ # If "encoded" is present, use it; otherwise prompt for it
145+ encoded = api_key ["encoded" ] if "encoded" in api_key .keys () and api_key ["encoded" ] != '' else input ("Encoded API key: " )
146+ hasEncodedValue = encoded is not None and encoded != ''
87147
88148 if (not hasEncodedValue ):
89149 api_Id = api_key ["id" ] if "id" in api_key .keys () and api_key ["id" ] != '' else input ("API Id: " )
@@ -110,7 +170,7 @@ def __connect(self, basic_auth : Optional[Tuple[str,str]] = None, api_key: Optio
110170 api_key = api_key ,
111171 basic_auth = basic_auth ,
112172 verify_certs = False ,
113- request_timeout = 300 )
173+ request_timeout = self . ES_TIMEOUT )
114174 if not self .elastic .ping ():
115175 raise Exception ("CogStack connection failed. Please check your host list and credentials and try again." )
116176 print ("CogStack connection established successfully." )
@@ -202,7 +262,7 @@ def read_data_with_scan(self,
202262 query : dict ,
203263 include_fields : list [str ]= None ,
204264 size : int = 1000 ,
205- request_timeout : int = 300 ,
265+ request_timeout : int = ES_TIMEOUT ,
206266 show_progress : bool = True ):
207267 """
208268 Retrieve documents from an Elasticsearch index using search query and elasticsearch scan helper function.
@@ -279,7 +339,7 @@ def read_data_with_scroll(self,
279339 include_fields :Optional [list [str ]]= None ,
280340 size : int = 1000 ,
281341 search_scroll_id : Optional [str ] = None ,
282- request_timeout : Optional [int ]= 300 ,
342+ request_timeout : Optional [int ]= ES_TIMEOUT ,
283343 show_progress : Optional [bool ] = True ):
284344
285345 """
@@ -384,7 +444,7 @@ def read_data_with_sorting(self,
384444 size : Optional [int ]= 1000 ,
385445 sort : Optional [dict | list [str ]] = {"id" : "asc" },
386446 search_after : Optional [list [str | int | float | Any | None ]] = None ,
387- request_timeout : Optional [int ]= 300 ,
447+ request_timeout : Optional [int ]= ES_TIMEOUT ,
388448 show_progress : Optional [bool ] = True ):
389449 """
390450 Retrieve documents from an Elasticsearch index using search query and convert them to a Pandas DataFrame.
0 commit comments