From b376516ccc361fae726869dae9b2a1243bf20ac5 Mon Sep 17 00:00:00 2001 From: Nilesh Pant Date: Fri, 15 Dec 2023 10:54:12 +0530 Subject: [PATCH 1/2] create webhook subscription after connection is complete --- apps/travelperk/connector.py | 24 ++++++- .../migrations/0006_auto_20231214_1927.py | 23 +++++++ apps/travelperk/models.py | 4 +- apps/travelperk/views.py | 65 +++++++------------ connectors/travelperk/apis/api_base.py | 3 +- 5 files changed, 71 insertions(+), 48 deletions(-) create mode 100644 apps/travelperk/migrations/0006_auto_20231214_1927.py diff --git a/apps/travelperk/connector.py b/apps/travelperk/connector.py index 11d390fd..01f56fa3 100644 --- a/apps/travelperk/connector.py +++ b/apps/travelperk/connector.py @@ -2,8 +2,8 @@ from django.conf import settings -from apps.travelperk.models import TravelperkCredential -from connectors.travelperk.core.client import Travelperk +from apps.travelperk.models import TravelperkCredential, TravelPerk +from connectors.travelperk import Travelperk logger = logging.getLogger(__name__) @@ -26,3 +26,23 @@ def __init__(self, credentials_object: TravelperkCredential, org_id: int): credentials_object.refresh_token = self.connection.refresh_token credentials_object.save() + + + def create_webhook(self, data: dict): + """ + Create Webhook in Travelperk + :param data: Webhook Data + :return: Webhook Data + """ + + response = self.connection.webhooks.create(data) + if response: + TravelPerk.objects.update_or_create( + org_id=self.org_id, + defaults={ + 'webhook_id': response['id'], + 'webhook_enabled': response['enabled'] + } + ) + + return response diff --git a/apps/travelperk/migrations/0006_auto_20231214_1927.py b/apps/travelperk/migrations/0006_auto_20231214_1927.py new file mode 100644 index 00000000..df40be26 --- /dev/null +++ b/apps/travelperk/migrations/0006_auto_20231214_1927.py @@ -0,0 +1,23 @@ +# Generated by Django 3.1.14 on 2023-12-14 19:27 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('travelperk', '0005_invoice_invoicelineitem_travelperkcredential'), + ] + + operations = [ + migrations.AddField( + model_name='travelperk', + name='webhook_enabled', + field=models.BooleanField(help_text='If Webhook Is Enabled', null=True), + ), + migrations.AddField( + model_name='travelperk', + name='webhook_subscription_id', + field=models.CharField(help_text='Webhook Subscription Id', max_length=255, null=True), + ), + ] diff --git a/apps/travelperk/models.py b/apps/travelperk/models.py index bc9fcaf5..78ab74e1 100644 --- a/apps/travelperk/models.py +++ b/apps/travelperk/models.py @@ -19,8 +19,6 @@ class Meta: db_table = 'travelperk_credentials' -from django.db import models - class InvoiceLineItem(models.Model): """ Travelperk Invoice Line Item Model @@ -103,6 +101,8 @@ class TravelPerk(models.Model): is_s3_connected = models.BooleanField(null=True, help_text='If S3 Is Connectoed') is_travelperk_connected = models.BooleanField(null=True, help_text='If Travelperk Is Connected') travelperk_connection_id = models.IntegerField(null=True, help_text='Travelperk Connection Id') + webhook_subscription_id = models.CharField(max_length=255, null=True, help_text='Webhook Subscription Id') + webhook_enabled = models.BooleanField(null=True, help_text='If Webhook Is Enabled') created_at = models.DateTimeField(auto_now_add=True, help_text='Created at datetime') updated_at = models.DateTimeField(auto_now=True, help_text='Updated at datetime') diff --git a/apps/travelperk/views.py b/apps/travelperk/views.py index 38be3180..d8996e85 100644 --- a/apps/travelperk/views.py +++ b/apps/travelperk/views.py @@ -5,11 +5,6 @@ from rest_framework.response import Response from rest_framework.views import status -from django.http import JsonResponse -from django.views.decorators.csrf import csrf_exempt -from django.views.decorators.http import require_POST -import json - from workato import Workato from workato.exceptions import * @@ -17,9 +12,9 @@ from apps.orgs.models import Org from apps.orgs.actions import create_connection_in_workato, upload_properties, post_folder, post_package from apps.travelperk.serializers import TravelperkSerializer, TravelPerkConfigurationSerializer -from apps.travelperk.models import TravelPerk, TravelPerkConfiguration +from apps.travelperk.models import TravelPerk, TravelPerkConfiguration, TravelperkCredential from apps.travelperk.actions import connect_travelperk -from connectors.travelperk import exceptions as travelperk_exc +from apps.travelperk.connector import TravelperkConnector from .helpers import get_refresh_token_using_auth_code @@ -225,53 +220,39 @@ class ConnectTravelperkView(generics.CreateAPIView): """ def post(self, request, *args, **kwargs): + try: - connector = Workato() org = Org.objects.get(id=kwargs['org_id']) - travelperk = TravelPerk.objects.get(org_id=org.id) - refresh_token = get_refresh_token_using_auth_code(request.data.get('code'), kwargs['org_id']) - - properties_payload = { - 'properties': { - 'TRAVELPERK_REFRESH_TOKEN': refresh_token - } - } - - data = { - "input": { - "key": "***" + + if refresh_token: + travelperk_credential = TravelperkCredential.objects.get(org=org) + travelperk_connection = TravelperkConnector(travelperk_credential, kwargs['org_id']) + + travelperk_webhook_data = { + 'name': 'travelperk webhook invoice', + 'url': 'https://webhook.site/e4ac2898-209f-4dd2-88cf-738dea95ecdc', + 'secret': 'some secret', + 'events': [ + 'invoice.issued' + ] } - } - upload_properties(org.managed_user_id, properties_payload) - - travelperk_connection = create_connection_in_workato(org.id, TRAVELPERK['connection'], org.managed_user_id, data) - if 'authorization_status' in travelperk_connection and travelperk_connection['authorization_status'] == 'success': - travelperk.is_connected = True - travelperk.save() - - recipes = connector.recipes.get(org.managed_user_id)['result'] - travelperk_configuration, _ = TravelPerkConfiguration.objects.update_or_create( - org_id=org.id, - recipe_id=recipes[0]['id'], + created_webhook = travelperk_connection.create_webhook(travelperk_webhook_data) + print('created_webhook', created_webhook) + TravelPerk.objects.update_or_create( + org=org, defaults={ - 'recipe_data': recipes[0]['code'], - 'is_recipe_enabled': True + 'webhook_id': created_webhook['id'], } ) - connector.recipes.post(org.managed_user_id, travelperk_configuration.recipe_id, None, 'start') + return Response( - data=travelperk_connection, + data=created_webhook, status=status.HTTP_200_OK ) - return Response( - data='Something went wrong while connecting to travelperk', - status=status.HTTP_400_BAD_REQUEST - ) - - except Exception as exception: + except Exception: error = traceback.format_exc() logger.error(error) diff --git a/connectors/travelperk/apis/api_base.py b/connectors/travelperk/apis/api_base.py index a73425de..814b9856 100644 --- a/connectors/travelperk/apis/api_base.py +++ b/connectors/travelperk/apis/api_base.py @@ -135,8 +135,7 @@ def _delete_request(self, api_url: str) -> Dict: ) if response.status_code == 200: - result = json.loads(response.text) - return result + return response.text elif response.status_code == 400: raise BadRequestError('Something wrong with the request body', response.text) From 7de529f38120ac0a6438f09a004cb582996939ba Mon Sep 17 00:00:00 2001 From: Nilesh Pant Date: Sun, 17 Dec 2023 11:51:05 +0530 Subject: [PATCH 2/2] fix tests --- tests/test_travelperk/fixtures.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_travelperk/fixtures.py b/tests/test_travelperk/fixtures.py index d7414a05..4cd2ea80 100644 --- a/tests/test_travelperk/fixtures.py +++ b/tests/test_travelperk/fixtures.py @@ -9,6 +9,8 @@ "is_s3_connected":"dtmmy", "created_at":"2022-11-29T15:39:49.221955Z", "updated_at":"2022-11-29T15:41:59.535831Z", + "webhook_subscription_id": "123", + "webhook_enabled": True, "org":2 }, "configurations": {