|
23 | 23 |
|
24 | 24 |
|
25 | 25 | class BrainAPI: |
26 | | - """Class for BrainAPI session.""" |
| 26 | + """Class for communicating with BrainAPI session.""" |
| 27 | + |
| 28 | + _instance = None |
27 | 29 |
|
28 | 30 | def __init__(self): |
29 | 31 | self.s = requests.Session() |
30 | 32 | self._authenticate() |
31 | 33 |
|
| 34 | + def __new__(cls): |
| 35 | + # Singleton pattern to ensure only one instance of BrainAPI exists. |
| 36 | + if cls._instance is None: |
| 37 | + cls._instance = super(BrainAPI, cls).__new__(cls) |
| 38 | + return cls._instance |
| 39 | + |
32 | 40 | def get_simulation_result_json(self, alpha_id): |
33 | 41 | """Get result of simulation as JSON dictionary.""" |
34 | 42 | return self._request("GET", "/alphas/" + alpha_id).json() |
35 | 43 |
|
36 | 44 | def start_simulation(self, simulate_data): |
37 | 45 | """Start simulation of provided alpha.""" |
38 | | - return self._request("POST", "/simulations", json=simulate_data) |
| 46 | + self.check_session_timeout() |
| 47 | + return self.s.post(f"{BRAIN_API_URL}/simulations", json=simulate_data) |
39 | 48 |
|
40 | 49 | def check_session_timeout(self): |
41 | 50 | """Check session time out and refresh session if necessary.""" |
@@ -334,6 +343,7 @@ def get_specified_alpha_stats( |
334 | 343 | .drop_duplicates(subset=["test"], keep="last") |
335 | 344 | .reset_index(drop=True) |
336 | 345 | ) |
| 346 | + |
337 | 347 | if check_prod_corr and not check_submission: |
338 | 348 | prod_corr_test = self.check_prod_corr_test(alpha_id) |
339 | 349 | is_tests = ( |
@@ -527,7 +537,7 @@ def get_datafields( |
527 | 537 | datafields_df = pd.DataFrame(datafields_list_flat) |
528 | 538 | return datafields_df |
529 | 539 |
|
530 | | - def _request(self, method: str, endpoint: str, max_retries: int = 20, **kwargs): |
| 540 | + def _request(self, method: str, endpoint: str, max_retries: int = 120, **kwargs): |
531 | 541 | """Generic request method with retry logic.""" |
532 | 542 | while max_retries > 0: |
533 | 543 | response = self.s.request(method, BRAIN_API_URL + endpoint, **kwargs) |
|
0 commit comments