+if 'GSHEETS_PICKLE_BASE64' in os.environ:
+ gsheets_pick_base64 = os.environ['GSHEETS_PICKLE_BASE64']
+else:
+ response = ssm.get_parameter(Name='GSHEETS_PICKLE_BASE64',
+ WithDecryption=True)
+ gsheets_pickle_base64 = response['Parameter']['Value']
+ os.environ['GSHEETS_PICKLE_BASE64'] = gsheets_pickle_base64
+gsheets_pickle = base64.b64decode(gsheets_pickle_base64)
+gsheets_creds = pickle.loads(gsheets_pickle)
+
+if gsheets_creds:
+ if gsheets_creds.valid:
+ print("Loaded valid GSheets credentials from SSM")
+ else:
+ gsheets_creds.refresh(Request())
+ gsheets_pickle = pickle.dumps(gsheets_creds)
+ gsheets_pickle_base64_bytes = base64.b64encode(gsheets_pickle)
+ gsheets_pickle_base64 = gsheets_pickle_base64_bytes.decode('us-ascii')
+ print("Storing refreshed GSheets credentials into SSM")
+ os.environ['GSHEETS_PICKLE_BASE64'] = gsheets_pickle_base64
+ ssm.put_parameter(Name='GSHEETS_PICKLE_BASE64',
+ Type='SecureString',
+ Value=gsheets_pickle_base64,
+ Overwrite=True)
+service = build('sheets',
+ 'v4',
+ credentials=gsheets_creds,
+ cache_discovery=False)
+sheets = service.spreadsheets()
+service = build('drive',
+ 'v3',
+ credentials=gsheets_creds,
+ cache_discovery=False)
+files = service.files()
+permissions = service.permissions()
+
+db = boto3.resource('dynamodb')
+
+turb = SimpleNamespace()
+turb.slack_client = slack_client
+turb.db = db
+turb.table = db.Table("turbot")
+turb.sheets = sheets
+turb.files = files
+turb.permissions = permissions
+