| 362 | |
| 363 | |
| 364 | class Google: |
| 365 | def __init__(self, client_config=None): |
| 366 | # Google cloud sql api versions |
| 367 | self._cloud_resource_manager_api_version = 'v1' |
| 368 | self._sqladmin_api_version = 'v1' |
| 369 | self._compute_api_version = 'v1' |
| 370 | |
| 371 | # Scope required for google cloud sql deployment |
| 372 | self._scopes = ['https://www.googleapis.com/auth/cloud-platform', |
| 373 | 'https://www.googleapis.com/auth/sqlservice.admin'] |
| 374 | |
| 375 | # Instance classed |
| 376 | self._instance_classes = [{'label': 'Standard', 'value': 'standard'}, |
| 377 | {'label': 'High Memory', 'value': 'highmem'}, |
| 378 | {'label': 'Shared', 'value': 'shared'}] |
| 379 | |
| 380 | self._client_config = client_config |
| 381 | self._credentials = None |
| 382 | self.credentials_json = None |
| 383 | self._project_id = None |
| 384 | self._regions = [] |
| 385 | self._availability_zones = {} |
| 386 | self._verification_successful = False |
| 387 | self._verification_error = None |
| 388 | self._redirect_url = None |
| 389 | |
| 390 | def to_state(self): |
| 391 | """Serialize the persistable state of this Google instance to a |
| 392 | plain dict for storage in `flask.session`. |
| 393 | |
| 394 | Live SDK objects (like `_credentials`) are intentionally NOT |
| 395 | included — they are rebuilt from `credentials_json` in |
| 396 | `from_state()`. Storing only data primitives lets the session |
| 397 | layer use a safe (non-binary-serializer) format. |
| 398 | """ |
| 399 | return { |
| 400 | 'client_config': self._client_config, |
| 401 | 'credentials_json': self.credentials_json, |
| 402 | 'redirect_url': self._redirect_url, |
| 403 | 'project_id': self._project_id, |
| 404 | 'regions': self._regions, |
| 405 | 'availability_zones': self._availability_zones, |
| 406 | 'verification_successful': self._verification_successful, |
| 407 | 'verification_error': self._verification_error, |
| 408 | } |
| 409 | |
| 410 | @classmethod |
| 411 | def from_state(cls, state): |
| 412 | """Rebuild a Google instance from a previously-serialized dict. |
| 413 | |
| 414 | The reverse of `to_state()`. The Google SDK credentials object is |
| 415 | reconstructed from the stored token dict so subsequent API calls |
| 416 | work without forcing the user back through the OAuth2 flow. |
| 417 | """ |
| 418 | if not isinstance(state, dict): |
| 419 | return None |
| 420 | obj = cls(client_config=state.get('client_config')) |
| 421 | obj.credentials_json = state.get('credentials_json') |
no outgoing calls