A Python library for PagerDuty.

Overview
https://travis-ci.org/dropbox/pygerduty.svg?branch=master

Pygerduty

Python Library for PagerDuty's REST API and Events API. This library was originally written to support v1 and is currently being updated to be compatible with v2 of the API. See "Migrating from v1 to v2" for more details.

This library is currently evolving and backwards compatibility cannot always be guaranteed at this time.

Installation

You can install with pip install pygerduty.

If you want to install from source, then python setup.py install.

Requirements

Tested on Python 2.7, or >= 3.6.

Known to work on Python 2.6.

Documentation

Pygerduty is a thin wrapper around PagerDuty's APIs. You will need to refer to the the PagerDuty Documentation for all available parameters to pass and all available attributes on responses.

The main methods available to resources are list, show, create, update, and delete. Not all resources have endpoints for all of the above methods. Again, refer to the PagerDuty Documentation to see all available endpoints.

Top level resources will be accessible via the PagerDuty object and nested resources available on containers returned from their parent resource.

Migrating from v1 to v2

In order to allow for a smooth transition between versions 1 and 2 of the library, version 1 library remains in the file called __init__.py inside of the pygerduty directory. Also in that directory you will see four other files:

  • v2.py — This file contains all updated logic compatible with v2 of the API.
  • events.py — PygerDuty also provides an Events API which is separate from the REST API that has had the recent update. Since the logic is mostly disjoint, we have created a new module for logic related to the Events API.
  • common.py — This file contains all common functions used by both v2.py and events.py.
  • version.py — Contains version info.

See the examples below to see how this affects how you will instantiate a client in v2.

Examples

Instantiating a client:

Version 1:

import pygerduty
pager = pygerduty.PagerDuty("foobar", "SOMEAPIKEY123456")

Version 2:

import pygerduty.v2
pager = pygerduty.v2.PagerDuty("SOMEAPIKEY123456")

Listing a resource:

for schedule in pager.schedules.list():
    print(schedule.id, schedule.name)

# PX7F8S3 Primary
# PJ48C0S Tertiary
# PCJ94SK Secondary

Getting all schedules associated with a user:

user = pager.users.show('PDSKF08')
for schedule in user.schedules.list():
    print(schedule.id, schedule.name)

# PDSARUD Ops
# PTDSKJH Support

Getting a resource by ID:

schedule = pager.schedules.show("PX7F8S3")

Creating a resource:

user = next(pager.users.list(query="gary", limit=1))
override = schedule.overrides.create(
    start="2012-12-16", end="2012-12-17", user_id=user.id)

Delete a resource:

schedule.overrides.delete(override.id)

Updating a resource:

pagerduty.users.update(user.id, name="Gary Example")

Acknowledging a group by incidents:

me = next(pager.users.list(query="[email protected]", limit=1))
for incident in pagerduty.incidents.list(status='triggered'):
    incident.acknowledge(requester_id=me.id)
Comments
  • New 'self' fields in PagerDuty REST API responses trigger TypeErrors

    New 'self' fields in PagerDuty REST API responses trigger TypeErrors

    PagerDuty have started returning new self fields in their responses. These fields self-reference the canonical URI of the resource in the response, e.g.:

    {
      [...]
      "self": "https://contoso.pagerduty.com/api/v1/escalation_policies/PABCDEF"
    }
    

    This causes obvious problems in pygerduty when the key-values from the response payload are passed to a Container constructor. The problem typically manifests with a stack trace not unlike:

      File "/Users/saj/.pex/install/pygerduty-0.28-py2-none-any.whl.ba6cbb714c785594762b7f0a3df561c0fae3a825/pygerduty-0.28-py2-none-any.whl/pygerduty/__init__.py", line 84, in create
        return self.container(self, **response.get(self.sname, {}))
    TypeError: __init__() got multiple values for keyword argument 'self'
    
    opened by saj 15
  • Cleanup Oncalls and add Tests

    Cleanup Oncalls and add Tests

    Added a unique id to the oncall container which ensures list works with the Collection implemented pagination logic (requires an id).

    Removed the overridden show() method which is inconsistent with how show works on other objects (the oncalls endpoint does not support getting a single oncall, as there is no PagerDuty id for them).

    Also tested that filtering with oncalls.list() works (for example oncalls.list(schedule_ids=['<id>']).

    I also added some tests for the extensions and oncalls objects. I don't think the extensions tests are exhaustive yet, but it's a start. Likely needs more work to ensure create/updating extensions works.

    opened by cugini-dbx 10
  • Added functionality for Incident

    Added functionality for Incident

    Two pieces of valuable information are not included with an incdient:

    1. Whether the summary_details includes a subject or a description
    2. The extra data contained in the details log entry

    With this small code update, an incident is now aware of both of these items. There are two new methods for incidents:

    1. incident.get_detail_log_entry(requester_id) #Returns json of log entry
    2. incident.has_subject() # Boolean return, if no subject, than it has details (incident.trigger_summary_data.subject/details)
    opened by excelciordan 10
  • Consolidate exceptions

    Consolidate exceptions

    I was troubleshooting a bug today in our code and noticed that these exceptions are defined in multiple places. I was attempting to catch pygerduty.v2.BadRequest and was getting pygerduty.common.BadRequest instead.

    These can all be defined in one place and imported. That way, when you throw the exception it is always an instance of the same class.

    Thanks!

    opened by amckenna-pinterest 7
  • Add support for Python 2 and 3

    Add support for Python 2 and 3

    Fixes #22.

    Please note that this pull request will drop support for Python 2.5.

    Tested with:

    • CPython 2.6.9
    • CPython 2.7.10
    • CPython 3.3.6
    • CPython 3.4.3

    I was forced to add a handful of unit tests to ensure basic functionality across the different runtimes. These tests offer minimal coverage. Tests for paginated responses and writes are missing. The tests that do exist may differ from your house style.

    opened by saj 7
  • Events module

    Events module

    @gmjosack in this diff you will see the following changes:

    Format Changes

    • Copied the contents of __init__.py into v2.py to allow for second version of the library.
    • Moved event methods into its own module and updated imports.

    V1-> V2 Changes

    • Changed the base path to: https://api.pagerduty.com/. (line 538)
    • Added version in the request header. (line 22)
    • Removed max number of 403 errors, since in v2 rate limited requests will now return a 429 Too many requests error code instead of 403 Forbidden. They no longer want us to retry a request that returns 403. (line 572)

    Changes to come

    • Updating tests to reflect new structure -- do you have advice on how to do this as to not disrupt the tests for v1? Perhaps a new directory v2_tests/?
    • Removing requester_id and moving this to the header of the request.
    opened by khardsonhurley 6
  • Escalation Policies not loading Escalation Rules

    Escalation Policies not loading Escalation Rules

    I can fetch the escalation Policy correctly however it does not load the rules properly.

    u'escalation_rules': <pygerduty.EscalationRulesobjectat0x10b7f21d0>
    

    I try to loop on it and get the following:

    Traceback (most recent call last):
      File "./verify.py", line 30, in <module>
        for rule in escalation_policy.escalation_rules:
    TypeError: 'EscalationRules' object is not iterable
    

    If I try and print the rules I get back the main escalation policy rather than the rules

    print escalation_policy.escalation_rules.__dict__
    

    Manually fetching the escalation policy via curl returns all the information:

    TOKEN="YOUR-TOKEN-HERE"
    COMPANY="YOUR-COMPANY-NAME-HERE"
    curl -H "Content-type: application/json" -H "Authorization: Token token=${TOKEN}" -X GET -G \
        --data-urlencode "include[]=teams" \
        --data-urlencode "query=API Escalation" \
        "https://${COMPANY}.pagerduty.com/api/v1/escalation_policies"
    

    So from what I can tell its having issues converting the rules.

    Running v0.29.0

    opened by tkensiski 6
  • Fix bug of argument order when raising IntegrationAPIError

    Fix bug of argument order when raising IntegrationAPIError

    The Exception class IntegrationAPIError() is defined like this:

    class IntegrationAPIError(Error):
        def __init__(self, message, event_type):
            self.event_type = event_type
            self.message = message
    

    When raising it IntegrationAPIError() the method PagerDuty.create_event() the parameters were reversed:

    if not response["status"] == "success":
        raise IntegrationAPIError(event_type, response["message"])
    

    Should be:

    if not response["status"] == "success":
        raise IntegrationAPIError(response["message"], event_type)
    

    This PR simply reverses the order of the parameters when raising the exception.

    CC/ @gmjosack

    opened by adnam 5
  • Changes to relieve bugs found during testing

    Changes to relieve bugs found during testing

    • Removed Entities, since this endpoint no longer exists.
    • Added new classes to provide easily readible objects.
    • Took requester out of init in events module.
    opened by khardsonhurley 4
  • getting event details from log_entries

    getting event details from log_entries

    I am new to pygerduty and still exploring.

    I have a case were i need to get the event details from a log entry...

    I know get_trigger_log_entry() gets me the details but the return i am getting is just <LogEntry: contexts=[], created_at=u'2016-05-02T11:42:42Z', agent=<Container: type=u'service'>, type=u'trigger', id=u'someID', channel=<Container: type=u'email', summary=u'SomeSummary'>>

    Is there a way i can completely get this info with pygerduty. The return of incidents.show has the link to the log entry but does not give me the content of it.

    Is there an attribute to get this content that I am missing.

    Can some one please help me with my requirement. Thanks in advance.

    opened by i5uhail 4
  • In order to delete a user, you must first remove them from any schedu…

    In order to delete a user, you must first remove them from any schedu…

    …les they're in. You can find the schedules a particular user is in with api/v1/users/user_id/schedules. This is faster than grabbing all schedules and digging through to find a specific user.

    opened by JohnTheodore 4
  • Add enable extension API

    Add enable extension API

    Hi, I'd like to add support of a relatively new API to enable an extension which is temporarily disabled by PagerDuty.

    API reference: https://developer.pagerduty.com/api-reference/reference/REST/openapiv3.json/paths/~1extensions~1%7Bid%7D~1enable/post

    opened by ymyzk 1
  • Support Collection.count() and exponential-backoff-and-retry

    Support Collection.count() and exponential-backoff-and-retry

    Minor changes in order to better support multi-threading..

    1. External pagination required the Collection.count() method to be updated to support when the base_container is not None (ex: when it is an Incident). This looks like it was probably an accidental oversight, as most other Collection methods already take this into consideration.
    2. Pagination in the REST API v2 does not use the /count endpoint anymore. Though empirical evidence suggests they have not removed support for it on the main incidents list, there are multiple documentation/community references that suggest it is no longer supported.
    3. Threading-off paginated requests in parallel (note to use one PagerDuty instance per thread) rapidly causes requests to run into HTTPError (429) and/or URLError (timeout), eliciting the need for an exponential-backoff-and-retry loop in order to not prematurely break out from iterator/generator loops.
    opened by jjinno 1
  • Need Events API v2 Support

    Need Events API v2 Support

    Need to support Events API v2 (not to be confused with REST API v2). New Events API comes with new Common Event Format payload requirements (PD-CEF). Need to create a Events() implementation to handle the new requirements.

    README should also be updated for reference to the PD-CEF & Events API v2 documentation.

    opened by jjinno 1
  • Generator functions bail on retry-able exceptions

    Generator functions bail on retry-able exceptions

    For tracking purposes.

    The original v1 version of Requester included an additive backoff/retry loop for execute_request(). This logic does not appear to have been ported to the v2 Requester class. Unfortunately, HTTPError(429) and URLError(Timeout) both can raise exceptions that will interrupt an iteration in progress. Because we want to allow uninterrupted iteration over things like Incident.list(), any applicable retries should occur before raising these exceptions.

    This retry logic is especially pertinent for multi-threading, as (for example) a parent thread may iterate over incidents, while child threads iterate over log_entries. There should be an expectation that rapid requests from the child, causing HTTP 429, should not raise an exception in the parent thread iterator as it does today.

    opened by jjinno 1
  • Bad URL: /incidents/{ID}/log_entries/count

    Bad URL: /incidents/{ID}/log_entries/count

    For tracking purposes.

    The v2 REST API no longer officially supports the /count path. Although empirical evidence would suggest it still exists for /incidents/count, there is no documentation supporting this, and plenty of support conversations suggesting otherwise. This is especially true for sub-container elements such as Incident.log_entries

    To be v2 compliant, all count() calls should be changed to use the documented total=true parameter on a single paginated request. The use of limit=1 should theoretically allow for the smallest possible request, though some testing on older incidents seems to suggest that this must be limit=2 or higher to avoid HTTP errors.

    opened by jjinno 1
Owner
Dropbox
Dropbox
alpaca-trade-api-python is a python library for the Alpaca Commission Free Trading API.

alpaca-trade-api-python is a python library for the Alpaca Commission Free Trading API. It allows rapid trading algo development easily, with support for both REST and streaming data interfaces

Alpaca 1.5k Jan 9, 2023
🐍 The official Python client library for Google's discovery based APIs.

Google API Client This is the Python client library for Google's discovery based APIs. To get started, please see the docs folder. These client librar

Google APIs 6.2k Jan 8, 2023
Python bindings for ArrayFire: A general purpose GPU library.

ArrayFire Python Bindings ArrayFire is a high performance library for parallel computing with an easy-to-use API. It enables users to write scientific

ArrayFire 402 Dec 20, 2022
Balanced API library in python.

Balanced Online Marketplace Payments v1.x requires Balanced API 1.1. Use v0.x for Balanced API 1.0. Installation pip install balanced Usage View Bala

Balanced 70 Oct 4, 2022
python library to the bitly api

bitly API python library Installation pip install bitly_api Run tests Your username is the lowercase name shown when you login to bitly, your access

Bitly 245 Aug 14, 2022
Braintree Python library

Braintree Python library The Braintree Python library provides integration access to the Braintree Gateway. TLS 1.2 required The Payment Card Industry

Braintree 230 Dec 18, 2022
A Python library for the Buildkite API

PyBuildkite A Python library and client for the Buildkite API. Usage To get the package, execute: pip install pybuildkite Then set up an instance of

Peter Yasi 29 Nov 30, 2022
Unofficial Coinbase Python Library

Unofficial Coinbase Python Library Python Library for the Coinbase API for use with three legged oAuth2 and classic API key usage Version 0.3.0 Requir

George Sibble 104 Dec 1, 2022
A Python library for the Discourse API

pydiscourse A Python library for working with Discourse. This is a fork of the original Tindie version. It was forked to include fixes, additional fun

Ben Lopatin 72 Oct 14, 2022
A Python library for the Docker Engine API

Docker SDK for Python A Python library for the Docker Engine API. It lets you do anything the docker command does, but from within Python apps – run c

Docker 6.1k Jan 3, 2023
A Python Library to interface with Flickr REST API, OAuth & JSON Responses

Python-Flickr Python-Flickr is A Python library to interface with Flickr REST API & OAuth Features Photo Uploading Retrieve user information Common Fl

Mike Helmick 40 Sep 25, 2021
Unofficial GoPro API Library for Python - connect to GoPro via WiFi.

GoPro API for Python Unofficial GoPro API Library for Python - connect to GoPro cameras via WiFi. Compatibility: HERO3 HERO3+ HERO4 (including HERO Se

Konrad Iturbe 1.3k Jan 1, 2023
A Python library wrapping the iFixit (Dozuki) API.

A Python library wrapping the iFixit API. Status Working, but incomplete. Fully tested and documented. Hacking I highly recommend using virtualenv: [$

James Pearson Hughes 13 May 24, 2021
:snake: A simple library to fetch data from the iTunes Store API made for Python >= 3.5

itunespy itunespy is a simple library to fetch data from the iTunes Store API made for Python 3.5 and beyond. Important: Since version 1.6 itunespy no

Fran González 56 Dec 22, 2022
Python JIRA Library is the easiest way to automate JIRA. Support for py27 was dropped on 2019-10-14, do not raise bugs related to it.

Jira Python Library This library eases the use of the Jira REST API from Python and it has been used in production for years. As this is an open-sourc

PyContribs 1.7k Jan 6, 2023
The official Python client library for the Kite Connect trading APIs

The Kite Connect API Python client - v3 The official Python client for communicating with the Kite Connect API. Kite Connect is a set of REST-like API

Zerodha Technology 756 Jan 6, 2023
A Python Library to interface with LinkedIn API, OAuth and JSON responses

#Overview Here's another library based on the LinkedIn API, OAuth and JSON responses. Hope this documentation explains everything you need to get star

Mike Helmick 69 Dec 11, 2022
PyMed is a Python library that provides access to PubMed.

IMPORTANT NOTE: I don't have time to maintain this library (as some of you might have noticed). The PubMed API is a little chaotic, without a clear do

Gijs Wobben 143 Dec 21, 2022
The Official Twilio SendGrid Led, Community Driven Python API Library

The default branch name for this repository has been changed to main as of 07/27/2020. This library allows you to quickly and easily use the SendGrid

Twilio SendGrid 1.4k Jan 7, 2023