# Buggy code
def send_push(
    self, api_key, title, body, url=None, destination=None, destination_type=None
):
    push_type = "link" if url else "note"
    notification = {"type": push_type, "title": title, "body": body}
    if url:
        notification["url"] = url
    if destination:
        notification[destination_type] = destination

    # Make the request
    headers = {
        "Authorization": b"Basic " + base64.b64encode(api_key.encode("ascii")),
        "Content-Type": "application/json",
        "Accept": "application/json",
        "User-Agent": "Flexget",
    }
    try:
        response = requests.post(PUSHBULLET_URL, headers=headers, json=notification)
    except RequestException as e:
        if e.response is not None:
            if e.response.status_code == 429:
                reset_time = datetime.datetime.fromtimestamp(
                    int(e.response.headers["X-Ratelimit-Reset"])
                ).strftime("%Y-%m-%d %H:%M:%S")
                message = (
                    "Monthly Pushbullet database operations limit reached. Next reset: %s"
                    % reset_time
                )
            else:
                message = e.response.json()["error"]["message"]
        else:
            message = str(e)
        raise PluginWarning(message)

    reset_time = datetime.datetime.fromtimestamp(
        int(response.headers["X-Ratelimit-Reset"])
    ).strftime("%Y-%m-%d %H:%M:%S")
    remaining = response.headers["X-Ratelimit-Remaining"]
    log.debug(
        "Pushbullet notification sent. Database operations remaining until next reset: %s. "
        "Next reset at: %s",
        remaining,
        reset_time,
    )


# Traceback (KeyError)
2019-11-13 10:30 ERROR    notify_entry  NexBox    'x-ratelimit-reset'
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/flexget/components/notify/notify.py", line 104, in send_notification
send_notification(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/flexget/components/notify/notification_framework.py", line 124, in send_notification
title, message, rendered_config
File "/usr/local/lib/python3.5/dist-packages/flexget/components/notify/notifiers/pushbullet.py", line 89, in notify
self.send_push(key, title, message, config.get('url'), d, 'device_iden')
File "/usr/local/lib/python3.5/dist-packages/flexget/components/notify/notifiers/pushbullet.py", line 130, in send_push
int(response.headers['X-Ratelimit-Reset'])
File "/usr/local/lib/python3.5/dist-packages/requests/structures.py", line 52, in __getitem__
return self._store[key.lower()][1]
KeyError: 'x-ratelimit-reset'

# Fixed code
def send_push(
    self, api_key, title, body, url=None, destination=None, destination_type=None
):
    push_type = "link" if url else "note"
    notification = {"type": push_type, "title": title, "body": body}
    if url:
        notification["url"] = url
    if destination:
        notification[destination_type] = destination

    # Make the request
    headers = {
        "Authorization": b"Basic " + base64.b64encode(api_key.encode("ascii")),
        "Content-Type": "application/json",
        "Accept": "application/json",
        "User-Agent": "Flexget",
    }
    try:
        response = requests.post(PUSHBULLET_URL, headers=headers, json=notification)
    except RequestException as e:
        if e.response is not None:
            if e.response.status_code == 429:
                reset_time = e.response.headers.get("X-Ratelimit-Reset")
                if reset_time:
                    reset_time = datetime.datetime.fromtimestamp(
                        int(reset_time)
                    ).strftime("%Y-%m-%d %H:%M:%S")
                    message = f"Monthly Pushbullet database operations limit reached. Next reset: {reset_time}"
            else:
                message = e.response.json()["error"]["message"]
        else:
            message = str(e)
        raise PluginWarning(message)

    reset_time = datetime.datetime.fromtimestamp(
        int(response.headers["X-Ratelimit-Reset"])
    ).strftime("%Y-%m-%d %H:%M:%S")
    remaining = response.headers["X-Ratelimit-Remaining"]
    log.debug(
        "Pushbullet notification sent. Database operations remaining until next reset: %s. "
        "Next reset at: %s",
        remaining,
        reset_time,
    )