def post(self): user = auth_util.get_user(flask.request) backfill = api.payload source = Service.get(backfill['source'], parent=user.key) dest = Service.get(backfill['dest'], parent=user.key) start = datetime.datetime.fromisoformat(backfill.get('start')) end = datetime.datetime.fromisoformat(backfill.get('end')) task_util.xsync_tasks_backfill(source.key, dest.key, start, end) # TODO: pre-check there are credentials. return backfill
def get(self): bot = auth_util.get_bot(flask.request) service = Service.get('strava', parent=bot.key) club_query = ds_util.client.query(kind='Club', ancestor=service.key) return [WrapEntity(club) for club in club_query.fetch()]
def get(self, club_id): bot = auth_util.get_bot(flask.request) service = Service.get('strava', parent=bot.key) club = StravaClubWorker(club_id, service).sync_club() return WrapEntity(club)
def _withings_trainerroad(request, user_key, measure): params = task_util.get_payload(request) user_key = params['user_key'] measure = params['measure'] logging.info('ProcessMeasure: withings_trainerroad: %s %s', user_key, measure) trainerroad_service = Service.get('trainerroad', parent=user_key) if not Service.has_credentials(trainerroad_service, required_key='password'): logging.debug('ProcessMeasure: Trainerroad not connected') return responses.OK if not measure.get('weight'): logging.debug('ProcessMeasure: Skipping non-weight measure.') return responses.OK try: client = trainerroad_create_client(trainerroad_service) with client: client.weight = measure['weight'] except Exception: logging.exception('ProcessMeasure: Failed: %s', measure) return responses.OK_SYNC_EXCEPTION return responses.OK
def post(self, name): bot = auth_util.get_bot(flask.request) service = Service.get(name, parent=bot.key) Service.clear_credentials(service) ds_util.client.put(service) return WrapEntity(service)
def post(self, name): user = auth_util.get_user(flask.request) connect_userpass = api.payload service = Service.get(name, parent=user.key) Service.update_credentials_userpass(service, connect_userpass) ds_util.client.put(service) return WrapEntity(service)
def post(self, name): bot = auth_util.get_bot(flask.request) force = api.payload.get('force', False) service = Service.get(name, parent=bot.key) task_util.sync_service(service, force=force) return WrapEntity(service)
def post(self, name): user = auth_util.get_user(flask.request) new_service = api.payload existing_service = Service.get(name, parent=user.key) existing_service.update(new_service) ds_util.client.put(existing_service) return WrapEntity(existing_service)
def get(self): bot = auth_util.get_bot(flask.request) service = Service.get('slack', parent=bot.key) query = ds_util.client.query(kind='SlackWorkspace', ancestor=service.key) workspaces = [e for e in query.fetch()] return {'service': WrapEntity(service), 'workspaces': workspaces}
def get(self): user = auth_util.get_user(flask.request) service = Service.get('strava', parent=user.key) segments_query = ds_util.client.query( kind='Segment', ancestor=service.key, order=['-id'] ) segments = [WrapEntity(a) for a in segments_query.fetch()] return segments
def get(self, club_id): bot = auth_util.get_bot(flask.request) service = Service.get('strava', parent=bot.key) club = Club.get(club_id, parent=service.key) if club is not None: ds_util.client.delete(club.key) return None
def _oauth(user: Entity, dest: str, redirect_uri: str): """Step 2. Stores credentials.""" service = Service.get(SERVICE_NAME, parent=user.key) state = flask.session['state'] flow = google_auth_oauthlib.flow.Flow.from_client_config( config.gcp_web_creds, scopes=SCOPES, state=state) flow.redirect_uri = redirect_uri authorization_response = flask.request.url logging.debug('auth_response: %s', authorization_response) flow.fetch_token(authorization_response=authorization_response) creds = _credentials_to_dict(flow.credentials) logging.debug('creds: %s', creds) Service.update_credentials(service, creds) task_util.sync_service(Service.get(SERVICE_NAME, parent=user.key)) return flask.redirect(config.devserver_url + dest)
def _create_slack_client_for_team(team_id): slack_service = Service.get('slack', parent=Bot.key()) installation_store = DatastoreInstallationStore(ds_util.client, parent=slack_service.key) slack_bot = installation_store.find_bot( enterprise_id=None, team_id=team_id, is_enterprise_install=False, ) return WebClient(slack_bot.bot_token)
def get(self): user = auth_util.get_user(flask.request) strava = Service.get('strava', parent=user.key) strava_connected = Service.has_credentials(strava) athlete = Athlete.get_private(strava.key) return dict( user=WrapEntity(user), athlete=WrapEntity(athlete), signup_complete=strava_connected, )
def get(self): user = auth_util.get_user(flask.request) service = Service.get('strava', parent=user.key) activities_query = ds_util.client.query( kind='Activity', ancestor=service.key, order=['-start_date'] ) one_year_ago = datetime.datetime.now( datetime.timezone.utc ) - datetime.timedelta(days=365) activities_query.add_filter('start_date', '>', one_year_ago) return [WrapEntity(a) for a in activities_query.fetch(limit=20)]
def get(self): user = auth_util.get_user(flask.request) service = Service.get('strava', parent=user.key) segments_arg = get_arg('segments') segments = [] for segment_id in segments_arg: entity = ds_util.client.get( ds_util.client.key('Segment', segment_id, parent=service.key) ) segments.append(WrapEntity(entity)) return segments
def get(self, club_id): club_id = int(club_id) auth_util.get_user(flask.request) bot_strava = Service.get('strava', parent=Bot.key()) club = Club.get(club_id, parent=bot_strava.key) # Find the user's club reference. if club is None: flask.abort(404) return WrapEntity(club)
def get(self): bot = auth_util.get_bot(flask.request) user_entities = [bot] user_entities += ds_util.client.query(kind='User').fetch() users = [] for user_entity in user_entities: users.append({ 'user': WrapEntity(user_entity), 'google': Service.get('google', parent=user_entity.key), 'strava': Service.get('strava', parent=user_entity.key), 'withings': Service.get('withings', parent=user_entity.key), 'fitbit': Service.get('fitbit', parent=user_entity.key), }) return users
def _create_slack_client(event): slack_service = Service.get('slack', parent=Bot.key()) installation_store = DatastoreInstallationStore(ds_util.client, parent=slack_service.key) slack_bot = installation_store.find_bot( enterprise_id=event.get('authorizations', [{}])[0].get('enterprise_id'), team_id=event.get('authorizations', [{}])[0].get('team_id'), is_enterprise_install=event.get('authorizations', [{}])[0].get('is_enterprise_install'), ) return WebClient(slack_bot.bot_token)
def _create_unfurls(event): strava = Service.get('strava', parent=Bot.key()) strava_client = ClientWrapper(strava) unfurls = {} for link in event['event']['links']: alt_url = _resolve_rewrite_link(link) unfurl = _unfurl(strava_client, link, alt_url) if unfurl: unfurls[link['url']] = unfurl logging.warning(f'_create_unfurls: {unfurls}') return unfurls
def _oauth(request: flask.Request, user: Entity, dest: str): code = request.args.get('code') service = Service.get(SERVICE_NAME, parent=user.key) client = stravalib.client.Client() creds = client.exchange_code_for_token( client_id=config.strava_creds['client_id'], client_secret=config.strava_creds['client_secret'], code=code, ) Service.update_credentials(service, dict(creds)) task_util.sync_service(service) return flask.redirect(config.devserver_url + dest)
def redirect(user): service = Service.get(SERVICE_NAME, parent=user.key) code = flask.request.args.get('code') dest = flask.request.args.get('dest', '') auth = create_auth(callback_uri=get_callback_uri(dest)) creds_dict = create_creds_dict(auth.get_credentials(code)) Service.update_credentials(service, creds_dict) task_util.sync_service(service) return flask.redirect(config.devserver_url + dest)
def get(self, club_id): club_id = int(club_id) auth_util.get_user(flask.request) bot_strava = Service.get('strava', parent=Bot.key()) club = Club.get(club_id, parent=bot_strava.key) # Find the user's club reference. if club is None: flask.abort(404) activities_query = ds_util.client.query(kind='Activity', ancestor=club.key) all_activities = [a for a in activities_query.fetch()] return [WrapEntity(a) for a in all_activities]
def tasks_rides(): payload = task_util.get_payload(flask.request) service = Service.get('google', Bot.key()) data = payload['data'] logging.info('tasks_rides: %s', data.get('historyId')) try: client = create_gmail_client(service) sync_helper.do( PubsubWorker(service, data, client), work_key='%s/%s' % (service.key.parent.name, data['historyId']), ) except SyncException: return responses.OK_SYNC_EXCEPTION return responses.OK
def redirect(user): service = Service.get(SERVICE_NAME, parent=user.key) code = flask.request.args.get('code') dest = flask.request.args.get('dest', '') auth_client = fitbit.FitbitOauth2Client( client_id=config.fitbit_creds['client_id'], client_secret=config.fitbit_creds['client_secret'], redirect_uri=get_redirect_uri(dest), ) creds = auth_client.fetch_access_token(code) Service.update_credentials(service, creds) task_util.sync_service(service) return flask.redirect(config.devserver_url + dest)
def _withings_garmin(request, user_key, measure): params = task_util.get_payload(request) user_key = params['user_key'] measure = params['measure'] logging.info('ProcessMeasure: withings_garmin: %s %s', user_key, measure) garmin_service = Service.get('garmin', parent=user_key) if not Service.has_credentials(garmin_service, required_key='password'): logging.debug('ProcessMeasure: Garmin not connected') return responses.OK if not measure.get('weight'): logging.debug('ProcessMeasure: Skipping non-weight measure.') return responses.OK try: client = garmin_client.create(garmin_service) client.set_weight(measure['weight'], measure['date']) except Exception: logging.exception('ProcessMeasure: Failed: %s', measure) return responses.OK_SYNC_EXCEPTION return responses.OK
def get(self, name): user = auth_util.get_user(flask.request) service = Service.get(name, parent=user.key) return WrapEntity(service)
def sync_club(club_id): logging.debug('Syncing: %s', club_id) service = Service.get('strava', parent=Bot.key()) sync_helper.do(ClubWorker(club_id, service), work_key=service.key) return responses.OK
def init(user): # Creates the service if it doesn't exist. Service.get(SERVICE_NAME, parent=user.key) dest = flask.request.args.get('dest', '') return get_auth_url_response(dest)
def get_service(user_id, name): user = User.get({'sub': user_id}) return Service.get(name, parent=user.key)