From 0bc527e78c8015a54dd6aa1389279e0279f36eb3 Mon Sep 17 00:00:00 2001 From: "Peter.Morton" Date: Fri, 29 Aug 2025 10:18:38 -0500 Subject: [PATCH] Adding posting.sh example for copilot cloud events --- copilot/posting/copilotux/cloud-events.py | 49 +++++++++++++++++++ .../copilotux/smart-transfer.posting.yaml | 34 +++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 copilot/posting/copilotux/cloud-events.py create mode 100644 copilot/posting/copilotux/smart-transfer.posting.yaml diff --git a/copilot/posting/copilotux/cloud-events.py b/copilot/posting/copilotux/cloud-events.py new file mode 100644 index 0000000..cbb3e6f --- /dev/null +++ b/copilot/posting/copilotux/cloud-events.py @@ -0,0 +1,49 @@ +from posting import Auth, Header, RequestModel, Posting +import httpx + +def get_oauth2_token(client_id, client_secret, token_url, audience): + """ + Request an OAuth2 access token using the Client Credentials Grant flow. + + Args: + client_id (str): The client ID provided by the OAuth2 provider. + client_secret (str): The client secret provided by the OAuth2 provider. + token_url (str): The URL to request the access token. + + Returns: + str: The access token if the request is successful. + """ + # Prepare the data for the token request + data = { + "grant_type": "client_credentials", # Grant type for Client Credentials flow + "client_id": client_id, + "client_secret": client_secret, + "audience": audience + } + + try: + # Make the POST request to the token endpoint + response = httpx.post(token_url, data=data) + + # Raise an exception if the request failed + response.raise_for_status() + + # Parse the JSON response + token_data = response.json() + + print ("Successfully obtained access_token") + + # Return the access token + return token_data.get("access_token") + except httpx.RequestError as e: + print(f"An error occurred while requesting the token: {e}") + except httpx.HTTPStatusError as e: + print(f"HTTP error occurred: {e.response.status_code} - {e.response.text}") + except KeyError: + print("The response did not contain an access token.") + return None + +def setup(posting: Posting) -> None: + if not posting.get_variable("auth_token"): + token = get_oauth2_token(posting.variables["CLIENT_ID_V2"], posting.variables["CLIENT_SECRET_V2"],posting.variables["TOKEN_URL"],posting.variables["AUDIENCE"],) + posting.set_variable("auth_token", token) diff --git a/copilot/posting/copilotux/smart-transfer.posting.yaml b/copilot/posting/copilotux/smart-transfer.posting.yaml new file mode 100644 index 0000000..9f604c3 --- /dev/null +++ b/copilot/posting/copilotux/smart-transfer.posting.yaml @@ -0,0 +1,34 @@ +name: Smart Transfer +method: POST +url: $BASE_URL/v1/cloud-event +body: + content: |- + { + "customerId": "$CUSTOMER_ID", + "source": "//iva/us-east-prd/transfer", + "id": "2025051416334592900000000000000001", + "type": "iva.transfer.data.v1", + "time": "2025-05-14T16:33:45.929Z", + "upn": "$UPN", + "userUUID": "$USER_UUID", + "data": { + "conversationId": "2025051416334592900000000000000001", + "customData": { + "firstName": "John", + "lastName": "Smith", + "phoneNumber": "9887655432", + "accountID": "1234", + "summary": "summary test", + "lastContactDate": "09/09/2020", + "purpose": "Testing Purpose" + } + } + } + content_type: application/json +headers: +- name: Authorization + value: Bearer $auth_token +- name: content-type + value: application/json +scripts: + setup: cloud-events.py