Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for auto refreshing token #72

Merged
merged 6 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions python/Auth0/config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"auth" : "Bearer <your_api_key>",
"auth0_query_url" : "https://your_tenant.auth0.com/api/v2/users?per_page=5&sort=created_at%3A-1&fields=created_at%2Cnickname%2Cemail%2Cupdated_at&q=user_metadata.newsletter%3D%22true%22"

"auth0_query_url" : "https://your_tenant.auth0.com/api/v2/users?per_page=5&sort=created_at%3A-1&fields=created_at%2Cnickname%2Cemail%2Cupdated_at&q=user_metadata.newsletter%3D%22true%22",
"api_url" : "<your api url>",
"request_data" : {"client_id":"<your client id>","client_secret":"<your client secret>","audience":"<your audience>","grant_type":"client_credentials"}

}
24 changes: 20 additions & 4 deletions python/Auth0/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
from scramjet.streams import Stream
import requests
import json

class ShiftArray:
def __init__(self):
Expand Down Expand Up @@ -32,11 +33,21 @@ def lookahead(iterable):
}

async def get_auth(stream):
headers = {'authorization' : run.auth}
token_header = {'content-type': 'application/json',}
last = ""
buffer = ShiftArray()
response = requests.post(run.api_url, headers=token_header, data=run.data)
token = json.loads(response.text)['access_token']

while True:
headers = {'authorization' : f"Bearer {token}"}
users = requests.get(run.query, headers=headers).json()

if "error" in str(users):
response = requests.post(run.api_url, headers=token_header, data=run.data)
token = json.loads(response.text)['access_token']
continue

if users[-1]['email'] != last:
for result, has_more in lookahead(users):
if not buffer.contains(result['email']):
Expand All @@ -49,7 +60,12 @@ async def get_auth(stream):
async def run(context, input):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's add error handling in run (try/catch lock)?

config = context.config
stream = Stream()
run.auth = config["auth"]
run.query = config['auth0_query_url']
asyncio.gather(get_auth(stream), return_exceptions=True)
try:
run.query = config['auth0_query_url']
run.api_url = config['api_url']
run.data = json.dumps(config['request_data'])
asyncio.gather(get_auth(stream), return_exceptions=True)
except Exception as error:
raise Exception(f"Config not loaded: {error}")
return
return stream.map(lambda x : x + '\n')
4 changes: 2 additions & 2 deletions python/Auth0/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@scramjet/stripe-read",
"name": "@scramjet/auth0-read",
"version": "0.1.0",
"main": "main.py",
"author": "gzukowski",
"license": "GPL-3.0",
"description": "Reading from stripe",
"description": "Reading from Auth0",
"keywords": [
"Stripe",
"Mailchimp",
Expand Down
14 changes: 9 additions & 5 deletions python/ChimpInsert/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@ async def insert_info(info):
print("No data received.")

async def run(context, input):
run.audience_id = context.config['audience_id']
mailchimp.set_config({
"api_key": context.config['mailchimp_api'],
"server": context.config['mailchimp_server'],
})
try:
run.audience_id = context.config['audience_id']
mailchimp.set_config({
"api_key": context.config['mailchimp_api'],
"server": context.config['mailchimp_server'],
})
except Exception as error:
raise Exception(f"Config not loaded: {error}")
return
return input.each(insert_info)


Expand Down
6 changes: 5 additions & 1 deletion python/Stripe/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ async def get_event(stream):


async def run(context, input):
stripe.api_key = context.config['stripe_api']
try:
stripe.api_key = context.config['stripe_api']
except Exception as error:
raise Exception(f"Config not loaded: {error}")
return
stream = Stream()
asyncio.gather(get_event(stream), return_exceptions=True)
return stream.map(lambda x : x + "\n")
Expand Down
Loading