From 6ce4729f273d3cce432033e627eabf4f3aba031f Mon Sep 17 00:00:00 2001
From: Anton Sarukhanov <code@ant.sr>
Date: Thu, 14 Jul 2016 23:33:05 -0400
Subject: [PATCH] Cleanup

---
 README.md                               |   2 +-
 api/.app.py.swo                         | Bin 0 -> 12288 bytes
 api/__init__.py                         |   0
 api/api.py                              |  12 ++++++++++++
 app.py => api/app.py                    |   8 +++++++-
 decorators.py => api/decorators.py      |   0
 strava.py => api/strava.py              |  11 +++++------
 {templates => api/templates}/base.html  |   0
 {templates => api/templates}/index.html |   0
 util.py => api/util.py                  |   5 ++---
 config.py.dist                          |   4 ----
 run.py                                  |   4 ++++
 12 files changed, 31 insertions(+), 15 deletions(-)
 create mode 100644 api/.app.py.swo
 create mode 100644 api/__init__.py
 create mode 100644 api/api.py
 rename app.py => api/app.py (73%)
 rename decorators.py => api/decorators.py (100%)
 rename strava.py => api/strava.py (81%)
 rename {templates => api/templates}/base.html (100%)
 rename {templates => api/templates}/index.html (100%)
 rename util.py => api/util.py (59%)
 delete mode 100644 config.py.dist
 create mode 100644 run.py

diff --git a/README.md b/README.md
index 5e58d82..a25dfde 100644
--- a/README.md
+++ b/README.md
@@ -10,5 +10,5 @@ dynamic/interactive parts of [ant.sr](https://ant.sr).
 
 # Development Server
 
-* `flask run`
+* `./run.py`
 * Visit [localhost:5000](http://127.0.0.1:5000).
diff --git a/api/.app.py.swo b/api/.app.py.swo
new file mode 100644
index 0000000000000000000000000000000000000000..f0d79534215d138ef7a155ac0b1f677cbbdecd18
GIT binary patch
literal 12288
zcmeI2J#W-N5Qg`tC<q@3JqC*Sq&pJ`A_(PBQh-E3#mCVt8+)Bw;*ZsOedH)eO92uM
zzk$HtfQtMQBuYvOUaxJ)U2{mOPRvTL);lvhGyANY(|Kp@>4S&fMsSm1oM&uv_-2@#
z{=xd|jET(VS!JIyVxyv6IVTA2*Dm2Tp;aRt=*j$T6hQ=tz)1;|HtyeEV(T}quT`n8
zUb*b8EI&DER@zSlhyW2F0z`la5CI}U1c<-_5wN<$4$$wZrsqq|+?ko1B~3(t2oM1x
zKm>>Y5g-CYfCvx)B0vO)zycBw5o4duFt)vf=JEf3`}_avImSLi@1VEP5V{2|Ll>Y9
z^z$rZN6<Iu3v>v5fL=ml=oxem>O&Wy73df0e20#pL+B&)9y)-YL#~en<VY<O0U|&I
zhyW2F0z`la5CJ0a9}w^`jI^c6yhw%CFRT$`F{r05tK5z0k@E!MNQQe{ly>BIStMgG
z)Vz-HyLZ@@&^ie7%*uVswaG(S6uDt8&+#&<;37&@HfSH}QnATXue2)hRH}1hz1H1U
zYS;f*Yg;QH-)r+sr1Dq=nA!q}aJgan8`+%0<fWDVs{7h|vrTh%wL5j321^AbH|mva
zsMJUrPBZzA`H*bOftbo^WEM%otxR<ysumR1`@zVj2{u&Fo#79Jw$ZA!<^p218&ov8
zp2R9a3S3HM(-Q>E#^gIMB<{Y6sRO)$cRvxup3faU&U+|YSdoR28;Q2mSn@jQ;IS!z
zm$}=EWSARab5pd4>+?Bhee%{(qpP>YRjt<H3it3{M#`YE>S+o{&nr#BW4u_u0mh^o
Aw*UYD

literal 0
HcmV?d00001

diff --git a/api/__init__.py b/api/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/api/api.py b/api/api.py
new file mode 100644
index 0000000..dbeb7e9
--- /dev/null
+++ b/api/api.py
@@ -0,0 +1,12 @@
+import json
+from strava import Strava
+
+class Api():
+    """API interfaces"""
+    def __init__(self, api_name):
+        def strava():
+            """Strava API - get my recent rides"""
+            s = Strava()
+            return json.dumps(s.get_athlete())
+        functions = {name:f for (name, f) in locals() if l != 'self'}
+        return functions[api_name]
diff --git a/app.py b/api/app.py
similarity index 73%
rename from app.py
rename to api/app.py
index 404dd4c..fd1a6fa 100644
--- a/app.py
+++ b/api/app.py
@@ -2,8 +2,9 @@ from flask import Flask, redirect, render_template, url_for
 from strava import Strava
 from decorators import admin
 
-app = Flask(__name__)
+app = Flask(__name__, instance_relative_config=True)
 app.config.from_object('config')
+app.config.from_pyfile('config.py')
 
 @app.route('/')
 def index():
@@ -14,6 +15,11 @@ def index():
 def strava_auth():
     return Strava.authorize()
 
+@app.route('/api/<api_name>')
+def api(api_name):
+    from api import Api
+    return Api(api_name)
+
 @app.context_processor
 def cp_is_admin():
     from util import is_admin
diff --git a/decorators.py b/api/decorators.py
similarity index 100%
rename from decorators.py
rename to api/decorators.py
diff --git a/strava.py b/api/strava.py
similarity index 81%
rename from strava.py
rename to api/strava.py
index 52785d6..434675c 100644
--- a/strava.py
+++ b/api/strava.py
@@ -1,7 +1,6 @@
-from flask import request, redirect
+from flask import request, redirect, current_app
 from stravalib.client import Client
 from requests.exceptions import HTTPError
-import config
 
 class Strava():
     @classmethod
@@ -14,7 +13,7 @@ class Strava():
             return strava.get_athlete().email
         except (HTTPError, AttributeError):
             return False
-        
+
     @classmethod
     def authorize(cls, code = None):
         """Redirect to Strava to get an access token."""
@@ -24,14 +23,14 @@ class Strava():
             source_url = request.referrer
             authorized_url = request.url
             authorize_url = strava.authorization_url(
-                client_id=config.STRAVA_APP_ID, redirect_uri=authorized_url, state=source_url)
+                client_id=current_app.config['STRAVA_APP_ID'], redirect_uri=authorized_url, state=source_url)
             return redirect(authorize_url)
         else:
             source_url = request.args.get('state', None)
             strava = Client()
             access_token = strava.exchange_code_for_token(
-                client_id=config.STRAVA_APP_ID,
-                client_secret=config.STRAVA_APP_SECRET,
+                client_id=current_app.config['STRAVA_APP_ID'],
+                client_secret=current_app.config['STRAVA_APP_SECRET'],
                 code=code)
             strava.access_token = access_token
             # TODO: store the access token
diff --git a/templates/base.html b/api/templates/base.html
similarity index 100%
rename from templates/base.html
rename to api/templates/base.html
diff --git a/templates/index.html b/api/templates/index.html
similarity index 100%
rename from templates/index.html
rename to api/templates/index.html
diff --git a/util.py b/api/util.py
similarity index 59%
rename from util.py
rename to api/util.py
index a42b05f..bdbbcc4 100644
--- a/util.py
+++ b/api/util.py
@@ -1,9 +1,8 @@
-from flask import request
-import config
+from flask import request, current_app
 
 def is_admin():
     try:
-        admin_ip = config.ADMIN_IP
+        admin_ip = current_app.config.ADMIN_IP
     except AttributeError:
         admin_ip = '127.0.0.1'
     return request.remote_addr == admin_ip
diff --git a/config.py.dist b/config.py.dist
deleted file mode 100644
index 01cdaf9..0000000
--- a/config.py.dist
+++ /dev/null
@@ -1,4 +0,0 @@
-STRAVA_APP_ID = '12345'
-STRAVA_APP_SECRET = 'your-strava-app-secret'
-ADMIN_IP = '127.0.0.1'
-
diff --git a/run.py b/run.py
new file mode 100644
index 0000000..53c616f
--- /dev/null
+++ b/run.py
@@ -0,0 +1,4 @@
+#!/usr/bin/env python
+from api.app import app
+
+app.run()
-- 
GitLab