-
Notifications
You must be signed in to change notification settings - Fork 0
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
Slack addon, three different lead states #79
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a660aae
Slack addon, three different lead states
gzukowski 3402c52
Added missing shiftarray module
gzukowski 1eaf134
oop stripe seq
gzukowski 7f2f569
Added suggested changes
gzukowski 40b54db
Added suggested changes
gzukowski 9320fa9
Merge branch 'feat/slack-addon' of github.com:scramjetorg/platform-sa…
gzukowski ae6f412
Corrected logger and deleted debug leftover
gzukowski 1b8ac25
Merge branch 'main' into feat/slack-addon
gzukowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,91 @@ | ||
import asyncio | ||
from scramjet.streams import Stream | ||
import requests | ||
import requests_async | ||
import json | ||
from shiftarray import ShiftArray | ||
|
||
class ShiftArray: | ||
def __init__(self): | ||
self.array = [] | ||
provides = {"provides": "pipe", "contentType": "text/plain"} | ||
|
||
def append(self, value): | ||
if len(self.array) == 5: | ||
self.array.pop(-1) | ||
self.array.append(value) | ||
WAIT_TIME_ON_USER = 5 | ||
WAIT_TIME_ERROR = 3 | ||
|
||
def contains(self, value): | ||
return value in self.array | ||
class Auth0: | ||
def __init__(self, verified_url, users_url, api_url, request_data, stream): | ||
self.verified_url = verified_url | ||
self.users_url = users_url | ||
self.api_url = api_url | ||
self.request_data = request_data | ||
self.token_header = { | ||
"content-type": "application/json", | ||
} | ||
self.stream = stream | ||
|
||
def get_values(self): | ||
return self.array | ||
def lookahead(self, iterable): | ||
it = iter(iterable) | ||
last = next(it) | ||
for val in it: | ||
yield last, True | ||
last = val | ||
yield last, False | ||
|
||
async def get_auth(self): | ||
last_verified = "" | ||
last_user = "" | ||
buffer_verified = ShiftArray() | ||
buffer_users = ShiftArray() | ||
response = requests.post( | ||
self.api_url, headers=self.token_header, data=self.request_data | ||
) | ||
token = json.loads(response.text)["access_token"] | ||
while True: | ||
headers = {"authorization": f"Bearer {token}"} | ||
verified = await requests_async.get(self.verified_url, headers=headers) | ||
users = await requests_async.get(self.users_url, headers=headers) | ||
if verified.status_code != 200: | ||
await asyncio.sleep(WAIT_TIME_ERROR) | ||
response = requests.post( | ||
self.api_url, headers=self.token_header, data=self.request_data | ||
) | ||
token = json.loads(response.text)["access_token"] | ||
continue | ||
verified = verified.json() | ||
users = users.json() | ||
if verified[-1]["email"] != last_verified: | ||
for result, has_more in self.lookahead(verified): | ||
if not buffer_verified.contains(result["email"]): | ||
self.stream.write( | ||
result["email"] + " " + result["nickname"] + " auth0-newsletter" | ||
) | ||
buffer_verified.append(result["email"]) | ||
if not has_more: | ||
last_verified = result["email"] | ||
|
||
def lookahead(iterable): | ||
it = iter(iterable) | ||
last = next(it) | ||
for val in it: | ||
yield last, True | ||
last = val | ||
yield last, False | ||
if users[-1]["email"] != last_user: | ||
for result, has_more in self.lookahead(users): | ||
if not buffer_users.contains(result["email"]): | ||
self.stream.write( | ||
result["email"] + " " + result["nickname"] + " auth0-user" | ||
) | ||
buffer_users.append(result["email"]) | ||
if not has_more: | ||
last_user = result["email"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be better to keep the update time instead of email - users may change and we could just push the changed users as well, but email will do for now. |
||
await asyncio.sleep(WAIT_TIME_ON_USER) | ||
|
||
provides = { | ||
'provides': 'pipe', | ||
'contentType': 'text/plain' | ||
} | ||
|
||
async def get_auth(stream): | ||
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']): | ||
stream.write(result['email'] +" "+result['nickname']+" auth0-user") | ||
buffer.append(result['email']) | ||
if not has_more: | ||
last = result['email'] | ||
await asyncio.sleep(5) | ||
|
||
async def run(context, input): | ||
config = context.config | ||
stream = Stream() | ||
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') | ||
config = context.config | ||
stream = Stream() | ||
try: | ||
run.verified_url = config["auth0_verified_url"] | ||
run.users_url = config["auth0_users_url"] | ||
run.api_url = config["api_url"] | ||
run.data = json.dumps(config["request_data"]) | ||
except Exception as error: | ||
raise Exception(f"Config not loaded: {error}") | ||
|
||
asyncio.gather( | ||
Auth0(run.verified_url,run.users_url ,run.api_url, run.data, stream).get_auth(), | ||
return_exceptions=True, | ||
) | ||
return stream.map(lambda x: x + "\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
scramjet-framework-py | ||
requests | ||
requests_async |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class ShiftArray: | ||
def __init__(self, length=5): | ||
self.array = [] | ||
self.length = length | ||
|
||
def append(self, value): | ||
if len(self.array) == self.length: | ||
self.array.pop(-1) | ||
self.array.append(value) | ||
|
||
def contains(self, value): | ||
return value in self.array | ||
|
||
def get_values(self): | ||
return self.array | ||
|
||
def __str__(self): | ||
return str(self.array) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
{ | ||
"mailchimp_api" : "<your_api_key>", | ||
"audience_id" : "<your_audience>", | ||
"mailchimp_server" : "<your_server>" | ||
"slack_api_url" : "", | ||
"slack_channel_id" : "", | ||
"mailchimp_api" : "", | ||
"audience_id" : "", | ||
"mailchimp_server" : "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a
asyncio.sleep(WAIT_TIME_ERROR)
here - hold back on error (also define a constant near the top of the file).