import requests
import json

from amon.apps.notifications.models import notifications_model
        
def send_opsgenie_notification(message=None, auth=None):
    sent = False
    url = "https://api.opsgenie.com/v1/json/alert"


    # Message is limited to 130 chars
    data = {
            'apiKey': auth.get('api_key'),
            'message': message,
        }

    data = json.dumps(data)
    error = None
    
    try:
        r = requests.post(url, data=data, timeout=5)
    except Exception as e:
        error = e

    return error
import requests
import json

from amon.apps.notifications.models import notifications_model

def send_opsgenie_notification(message=None, auth=None):
    sent = False
    url = "https://api.opsgenie.com/v2/alerts"
    headers = {
        'Authorization': 'GenieKey '+ auth.get('api_key'),
        'Content-Type': 'application/json'
    }

    # Message is limited to 130 chars
    data = {
            'message': message,
        }

    data = json.dumps(data)
    error = None

    try:
        r = requests.post(url, data=data, timeout=5, headers=headers)
    except Exception as e:
        error = e

    return error
Switch to OpsGenie API V2
Switch to OpsGenie API V2

  import requests
  import json
  
  from amon.apps.notifications.models import notifications_model
-         
+ 
  def send_opsgenie_notification(message=None, auth=None):
      sent = False
-     url = "https://api.opsgenie.com/v1/json/alert"
+     url = "https://api.opsgenie.com/v2/alerts"
- 
+     headers = {
+         'Authorization': 'GenieKey '+ auth.get('api_key'),
+         'Content-Type': 'application/json'
+     }
  
      # Message is limited to 130 chars
      data = {
-             'apiKey': auth.get('api_key'),
              'message': message,
          }
  
      data = json.dumps(data)
      error = None
-     
+ 
      try:
-         r = requests.post(url, data=data, timeout=5)
+         r = requests.post(url, data=data, timeout=5, headers=headers)
      except Exception as e:
          error = e
  
      return error
Switch to OpsGenie API V2
## Code Before:
import requests
import json

from amon.apps.notifications.models import notifications_model
        
def send_opsgenie_notification(message=None, auth=None):
    sent = False
    url = "https://api.opsgenie.com/v1/json/alert"


    # Message is limited to 130 chars
    data = {
            'apiKey': auth.get('api_key'),
            'message': message,
        }

    data = json.dumps(data)
    error = None
    
    try:
        r = requests.post(url, data=data, timeout=5)
    except Exception as e:
        error = e

    return error
## Instruction:
Switch to OpsGenie API V2
## Code After:
import requests
import json

from amon.apps.notifications.models import notifications_model

def send_opsgenie_notification(message=None, auth=None):
    sent = False
    url = "https://api.opsgenie.com/v2/alerts"
    headers = {
        'Authorization': 'GenieKey '+ auth.get('api_key'),
        'Content-Type': 'application/json'
    }

    # Message is limited to 130 chars
    data = {
            'message': message,
        }

    data = json.dumps(data)
    error = None

    try:
        r = requests.post(url, data=data, timeout=5, headers=headers)
    except Exception as e:
        error = e

    return error