def addwebhooksub(): """ Adds a new Strava webhook subscription to the database and Strava API. Kicks off callback verification process. Called by Strava Activity admin page inputs. """ # Get POST request info # athID = int(request.form['athID']) # callbackurl = str(request.form['callbackURL']) # Generate 14 character verify token string verifytoken = secrets.token_hex(7) # Insert token into database, will be updated if subID if successful, otherwise row will be deleted DBQueriesStrava.insertVerifyToken(verifytoken) application.logger.debug( f"New verification token {verifytoken} has been added to database") # Get Strava API access credentials client = OAuthStrava.getAuth() try: # Send request to create webhook subscription, will be given the new subscription ID in response application.logger.debug( f"Callback url is {os.getenv('STRAVA_CALLBACK_URL')}") # postDat = {"client_id": os.getenv("STRAVA_CLIENT_ID"), # "client_secret": os.getenv("STRAVA_CLIENT_SECRET"), # "callback_url": os.getenv('FULL_STRAVA_CALLBACK_URL'), # "verify_token": verifytoken} # # r = requests.post("https://www.strava.com/api/v3/push_subscriptions", data=postDat) # resp = r.json() resp = client.create_subscription( client_id=os.getenv("STRAVA_CLIENT_ID"), client_secret=os.getenv("STRAVA_CLIENT_SECRET"), # callback_url=os.getenv('FULL_STRAVA_CALLBACK_URL'), callback_url=os.getenv('STRAVA_CALLBACK_URL'), verify_token=verifytoken) application.logger.debug(resp) # application.logger.debug(f"New sub id is {resp['id']}, updating database") application.logger.debug(f"New sub id is {resp.id}, updating database") # Update database with new sub id # DBQueriesStrava.updateSubId(resp["id"], verifytoken) DBQueriesStrava.updateSubId(resp.id, verifytoken) # application.logger.debug(f"New sub id {resp['id']} has been added to the database") application.logger.debug( f"New sub id {resp.id} has been added to the database") return Response(status=200) except Exception as e: application.logger.debug( f"Webhook creation process failed with the error {e}") # logging.error(e, exc_info=True) DBQueriesStrava.deleteVerifyTokenRecord(verifytoken) return Response(status=400, response=str(e))
def createStravaWebhook(client): """ Creates new Strava webhook subscription. Client information and client generated token are pulled from environmental variables and the callback URL is set to a dedicated callback address on the application. If subscription is successful, a subscription ID will be provided by Strava and this ID will be inserted into Postgres. Returns ------- Integer. Strava subscription id """ try: # Kick off process to create new webhook #### The following may not be true, HTTPS may work, server was experiencing other problems that may have #### interferred in the process, need to test. # callback URL needs to be a HTTP url, not HTTPS, so the elastic beanstalk base environment URL is provided # as all calls to leavittmapping.com are redirected to HTTPS, consider making HTTP only mapping to domain. application.logger.debug( f"Attempting to create a new Strava webhook subscription with the values: client id: " f"{os.getenv('STRAVA_CLIENT_ID')} client_secret: {os.getenv('STRAVA_CLIENT_SECRET')}" f"callback url: {(os.getenv('httpSiteIndex') + os.getenv('strava_callback_url'))} " f"and the verify token: {os.getenv('STRAVA_VERIFY_TOKEN')}") response = client.create_subscription( client_id=os.getenv("STRAVA_CLIENT_ID"), client_secret=os.getenv("STRAVA_CLIENT_SECRET"), callback_url=(os.getenv('httpSiteIndex') + os.getenv('strava_callback_url')), verify_token=os.getenv("STRAVA_VERIFY_TOKEN")) application.logger.debug(f"Response id is {response.id}") # Update database with sub id DBQueriesStrava.updateSubId(response.id) return response.id except Exception as e: # Something broke, log error application.logger.error( f"Create subscription function failed with the error {e}")