aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/util/WPRestClient.java
blob: 8793ccf0c7953c051d1a21bfd69ffd9fa6c772f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/**
 * Interface to the WordPress.com REST API.
 */
package org.wordpress.android.util;

import android.os.AsyncTask;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyError;
import com.wordpress.rest.Oauth;
import com.wordpress.rest.RestClient;
import com.wordpress.rest.RestRequest;
import com.wordpress.rest.RestRequest.ErrorListener;
import com.wordpress.rest.RestRequest.Listener;

import org.json.JSONException;
import org.json.JSONObject;
import org.wordpress.android.WordPress;
import org.wordpress.android.models.Note;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class WPRestClient {
    
    private static final String NOTIFICATION_FIELDS="id,type,unread,body,subject,timestamp";
    private static final String COMMENT_REPLY_CONTENT_FIELD="content";
    
    private RestClient mRestClient;
    private Authenticator mAuthenticator;
    
    /** Socket timeout in milliseconds for rest requests */
    public static final int REST_TIMEOUT_MS = 30000;

    /** Default number of retries for POST rest requests */
    public static final int REST_MAX_RETRIES_POST = 0;

    /** Default number of retries for GET rest requests */
    public static final int REST_MAX_RETRIES_GET = 3;
    
    /** Default backoff multiplier for rest requests */
    public static final float REST_BACKOFF_MULT = 2f;
    
    public WPRestClient(RequestQueue queue, Authenticator authenticator){
        this(queue, authenticator, RestClient.REST_CLIENT_VERSIONS.V1);
    }

    public WPRestClient(RequestQueue queue, Authenticator authenticator, RestClient.REST_CLIENT_VERSIONS version){
        mAuthenticator = authenticator;
        mRestClient = new RestClient(queue, version);
        mRestClient.setUserAgent(WordPress.getUserAgent());
    }

    /**
     * Reply to a comment using a Note.Reply object.
     * 
     * https://developer.wordpress.com/docs/api/1/post/sites/%24site/posts/%24post_ID/replies/new/
     */
    public void replyToComment(Note.Reply reply, Listener listener, ErrorListener errorListener){
        Map<String, String> params = new HashMap<String, String>();
        params.put(COMMENT_REPLY_CONTENT_FIELD, reply.getContent());
        post(reply.getRestPath(), params, null, listener, errorListener);
    }
    /**
     * Reply to a comment.
     * 
     * https://developer.wordpress.com/docs/api/1/post/sites/%24site/posts/%24post_ID/replies/new/
     */
    public void replyToComment(String siteId, String commentId, String content, Listener listener, ErrorListener errorListener){
        Map<String, String> params = new HashMap<String, String>();
        params.put(COMMENT_REPLY_CONTENT_FIELD, content);
        String path = String.format("sites/%s/comments/%s/replies/new", siteId, commentId);
        post(path, params, null, listener, errorListener);
    }
    /**
     * Follow a site given an ID or domain
     * 
     * https://developer.wordpress.com/docs/api/1/post/sites/%24site/follows/new/
     */
    public void followSite(String siteId, Listener listener, ErrorListener errorListener){
        String path = String.format("sites/%s/follows/new", siteId);
        post(path, listener, errorListener);
    }
    /**
     * Unfollow a site given an ID or domain
     * 
     * https://developer.wordpress.com/docs/api/1/post/sites/%24site/follows/mine/delete/
     */
    public void unfollowSite(String siteId, Listener listener, ErrorListener errorListener){
        String path = String.format("sites/%s/follows/mine/delete", siteId);
        post(path, listener, errorListener);
    }

    /**
     * Update the seen timestamp.
     * 
     * https://developer.wordpress.com/docs/api/1/post/notifications/seen
     */
    public void markNotificationsSeen(String timestamp, Listener listener, ErrorListener errorListener){
        Map<String, String> params = new HashMap<String, String>();
        params.put("time", timestamp);
        post("notifications/seen", params, null, listener, errorListener);
    }
    /**
     * Moderate a comment.
     * 
     * http://developer.wordpress.com/docs/api/1/sites/%24site/comments/%24comment_ID/
     */
    public void moderateComment(String siteId, String commentId, String status, Listener listener, ErrorListener errorListener){
        Map<String, String> params = new HashMap<String, String>();
        params.put("status", status);
        String path = String.format("sites/%s/comments/%s/", siteId, commentId);
        post(path, params, null, listener, errorListener);
    }
    
    /**
     * Get all a site's themes
     */
    public void getThemes(String siteId, int limit, int offset, Listener listener, ErrorListener errorListener) {
        String path = String.format("sites/%s/themes?limit=%d&offset=%d", siteId, limit, offset);
        get(path, listener, errorListener);
    }
    
    /**
     * Set a site's theme
     */
    public void setTheme(String siteId, String themeId, Listener listener, ErrorListener errorListener) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("theme", themeId);
        String path = String.format("sites/%s/themes/mine", siteId);
        post(path, params, null, listener, errorListener);
    }
    
    /**
     * Get a site's current theme
     */
    public void getCurrentTheme(String siteId, Listener listener, ErrorListener errorListener) {
        String path = String.format("sites/%s/themes/mine", siteId);
        get(path, listener, errorListener);
    }
    
    /**
     * Get a site's stats for clicks
     */
    public void getStatsClicks(String siteId, String date, Listener listener, ErrorListener errorListener) {
        String path = String.format("sites/%s/stats/clicks?date=%s", siteId, date);
        get(path, listener, errorListener);
    }

    /**
     * Get a site's stats for geoviews (views by country)
     */
    public void getStatsGeoviews(String siteId, String date, Listener listener, ErrorListener errorListener) {
        String path = String.format("sites/%s/stats/country-views?date=%s", siteId, date);
        get(path, listener, errorListener);
    }

    /**
     * Get a site's stats for most commented posts
     */
    public void getStatsMostCommented(String siteId, Listener listener, ErrorListener errorListener) {
        String path = "stats/most_commented";
        Map<String, String> params = new HashMap<String, String>();
        params.put("blog", siteId);
        getXL(path, params, listener, errorListener);
    }

    /**
     * Get a site's stats for top commenters
     */
    public void getStatsTopCommenters(String siteId, Listener listener, ErrorListener errorListener) {
        String path = "stats/top_commenters";
        Map<String, String> params = new HashMap<String, String>();
        params.put("blog", siteId);
        getXL(path, params, listener, errorListener);
    }

    /**
     * Get a site's stats for referrers
     */
    public void getStatsReferrers(String siteId, String date, Listener listener, ErrorListener errorListener) {
        String path = String.format("sites/%s/stats/referrers?date=%s", siteId, date);
        get(path, listener, errorListener);
    }

    /**
     * Get a site's stats for search engine terms
     */
    public void getStatsSearchEngineTerms(String siteId, String date, Listener listener, ErrorListener errorListener) {
        String path = String.format("sites/%s/stats/search-terms?date=%s", siteId, date);
        get(path, listener, errorListener);
    }

    /**
     * Get a site's stats for tags and categories
     */
    public void getStatsTagsAndCategories(String siteId, Listener listener, ErrorListener errorListener) {
        String path = "stats/tags_and_categories";
        Map<String, String> params = new HashMap<String, String>();
        params.put("blog", siteId);
        getXL(path, params, listener, errorListener);
    }

    /**
     * Get a site's stats for top authors
     */
    public void getStatsTopAuthors(String siteId, Listener listener, ErrorListener errorListener) {
        String path = "stats/top_authors";
        Map<String, String> params = new HashMap<String, String>();
        params.put("blog", siteId);
        getXL(path, params, listener, errorListener);
    }

    /**
     * Get a site's stats for top posts and pages
     */
    public void getStatsTopPosts(String siteId, String date, Listener listener, ErrorListener errorListener) {
        String path = String.format("sites/%s/stats/top-posts?date=%s", siteId, date);
        get(path, listener, errorListener);
    }

    /**
     * Get a site's stats for video plays
     */
    public void getStatsVideoPlays(String siteId, Listener listener, ErrorListener errorListener) {
        String path = "stats/video_plays";
        Map<String, String> params = new HashMap<String, String>();
        params.put("blog", siteId);
        getXL(path, params, listener, errorListener);
    }
    
    /**
     * Get a site's stats summary
     */
    public void getStatsSummary(String siteId, Listener listener, ErrorListener errorListener) {
        String path = String.format("sites/%s/stats", siteId);
        get(path, listener, errorListener);
    }

    /**
     * Get a site's stats summary for videos
     */
    public void getStatsVideoSummary(String siteId, Listener listener, ErrorListener errorListener) {
        String path = "stats/video_summary";
        Map<String, String> params = new HashMap<String, String>();
        params.put("blog", siteId);
        getXL(path, params, listener, errorListener);
    }

    public void getSiteDescription(String siteId, Listener listener, ErrorListener errorListener) {
        String path = String.format("rest/v1.1/sites/%s", siteId);
        Map<String, String> params = new HashMap<String, String>();
        params.put("fields", "description");
        get(path, params, null, listener, errorListener);
    }

    /**
     * This method is for simulating stats APIs using the XL Studio API simulator. It should be removed once the other APIs are implemented. 
     **/
    public void getXL(String path, Map<String, String> params, final Listener listener, final ErrorListener errorListener) {
        
        path = "https://simulator.xlstudio.com/apis/32/" + path;

        final String url_path = path;
        
        new AsyncTask<Void, Void, JSONObject>() {

            @Override
            protected JSONObject doInBackground(Void... params) {

                URL url;
                HttpURLConnection conn;
                BufferedReader rd;
                String line;
                String result = "";
                
                try {
                    url = new URL(url_path);
                    conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    conn.addRequestProperty("X-SIMULATOR-ACCESS-KEY", "bc88864498a705657486edb636196e31");
                    rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    while ((line = rd.readLine()) != null) {
                       result += line;
                    }
                    rd.close();
                    
                    return new JSONObject(result);
                 } catch (JSONException e) {
                     e.printStackTrace();
                 } catch (Exception e) {
                    e.printStackTrace();
                 }
                return null;
            }
            
            protected void onPostExecute(JSONObject result) {
                if (result != null)
                    listener.onResponse(result);
                else
                    errorListener.onErrorResponse(new VolleyError("JSONObject null"));
            };
            
        }.execute();
        
    }
    
    /**
     * 
     * Make GET request
     */
    public void get(String path, Listener listener, ErrorListener errorListener){
        get(path, null, null, listener, errorListener);
    }
    /**
     * Make GET request with params
     */
    public void get(String path, Map<String, String> params, RetryPolicy retryPolicy, Listener listener, ErrorListener errorListener){
        // turn params into querystring
        
        RestRequest request = mRestClient.makeRequest(Method.GET, mRestClient.getAbsoluteURL(path, params), null, listener, errorListener);
        if(retryPolicy == null) {
            retryPolicy = new DefaultRetryPolicy(REST_TIMEOUT_MS, REST_MAX_RETRIES_GET, REST_BACKOFF_MULT);
        } 
        request.setRetryPolicy(retryPolicy);
        Request authCheck = new Request(request, errorListener);
        authCheck.send();
    }
    /**
     * Make POST request
     */
    public void post(String path, Listener listener, ErrorListener errorListener){
        post(path, null, null, listener, errorListener);
    }
    /**
     * Make POST request with params
     */
    public void post(final String path, Map<String, String> params, RetryPolicy retryPolicy, Listener listener, ErrorListener errorListener){
        final RestRequest request = mRestClient.makeRequest(Method.POST, mRestClient.getAbsoluteURL(path), params, listener, errorListener);
        if(retryPolicy == null) {
            retryPolicy = new DefaultRetryPolicy(REST_TIMEOUT_MS, REST_MAX_RETRIES_POST, REST_BACKOFF_MULT); //Do not retry on failure
        } 
        request.setRetryPolicy(retryPolicy);
        Request authCheck = new Request(request, errorListener);
        authCheck.send();
    }
    /**
     * Interface that provides a method that should perform the necessary task to make sure
     * the provided Request will be authenticated.
     * 
     * The Authenticator must call Request.send() when it has completed its operations. For
     * convenience the Request class provides Request.setAccessToken so the Authenticator can
     * easily update the access token.
     */
    public interface Authenticator {
        void authenticate(Request request);
    }

    /**
     * Encapsulates the behaviour for asking the Authenticator for an access token. This
     * allows the request maker to disregard the authentication state when making requests.
     */
    public class Request {

        static public final String SITE_PREFIX = "https://public-api.wordpress.com/rest/v1/sites/";
        RestRequest mRequest;
        RestRequest.ErrorListener mListener;

        protected Request(RestRequest request, ErrorListener listener){
            mRequest = request;
            mListener = listener;
        }

        public String getSiteId() {
            // parse out the site id from the url
            String url = mRequest.getUrl();

            if (url.startsWith(SITE_PREFIX) && !SITE_PREFIX.equals(url)) {
                int marker = SITE_PREFIX.length();
                if (url.indexOf("/", marker) < marker)
                    return null;
                return url.substring(marker, url.indexOf("/", marker));
            }
            // not a sites/$siteId request
            return null;
        }

        /**
         * Attempt to send the request, checks to see if we have an access token and if not
         * asks the Authenticator to authenticate the request.
         * 
         * If no Authenticator is provided the request is always sent.
         */
        protected void send(){
            if (mAuthenticator == null) {
                mRestClient.send(mRequest);
            } else {
                mAuthenticator.authenticate(this);
            }
        }

        public void sendWithAccessToken(String token){
            mRequest.setAccessToken(token);
            mRestClient.send(mRequest);
        }

        public void sendWithAccessToken(Oauth.Token token){
            sendWithAccessToken(token.toString());
        }

        /**
         * If an access token cannot be obtained the request can be aborted and the
         * handler's onFailure method is called
         */
        public void abort(VolleyError error){
            if (mListener != null) {
                mListener.onErrorResponse(error);
            }
        }

    }

}