RatingsAndReviews

Revision 11 as of 2014-01-20 14:53:04

Clear message

Ratings & reviews

Contact: Michael Nelson (noodles)

After checking the code, the existing ratings and reviews client helper can do everything that we need[1][2]:

  1. Query for reviews of a specific packagename or packagename and version
  2. Submit new reviews
  3. Flag reviews as innapropriate etc.
  4. Add a +1 to a review (mark it as useful).

Example usage of public api

$ bzr branch lp:~rnr-developers/rnr-server/rnrclient
$ cd rnrclient && virtualenv venv && venv/bin/python setup.py install && venv/bin/python
>>> from rnrclient import RatingsAndReviewsAPI
>>> api = RatingsAndReviewsAPI("http://reviews.ubuntu.com/reviews/api/1.0")
>>> api.server_status()
u'ok'
>>> skype_reviews = api.get_reviews(packagename='skype')
>>> len(skype_reviews)
10
>>> [review.version for review in skype_reviews]
[u'4.2.0.11-0ubuntu0.12.04.1', u'4.2.0.11-0ubuntu0.12.04.1', 
u'4.1.0.20.0-0ubuntu0.13.04.2', u'4.1.0.20.0-0ubuntu0.13.04.2', 
u'4.1.0.20.0-0ubuntu0.13.04.2', u'4.1.0.20.0-0ubuntu0.12.04.2', 
u'4.1.0.20.0-0ubuntu0.12.04.2', u'4.1.0.20.0-0ubuntu0.12.04.2', 
u'4.1.0.20.0-0ubuntu0.12.04.2', u'4.1.0.20.0-0ubuntu0.12.04.2']
>>> skype_13_04_2_reviews = api.get_reviews(packagename='skype', 
                              version='4.1.0.20.0-0ubuntu0.13.04.2')
>>> len(skype_13_04_2_reviews)
3
>>> api.get_reviews(packagename="com.example.myapp")
[]

Example usage of the authenticated API to submit a review

First, create a new test account on https://login.staging.ubuntu.com.

The following example was run against the *staging* reviews server.

$ virtualenv testauth
$ . testauth/bin/activate
$ pip install requests requests_oauthlib
$ python

Then in the Python shell:

import requests_oauthlib, urlparse, requests, json
# Create u1 creds using the new account:
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
account_details = {"email": "your+email@example.com", "password": "yourpassword", "token_name": "testreviews"}
result = requests.post("https://login.staging.ubuntu.com/api/v2/tokens/oauth", headers=headers, data=json.dumps(account_details))
u1_creds = result.json()

# Make the review submission request
oauth1_session_plaintext = requests_oauthlib.OAuth1Session(u1_creds['consumer_key'], client_secret=u1_creds['consumer_secret'], resource_owner_key=u1_creds['token_key'], resource_owner_secret=u1_creds['token_secret'], signature_method='PLAINTEXT')

eg_data = {
    'arch_tag': 'i386',
    'distroseries': 'click',
    'language': 'en',
    'origin': 'com.ubuntu.developer.matiasb-testing.hello',
    'package_name': 'com.ubuntu.developer.matiasb-testing.hello',
    'rating': '4',
    'review_text': 'top stuff',
    'summary': 'install it',
    'version': '0.2'
}

response = oauth1_session_plaintext.post("https://reviews.staging.ubuntu.com/reviews/api/1.0/reviews/", data=json.dumps(eg_data), headers={'Content-Type': 'application/json'})                                  

# >>> response.reason
# 'OK'

# Creating a second review for the same package does not work

response = oauth1_session_plaintext.post("https://reviews.staging.ubuntu.com/reviews/api/1.0/reviews/", data=json.dumps(eg_data), headers={'Content-Type': 'application/json'})
# >>> response.reason
# 'BAD REQUEST'
# >>> response.text
# u'{"errors": {"__all__": ["A user cannot create multiple reviews for an app."]}}'

# The filter of all reviews for the package now includes our review:

response = requests.get("http://reviews.staging.ubuntu.com/reviews/api/1.0/reviews/filter/en/com.ubuntu.developer.matiasb-testing.hello/click/any/com.ubuntu.developer.matiasb-testing.hello/")

# >>> response.json()
# [list of reviews for that app.]

[1] We may need to update the bulk delivery of stats so that the click package index can consume click-packages only. [2] The tests in the rnrclient module look unmaintained (2 failures), but the rnrclient is tested by the lp:rnr-server project (to ensure it’s always compatible).