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 sync_task(): services_query = ds_util.client.query(kind='Service') services_query.add_filter('sync_enabled', '=', True) services_query.add_filter('syncing', '=', False) services = [ service for service in services_query.fetch() if Service.has_credentials(service) ] task_util.sync_services(services) return responses.OK
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 sync(): logging.debug('Syncing: google') payload = task_util.get_payload(flask.request) service = ds_util.client.get(payload['service_key']) if not Service.has_credentials(service): Service.set_sync_finished(service, error='No credentials') return responses.OK_NO_CREDENTIALS try: Service.set_sync_started(service) client = create_gmail_client(service) sync_helper.do(Worker(service, client), work_key=service.key) Service.set_sync_finished(service) return responses.OK except SyncException as e: Service.set_sync_finished(service, error=str(e)) return responses.OK_SYNC_EXCEPTION
def create_client(service): if not Service.has_credentials(service): raise Exception('Cannot create Withings client without creds: %s' % (service, )) creds_dict = service['credentials'] creds = create_creds2(creds_dict) def refresh_callback(new_credentials): """Updates credentials. Params: new_credentials: withings_api.Credentials2 """ logging.debug('Withings creds refresh for: %s', service.key) creds_dict = create_creds_dict(new_credentials) Service.update_credentials(service, creds_dict) return withings_api.WithingsApi(creds, refresh_cb=refresh_callback)
def sync(): logging.debug('Syncing: trainerroad') params = task_util.get_payload(flask.request) service = ds_util.client.get(params['service_key']) if not Service.has_credentials(service, required_key='password'): logging.warning('No creds: %s', service.key) Service.set_sync_finished(service, error='No credentials') return responses.OK_NO_CREDENTIALS try: Service.set_sync_started(service) sync_helper.do(Worker(service), work_key=service.key) Service.set_sync_finished(service) return responses.OK except SyncException as e: Service.set_sync_finished(service, error=str(e)) return responses.OK_SYNC_EXCEPTION
def create_client(service): if not Service.has_credentials(service): raise Exception('Cannot create Fitbit client without creds: %s', service) def refresh_callback(new_credentials): logging.debug('Fitbit creds refresh for: %s', service.key) Service.update_credentials(service, new_credentials) return fitbit.Fitbit( config.fitbit_creds['client_id'], config.fitbit_creds['client_secret'], access_token=service['credentials']['access_token'], refresh_token=service['credentials']['refresh_token'], expires_at=service['credentials']['expires_at'], redirect_uri=get_redirect_uri('frontend'), refresh_cb=refresh_callback, system=fitbit.Fitbit.METRIC, )
def create(service): if not Service.has_credentials(service, required_key='password'): raise Exception('Cannot create Garmin client without creds: %s' % (service, )) creds = service.get('credentials', {}) session_state = creds.get('session_state', {}) def refresh_callback(session_state): logging.debug('Garmin creds refresh for: %s', service.key) Service.update_credentials(service, {'session_state': session_state}) garmin = Garmin( creds['username'], Service.get_credentials_password(creds), refresh_callback=refresh_callback, ) try: garmin.set_session_state(**session_state) except ValueError: logging.exception('Invalid session_state, ignoring') del creds['session_state'] return garmin
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 process_event_task(): params = task_util.get_payload(flask.request) event = params['event'] logging.info('Event: %s', event.key) # First try to get the service using the event.key's service. # If this event is coming from an old subscription / secret url, which # embeds a service_key in it, then we might get these. service_key = event.key.parent service = ds_util.client.get(service_key) if service is None: logging.error('Event: No service: %s', event.key) return responses.OK_NO_SERVICE if not Service.has_credentials(service): logging.warning('Event: No credentials: %s', event.key) return responses.OK_NO_CREDENTIALS try: sync_helper.do(EventsWorker(service, event), work_key=event.key) except SyncException: return responses.OK_SYNC_EXCEPTION return responses.OK
def create_gmail_client(service): if not Service.has_credentials(service): raise Exception('Cannot create gmail client without creds: %s' % (service, )) creds = Credentials(**service['credentials']) return build('gmail', 'v1', cache_discovery=False, credentials=creds)
def _check_creds(self, service): return (service.key.name == 'garmin' and Service.has_credentials( service, required_key='password')) or (Service.has_credentials(service))