Skip to content

Commit 3ea4dec

Browse files
Update README.md. Add fetch users list, fetch comments list.
1 parent 7b50d6c commit 3ea4dec

10 files changed

Lines changed: 446 additions & 186 deletions

File tree

.idea/workspace.xml

Lines changed: 125 additions & 107 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,93 @@
1-
# flutter_wordpress
1+
# Flutter Wordpress
22

3-
Flutter Wordpress API
3+
This library uses [WordPress REST API V2](https://developer.wordpress.org/rest-api/) to provide a way for your application to interact with your WordPress website.
44

55
## Requirements
6-
- A Wordpress site using V2 REST API https://developer.wordpress.org/rest-api/
7-
- JWT Authentication for WP REST API V2 https://wordpress.org/plugins/jwt-authentication-for-wp-rest-api/
8-
- Flutter 1.0 https://flutter.io/docs/get-started/install
6+
For authentication and usage of administrator level REST APIs, you need to use either of the two popular authentication plugins in your WordPress site:
7+
1. [Application Passwords](https://wordpress.org/plugins/application-passwords/)
8+
2. [JWT Authentication for WP REST API](https://wordpress.org/plugins/jwt-authentication-for-wp-rest-api/)
9+
10+
## Getting Started
11+
12+
### 1. Import library
13+
14+
```dart
15+
import 'package:flutter_wordpress/flutter_wordpress.dart' as wp;
16+
```
17+
18+
### 2. Instantiate WordPress class
19+
20+
```dart
21+
wp.WordPress wordPress;
22+
23+
// adminName and adminKey is needed only for admin level APIs
24+
wordPress = wp.WordPress(
25+
baseUrl: 'http://localhost',
26+
authenticator: wp.WordPressAuthenticator.ApplicationPasswords,
27+
adminName: '',
28+
adminKey: '',
29+
);
30+
```
31+
32+
### 3. Authenticate User
33+
34+
```dart
35+
Future<wp.User> response = wordPress.authenticateUser(
36+
username: 'username',
37+
password: 'password',
38+
);
39+
40+
response.then((user) {
41+
print(user.toString());
42+
}).catchError((err) {
43+
print(err.toString());
44+
});
45+
```
46+
47+
### 4. Fetch Posts
48+
49+
```dart
50+
Future<List<wp.Post>> posts = wordPress.fetchPosts(
51+
params: wp.ParamsPostList(
52+
context: wp.WordPressContext.view,
53+
pageNum: 1,
54+
perPage: 20,
55+
order: wp.Order.desc,
56+
orderBy: wp.PostsOrderBy.date,
57+
),
58+
);
59+
```
60+
61+
### 5. Fetch Users
62+
63+
```dart
64+
Future<List<wp.User>> users = wordPress.fetchUsers(
65+
params: wp.ParamsUserList(
66+
context: wp.WordPressContext.view,
67+
pageNum: 1,
68+
perPage: 30,
69+
order: wp.Order.asc,
70+
orderBy: wp.UsersOrderBy.name,
71+
role: wp.UserRole.subscriber,
72+
),
73+
);
74+
```
75+
76+
### 6. Fetch Comments
77+
78+
```dart
79+
Future<List<wp.Comment>> comments = wordPress.fetchComments(
80+
params: wp.ParamsCommentList(
81+
context: wp.WordPressContext.view,
82+
pageNum: 1,
83+
perPage: 30,
84+
includePostIDs: [1],
85+
),
86+
);
87+
```
88+
89+
## Future Work
90+
1. Implementing OAuth 2.0 authentication.
91+
992

1093

example/lib/main.dart

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,41 +66,60 @@ class PostsBuilderState extends State<PostsBuilder> {
6666
adminKey: 'EOjD JsYA hKfM RHNI vufW hyUX',
6767
);
6868
} catch (err) {
69-
print(err);
69+
print(err.toString());
7070
}
7171

72-
/*if (wordPress != null)
73-
wordPress
74-
.authenticateUser(username: 'admin', password: 'hello')
75-
.then((user) {
76-
print("User: $user");
77-
}).catchError((err) {
78-
print(err.toString());
79-
});*/
72+
/* Future<wp.User> response = wordPress.authenticateUser(
73+
username: 'username',
74+
password: 'password',
75+
);
8076
81-
fetchPosts();
77+
response.then((user) {
78+
print(user.toString());
79+
}).catchError((err) {
80+
print(err.toString());
81+
});*/
8282

83-
/* Future<wp.JWTResponse> auth = wordPress.authenticateUser(
84-
username: 'admin', password: 'mypassword@123');
83+
Future<List<wp.User>> users = wordPress.fetchUsers(
84+
params: wp.ParamsUserList(
85+
context: wp.WordPressContext.view,
86+
pageNum: 1,
87+
perPage: 30,
88+
order: wp.Order.asc,
89+
orderBy: wp.UsersOrderBy.name,
90+
),
91+
);
8592

86-
auth.then((response) {
87-
fetchPosts();
93+
users.then((response) {
94+
print(response);
8895
}).catchError((err) {
89-
print(err.message);
90-
});*/
96+
print(err.toString());
97+
});
98+
99+
Future<List<wp.Comment>> comments = wordPress.fetchComments(
100+
params: wp.ParamsCommentList(
101+
context: wp.WordPressContext.view,
102+
pageNum: 1,
103+
perPage: 30,
104+
),
105+
);
106+
107+
comments.then((response) {
108+
print(response);
109+
}).catchError((err) {
110+
print(err.toString());
111+
});
91112
}
92113

93114
void fetchPosts() {
94115
setState(() {
95-
posts = wordPress.fetchPosts(
96-
params:
97-
wp.ParamsPostList(order: wp.Order.asc, includeAuthorIDs: [1, 2]));
116+
posts = wordPress.fetchPosts(params: wp.ParamsPostList());
98117
});
99118
}
100119

101120
void fetchUsers() {
102121
setState(() {
103-
users = wordPress.fetchUsers();
122+
users = wordPress.fetchUsers(params: wp.ParamsUserList());
104123
});
105124
}
106125

@@ -121,7 +140,7 @@ class PostsBuilderState extends State<PostsBuilder> {
121140
),
122141
Text(
123142
snapshot.data[i].content.rendered,
124-
)
143+
),
125144
],
126145
);
127146
},

lib/constants.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,25 @@ enum CommentStatus {
9191
spam,
9292
trash,
9393
}
94+
enum CommentType {
95+
comment,
96+
//TODO: Add all comment types
97+
}
9498

99+
/// Converts an enum string to enum value name.
95100
String enumStringToName(String enumString) {
96101
return enumString.split('.')[1];
97102
}
98103

104+
/// Formats a list of [items] to a comma(,) separated string to pass it as a
105+
/// URL parameter.
99106
String listToUrlString<T>(List<T> items) {
100107
if (items == null || items.length == 0) return '';
101108

102109
return items.join(',');
103110
}
104111

112+
/// Formats a [Map] of parameters to a string of URL friendly parameters.
105113
String constructUrlParams(Map<String, String> params) {
106114
StringBuffer p = new StringBuffer('/?');
107115
params.forEach((key, value) {

lib/flutter_wordpress.dart

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
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+
113
import 'dart:async' as async;
214
import 'dart:convert';
315

@@ -29,6 +41,8 @@ import 'schemas/wordpress_error.dart';
2941
import 'constants.dart';
3042

3143
import 'requests/params_post_list.dart';
44+
import 'requests/params_user_list.dart';
45+
import 'requests/params_comment_list.dart';
3246

3347
export 'schemas/jwt_response.dart';
3448
export 'schemas/avatar_urls.dart';
@@ -55,7 +69,12 @@ export 'schemas/wordpress_error.dart';
5569
export 'constants.dart';
5670

5771
export '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/
5978
class 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));

lib/parameters.dart

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)