Skip to content

Commit

Permalink
Merge pull request #50 from yringler/add-fetch-all
Browse files Browse the repository at this point in the history
Add fetch all
  • Loading branch information
SachinGanesh authored Jul 17, 2020
2 parents fdee207 + 1fb982b commit c7f4f49
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 16 deletions.
1 change: 1 addition & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
.buildlog/
.history
.svn/
*flutter_export_environment*

# IntelliJ related
*.iml
Expand Down
11 changes: 0 additions & 11 deletions example/ios/Flutter/flutter_export_environment.sh

This file was deleted.

50 changes: 46 additions & 4 deletions lib/flutter_wordpress.dart
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ class WordPress {
///
/// (**Note:** *Set only those fetch boolean parameters which you need because
/// the more information to fetch, the longer it will take to return all Posts*)
///
/// [fetchAll] will make as many API requests as is needed to get all posts.
/// This may take a while.
///
/// In case of an error, a [WordPressError] object is thrown.
async.Future<List<Post>> fetchPosts({
Expand All @@ -256,8 +259,14 @@ class WordPress {
bool fetchFeaturedMedia = false,
bool fetchAttachments = false,
String postType = "posts",
bool fetchAll = false
}) async {
final StringBuffer url = new StringBuffer(_baseUrl + URL_WP_BASE + "/" + postType);
if (fetchAll) {
postParams = postParams.copyWith(perPage: 100);
}

final StringBuffer url =
new StringBuffer(_baseUrl + URL_WP_BASE + "/" + postType);

url.write(postParams.toString());

Expand All @@ -280,6 +289,25 @@ class WordPress {
setAttachments: fetchAttachments,
));
}

if (fetchAll && response.headers["x-wp-totalpages"] != null) {
final totalPages = int.parse(response.headers["x-wp-totalpages"]);

for (int i = postParams.pageNum + 1; i <= totalPages; ++i) {
posts.addAll(await fetchPosts(
postParams: postParams.copyWith(pageNum: i),
fetchAuthor: fetchAuthor,
fetchComments: fetchComments,
orderComments: orderComments,
orderCommentsBy: orderCommentsBy,
fetchCategories: fetchCategories,
fetchTags: fetchTags,
fetchFeaturedMedia: fetchFeaturedMedia,
fetchAttachments: fetchAttachments,
));
}
}

return posts;
} else {
try {
Expand Down Expand Up @@ -534,7 +562,11 @@ class WordPress {
///
/// In case of an error, a [WordPressError] object is thrown.
async.Future<List<Category>> fetchCategories(
{@required ParamsCategoryList params}) async {
{@required ParamsCategoryList params, bool fetchAll = false}) async {
if (fetchAll) {
params = params.copyWith(perPage: 100);
}

final StringBuffer url = new StringBuffer(_baseUrl + URL_CATEGORIES);

url.write(params.toString());
Expand All @@ -547,6 +579,16 @@ class WordPress {
list.forEach((category) {
categories.add(Category.fromJson(category));
});

if (fetchAll && response.headers["x-wp-totalpages"] != null) {
final totalPages = int.parse(response.headers["x-wp-totalpages"]);

for (int i = params.pageNum + 1; i <= totalPages; ++i) {
categories.addAll(await fetchCategories(
params: params.copyWith(pageNum: i)));
}
}

return categories;
} else {
try {
Expand Down Expand Up @@ -652,7 +694,7 @@ class WordPress {
}

// yahya - @mymakarim

async.Future<dynamic> uploadMedia(File image) async {
final StringBuffer url = new StringBuffer(_baseUrl + URL_MEDIA);
var file = image.readAsBytesSync();
Expand All @@ -678,7 +720,7 @@ class WordPress {
}
}
}

// uploadMedia function added by: @GarvMaggu

async.Future<bool> createUser({@required User user}) async {
Expand Down
20 changes: 20 additions & 0 deletions lib/requests/params_category_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ class ParamsCategoryList {
};
}

ParamsCategoryList copyWith({
int pageNum,
int perPage
}) {
return ParamsCategoryList(
context: context,
order: order,
orderBy: orderBy,
pageNum: pageNum ?? this.pageNum,
perPage: perPage ?? this.perPage,
searchQuery: searchQuery,
slug: slug,
excludeCategoryIDs: excludeCategoryIDs,
hideEmpty: hideEmpty,
includeCategoryIDs: includeCategoryIDs,
parent: parent,
post: post
);
}

@override
String toString() {
return constructUrlParams(toMap());
Expand Down
28 changes: 28 additions & 0 deletions lib/requests/params_post_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,34 @@ class ParamsPostList {
};
}

ParamsPostList copyWith({
int pageNum ,
int perPage,
}) {
return ParamsPostList(
afterDate: afterDate,
beforeDate: beforeDate,
context: context,
excludeAuthorIDs: excludeAuthorIDs,
excludeCategories: excludeCategories,
excludePostIDs: excludePostIDs,
excludeTags: excludeTags,
includeAuthorIDs: includeAuthorIDs,
includeCategories: includeCategories,
includePostIDs: includePostIDs,
includeTags: includeTags,
offset: offset,
order: order,
orderBy: orderBy,
pageNum: pageNum ?? this.pageNum,
perPage: perPage ?? this.perPage,
postStatus: postStatus,
searchQuery: searchQuery,
slug: slug,
sticky: sticky
);
}

@override
String toString() {
return constructUrlParams(toMap());
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: flutter_wordpress
description: This library uses WordPress REST-API-V2 to provide a way for your application to
interact with your WordPress website.
version: 0.2.1
version: 0.2.2
authors:
- Sachin Ganesh <sachin@dreamsoftin.com>
- Suraj Shettigar <suraj@dreamsoftin.com>
Expand Down

0 comments on commit c7f4f49

Please sign in to comment.