~rnr-developers/rnr-server/rnrclient

26.1.2 by Anthony Lenton
Added Sphinx documentation.
1
Quickstart
2
==========
3
4
To use the client library for the ratings and review API you'll need to get
5
`the client library code`_.  That depends on `piston-mini-client`_
6
that you can also get from the `software-store ppa`_.
7
8
To make authenticated calls you'll also need a valid SSO token.
9
There's a brief snippet to get one using lazr.restfulclient farther down.
10
11
.. _the client library code: https://code.launchpad.net/~rnr-developers/rnr-server/rnrclient
12
.. _piston-mini-client: https://code.edge.launchpad.net/piston-mini-client
13
.. _software-store ppa: https://launchpad.net/~software-store-developers/+archive/daily-build
14
15
.. _public-api-calls:
16
17
=======================
18
Making public API calls
19
=======================
20
21
First thing to do is initialize the API::
22
23
    >>> from rnrclient import RatingsAndReviewsAPI
24
    >>> api_public = RatingsAndReviewsAPI('https://reviews.staging.ubuntu.com/reviews/api/1.0')
25
26
We can check that we've initialized the API correctly by using::
27
28
    >>> api_public.server_status()
29
    'ok'
30
31
We can also request the rating summaries::
32
33
    >>> stats = api_public.review_stats()
34
    >>> stats
35
    [<rnrclient.ReviewsStats object at 0x1a79d10>]
36
37
We can see there's just one package/app been reviewed so far.  For each item
38
in the list we can check which package/app it belongs to, what the average
39
rating is and the total number of ratings it has::
40
41
    >>> stats[0].package_name
42
    'liferea'
43
    >>> stats[0].app_name
44
    ''
45
    >>> stats[0].ratings_average # Average rating for this package/app
46
    '5.00'
47
    >>> stats[0].ratings_total   # Total amount of ratings for this app
48
    1
49
50
You can also use the public API to retrieve the reviews for any package
51
and/or application.  Here you can also provide an optional ``appname``
52
argument::
53
54
    >>> reviews = api_public.get_reviews(language='en', origin='ubuntu',
55
    ... distroseries='maverick', packagename='liferea')
56
    >>> reviews
57
    [<rnrclient.ReviewDetails object at 0x1bc3f90>]
58
59
As reported by the stats summary, there's only one review for this package.
60
We can look at the details for the review, including the review ID that we'll
61
use later on::
62
63
    >>> reviews[0].date_created
64
    '2011-01-10 12:16:24'
65
    >>> reviews[0].reviewer_username
66
    'elachuni'
67
    >>> reviews[0].summary
68
    'Some summary'
69
    >>> reviews[0].review_text
70
    'Some review text.  And then some more.'
71
    >>> reviews[0].rating
72
    5
73
    >>> reviews[0].language
74
    'en'
75
    >>> reviews[0].id
76
    1
77
78
==============================
79
Making authenticated API calls
80
==============================
81
82
For certain API calls like submitting or flagging reviews you need to have a
83
valid SSO token.  You can get this from seahorse if you already have one in
84
your keyring, or you can use a lazr.restfulclient snippet like the one farther
85
down to get a new one.  These snippets suppose that ``token`` is a dict
86
containing appropriate token bits.
87
26.1.3 by Anthony Lenton
Typo fix.
88
First we need to instantiate a new API client by passing in a valid oauth
26.1.2 by Anthony Lenton
Added Sphinx documentation.
89
authenticator::
90
91
    >>> from piston_mini_client.auth import OAuthAuthorizer
92
    >>> auth = OAuthAuthorizer(token_key=token['token'],
93
    ... token_secret=token['token_secret'], consumer_key=token['consumer_key'],
94
    ... consumer_secret=token['consumer_secret'])
95
    >>> api_authenticated = RatingsAndReviewsAPI('https://reviews.staging.ubuntu.com/reviews/api/1.0',
96
    ... auth=auth)
97
98
Now we've got an authenticated API, we can submit a new review.
99
We need to create a ReviewRequest object for that::
100
101
    >>> from rnrclient import ReviewRequest
102
    >>> review = ReviewRequest(package_name='3dchess', summary='Game is great', 
103
    ... version='0.8.1-16', review_text='Good graphics, great replay value', 
104
    ... distroseries='maverick', arch_tag='i386', app_name='3D Chess',
105
    ... rating=4, language='en', origin='ubuntu')
106
    >>> api_authenticated.submit_review(review=review)
107
    {'rating': 4, 'hide': False, 'package_name': '3dchess', 'language': 'en', 
108
    'review_text': 'Good graphics, great replay value', 
109
    'reviewer_username': 'elachuni', 'date_created': '2011-01-10 12:49:19', 
110
    'summary': 'Game is great', 'id': 2, 'app_name': '3D Chess'}
111
112
You can see the submit_review returns the newly created review.
113
114
We can also mark another review as useful (or not).  For this we'll just need
115
to know the right review id, that was fetched together with the review details
116
(see :ref:`public-api-calls` at the top of this page)::
117
118
    >>> api_authenticated.submit_usefulness(review_id=1, useful='True')
119
    'Created'
120
121
Finally, you can also flag a review for moderation, using the same review id::
122
123
    >>> api_authenticated.flag_review(review_id=1,
124
    ... reason='Inappropriate language', text='"Some" is inappropriate')
125
126
==============================================
127
Getting a valid token using lazr.restfulclient
128
==============================================
129
130
You can get a valid token by using lazr.restfulclient to talk to the sso api::
131
132
    >>> from lazr.restfulclient.resource import ServiceRoot
133
    >>> from lazr.restfulclient.authorize import BasicHttpAuthorizer
134
    >>> basic = BasicHttpAuthorizer('myemail@myhost.com', '...password...')
135
    >>> api = ServiceRoot(basic, "https://login.ubuntu.com/api/1.0")
136
    >>> token = api.authentications.authenticate(token_name="hacking-by-the-pool")
137
138
The token name is unimportant here, but it'll let you identify the token you
139
just created in `the web interface`_.  You'll need to provide a valid SSO
140
email/password for this to work.  Also note that
141
``reviews.staging.ubuntu.com`` works against production SSO, so staging tokens
142
will not work.
143
144
.. _the web interface: https://login.staging.ubuntu.com/+applications