Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #102 from codelabsab/check_http_code
Browse files Browse the repository at this point in the history
Check response from request when adding event
  • Loading branch information
parberge authored Sep 4, 2019
2 parents 8c9a3aa + 63f1e80 commit 4c94c46
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
10 changes: 8 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,14 @@ def interactive():
if payload.get('callback_id') == 'add':
events = json_factory(payload)
for event in events:
post_event(f"{config['backend_url']}/event/users/{user_id}", json.dumps(event))
logger.info(f"python url is: {config['backend_url']}")
response = post_event(f"{config['backend_url']}/event/users/{user_id}", json.dumps(event))

if response.status_code != 200:
logger.debug(
f"Event {event} got unexpected response from backend: {response.text}"
)
slack_responder(url=response_url, msg=f'Failed to create event {event}')

slack_responder(url=response_url, msg='Added successfully')
return ''
else:
Expand Down
17 changes: 7 additions & 10 deletions chalicelib/lib/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@

def post_event(url, data):
"""
Uses new python chalice backend
only returns back at this point, no DB connection
Add event
:method: POST
:url: '/event'
:return: current_request.raw_body.decode() (same data you send in)
url: URL to backend API
data: A dict with the event to add
:return: requests response object
"""
headers={'Content-Type': 'application/json'}
headers = {'Content-Type': 'application/json'}
res = requests.post(url=url, json=data, headers=headers)
if res.status_code == 200:
return True
else:
return False
return res
7 changes: 4 additions & 3 deletions tests/test_timereport.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def test_create_event():
when(requests).post(
url=fake_url, json=fake_data, headers={"Content-Type": "application/json"}
).thenReturn(mock({"status_code": 200}))

assert post_event(fake_url, fake_data) is True
response = post_event(fake_url, fake_data)
assert response.status_code == 200
unstub()


Expand All @@ -80,7 +80,8 @@ def test_create_event_failure():
when(requests).post(
url=fake_url, json=fake_data, headers={"Content-Type": "application/json"}
).thenReturn(mock({"status_code": 500}))
assert post_event(fake_url, fake_data) is False
response = post_event(fake_url, fake_data)
assert response.status_code != 200
unstub()


Expand Down

0 comments on commit 4c94c46

Please sign in to comment.