Skip to content

Commit

Permalink
Change how routing key is included in event payloads
Browse files Browse the repository at this point in the history
Including the routing key via the `send_event` method broke some downstream usage of the client, which depended on being able to call `post` directly with an explicitly-defined payload that lacked a routing key (which was possible when using the header).

To remedy this, the `post` method is overridden in EventsAPIClient to add the `routing_key` parameter to the body before sending.
  • Loading branch information
Deconstrained committed Mar 18, 2021
1 parent 09bd627 commit f3ce00d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
15 changes: 11 additions & 4 deletions pdpyras.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,10 +697,7 @@ def send_event(self, action, dedup_key=None, **properties):
if action not in actions:
raise ValueError("Event action must be one of: "+', '.join(actions))

event = {
'event_action':action,
'routing_key': self.api_key
}
event = {'event_action':action}

event.update(properties)
if isinstance(dedup_key, string_types):
Expand All @@ -716,6 +713,16 @@ def send_event(self, action, dedup_key=None, **properties):
"deduplication key.", response=response)
return response_body['dedup_key']

def post(self, *args, **kw):
"""
Override of ``requests.Session.post``
Adds the ``routing_key`` parameter to the body before sending.
"""
if 'json' in kw and hasattr(kw['json'], 'update'):
kw['json'].update({'routing_key': self.api_key})
return super(EventsAPISession, self).post(*args, **kw)

def trigger(self, summary, source, dedup_key=None, severity='critical',
payload=None, custom_details=None, images=None, links=None):
"""
Expand Down
20 changes: 20 additions & 0 deletions test_pdpyras.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,26 @@ def test_send_event(self):
},
parent.request.call_args[1]['json'])

def test_send_explicit_event(self):
# test sending an event by calling `post` directly as opposed to any of
# the methods written into the client for sending events
sess = pdpyras.EventsAPISession('routingkey')
parent = MagicMock()
parent.request = MagicMock()
parent.request.side_effect = [Response(202, '{"dedup_key":"abc123"}')]
with patch.object(sess, 'parent', new=parent):
response = sess.post('/v2/enqueue', json={
'payload': {
'summary': 'testing 123',
'source': 'pdpyras integration',
'severity': 'critical'
},
'event_action': 'trigger'
})
json_sent = parent.request.call_args[1]['json']
self.assertTrue('routing_key' in json_sent)
self.assertEqual(json_sent['routing_key'], 'routingkey')

class APISessionTest(SessionTest):

def debug(self, sess):
Expand Down

0 comments on commit f3ce00d

Please sign in to comment.