Validates the API key and retrieves the valid sessions for the given API key. :param api_key: The API key to validate and retrieve sessions for. :return: The project with all valid sessions inner joined :raises RuntimeError: If there was an error retrieving the proj
(self, api_key, parent_key=None)
| 116 | return response |
| 117 | |
| 118 | async def get_session_ids(self, api_key, parent_key=None): |
| 119 | """ |
| 120 | Validates the API key and retrieves the valid sessions for the given API key. |
| 121 | |
| 122 | :param api_key: The API key to validate and retrieve sessions for. |
| 123 | :return: The project with all valid sessions inner joined |
| 124 | :raises RuntimeError: If there was an error retrieving the project id with the API key or if no valid session was found. |
| 125 | """ |
| 126 | api_keys = [api_key] |
| 127 | if parent_key: |
| 128 | api_keys.append(parent_key) |
| 129 | |
| 130 | logging.info(f'Validating api_keys: {",".join(api_keys)}') |
| 131 | |
| 132 | url = f'{self.supabase_url}/rest/v1/projects?api_key=in.({",".join(api_keys)})&select=id' |
| 133 | headers = { |
| 134 | "apiKey": self.supabase_key, |
| 135 | "Authorization": f"Bearer {self.supabase_key}", |
| 136 | } |
| 137 | |
| 138 | async with httpx.AsyncClient() as async_client: |
| 139 | response = await async_client.get(url, headers=headers) |
| 140 | |
| 141 | if not response.is_success: |
| 142 | raise RuntimeError(f"Error retrieving project id with API key {api_key}") |
| 143 | |
| 144 | json = response.json() |
| 145 | |
| 146 | project_ids = [project["id"] for project in json] |
| 147 | |
| 148 | url = f'{self.supabase_url}/rest/v1/sessions?or=(project_id.in.({",".join(project_ids)}),project_id_secondary.in.({",".join(project_ids)}))&select=id' |
| 149 | headers = { |
| 150 | "apiKey": self.supabase_key, |
| 151 | "Authorization": f"Bearer {self.supabase_key}", |
| 152 | } |
| 153 | |
| 154 | async with httpx.AsyncClient() as async_client: |
| 155 | response = await async_client.get(url, headers=headers) |
| 156 | |
| 157 | if not response.is_success: |
| 158 | raise RuntimeError(f"Error retrieving project id with API key {api_key}") |
| 159 | |
| 160 | json = response.json() |
| 161 | |
| 162 | if len(json) == 0: |
| 163 | raise RuntimeError("No valid session found") |
| 164 | |
| 165 | return json |