Opsgenie Python Heartbeat API

To use the Heartbeat functionalities of Opsgenie's API via the Python SDK you will first have to import the Heartbeat client from the SDK library and (configure)(doc:opsgenie-python-api-v2-1#section-client-initialization) it with the API Key that you procured from your Opsgenie Integrations. For additional configurations, you may refer to the Python SDK Configurations page present in the Opsgenie documentation.

# Importing relevant Opsgenie SDK libraries including the Alert API client
import opsgenie_sdk

class Example:
    def __init__(self, opsgenie_api_key):
        self.conf = self.conf = opsgenie_sdk.configuration.Configuration()
        self.conf.api_key['Authorization'] = '<Your-API-Key>'

        self.api_client = opsgenie_sdk.api_client.ApiClient(configuration=self.conf)
        self.heartbeat_api = opsgenie_sdk.HeartbeatApi(api_client=self.api_client)

Ping Heartbeat Request

You can get the heartbeat request using the Heatbeat client that is initialized prior to performing any Opsgenie API functionality.

In performing a heartbeat ping, you pass the heartbeatName to the ping function. To know how to do so exactly please visit the Ping Heartbeat Request section in the Opsgenie REST API documentation here.

def ping(self):
  try:
    ping_response = self.heartbeat_api.ping(self.heartbeat_name)
    print(ping_response)
    return ping_response
    except ApiException as err:
      print("Exception when calling HeartBeatApi->ping: %s\n" % err)

Add Heartbeat Request

You can add a heartbeat request using the Heatbeat client that is initialized prior to performing any Opsgenie API functionality.

When adding a heartbeat, please ensure you pass the required and right values in as a CreateHeartbeatPayload object to the create_heartbeat function via the create_heartbeat_payload parameter. To know which values are required and can be used, please visit the Add Heartbeat section in the Opsgenie REST API documentation here.

def add(self):
  owner_team = opsgenie_client.CreateHeartbeatPayloadOwnerTeam(name='Sample')
  body = opsgenie_client.CreateHeartbeatPayload(name='PythonSDK', description='Created via the python sdk', interval=5, owner_team=owner_team, interval_unit='minutes', enabled=True, alert_message='testingPythonSDK', alert_tags=['tag1'], alert_priority='P2')
  try:
    create_response = self.heartbeat_api.create_heartbeat(create_heartbeat_payload=body)
    print(create_response)
    return create_response
  except ApiException as err:
    print("Exception when calling HeartBeatApi->add: %s\n" % err)

Get Heartbeat Request

You can get a heartbeat request using the Heatbeat client that is initialized prior to performing any Opsgenie API functionality.

When getting a heartbeat, please ensure you pass the heartbeatName to the get_heartbeat function. To know exactly how to do so, please visit the Get Heartbeat section in the Opsgenie REST API documentation here.

def get(self):
  try:
    response = self.heartbeat_api.get_heartbeat('PythonSDK')
    print(response)
  except ApiException as err:
    print("Exception when calling HeartBeatApi->get: %s\n" % err)

List Heartbeats

You can list heartbeat requests using the Heatbeat client that is initialized prior to performing any Opsgenie API functionality.

Use the list_heart_beats function to get a list of your heartbeats.

def list(self):
  try:
    response = self.heartbeat_api.list_heart_beats()
    print(response)
  except ApiException as err:
    print("Exception when calling HeartBeatApi->list: %s\n" % err)

Update Heartbeat Request (Partial)

You can update a heartbeat using the Heatbeat client that is initialized prior to performing any Opsgenie API functionality.

When updating a heartbeat, please ensure you pass the heartbeatName to the update_heartbeat function. The information to update can be passed in as an UpdateHeartbeatPayload via the update_heartbeat_payload parameter. To know exactly how to do so, please visit the Update Heartbeat Request (Partial) section in the Opsgenie REST API documentation here.

def update(self):
  body = opsgenie_client.UpdateHeartbeatPayload(description='UpdatedPythonSDK')
  try:
    update_response = self.heartbeat_api.update_heartbeat(name='PythonSDK', update_heartbeat_payload=body)
    print(update_response)
    return update_response
  except ApiException as err:
    print("Exception when calling HeartBeatApi->update: %s\n" % err)

Delete Heartbeat Request

You can delete a heartbeat request using the Heatbeat client that is initialized prior to performing any Opsgenie API functionality.

When deleting a heartbeat, please ensure you pass the heartbeatName to the delete_heartbeat function. To know exactly how to do so, please visit the Delete Heartbeat section in the Opsgenie REST API documentation here.

def delete(self):
  try:
    delete_response = self.heartbeat_api.delete_heartbeat('PythonSDK')
    print(delete_response)
    return delete_response
  except ApiException as err:
    print("Exception when calling HeartBeatApi->delete: %s\n" % err)

Enable Heartbeat Request

You can enable a heartbeat request using the Heatbeat client that is initialized prior to performing any Opsgenie API functionality.

When enabling a heartbeat, please ensure you pass the heartbeatName to the enable_heartbeat function. To know exactly how to do so, please visit the Enable Heartbeat section in the Opsgenie REST API documentation here.

def enable(self):
  try:
    enable_response = self.heartbeat_api.enable_heartbeat('PythonSDK')
    print(enable_response)
    return enable_response
  except ApiException as err:
    print("Exception when calling HeartBeatApi->enable: %s\n" % err)

Disable Heartbeat Request

You can disable a heartbeat request using the Heatbeat client that is initialized prior to performing any Opsgenie API functionality.

When disabling a heartbeat, please ensure you pass the heartbeatName to the disable_heartbeat function. To know exactly how to do so, please visit the Disable Heartbeat section in the Opsgenie REST API documentation here.

def disable(self):
  try:
    disable_response = self.heartbeat_api.disable_heartbeat('PythonSDK')
    print(disable_response)
    return disable_response
  except ApiException as err:
    print("Exception when calling HeartBeatApi->disable: %s\n" % err)