Skip to content

Commit

Permalink
Fix header lookup (#415)
Browse files Browse the repository at this point in the history
* Fix header lookup

* Changelog

* Value is also bytes

* Missing else statement

* Incorporate review comments
  • Loading branch information
babolivier authored Oct 8, 2021
1 parent 0fc877f commit 26d858f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
1 change: 1 addition & 0 deletions changelog.d/415.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a long-standing bug with error handling around missing headers when dealing with the OpenMarket API, which could cause the wrong assumption that sending a SMS failed when it didn't.
30 changes: 18 additions & 12 deletions sydent/sms/openmarket.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ async def sendTextSMS(
headers = dict(resp.headers.getAllRawHeaders())

request_id = None
if "X-Request-Id" in headers:
request_id = headers["X-Request-Id"][0]
if b"X-Request-Id" in headers:
request_id = headers[b"X-Request-Id"][0].decode("UTF-8")

# Catch errors from the API. The documentation says a success code should be 202
# Accepted, but let's be broad here just in case and accept all 2xx response
Expand Down Expand Up @@ -132,20 +132,26 @@ async def sendTextSMS(
),
)

if "Location" not in headers:
raise Exception("Got response from sending SMS with no location header")
# Nominally we should parse the URL, but we can just split on '/' since
# we only care about the last part.
parts = headers["Location"][0].split("/")
if len(parts) < 2:
raise Exception(
"Got response from sending SMS with malformed location header"
)
ticket_id = None
if b"Location" not in headers:
logger.error("Got response from sending SMS with no location header")
else:
# Nominally we should parse the URL, but we can just split on '/' since
# we only care about the last part.
value = headers[b"Location"][0].decode("UTF-8")
parts = value.split("/")
if len(parts) < 2:
logger.error(
"Got response from sending SMS with malformed location header: %s",
value,
)
else:
ticket_id = parts[-1]

logger.info(
"Successfully sent SMS (ticket ID: %s, request ID %s), OpenMarket API"
" responded with code %d",
parts[-1],
ticket_id,
request_id,
resp.code,
)

0 comments on commit 26d858f

Please sign in to comment.