Skip to content

Commit

Permalink
Merge pull request #10 from kevinjqiu/refactoring-resources
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
kevinjqiu committed Sep 11, 2014
2 parents b6762a5 + 5bc202a commit 65f265c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 24 deletions.
33 changes: 33 additions & 0 deletions mailchute/api/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import bottle
from bottle import response
from mailchute.api import resource


def _route(app, uri, methods, handler):
app.route(uri, ['OPTIONS'])(lambda *a, **kw: {})
app.route(uri, methods)(handler)


def create_app():
app = bottle.app()
_route(app, '/emails', ['GET'], resource.get_emails)
_route(
app, '/emails/<email_id:int>', ['DELETE'],
resource.delete_email)
_route(
app, '/raw_messages/<raw_message_id>', ['GET'],
resource.get_raw_message)

@app.hook('after_request')
def enable_cors():
ALLOWED_METHODS = 'PUT, GET, POST, DELETE, OPTIONS'
ALLOWED_HEADERS = \
'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = ALLOWED_METHODS
response.headers['Access-Control-Allow-Headers'] = ALLOWED_HEADERS

return app


app = create_app()
6 changes: 3 additions & 3 deletions mailchute/api/cli.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import bottle
from mailchute import settings
from mailchute.api.resource import app
from mailchute.api.app import app


def main(): # pragma: no cover
import mailchute.api.resource as _
def main(): # pragma: no cover
# import mailchute.api.resource as _
bottle.run(
app,
host=settings.API['host'],
Expand Down
19 changes: 0 additions & 19 deletions mailchute/api/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
response, IncomingEmailDTO, RawMessageDTO)


app = bottle.app()


app.route('/emails', ['OPTIONS'])(lambda: {})
@app.route('/emails', ['GET'])
@response('emails', IncomingEmailDTO)
def get_emails():
inbox = bottle.request.query.get('inbox', None)
Expand All @@ -28,8 +23,6 @@ def get_emails():
return emails


app.route('/emails/<email_id:int>', ['OPTIONS'])(lambda *a, **kw: {})
@app.route('/emails/<email_id:int>', ['DELETE'])
@response('emails', None)
def delete_email(email_id):
if not db.session.query(IncomingEmail).filter_by(id=email_id).count():
Expand All @@ -41,8 +34,6 @@ def delete_email(email_id):
db.session.commit()


app.route('/raw_messages/<raw_message_id>', ['OPTIONS'])(lambda *a, **kw: {})
@app.route('/raw_messages/<raw_message_id>', ['GET'])
@response('raw_messages', RawMessageDTO)
def get_raw_message(raw_message_id):
try:
Expand All @@ -53,13 +44,3 @@ def get_raw_message(raw_message_id):
)
except sqlalchemy.orm.exc.NoResultFound:
raise NotFound()


@app.hook('after_request')
def enable_cors():
ALLOWED_METHODS = 'PUT, GET, POST, DELETE, OPTIONS'
ALLOWED_HEADERS = \
'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'
bottle.response.headers['Access-Control-Allow-Origin'] = '*'
bottle.response.headers['Access-Control-Allow-Methods'] = ALLOWED_METHODS
bottle.response.headers['Access-Control-Allow-Headers'] = ALLOWED_HEADERS
2 changes: 1 addition & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from webtest import TestApp
from tests.base import BaseTestCase, Fixture

from mailchute.api.resource import app
from mailchute.api.app import app
from mailchute.api.cli import main as api_main
from mailchute.db import session
from mailchute.model import IncomingEmail
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from webtest import TestApp
from mailchute.api.resource import app
from mailchute.api.app import app


app = TestApp(app)
Expand Down

0 comments on commit 65f265c

Please sign in to comment.