Skip to content

Commit 0056a12

Browse files
Add fetching of categories, tags, pages. Fix Post class to use enums.
1 parent 3ea4dec commit 0056a12

27 files changed

Lines changed: 763 additions & 419 deletions

.idea/workspace.xml

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

example/lib/main.dart

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,15 @@ class PostsBuilderState extends State<PostsBuilder> {
6969
print(err.toString());
7070
}
7171

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

8374
Future<List<wp.User>> users = wordPress.fetchUsers(
8475
params: wp.ParamsUserList(
8576
context: wp.WordPressContext.view,
8677
pageNum: 1,
8778
perPage: 30,
8879
order: wp.Order.asc,
89-
orderBy: wp.UsersOrderBy.name,
80+
orderBy: wp.UserOrderBy.name,
9081
),
9182
);
9283

@@ -109,6 +100,33 @@ class PostsBuilderState extends State<PostsBuilder> {
109100
}).catchError((err) {
110101
print(err.toString());
111102
});
103+
104+
Future<List<wp.Page>> pages = wordPress.fetchPages(
105+
params: wp.ParamsPageList(),
106+
);
107+
pages.then((response) {
108+
print(response);
109+
}).catchError((err) {
110+
print(err.toString());
111+
});
112+
113+
Future<List<wp.Category>> categories = wordPress.fetchCategories(
114+
params: wp.ParamsCategoryList(),
115+
);
116+
categories.then((response) {
117+
print(response);
118+
}).catchError((err) {
119+
print(err.toString());
120+
});
121+
122+
Future<List<wp.Tag>> tags = wordPress.fetchTags(
123+
params: wp.ParamsTagList(),
124+
);
125+
tags.then((response) {
126+
print(response);
127+
}).catchError((err) {
128+
print(err.toString());
129+
});
112130
}
113131

114132
void fetchPosts() {
@@ -131,6 +149,8 @@ class PostsBuilderState extends State<PostsBuilder> {
131149
if (snapshot.hasData) {
132150
return ListView.separated(
133151
itemBuilder: (context, i) {
152+
print(
153+
"Post Status: ${snapshot.data[i].status} ${snapshot.data[i].format} ${snapshot.data[i].commentStatus} ${snapshot.data[i].pingStatus}");
134154
return Column(
135155
crossAxisAlignment: CrossAxisAlignment.start,
136156
children: <Widget>[

lib/constants.dart

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
const URL_WP_BASE = '/wp-json/wp/v2';
21
const URL_JWT_BASE = '/wp-json/jwt-auth/v1';
2+
const URL_WP_BASE = '/wp-json/wp/v2';
33

44
const URL_JWT_TOKEN = '$URL_JWT_BASE/token';
55
const URL_JWT_TOKEN_VALIDATE = '$URL_JWT_BASE/token/validate';
66

7+
const URL_CATEGORIES = '$URL_WP_BASE/categories';
8+
const URL_COMMENTS = '$URL_WP_BASE/comments';
9+
const URL_PAGES = '$URL_WP_BASE/pages';
710
const URL_POSTS = '$URL_WP_BASE/posts';
11+
const URL_TAGS = '$URL_WP_BASE/tags';
812
const URL_USERS = '$URL_WP_BASE/users';
9-
const URL_COMMENTS = '$URL_WP_BASE/comments';
1013

1114
enum WordPressAuthenticator {
1215
JWT,
@@ -19,7 +22,7 @@ enum Order {
1922
desc,
2023
}
2124

22-
enum PostsOrderBy {
25+
enum PostOrderBy {
2326
author,
2427
date,
2528
id,
@@ -30,7 +33,7 @@ enum PostsOrderBy {
3033
slug,
3134
title,
3235
}
33-
enum PostStatus {
36+
enum PostPageStatus {
3437
publish,
3538
future,
3639
draft,
@@ -45,7 +48,7 @@ enum PostPingStatus {
4548
open,
4649
closed,
4750
}
48-
enum ObjectFormat {
51+
enum PostFormat {
4952
standard,
5053
aside,
5154
chat,
@@ -58,7 +61,7 @@ enum ObjectFormat {
5861
audio,
5962
}
6063

61-
enum UsersOrderBy {
64+
enum UserOrderBy {
6265
id,
6366
include,
6467
name,
@@ -75,7 +78,7 @@ enum UserRole {
7578
administrator,
7679
}
7780

78-
enum CommentsOrderBy {
81+
enum CommentOrderBy {
7982
date,
8083
date_gmt,
8184
id,
@@ -96,6 +99,29 @@ enum CommentType {
9699
//TODO: Add all comment types
97100
}
98101

102+
enum CategoryTagOrderBy {
103+
id,
104+
include,
105+
name,
106+
slug,
107+
term_group,
108+
description,
109+
count,
110+
}
111+
112+
enum PageOrderBy {
113+
author,
114+
date,
115+
id,
116+
include,
117+
modified,
118+
parent,
119+
relevance,
120+
slug,
121+
title,
122+
menu_order,
123+
}
124+
99125
/// Converts an enum string to enum value name.
100126
String enumStringToName(String enumString) {
101127
return enumString.split('.')[1];

lib/flutter_wordpress.dart

Lines changed: 93 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/// This library uses WordPress REST API V2 to provide a way for your application
2-
/// to interact with your WordPress website.
1+
/// This library uses [WordPress REST API V2](https://developer.wordpress.org/rest-api/)
2+
/// to provide a way for your application to interact with your WordPress website.
33
///
44
/// We use terminologies similar to the [WordPress REST API](https://developer.wordpress.org/rest-api/)
55
///
@@ -17,24 +17,11 @@ import 'package:meta/meta.dart';
1717
import 'package:http/http.dart' as http;
1818

1919
import 'schemas/jwt_response.dart';
20-
import 'schemas/avatar_urls.dart';
21-
import 'schemas/capabilities.dart';
2220
import 'schemas/category.dart';
2321
import 'schemas/comment.dart';
24-
import 'schemas/content.dart';
25-
import 'schemas/excerpt.dart';
26-
import 'schemas/guid.dart';
27-
import 'schemas/labels.dart';
28-
import 'schemas/links.dart';
29-
import 'schemas/media.dart';
3022
import 'schemas/page.dart';
31-
import 'schemas/post_statuses.dart';
32-
import 'schemas/post_types.dart';
3323
import 'schemas/post.dart';
34-
import 'schemas/settings.dart';
3524
import 'schemas/tag.dart';
36-
import 'schemas/taxonomies.dart';
37-
import 'schemas/title.dart';
3825
import 'schemas/user.dart';
3926
import 'schemas/wordpress_error.dart';
4027

@@ -43,6 +30,9 @@ import 'constants.dart';
4330
import 'requests/params_post_list.dart';
4431
import 'requests/params_user_list.dart';
4532
import 'requests/params_comment_list.dart';
33+
import 'requests/params_category_list.dart';
34+
import 'requests/params_tag_list.dart';
35+
import 'requests/params_page_list.dart';
4636

4737
export 'schemas/jwt_response.dart';
4838
export 'schemas/avatar_urls.dart';
@@ -71,6 +61,9 @@ export 'constants.dart';
7161
export 'requests/params_post_list.dart';
7262
export 'requests/params_user_list.dart';
7363
export 'requests/params_comment_list.dart';
64+
export 'requests/params_category_list.dart';
65+
export 'requests/params_tag_list.dart';
66+
export 'requests/params_page_list.dart';
7467

7568
/// If [WordPressAuthenticator.ApplicationPasswords] is used as an authenticator,
7669
/// [adminName] and [adminKey] is necessary for authentication.
@@ -216,6 +209,34 @@ class WordPress {
216209
}
217210
}
218211

212+
/// This methods returns a list of [Page] based on the filter parameters
213+
/// specified through [ParamsPageList] object. By default it returns only
214+
/// [ParamsPageList.perPage] number of pages in page [ParamsPageList.pageNum].
215+
async.Future<List<Page>> fetchPages({@required ParamsPageList params}) async {
216+
final StringBuffer url = new StringBuffer(_baseUrl + URL_PAGES);
217+
218+
url.write(params.toString());
219+
220+
final response = await http.get(url.toString(), headers: _urlHeader);
221+
222+
if (response.statusCode == 200) {
223+
List<Page> pages = new List<Page>();
224+
dynamic list = json.decode(response.body);
225+
list.forEach((page) {
226+
pages.add(Page.fromJson(page));
227+
});
228+
return pages;
229+
} else {
230+
try {
231+
WordPressError err =
232+
WordPressError.fromJson(json.decode(response.body));
233+
throw err;
234+
} catch (e) {
235+
throw new WordPressError(message: response.body);
236+
}
237+
}
238+
}
239+
219240
/// This methods returns a list of [User] based on the filter parameters
220241
/// specified through [ParamsUserList] object. By default it returns only
221242
/// [ParamsUserList.perPage] number of users in page [ParamsUserList.pageNum].
@@ -272,4 +293,61 @@ class WordPress {
272293
}
273294
}
274295
}
296+
297+
/// This methods returns a list of [Category] based on the filter parameters
298+
/// specified through [ParamsCategoryList] object. By default it returns only
299+
/// [ParamsCategoryList.perPage] number of categories in page [ParamsCategoryList.pageNum].
300+
async.Future<List<Category>> fetchCategories(
301+
{@required ParamsCategoryList params}) async {
302+
final StringBuffer url = new StringBuffer(_baseUrl + URL_CATEGORIES);
303+
304+
url.write(params.toString());
305+
306+
final response = await http.get(url.toString(), headers: _urlHeader);
307+
308+
if (response.statusCode == 200) {
309+
List<Category> categories = new List<Category>();
310+
dynamic list = json.decode(response.body);
311+
list.forEach((category) {
312+
categories.add(Category.fromJson(category));
313+
});
314+
return categories;
315+
} else {
316+
try {
317+
WordPressError err =
318+
WordPressError.fromJson(json.decode(response.body));
319+
throw err;
320+
} catch (e) {
321+
throw new WordPressError(message: response.body);
322+
}
323+
}
324+
}
325+
326+
/// This methods returns a list of [Tag] based on the filter parameters
327+
/// specified through [ParamsTagList] object. By default it returns only
328+
/// [ParamsTagList.perPage] number of tags in page [ParamsTagList.pageNum].
329+
async.Future<List<Tag>> fetchTags({@required ParamsTagList params}) async {
330+
final StringBuffer url = new StringBuffer(_baseUrl + URL_TAGS);
331+
332+
url.write(params.toString());
333+
334+
final response = await http.get(url.toString(), headers: _urlHeader);
335+
336+
if (response.statusCode == 200) {
337+
List<Tag> tags = new List<Tag>();
338+
dynamic list = json.decode(response.body);
339+
list.forEach((tag) {
340+
tags.add(Tag.fromJson(tag));
341+
});
342+
return tags;
343+
} else {
344+
try {
345+
WordPressError err =
346+
WordPressError.fromJson(json.decode(response.body));
347+
throw err;
348+
} catch (e) {
349+
throw new WordPressError(message: response.body);
350+
}
351+
}
352+
}
275353
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import 'package:flutter_wordpress/constants.dart';
2+
3+
/// This class holds all arguments which can be used to filter Categories when using
4+
/// [WordPress.fetchCategories] method.
5+
///
6+
/// [List Categories' Arguments](https://developer.wordpress.org/rest-api/reference/categories/#list-categories)
7+
class ParamsCategoryList {
8+
final WordPressContext context;
9+
final int pageNum;
10+
final int perPage;
11+
final String searchQuery;
12+
final List<int> excludeCategoryIDs;
13+
final List<int> includeCategoryIDs;
14+
final Order order;
15+
final CategoryTagOrderBy orderBy;
16+
final bool hideEmpty;
17+
final int parent;
18+
final int post;
19+
final String slug;
20+
21+
ParamsCategoryList({
22+
this.context = WordPressContext.view,
23+
this.pageNum = 1,
24+
this.perPage = 10,
25+
this.searchQuery = '',
26+
this.excludeCategoryIDs,
27+
this.includeCategoryIDs,
28+
this.order = Order.asc,
29+
this.orderBy = CategoryTagOrderBy.name,
30+
this.hideEmpty,
31+
this.parent,
32+
this.post,
33+
this.slug = '',
34+
});
35+
36+
Map<String, String> toMap() {
37+
return {
38+
'context': '${enumStringToName(this.context.toString())}',
39+
'page': '${this.pageNum}',
40+
'per_page': '${this.perPage}',
41+
'search': '${this.searchQuery}',
42+
'exclude': '${listToUrlString(this.excludeCategoryIDs)}',
43+
'include': '${listToUrlString(this.includeCategoryIDs)}',
44+
'order': '${enumStringToName(this.order.toString())}',
45+
'orderby': '${enumStringToName(this.orderBy.toString())}',
46+
'hide_empty': '${this.hideEmpty == null ? '': this.hideEmpty}',
47+
'parent': '${this.parent == null ? '': this.parent}',
48+
'post': '${this.post == null ? '': this.post}',
49+
'slug': '${this.slug}',
50+
};
51+
}
52+
53+
@override
54+
String toString() {
55+
return constructUrlParams(toMap());
56+
}
57+
}

lib/requests/params_comment_list.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ParamsCommentList {
1818
final List<int> includeCommentIDs;
1919
final int offset;
2020
final Order order;
21-
final CommentsOrderBy orderBy;
21+
final CommentOrderBy orderBy;
2222
final List<int> includeParentIDs;
2323
final List<int> excludeParentIDs;
2424
final List<int> includePostIDs;
@@ -40,7 +40,7 @@ class ParamsCommentList {
4040
this.includeCommentIDs,
4141
this.offset,
4242
this.order = Order.desc,
43-
this.orderBy = CommentsOrderBy.date_gmt,
43+
this.orderBy = CommentOrderBy.date_gmt,
4444
this.includeParentIDs,
4545
this.excludeParentIDs,
4646
this.includePostIDs,

0 commit comments

Comments
 (0)