I've written this snippet to extract a review request status from review-board in python. It works well but i'm very new to python and i would like to know if i could write this any better.
Should i put conn.close
in it's own try statement ?
This script is supposed to run as part of a post-commit hook in mercurial. If the review-board server is not responding i still want to be able to commit without any issues. If the commit is non existent then an empty string is ok.
import httplib
import json
def getReviewStatus(reviewboardUrl, id):
try:
conn = httplib.HTTPConnection(reviewboardUrl, 80)
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json; charset=UTF-8'
}
conn.request("GET", "/api/review-requests/%s/" % id, headers=headers)
return json.loads(conn.getresponse().read())['review_request']['status']
except:
return ""
finally:
conn.close()