diff options
author | Cédric Bonhomme <cedric@cedricbonhomme.org> | 2014-06-22 23:26:55 +0200 |
---|---|---|
committer | Cédric Bonhomme <cedric@cedricbonhomme.org> | 2014-06-22 23:26:55 +0200 |
commit | a099f1b05034b77441e75b5d6eb47e064d235f5b (patch) | |
tree | c99d8881659909550387cc85bced1672c5504b91 | |
parent | Updated translations. (diff) | |
download | newspipe-a099f1b05034b77441e75b5d6eb47e064d235f5b.tar.gz newspipe-a099f1b05034b77441e75b5d6eb47e064d235f5b.tar.bz2 newspipe-a099f1b05034b77441e75b5d6eb47e064d235f5b.zip |
The API has been updated.
-rw-r--r-- | README.rst | 14 | ||||
-rw-r--r-- | pyaggr3g470r/rest.py | 67 |
2 files changed, 69 insertions, 12 deletions
@@ -152,7 +152,7 @@ It is possible to get your articles *via* a dedicated Web service. See the examp .. code:: python >>> import requests, json - >>> r = requests.get("https://pyaggr3g470r.herokuapp.com/articles.json/", auth=("your-email", "your-password")) + >>> r = requests.get("https://pyaggr3g470r.herokuapp.com/api/v1.0/articles", auth=("your-email", "your-password")) >>> r.status_code 200 >>> rjson = json.loads(r.text) @@ -165,9 +165,15 @@ Possible parameters: .. code:: bash - $ curl --user your-email:your-password "https://pyaggr3g470r.herokuapp.com/articles.json/?filter_=unread&feed=24" - $ curl --user your-email:your-password "https://pyaggr3g470r.herokuapp.com/articles.json/?filter_=read&feed=24&limit=20" - $ curl --user your-email:your-password "https://pyaggr3g470r.herokuapp.com/articles.json/?filter_=all&feed=24&limit=20" + $ curl --user your-email:your-password "https://pyaggr3g470r.herokuapp.com/api/v1.0/articles?filter_=unread&feed=24" + $ curl --user your-email:your-password "https://pyaggr3g470r.herokuapp.com/api/v1.0/articles?filter_=read&feed=24&limit=20" + $ curl --user your-email:your-password "https://pyaggr3g470r.herokuapp.com/api/v1.0/articles?filter_=all&feed=24&limit=20" + +To get an article: + +.. code:: bash + + $ curl --user your-email:your-password "https://pyaggr3g470r.herokuapp.com/api/v1.0/articles/84566" Donation diff --git a/pyaggr3g470r/rest.py b/pyaggr3g470r/rest.py index 1c597bdd..4b67a419 100644 --- a/pyaggr3g470r/rest.py +++ b/pyaggr3g470r/rest.py @@ -30,7 +30,7 @@ from functools import wraps from flask import g, Response, request, session, jsonify from flask.ext.restful import Resource, reqparse -from pyaggr3g470r import api +from pyaggr3g470r import api, db from pyaggr3g470r.models import User, Article def authenticate(func): @@ -61,7 +61,7 @@ def authenticate(func): {'WWWAuthenticate':'Basic realm="Login Required"'}) return wrapper -class ArticleAPI(Resource): +class ArticleListAPI(Resource): """ Defines a RESTful API for Article elements. """ @@ -71,11 +71,11 @@ class ArticleAPI(Resource): #self.reqparse = reqparse.RequestParser() #self.reqparse.add_argument('item_id', type = str, location = 'json') #self.reqparse.add_argument('item_title', type = unicode, location = 'json') - super(ArticleAPI, self).__init__() + super(ArticleListAPI, self).__init__() def get(self): """ - Get the list of articles. + Returns a list of articles. """ feeds = {feed.id: feed.title for feed in g.user.feeds if feed.enabled} articles = Article.query.filter(Article.feed_id.in_(feeds.keys()), @@ -94,32 +94,83 @@ class ArticleAPI(Resource): articles = articles.limit(limit) return jsonify(result= [{ + "id": article.id, "title": article.title, "link": article.link, "content": article.content, "readed": article.readed, "like": article.like, "date": article.date, - "retrieved_date": article.retrieved_date + "retrieved_date": article.retrieved_date, + "feed_id": article.source.id, + "feed_name": article.source.title } for article in articles] ) def post(self): + pass + + def put(self): + pass + + def delete(self): + pass + +class ArticleAPI(Resource): + """ + Defines a RESTful API for Article elements. + """ + method_decorators = [authenticate] + + def __init__(self): + super(ArticleAPI, self).__init__() + + def get(self, id=None): + """ + Returns an article. + """ + result = [] + if id is not None: + article = Article.query.filter(Article.user_id == g.user.id, Article.id == id).first() + if article is not None: + if not article.readed: + article.readed = True + db.session.commit() + result.append(article) + + return jsonify(result= [{ + "id": article.id, + "title": article.title, + "link": article.link, + "content": article.content, + "readed": article.readed, + "like": article.like, + "date": article.date, + "retrieved_date": article.retrieved_date, + "feed_id": article.source.id, + "feed_name": article.source.title + } + for article in result] + ) + + def post(self, id): """ Update an article. """ pass - def put(self): + def put(self, id): """ Create an article. """ pass - def delete(self): + def delete(self, id): """ + Delete an article. """ pass -api.add_resource(ArticleAPI, '/articles.json/', endpoint = 'articles.json') +api.add_resource(ArticleListAPI, '/api/v1.0/articles', endpoint = 'articles.json') +api.add_resource(ArticleAPI, '/api/v1.0/articles/<int:id>', endpoint = 'article.json') |