Skip to content

Commit

Permalink
Merge pull request #21 from harmjanr/master
Browse files Browse the repository at this point in the history
Meta data & result header
  • Loading branch information
SachinGanesh authored Jan 2, 2020
2 parents 9220682 + 1350311 commit ef188be
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
9 changes: 7 additions & 2 deletions lib/flutter_wordpress.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import 'schemas/jwt_response.dart';
import 'schemas/category.dart';
import 'schemas/comment.dart';
import 'schemas/comment_hierarchy.dart';
import 'schemas/fetch_user_result.dart';
import 'schemas/media.dart';
import 'schemas/page.dart';
import 'schemas/post.dart';
Expand All @@ -44,6 +45,7 @@ export 'schemas/comment.dart';
export 'schemas/comment_hierarchy.dart';
export 'schemas/content.dart';
export 'schemas/excerpt.dart';
export 'schemas/fetch_user_result.dart';
export 'schemas/guid.dart';
export 'schemas/labels.dart';
export 'schemas/links.dart';
Expand Down Expand Up @@ -367,7 +369,8 @@ class WordPress {
/// [ParamsUserList.perPage] number of users in page [ParamsUserList.pageNum].
///
/// In case of an error, a [WordPressError] object is thrown.
async.Future<List<User>> fetchUsers({@required ParamsUserList params}) async {
async.Future<FetchUsersResult> fetchUsers(
{@required ParamsUserList params}) async {
final StringBuffer url = new StringBuffer(_baseUrl + URL_USERS);

url.write(params.toString());
Expand All @@ -377,10 +380,12 @@ class WordPress {
if (response.statusCode >= 200 && response.statusCode < 300) {
List<User> users = new List<User>();
final list = json.decode(response.body);
int totalUsers = int.parse(response.headers['x-wp-total']);

list.forEach((user) {
users.add(User.fromJson(user));
});
return users;
return FetchUsersResult(users, totalUsers, params.pageNum);
} else {
try {
WordPressError err =
Expand Down
13 changes: 13 additions & 0 deletions lib/schemas/fetch_user_result.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'user.dart';

class FetchUsersResult {
List<User> users;
int totalUsers;
int currentPage;

FetchUsersResult(List<User> users, int totalUsers, int currentPage) {
this.users = users;
this.totalUsers = totalUsers;
this.currentPage = currentPage;
}
}
21 changes: 11 additions & 10 deletions lib/schemas/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class User {
UserCapabilities capabilities;
UserExtraCapabilities extraCapabilities;
AvatarUrls avatarUrls;
// List<Null> meta;
Map<String, dynamic> meta;
Links lLinks;

User(
Expand All @@ -40,7 +40,7 @@ class User {
this.capabilities,
this.extraCapabilities,
this.avatarUrls,
// this.meta,
this.meta,
this.lLinks});

User.fromJson(Map<String, dynamic> json) {
Expand All @@ -67,12 +67,13 @@ class User {
avatarUrls = json['avatar_urls'] != null
? new AvatarUrls.fromJson(json['avatar_urls'])
: null;
/*if (json['meta'] != null) {
meta = new List<Null>();
json['meta'].forEach((v) {
meta.add(new Null.fromJson(v));

if (json['meta'] != null) {
meta = new Map<String, dynamic>();
json['meta'].forEach((k, v) {
meta[k] = v;
});
}*/
}
lLinks = json['_links'] != null ? new Links.fromJson(json['_links']) : null;
}

Expand Down Expand Up @@ -101,9 +102,9 @@ class User {
if (this.avatarUrls != null) {
data['avatar_urls'] = this.avatarUrls.toJson();
}
/*if (this.meta != null) {
data['meta'] = this.meta.map((v) => v.toJson()).toList();
}*/
if (this.meta != null) {
data['meta'] = this.meta;
}
if (this.lLinks != null) {
data['_links'] = this.lLinks.toJson();
}
Expand Down

0 comments on commit ef188be

Please sign in to comment.