Skip to content

Commit

Permalink
リファクタ、コードの整理。
Browse files Browse the repository at this point in the history
  • Loading branch information
Shigeru KANEMOTO committed Mar 24, 2011
1 parent 7febc85 commit 47bc290
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 29 deletions.
46 changes: 19 additions & 27 deletions application.py
Expand Up @@ -9,7 +9,8 @@
app.debug = True

from google.appengine.ext import db
import datetime, re
import datetime
import re
import tepco

################################################################################
Expand Down Expand Up @@ -116,38 +117,29 @@ def dict_from_usage(usage):
'capacity_updated': str(usage.capacity_updated),
}

def resultHundler(obj):
if not obj:
abort(404)
RE_CALLBACK = re.compile(r'^[a-zA-Z0-9_.]+$')

if type(obj).__name__ == 'list' and len(obj) == 0:
def resultHandler(data):
if not data:
abort(404)

_callback = None
if request.method == "POST" and request.form.get("callback") is not None:
_callback = request.form.get("callback")
elif request.args.get("callback") is not None:
_callback = request.args.get("callback")

ret = None
if _callback is not None and re.search(r'^[a-zA-Z0-9]+$', _callback):
# XXX security risk
if type(obj).__name__ == 'list':
ret = Response("%s(%s);"%(_callback, json.dumps(obj, indent=2)), mimetype='application/json')
else:
ret = Response("%s(%s);"%(_callback, json.dumps(dict_from_usage(obj), indent=2)), mimetype='application/json')
elif type(obj).__name__ == 'list' :
# XXX security risk
ret = Response(json.dumps(obj, indent=2), mimetype='application/json')
if request.method == 'POST':
callback = request.form.get('callback')
else:
ret = jsonify(dict_from_usage(obj))
callback = request.args.get('callback')
if callback and not RE_CALLBACK.search(callback):
abort(404)

return ret;
data = json.dumps(data, indent=2)
if callback:
return Response('%s(%s);' % (callback, data), mimetype='text/javascript')
else:
return Response(data, mimetype='application/json')

@app.route('/latest.json')
def latest():
usage = Usage.all().order('-entryfor').get()
return resultHundler(usage)
return resultHandler(dict_from_usage(usage))

@app.route('/<int:year>/<int:month>/<int:day>/<int:hour>.json')
def hour(year, month, day, hour):
Expand All @@ -157,7 +149,7 @@ def hour(year, month, day, hour):
usage = usage.filter('day =', day)
usage = usage.filter('hour =', hour)
usage = usage.get()
return resultHundler(usage)
return resultHandler(dict_from_usage(usage))

@app.route('/<int:year>/<int:month>/<int:day>.json')
def day(year, month, day):
Expand All @@ -167,7 +159,7 @@ def day(year, month, day):
usage = usage.filter('day =', day)
usage = usage.order('entryfor')
usage = [dict_from_usage(u) for u in usage]
return resultHundler(usage)
return resultHandler(usage)

@app.route('/<int:year>/<int:month>.json')
def month(year, month):
Expand All @@ -176,5 +168,5 @@ def month(year, month):
usage = usage.filter('month =', month)
usage = usage.order('entryfor')
usage = [dict_from_usage(u) for u in usage]
return resultHundler(usage)
return resultHandler(usage)

Empty file modified crossdomain.xml 100755 → 100644
Empty file.
7 changes: 5 additions & 2 deletions templates/top.html
Expand Up @@ -88,8 +88,8 @@ <h2>APIの例</h2>
<h2>APIの使い方</h2>
<p>
<ul>
<li>URLに日時を与えて、単純にGETしてください</li>
<li>URLの最後に「?callback=<i>コールバック関数名</i>」を付けると、JSONPになります。</li>
<li>URLに日時を与えて、GETまたはPOSTしてください</li>
<li>URLの最後に「?callback=<i>コールバック関数名</i>」を付けると、JSONPになります。関数名に使える文字は、英数字と「_」と「.」です。それ以外の文字が入っていると404のエラーになります。</li>
<li>データが存在しない場合には404のエラーになります。</li>
<li>Adobe Flashのために、「/crossdomain.xml」が有ります。</li>
</ul>
Expand Down Expand Up @@ -136,6 +136,9 @@ <h2>メモ</h2>
<h2>履歴</h2>
<p>
<ul>
<li>
2011-3-24 <a href="https://github.com/withgod">withgodさん</a>からの<a href="https://github.com/sgk/tepco-usage-api/pull/1">プルリクエスト</a>をマージしました。JSONPのcallback関数名をチェックします。POSTも使えるようにしました。
</li>
<li>
2011-3-24 JSONPに対応しました。
Adobe Flashのための「/crossdomain.xml」を置きました。
Expand Down

0 comments on commit 47bc290

Please sign in to comment.