Skip to content

Commit

Permalink
chore(webhooks): update some entry points
Browse files Browse the repository at this point in the history
  • Loading branch information
acostapazo committed Aug 10, 2023
1 parent f9af88b commit 77f6d8d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
3 changes: 3 additions & 0 deletions alice/onboarding/onboarding_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,9 @@ def create_report(
response = self.session.get(
f"{self.url}/user/report", headers=headers, timeout=self.timeout
)
import pdb

pdb.set_trace()
except requests.exceptions.Timeout:
return Failure(OnboardingError.timeout(operation="create_report"))

Expand Down
2 changes: 2 additions & 0 deletions alice/onboarding/onboarding_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def from_response(operation: str, response: Response) -> "OnboardingError":
code = response.status_code
try:
message = response.json()
# old {'error': {'message': 'Method Not Allowed: to unlock it please open a ticket with the support team', 'type': 'EntryPointNotAvailableHttpError'}}
# new {'detail': 'Webhook Result not found'}
except Exception:
message = {"message": "no content"}
return OnboardingError(operation=operation, code=code, message=message)
Expand Down
10 changes: 8 additions & 2 deletions alice/webhooks/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ def __init__(
self.verbose = verbose

@early_return
def get_available_events(
def get_subscriptable_events(
self, verbose: Optional[bool] = False
) -> Result[bool, Union[OnboardingError, AuthError]]:
"""
Get public available events.
Get public subscriptable events.
Parameters
----------
Expand All @@ -74,6 +74,12 @@ def get_available_events(
)
)

@early_return
def get_available_events(
self, verbose: Optional[bool] = False
) -> Result[bool, Union[OnboardingError, AuthError]]:
return self.get_subscriptable_events(verbose)

@early_return
def create_webhook(
self, webhook: Webhook, verbose: Optional[bool] = False
Expand Down
17 changes: 13 additions & 4 deletions alice/webhooks/webhooks_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ def _auth_headers(self, token: str) -> Dict[str, Any]:

@early_return
@timeit
def get_available_events(
def get_subscriptable_events(
self, verbose: Optional[bool] = False
) -> Result[Response, AuthError]:
"""
Get public available events.
Get public subscriptable events.
Parameters
----------
Expand All @@ -55,17 +55,26 @@ def get_available_events(
-------
A Response object [requests library] if success
"""
print_intro("get_available_events", verbose=verbose)
print_intro("get_subscriptable_events", verbose=verbose)

backend_token = self.auth.create_backend_token().unwrap_or_return()
print_token("backend_token_with_user", backend_token, verbose=verbose)
headers = self._auth_headers(backend_token)
response = self.session.get(self.url + "/webhook/events", headers=headers)
response = self.session.get(
self.url + "/webhook/subscriptable/events", headers=headers
)

print_response(response=response, verbose=verbose)

return Success(response)

@early_return
@timeit
def get_available_events(
self, verbose: Optional[bool] = False
) -> Result[Response, AuthError]:
return self.get_subscriptable_events(verbose)

@early_return
@timeit
def create_webhook(
Expand Down
1 change: 0 additions & 1 deletion examples/onboarding_wait_for_completion_false.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def onboarding_example(api_key: str, verbose: Optional[bool] = False) -> None:
config = Config(
api_key=api_key,
verbose=verbose,
onboarding_url="https://apis.staging.alicebiometrics.com/onboarding",
)
onboarding = Onboarding.from_config(config)

Expand Down
21 changes: 12 additions & 9 deletions examples/onboarding_with_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def configure_webhooks(api_key: str, verbose: Optional[bool] = False) -> None:
webhooks_client = Webhooks.from_config(config)

# Check Available events
available_events = webhooks_client.get_available_events().unwrap()
selected_event = available_events[0]
subscriptable_events = webhooks_client.get_subscriptable_events().unwrap_or_raise()
selected_event = subscriptable_events[0]

# Create a new Webhook
webhook = Webhook(
Expand All @@ -26,32 +26,33 @@ def configure_webhooks(api_key: str, verbose: Optional[bool] = False) -> None:
event_version=selected_event.get("version"),
secret=str(secrets.token_hex(20)),
)
webhook_id = webhooks_client.create_webhook(webhook).unwrap()
webhook_id = webhooks_client.create_webhook(webhook).unwrap_or_raise()

# Update an existent Webhook
webhook_to_update = Webhook(
webhook_id=webhook_id, # Needed if we want to update
active=False,
post_url="http://alicebiometrics.com",
api_key="b0b905d6-228f-44bf-a130-c85d7aecd765",
event_name="user_created",
event_name="user.created",
event_version="1",
algorithm="sha512",
secret=str(secrets.token_hex(20)),
)
webhooks_client.update_webhook(webhook_to_update)
webhooks_client.update_webhook(webhook_to_update).unwrap_or_raise()

# Update the activation of a Webhook
webhooks_client.update_webhook_activation(webhook_id, True)
retrieved_webhook = webhooks_client.get_webhook(webhook_id).unwrap()
retrieved_webhook = webhooks_client.get_webhook(webhook_id).unwrap_or_raise()
assert retrieved_webhook.active

# Send a ping using configured webhook
result = webhooks_client.ping_webhook(webhook_id)

# Retrieve an existent Webhook
retrieved_webhook = webhooks_client.get_webhook(webhook_id).unwrap()
retrieved_webhook = webhooks_client.get_webhook(webhook_id).unwrap_or_raise()
assert retrieved_webhook.active
breakpoint()
assert retrieved_webhook.post_url == "http://alicebiometrics.com"

# Retrieve all configured webhooks
Expand All @@ -62,10 +63,12 @@ def configure_webhooks(api_key: str, verbose: Optional[bool] = False) -> None:
assert_success(result)

# Retrieve all webhook results of a specific webhook
webhook_results = webhooks_client.get_webhook_results(webhook_id).unwrap()
webhook_results = webhooks_client.get_webhook_results(webhook_id).unwrap_or_raise()

# Retrieve las webhook result of a specific webhook
last_webhook_result = webhooks_client.get_last_webhook_result(webhook_id).unwrap()
last_webhook_result = webhooks_client.get_last_webhook_result(
webhook_id
).unwrap_or_raise()


if __name__ == "__main__":
Expand Down

0 comments on commit 77f6d8d

Please sign in to comment.