Skip to content
Browse files

removed double check of POST and f.close line not needed.

  • Loading branch information...
1 parent c64f255 commit 6217f24b5920d63f34ddf93f2669dec68200c4bc @orangethirty committed Mar 1, 2013
Showing with 35 additions and 49 deletions.
  1. +0 −3 utils.py
  2. +35 −46 web2txt.py
View
3 utils.py
@@ -6,15 +6,13 @@ def load_config():
"""returns configuration as dictionary"""
with open('config.json', 'r') as f:
config = json.load(f)
- f.close()
return config
def load_carriers():
"""loads the carriers from carriers.json"""
with open('carriers.json', 'r') as f:
carriers = json.load(f)
- f.close()
return carriers
@@ -23,4 +21,3 @@ def logging(log):
with open('web2txt.log', 'a') as f:
debug_log = "web2txt debug message: {0} * {1} \n".format(strftime("%Y-%m-%d %H:%M:%S"), log)
f.write(debug_log)
- f.close
View
81 web2txt.py
@@ -23,66 +23,55 @@ def index():
@app.route("/text", methods=['POST'])
def send_text():
"""Sends the txt message from data passed through POST."""
-
- if request.method == 'POST':
+
+ if request.headers['Content-Type'] == 'application/json':
+ #converts json to python dict
+ data = request.json
+ #get list of carriers from carriers.json
+ config = load_config()
+ carriers = load_carriers()
- if request.headers['Content-Type'] == 'application/json':
- #converts json to python dict
- data = request.json
- #get list of carriers from carriers.json
- config = load_config()
- carriers = load_carriers()
-
- #authenticate request
- if data['api_key'] == config['api_key']:
-
- if data['carrier'] in carriers:
- #prepare the message
- carrier_choice = data['carrier']
- carrier = carriers[carrier_choice]
- number = data['number']
- msg = data['msg']
- to = "{0}{1}".format(number, carrier)
- sender = config['from']
- #sends the actual message
- mail = smtplib.SMTP(config['smtp_address'])
- mail.starttls()
- mail.login(config['username'], config['password'])
- mail.sendmail(sender, to, msg)
- mail.quit()
- #prepare the json response.
- log = "Message: '{0}' was sent succesfuly sent to '{1}'.".format(msg, to)
- logging(log)
- resp = {"response" : log}
- response = Response(json.dumps(resp), status=200, mimetype='application/json')
- return response
+ #authenticate request
+ if data['api_key'] == config['api_key']:
- #if the carrier is not supported or found in the carriers list.
- else:
- log = "Carrier not supported."
- #log to web2txt.log file
+ if data['carrier'] in carriers:
+ #prepare the message
+ carrier_choice = data['carrier']
+ carrier = carriers[carrier_choice]
+ number = data['number']
+ msg = data['msg']
+ to = "{0}{1}".format(number, carrier)
+ sender = config['from']
+ #sends the actual message
+ mail = smtplib.SMTP(config['smtp_address'])
+ mail.starttls()
+ mail.login(config['username'], config['password'])
+ mail.sendmail(sender, to, msg)
+ mail.quit()
+ #prepare the json response.
+ log = "Message: '{0}' was sent succesfuly sent to '{1}'.".format(msg, to)
logging(log)
resp = {"response" : log}
- response = Response(json.dumps(resp), status=404, mimetype='application/json')
+ response = Response(json.dumps(resp), status=200, mimetype='application/json')
return response
- #if the content type is not json
- else:
- log = "Wrong request content-type. API only support JSON"
+ #if the carrier is not supported or found in the carriers list.
+ else:
+ log = "Carrier not supported."
#log to web2txt.log file
logging(log)
resp = {"response" : log}
- response = Response(json.dumps(resp), status=415, mimetype='application/json')
- return response
+ response = Response(json.dumps(resp), status=404, mimetype='application/json')
+ return response
- #if the request is not a POST. note that flask handles this but included anyways.
+ #if the content type is not json
else:
- log = "Method Not Allowed. The method GET is not allowed for the requested URL."
+ log = "Wrong request content-type. API only support JSON"
#log to web2txt.log file
logging(log)
resp = {"response" : log}
- response = Response(json.dumps(resp), status=405, mimetype='application/json')
- return response
+ response = Response(json.dumps(resp), status=415, mimetype='application/json')
+ return response
if __name__ == "__main__":

0 comments on commit 6217f24

Please sign in to comment.
Something went wrong with that request. Please try again.