From a0c12e708f4527bb6283fd4386c2b7f6a2dbaa4b Mon Sep 17 00:00:00 2001 From: gzukowski Date: Wed, 14 Jun 2023 14:02:31 +0000 Subject: [PATCH 1/4] Added support for auto refreshing token --- python/Auth0/config.json | 7 +++++-- python/Auth0/main.py | 16 ++++++++++++++-- python/Auth0/package.json | 4 ++-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/python/Auth0/config.json b/python/Auth0/config.json index 1bddd69..4b4d928 100644 --- a/python/Auth0/config.json +++ b/python/Auth0/config.json @@ -1,4 +1,7 @@ { - "auth" : "Bearer ", - "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" : "", + "request_data" : {"client_id":"","client_secret":"","audience":"","grant_type":"client_credentials"} + } diff --git a/python/Auth0/main.py b/python/Auth0/main.py index fbb7e8e..887f707 100644 --- a/python/Auth0/main.py +++ b/python/Auth0/main.py @@ -1,6 +1,7 @@ import asyncio from scramjet.streams import Stream import requests +import json class ShiftArray: def __init__(self): @@ -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']): @@ -49,7 +60,8 @@ async def get_auth(stream): async def run(context, input): config = context.config stream = Stream() - run.auth = config["auth"] 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) return stream.map(lambda x : x + '\n') \ No newline at end of file diff --git a/python/Auth0/package.json b/python/Auth0/package.json index 2014176..8f6f6b1 100644 --- a/python/Auth0/package.json +++ b/python/Auth0/package.json @@ -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", From 3920a7c525c556bf421ffb2f42de2c7cb1900c59 Mon Sep 17 00:00:00 2001 From: gzukowski Date: Wed, 14 Jun 2023 14:07:56 +0000 Subject: [PATCH 2/4] Added missing repo url --- python/Auth0/package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/Auth0/package.json b/python/Auth0/package.json index 8f6f6b1..3312173 100644 --- a/python/Auth0/package.json +++ b/python/Auth0/package.json @@ -1,10 +1,10 @@ { - "name": "@scramjet/auth0-read", + "name": "@scramjet/stripe-read", "version": "0.1.0", "main": "main.py", "author": "gzukowski", "license": "GPL-3.0", - "description": "Reading from auth0", + "description": "Reading from stripe", "keywords": [ "Stripe", "Mailchimp", @@ -12,7 +12,7 @@ ], "repository": { "type": "git", - "url": "" + "url": "https://github.com/scramjetorg/platform-samples/tree/main/python/Auth0" }, "engines": { "python3": "3.8.0" From a165f482443e0a24e693aa972ccfb08889d2a050 Mon Sep 17 00:00:00 2001 From: gzukowski <85632612+gzukowski@users.noreply.github.com> Date: Wed, 14 Jun 2023 16:11:08 +0200 Subject: [PATCH 3/4] Added missing url --- python/Auth0/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/Auth0/package.json b/python/Auth0/package.json index 15ccc0a..4c80971 100644 --- a/python/Auth0/package.json +++ b/python/Auth0/package.json @@ -4,7 +4,7 @@ "main": "main.py", "author": "gzukowski", "license": "GPL-3.0", - "description": "Reading from auth0", + "description": "Reading from Auth0", "keywords": [ "Stripe", "Mailchimp", From 25bf3af28e26310be7c68f3e9cd4101ec1b4c98a Mon Sep 17 00:00:00 2001 From: gzukowski Date: Mon, 19 Jun 2023 13:31:06 +0000 Subject: [PATCH 4/4] Specifying the place of exception Specifying the place of exception --- python/Auth0/main.py | 2 +- python/Auth0/package.json | 4 ++-- python/ChimpInsert/main.py | 14 +++++++++----- python/Stripe/main.py | 6 +++++- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/python/Auth0/main.py b/python/Auth0/main.py index 8fcc390..a0366e2 100644 --- a/python/Auth0/main.py +++ b/python/Auth0/main.py @@ -66,6 +66,6 @@ async def run(context, input): run.data = json.dumps(config['request_data']) asyncio.gather(get_auth(stream), return_exceptions=True) except Exception as error: - print(error) + raise Exception(f"Config not loaded: {error}") return return stream.map(lambda x : x + '\n') \ No newline at end of file diff --git a/python/Auth0/package.json b/python/Auth0/package.json index 3312173..4c80971 100644 --- a/python/Auth0/package.json +++ b/python/Auth0/package.json @@ -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", diff --git a/python/ChimpInsert/main.py b/python/ChimpInsert/main.py index 3cf35e0..1bc538c 100644 --- a/python/ChimpInsert/main.py +++ b/python/ChimpInsert/main.py @@ -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) diff --git a/python/Stripe/main.py b/python/Stripe/main.py index 7281f0d..d26c27c 100644 --- a/python/Stripe/main.py +++ b/python/Stripe/main.py @@ -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")