Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, api_key, name=None):
"""
Basic constructor for API sessions.
:param api_key:
The API credential to use.
:param name:
Identifying label for the session to use in log messages
"""
self.parent = super(PDSession, self)
self.parent.__init__()
self.api_key = api_key
if isinstance(name, string_types) and name:
my_name = name
else:
my_name = self.trunc_key
self.log = logging.getLogger('pdpyras.%s(%s)'%(
self.__class__.__name__, my_name))
self.retry = {}
raise ValueError(local+" must be a dict")
event = {'payload': {'summary':summary, 'source':source,
'severity':severity}}
if type(payload) is dict:
event['payload'].update(payload)
if type(custom_details) is dict:
details = event.setdefault('payload', {}).get('custom_details', {})
details.update(custom_details)
event['payload']['custom_details'] = details
if images:
event['images'] = images
if links:
event['links'] = links
return self.send_event('trigger', dedup_key=dedup_key, **event)
class APISession(PDSession):
"""
Reusable PagerDuty REST API session objects for making API requests.
Includes some convenience functions as well, i.e. :attr:`rget`, :attr:`find`
and :attr:`iter_all`, to eliminate some repetitive tasks associated with
making API calls.
Inherits from :class:`PDSession`.
:param api_key:
REST API access token to use for HTTP requests
:param name:
Optional name identifier for logging. If unspecified or ``None``, it
will be the last four characters of the REST API token.
:param default_from:
Email address of a valid PagerDuty user to use in API requests by
@property
def trunc_key(self):
"""Truncated key for secure display/identification purposes."""
return last_4(self.api_key)
@property
def user_agent(self):
return 'pdpyras/%s python-requests/%s Python/%d.%d'%(
__version__,
requests.__version__,
sys.version_info.major,
sys.version_info.minor
)
class EventsAPISession(PDSession):
"""
Session class for submitting events to the PagerDuty v2 Events API.
Provides methods for submitting events to the Events API.
Inherits from :class:`PDSession`.
"""
permitted_methods = ('POST',)
url = "https://events.pagerduty.com"
@property
def auth_header(self):
return {'X-Routing-Key': self.api_key}