|  |  |  | @ -1,13 +1,8 @@ | 
		
	
		
			
				|  |  |  |  | from apistar import Include, Route, http | 
		
	
		
			
				|  |  |  |  | from apistar.interfaces import Router | 
		
	
		
			
				|  |  |  |  | from apistar.frameworks.wsgi import WSGIApp as App | 
		
	
		
			
				|  |  |  |  | from apistar.backends import sqlalchemy_backend | 
		
	
		
			
				|  |  |  |  | from apistar.backends.sqlalchemy_backend import Session | 
		
	
		
			
				|  |  |  |  | from apistar.handlers import docs_urls, static_urls | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | from .models import Base, NewsArticle, NewsSource | 
		
	
		
			
				|  |  |  |  | from .renders import JSONRenderer | 
		
	
		
			
				|  |  |  |  | from apistar import Route, http, App | 
		
	
		
			
				|  |  |  |  | from sqlalchemy import create_engine | 
		
	
		
			
				|  |  |  |  | from .models import NewsArticle, NewsSource | 
		
	
		
			
				|  |  |  |  | from .schema import NewsSourceSchema, NewsArticleSchema | 
		
	
		
			
				|  |  |  |  | from .util import Session, SQLAlchemySession, SQLAlchemyHook | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | news_source_schema = NewsSourceSchema() | 
		
	
		
			
				|  |  |  |  | news_article_schema = NewsArticleSchema() | 
		
	
	
		
			
				
					|  |  |  | @ -15,22 +10,22 @@ news_article_schema = NewsArticleSchema() | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | def get_sources(session: Session): | 
		
	
		
			
				|  |  |  |  |     sources = session.query(NewsSource).all() | 
		
	
		
			
				|  |  |  |  |     return http.Response(news_source_schema.dump(sources, many=True).data, status=200) | 
		
	
		
			
				|  |  |  |  |     return http.JSONResponse(news_source_schema.dump(sources, many=True).data, status_code=200) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | def add_source(session: Session, request_data: http.RequestData, router: Router): | 
		
	
		
			
				|  |  |  |  | def add_source(session: Session, request_data: http.RequestData, app: App): | 
		
	
		
			
				|  |  |  |  |     news_source_data, errors = news_source_schema.load(request_data) | 
		
	
		
			
				|  |  |  |  |     if errors: | 
		
	
		
			
				|  |  |  |  |         msg = {"message": "400 Bad Request", "error": errors} | 
		
	
		
			
				|  |  |  |  |         return http.Response(msg, status=400) | 
		
	
		
			
				|  |  |  |  |         return http.JSONResponse(msg, status_code=400) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     news_source = NewsSource(**news_source_data) | 
		
	
		
			
				|  |  |  |  |     session.add(news_source) | 
		
	
		
			
				|  |  |  |  |     session.flush() | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     headers = {"Location": router.reverse_url('get_source', {"id": news_source.id})} | 
		
	
		
			
				|  |  |  |  |     headers = {"Location": app.reverse_url('get_source', {"id": news_source.id})} | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     return http.Response(news_source_schema.dump(news_source).data, status=201, headers=headers) | 
		
	
		
			
				|  |  |  |  |     return http.JSONResponse(news_source_schema.dump(news_source).data, status_code=201, headers=headers) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | def delete_source(session: Session, id: int): | 
		
	
	
		
			
				
					|  |  |  | @ -38,47 +33,47 @@ def delete_source(session: Session, id: int): | 
		
	
		
			
				|  |  |  |  |     news_source = session.query(NewsSource).filter_by(id=id).one_or_none() | 
		
	
		
			
				|  |  |  |  |     if news_source is None: | 
		
	
		
			
				|  |  |  |  |         msg = {"message": "404 Not Found"} | 
		
	
		
			
				|  |  |  |  |         return http.Response(msg, status=404) | 
		
	
		
			
				|  |  |  |  |         return http.JSONResponse(msg, status_code=404) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     msg = {"message": "200 OK"} | 
		
	
		
			
				|  |  |  |  |     return http.Response(msg, status=200) | 
		
	
		
			
				|  |  |  |  |     return http.JSONResponse(msg, status_code=200) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | def get_source(session: Session, id: int): | 
		
	
		
			
				|  |  |  |  |     news_source = session.query(NewsSource).filter_by(id=id).one_or_none() | 
		
	
		
			
				|  |  |  |  |     if news_source is None: | 
		
	
		
			
				|  |  |  |  |         msg = {"message": "404 Not Found"} | 
		
	
		
			
				|  |  |  |  |         return http.Response(msg, status=404) | 
		
	
		
			
				|  |  |  |  |         return http.JSONResponse(msg, status_code=404) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     return http.Response(news_source_schema.dump(news_source).data, status=200) | 
		
	
		
			
				|  |  |  |  |     return http.JSONResponse(news_source_schema.dump(news_source).data, status_code=200) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | def get_articles(session: Session): | 
		
	
		
			
				|  |  |  |  |     articles = session.query(NewsArticle).all() | 
		
	
		
			
				|  |  |  |  |     return http.Response(news_article_schema.dump(articles, many=True).data, status=200) | 
		
	
		
			
				|  |  |  |  |     return http.JSONResponse(news_article_schema.dump(articles, many=True).data, status_code=200) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | def add_article(session: Session, request_data: http.RequestData, router: Router): | 
		
	
		
			
				|  |  |  |  | def add_article(session: Session, request_data: http.RequestData, app: App): | 
		
	
		
			
				|  |  |  |  |     news_article_data, errors = news_article_schema.load(request_data) | 
		
	
		
			
				|  |  |  |  |     if errors: | 
		
	
		
			
				|  |  |  |  |         msg = {"message": "400 Bad Request", "error": errors} | 
		
	
		
			
				|  |  |  |  |         return http.Response(msg, status=400) | 
		
	
		
			
				|  |  |  |  |         return http.JSONResponse(msg, status_code=400) | 
		
	
		
			
				|  |  |  |  |     news_article = NewsArticle(**news_article_data) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     session.add(news_article) | 
		
	
		
			
				|  |  |  |  |     session.flush | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     headers = {"Location": router.reverse_url('get_source', {"id": news_article.id})} | 
		
	
		
			
				|  |  |  |  |     headers = {"Location": app.reverse_url('get_source', {"id": news_article.id})} | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     return http.Response(news_article_schema.dump(news_article), status=201, headers=headers) | 
		
	
		
			
				|  |  |  |  |     return http.JSONResponse(news_article_schema.dump(news_article), status_code=201, headers=headers) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | def get_article(session: Session, id: int): | 
		
	
		
			
				|  |  |  |  |     news_article = session.query(NewsArticle).filter_by(id=id).one_or_none() | 
		
	
		
			
				|  |  |  |  |     if news_article is None: | 
		
	
		
			
				|  |  |  |  |         msg = {"message": "404 Not Found"} | 
		
	
		
			
				|  |  |  |  |         return http.Response(msg, status=404) | 
		
	
		
			
				|  |  |  |  |     return http.Response(news_article_schema.dump(news_article), status=200) | 
		
	
		
			
				|  |  |  |  |         return http.JSONResponse(msg, status_code=404) | 
		
	
		
			
				|  |  |  |  |     return http.JSONResponse(news_article_schema.dump(news_article), status_code=200) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | def delete_article(session: Session, id): | 
		
	
	
		
			
				
					|  |  |  | @ -86,10 +81,10 @@ def delete_article(session: Session, id): | 
		
	
		
			
				|  |  |  |  |     news_article = session.query(NewsArticle).filter_by(id=id).one_or_none() | 
		
	
		
			
				|  |  |  |  |     if news_article is None: | 
		
	
		
			
				|  |  |  |  |         msg = {"message": "404 Not Found"} | 
		
	
		
			
				|  |  |  |  |         return http.Response(msg, status=404) | 
		
	
		
			
				|  |  |  |  |         return http.JSONResponse(msg, status_code=404) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     msg = {"message": "200 OK"} | 
		
	
		
			
				|  |  |  |  |     return http.Response(msg, status=200) | 
		
	
		
			
				|  |  |  |  |     return http.JSONResponse(msg, status_code=200) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | routes = [ | 
		
	
	
		
			
				
					|  |  |  | @ -101,29 +96,22 @@ routes = [ | 
		
	
		
			
				|  |  |  |  |     Route('/articles', 'POST', add_article), | 
		
	
		
			
				|  |  |  |  |     Route('/articles/{id}', 'GET', get_article), | 
		
	
		
			
				|  |  |  |  |     Route('/articles/{id}', 'DELETE', delete_article), | 
		
	
		
			
				|  |  |  |  |     Include('/docs', docs_urls), | 
		
	
		
			
				|  |  |  |  |     Include('/static', static_urls) | 
		
	
		
			
				|  |  |  |  | ] | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | _settings = { | 
		
	
		
			
				|  |  |  |  |     'DATABASE': { | 
		
	
		
			
				|  |  |  |  |         'URL': 'postgresql://apistar:local@localhost/news', | 
		
	
		
			
				|  |  |  |  |         'METADATA': Base.metadata | 
		
	
		
			
				|  |  |  |  |     }, | 
		
	
		
			
				|  |  |  |  |     'RENDERERS': [JSONRenderer()] | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | routes = routes | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | commands = sqlalchemy_backend.commands | 
		
	
		
			
				|  |  |  |  | components = [SQLAlchemySession(engine=create_engine('postgresql://apistar:local@localhost/news'))] | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | components = sqlalchemy_backend.components | 
		
	
		
			
				|  |  |  |  | hooks = [SQLAlchemyHook()] | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | def application_factory(settings={}): | 
		
	
		
			
				|  |  |  |  | def application_factory(): | 
		
	
		
			
				|  |  |  |  |     """Returns an instance of Cookie API""" | 
		
	
		
			
				|  |  |  |  |     app_settings = {**_settings, **settings} | 
		
	
		
			
				|  |  |  |  |     return App(settings=app_settings, | 
		
	
		
			
				|  |  |  |  |                commands=commands, | 
		
	
		
			
				|  |  |  |  |                components=components, | 
		
	
		
			
				|  |  |  |  |     return App(components=components, | 
		
	
		
			
				|  |  |  |  |                event_hooks=hooks, | 
		
	
		
			
				|  |  |  |  |                routes=routes) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | if __name__ == "__main__": | 
		
	
		
			
				|  |  |  |  |     app = application_factory() | 
		
	
		
			
				|  |  |  |  |     app.serve('0.0.0.0', 8080, debug=True) | 
		
	
	
		
			
				
					|  |  |  | 
 |