* feat(api): dynamic-api 1/2
* feat(api): dynamic-api 2/2
feat(api): core-api 1/2
* feat(api): changed schemas
feat(api): aipkey authorizer
feat(api): jwt authorizer payload
feat(api): core-api 2/3
* feat(api): apikey authorizer
feat(api): shared context
feat(api): response editor
feat(api): middleware
feat(api): custom router
feat(api): fix auth double call
* feat(api): dashboard
feat(api): insights
feat(api): public api v1
* feat(api): allow full CORS
* feat(api): use decouple-config instead of env
feat(api): fixed conflict slack endpoint
feat(api): fixed favorite errors param
* feat(api): migration fixes
* feat(api): changes
* feat(api): crons
* feat(api): changes and fixes
* feat(api): added new endpoints
feat(api): applied new changes
feat(api): Docker image
* feat(api): EE 1/4
* feat(api): EE core_dynamic
* feat(api): global routers generator
* feat(api): project authorizer
feat(api): docker image
feat(api): crons
* feat(api): EE trace activity
* feat(api): changed ORRouter
* feat(api): EE trace activity parameters&payload
* feat(api): EE trace activity action name & path_format
* feat(db): user trace
* feat(api): EE trace activity ignore routes and hide attribute
feat(api): fix funnel payload schema
* feat(api): mobile support
* feat(api): changed build script
* feat(api): changed mobile sign endpoint
feat(api): changed requirements.txt
* feat(api): changed dockerfile
* feat(api): changed mobile-env-var
* feat(api): removed insights
* feat(api): changed EE Dockerfile
* feat(api): cast session_id to str for signing
* feat(api): fixed error_id type
* feat(api): fixed /errors priority conflict
* feat(api): fixed /errors/{errorId} default params
* feat(api): fixed change password after invitation
* feat(api): use background task for emails instead of low-timeout-api
feat(api): EE fixed missing required params
* feat(api): funnel-insights payload change
* feat(api): funnel-insights payload change
* feat(api): changed edit user payload schema
* feat(api): changed metrics payload schema
* feat(api): changed metrics payload schema
* feat(api): changed edit user default values
feat(api): fixed change error status route
* feat(api): changed edit user
* feat(api): stop user from changing his own role
* feat(api): changed add slack
* feat(api): changed get funnel
* feat(api): changed get funnel on the fly payload
feat(api): changed update payload
* feat(api): changed get funnel on the fly payload
* feat(api): changed update funnel payload
* feat(api): changed get funnel-sessions/issues on the fly payload
* feat(api): fixed funnel missing rangeValue
* feat(api): fixes
* feat(api): iceServers configuration
* feat(api): fix issueId casting
* feat(api): changed issues-sessions endpoint payload-schema
* feat(api): EE changed traces-ignored-routes
* feat(api): EE include core sessions.py
* feat(api): EE check licence on every request if expired
* feat(api): move general stats to dynamic
* feat(api): code cleanup
feat(api): removed sentry
* feat(api): changed traces-ignore-routes
* feat(api): changed dependencies
* feat(api): changed jwt-auth-response code
* feat(api): changed traces-ignore-routes
* feat(api): changed traces-ignore-routes
* feat(api): removed PyTZ
feat(api): migrated time-helper to zoneinfo
* feat(api): EE added missing dependency
feat(api): changed base docker image
* feat(api): merge after roles
* feat(api): EE roles fastapi
* feat(db): handel HTTPExceptions
* feat(db): changed payload schema
* feat(db): changed payload schema
* feat(api): included insights
* feat(api): removed unused helper
* feat(api): merge from dev to fatsapi
* feat(api): merge fixes
feat(api): SAML migration
* feat(api): changed GET /signup response
feat(api): changed EE Dockerfile
* feat(api): changed edition detection
* feat(api): include ee endpoints
* feat(api): add/edit member changes
* feat(api): saml changed redirect
* feat(api): track session's replay
feat(api): track error's details
* feat(api): ignore tracking for read roles
* feat(api): define global queue
feat(api): define global scheduler
feat(api): traces use queue
feat(api): traces batch insert
feat(DB): changed traces schema
* feat(api): fix signup captcha
* feat(api): fix signup captcha
* feat(api): optional roleId
feat(api): set roleId to member if None
* feat(api): fixed edit role
* feat(api): return role details when creating a new member
* feat(api): trace: use BackgroundTasks instead of BackgroundTask to not override previous tasks
* feat(api): trace: use BackgroundTask if no other background task is defined
* feat(api): optimised delete metadata
* feat(api): Notification optional message
* feat(api): fix background-task reference
* feat(api): fix trace-background-task
* feat(api): fixed g-captcha for reset password
* feat(api): fix edit self-user
* feat(api): fixed create github-issue
* feat(api): set misfire_grace_time for crons
* feat(api): removed chalice
feat(api): freeze dependencies
* feat(api): refactored blueprints
* feat(api): /metadata/session_search allow projectId=None
* feat(api): public API, changed userId type
* feat(api): fix upload sourcemaps
* feat(api): user-trace support ApiKey endpoints
* feat(api): fixed user-trace foreign key type
* feat(api): fixed trace schema
* feat(api): trace save auth-method
* feat(api): trace fixed auth-method
* feat(api): trace changed schema
184 lines
6.9 KiB
Python
184 lines
6.9 KiB
Python
from chalicelib.utils import pg_client, helper
|
|
from chalicelib.utils.TimeUTC import TimeUTC
|
|
import requests
|
|
|
|
|
|
def get_by_id(webhook_id):
|
|
with pg_client.PostgresClient() as cur:
|
|
cur.execute(
|
|
cur.mogrify("""\
|
|
SELECT
|
|
w.*
|
|
FROM public.webhooks AS w
|
|
where w.webhook_id =%(webhook_id)s AND deleted_at ISNULL;""",
|
|
{"webhook_id": webhook_id})
|
|
)
|
|
w = helper.dict_to_camel_case(cur.fetchone())
|
|
if w:
|
|
w["createdAt"] = TimeUTC.datetime_to_timestamp(w["createdAt"])
|
|
return w
|
|
|
|
|
|
def get(tenant_id, webhook_id):
|
|
with pg_client.PostgresClient() as cur:
|
|
cur.execute(
|
|
cur.mogrify("""\
|
|
SELECT
|
|
webhook_id AS integration_id, webhook_id AS id, w.*
|
|
FROM public.webhooks AS w
|
|
where w.webhook_id =%(webhook_id)s AND deleted_at ISNULL;""",
|
|
{"webhook_id": webhook_id})
|
|
)
|
|
w = helper.dict_to_camel_case(cur.fetchone())
|
|
if w:
|
|
w["createdAt"] = TimeUTC.datetime_to_timestamp(w["createdAt"])
|
|
return w
|
|
|
|
|
|
def get_by_type(tenant_id, webhook_type):
|
|
with pg_client.PostgresClient() as cur:
|
|
cur.execute(
|
|
cur.mogrify("""\
|
|
SELECT
|
|
w.webhook_id AS integration_id, w.webhook_id AS id,w.webhook_id,w.endpoint,w.auth_header,w.type,w.index,w.name,w.created_at
|
|
FROM public.webhooks AS w
|
|
WHERE w.type =%(type)s AND deleted_at ISNULL;""",
|
|
{"type": webhook_type})
|
|
)
|
|
webhooks = helper.list_to_camel_case(cur.fetchall())
|
|
for w in webhooks:
|
|
w["createdAt"] = TimeUTC.datetime_to_timestamp(w["createdAt"])
|
|
return webhooks
|
|
|
|
|
|
def get_by_tenant(tenant_id, replace_none=False):
|
|
with pg_client.PostgresClient() as cur:
|
|
cur.execute("""\
|
|
SELECT
|
|
webhook_id AS integration_id, webhook_id AS id, w.*
|
|
FROM public.webhooks AS w
|
|
WHERE deleted_at ISNULL;"""
|
|
)
|
|
all = helper.list_to_camel_case(cur.fetchall())
|
|
if replace_none:
|
|
for w in all:
|
|
w["createdAt"] = TimeUTC.datetime_to_timestamp(w["createdAt"])
|
|
for k in w.keys():
|
|
if w[k] is None:
|
|
w[k] = ''
|
|
else:
|
|
for w in all:
|
|
w["createdAt"] = TimeUTC.datetime_to_timestamp(w["createdAt"])
|
|
return all
|
|
|
|
|
|
def update(tenant_id, webhook_id, changes, replace_none=False):
|
|
allow_update = ["name", "index", "authHeader", "endpoint"]
|
|
with pg_client.PostgresClient() as cur:
|
|
sub_query = [f"{helper.key_to_snake_case(k)} = %({k})s" for k in changes.keys() if k in allow_update]
|
|
print(cur.mogrify(f"""\
|
|
UPDATE public.webhooks
|
|
SET {','.join(sub_query)}
|
|
WHERE webhook_id =%(id)s AND deleted_at ISNULL
|
|
RETURNING webhook_id AS integration_id, webhook_id AS id,*;""",
|
|
{"id": webhook_id, **changes}))
|
|
cur.execute(
|
|
cur.mogrify(f"""\
|
|
UPDATE public.webhooks
|
|
SET {','.join(sub_query)}
|
|
WHERE webhook_id =%(id)s AND deleted_at ISNULL
|
|
RETURNING webhook_id AS integration_id, webhook_id AS id,*;""",
|
|
{"id": webhook_id, **changes})
|
|
)
|
|
w = helper.dict_to_camel_case(cur.fetchone())
|
|
w["createdAt"] = TimeUTC.datetime_to_timestamp(w["createdAt"])
|
|
if replace_none:
|
|
for k in w.keys():
|
|
if w[k] is None:
|
|
w[k] = ''
|
|
return w
|
|
|
|
|
|
def add(tenant_id, endpoint, auth_header=None, webhook_type='webhook', name="", replace_none=False):
|
|
with pg_client.PostgresClient() as cur:
|
|
query = cur.mogrify("""\
|
|
INSERT INTO public.webhooks(endpoint,auth_header,type,name)
|
|
VALUES (%(endpoint)s, %(auth_header)s, %(type)s,%(name)s)
|
|
RETURNING webhook_id AS integration_id, webhook_id AS id,*;""",
|
|
{"endpoint": endpoint, "auth_header": auth_header,
|
|
"type": webhook_type, "name": name})
|
|
cur.execute(
|
|
query
|
|
)
|
|
w = helper.dict_to_camel_case(cur.fetchone())
|
|
w["createdAt"] = TimeUTC.datetime_to_timestamp(w["createdAt"])
|
|
if replace_none:
|
|
for k in w.keys():
|
|
if w[k] is None:
|
|
w[k] = ''
|
|
return w
|
|
|
|
|
|
def add_edit(tenant_id, data, replace_none=None):
|
|
if data.get("webhookId") is not None:
|
|
return update(tenant_id=tenant_id, webhook_id=data["webhookId"],
|
|
changes={"endpoint": data["endpoint"],
|
|
"authHeader": None if "authHeader" not in data else data["authHeader"],
|
|
"name": data["name"] if "name" in data else ""}, replace_none=replace_none)
|
|
else:
|
|
return add(tenant_id=tenant_id,
|
|
endpoint=data["endpoint"],
|
|
auth_header=None if "authHeader" not in data else data["authHeader"],
|
|
name=data["name"] if "name" in data else "", replace_none=replace_none)
|
|
|
|
|
|
def delete(tenant_id, webhook_id):
|
|
with pg_client.PostgresClient() as cur:
|
|
cur.execute(
|
|
cur.mogrify("""\
|
|
UPDATE public.webhooks
|
|
SET deleted_at = (now() at time zone 'utc')
|
|
WHERE webhook_id =%(id)s AND deleted_at ISNULL
|
|
RETURNING *;""",
|
|
{"id": webhook_id})
|
|
)
|
|
return {"data": {"state": "success"}}
|
|
|
|
|
|
def trigger_batch(data_list):
|
|
webhooks_map = {}
|
|
for w in data_list:
|
|
if w["destination"] not in webhooks_map:
|
|
webhooks_map[w["destination"]] = get_by_id(webhook_id=w["destination"])
|
|
__trigger(hook=webhooks_map[w["destination"]], data=w["data"])
|
|
|
|
|
|
def __trigger(hook, data):
|
|
if hook["type"] == 'webhook':
|
|
headers = {}
|
|
if hook["authHeader"] is not None and len(hook["authHeader"]) > 0:
|
|
headers = {"Authorization": hook["authHeader"]}
|
|
|
|
# body = {
|
|
# "webhookId": hook["id"],
|
|
# "createdAt": TimeUTC.now(),
|
|
# "event": event,
|
|
# "data": data
|
|
# }
|
|
|
|
r = requests.post(url=hook["endpoint"], json=data, headers=headers)
|
|
if r.status_code != 200:
|
|
print("=======> webhook: something went wrong")
|
|
print(r)
|
|
print(r.status_code)
|
|
print(r.text)
|
|
return
|
|
response = None
|
|
try:
|
|
response = r.json()
|
|
except:
|
|
try:
|
|
response = r.text
|
|
except:
|
|
print("no response found")
|
|
return response
|