1+ /// This library uses WordPress REST API V2 to provide a way for your application
2+ /// to interact with your WordPress website.
3+ ///
4+ /// We use terminologies similar to the [WordPress REST API] (https://developer.wordpress.org/rest-api/)
5+ ///
6+ /// For authentication and usage of administrator level APIs, we have implemented
7+ /// two popular authentication plugins:
8+ ///
9+ /// 1. [Application Passwords] (https://wordpress.org/plugins/application-passwords/)
10+ /// 2. [JWT Authentication for WP REST API] (https://wordpress.org/plugins/jwt-authentication-for-wp-rest-api/)
11+ library flutter_wordpress;
12+
113import 'dart:async' as async;
214import 'dart:convert' ;
315
@@ -29,6 +41,8 @@ import 'schemas/wordpress_error.dart';
2941import 'constants.dart' ;
3042
3143import 'requests/params_post_list.dart' ;
44+ import 'requests/params_user_list.dart' ;
45+ import 'requests/params_comment_list.dart' ;
3246
3347export 'schemas/jwt_response.dart' ;
3448export 'schemas/avatar_urls.dart' ;
@@ -55,7 +69,12 @@ export 'schemas/wordpress_error.dart';
5569export 'constants.dart' ;
5670
5771export 'requests/params_post_list.dart' ;
72+ export 'requests/params_user_list.dart' ;
73+ export 'requests/params_comment_list.dart' ;
5874
75+ /// If [WordPressAuthenticator.ApplicationPasswords] is used as an authenticator,
76+ /// [adminName] and [adminKey] is necessary for authentication.
77+ /// https://wordpress.org/plugins/application-passwords/
5978class WordPress {
6079 String _baseUrl;
6180 WordPressAuthenticator _authenticator;
@@ -64,8 +83,6 @@ class WordPress {
6483 'Authorization' : '' ,
6584 };
6685
67- /// Take in base url and remove trailing '/' from the url if it exists.
68- /// Take in the wordpress context and get the enum name.
6986 WordPress (
7087 {@required String baseUrl,
7188 WordPressAuthenticator authenticator,
@@ -134,6 +151,10 @@ class WordPress {
134151 }
135152 }
136153
154+ /// This returns [User] object if the user with [id] , [email] or [username]
155+ /// exists. Otherwise throws [WordPressError] .
156+ ///
157+ /// Only one parameter is enough to search for the user.
137158 async .Future <User > fetchUser ({int id, String email, String username}) async {
138159 final StringBuffer url = new StringBuffer (_baseUrl + URL_USERS );
139160 final Map <String , String > params = {
@@ -166,12 +187,12 @@ class WordPress {
166187 }
167188 }
168189
190+ /// This methods returns a list of [Post] based on the filter parameters
191+ /// specified through [ParamsPostList] object. By default it returns only
192+ /// [ParamsPostList.perPage] number of posts in page [ParamsPostList.pageNum] .
169193 async .Future <List <Post >> fetchPosts ({@required ParamsPostList params}) async {
170194 final StringBuffer url = new StringBuffer (_baseUrl + URL_POSTS );
171195
172- print (params.toString ());
173- print (params.toMap ());
174-
175196 url.write (params.toString ());
176197
177198 final response = await http.get (url.toString (), headers: _urlHeader);
@@ -195,15 +216,18 @@ class WordPress {
195216 }
196217 }
197218
198- async .Future <List <User >> fetchUsers (
199- {int pageNum = 1 , int perPage = 10 }) async {
200- //TODO: Implement parameters
201- final url = _baseUrl + URL_USERS + constructUrlParams (new Map ());
219+ /// This methods returns a list of [User] based on the filter parameters
220+ /// specified through [ParamsUserList] object. By default it returns only
221+ /// [ParamsUserList.perPage] number of users in page [ParamsUserList.pageNum] .
222+ async .Future <List <User >> fetchUsers ({@required ParamsUserList params}) async {
223+ final StringBuffer url = new StringBuffer (_baseUrl + URL_USERS );
202224
203- final response = await http.get (url, headers: _urlHeader);
225+ url.write (params.toString ());
226+
227+ final response = await http.get (url.toString (), headers: _urlHeader);
204228
205229 if (response.statusCode == 200 ) {
206- List <User > users = new List ();
230+ List <User > users = new List < User > ();
207231 dynamic list = json.decode (response.body);
208232 list.forEach ((user) {
209233 users.add (User .fromJson (user));
@@ -220,16 +244,19 @@ class WordPress {
220244 }
221245 }
222246
223- async .Future <List <Comment >> fetchComments ({Post post, int id}) async {
224- //TODO: Implement parameters
247+ /// This methods returns a list of [Comment] based on the filter parameters
248+ /// specified through [ParamsCommentList] object. By default it returns only
249+ /// [ParamsCommentList.perPage] number of comments in page [ParamsCommentList.pageNum] .
250+ async .Future <List <Comment >> fetchComments (
251+ {@required ParamsCommentList params}) async {
225252 final StringBuffer url = new StringBuffer (_baseUrl + URL_COMMENTS );
226- Map <String , String > params;
227- if (post != null ) ;
228253
229- final response = await http.get (url, headers: _urlHeader);
254+ url.write (params.toString ());
255+
256+ final response = await http.get (url.toString (), headers: _urlHeader);
230257
231258 if (response.statusCode == 200 ) {
232- List <Comment > comments = new List ();
259+ List <Comment > comments = new List < Comment > ();
233260 dynamic list = json.decode (response.body);
234261 list.forEach ((comment) {
235262 comments.add (Comment .fromJson (comment));
0 commit comments