Skip to content

Commit 7b50d6c

Browse files
Implemented authorization function and fetching of Posts with parameters.
1 parent 06e2497 commit 7b50d6c

18 files changed

Lines changed: 782 additions & 340 deletions

.idea/workspace.xml

Lines changed: 170 additions & 130 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: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,31 +51,50 @@ class PostsBuilder extends StatefulWidget {
5151

5252
class PostsBuilderState extends State<PostsBuilder> {
5353
wp.WordPress wordPress;
54-
Future<List<wp.Posts>> posts;
55-
Future<List<wp.Users>> users;
54+
Future<List<wp.Post>> posts;
55+
Future<List<wp.User>> users;
5656

5757
@override
5858
void initState() {
5959
super.initState();
6060

61-
wordPress = wp.WordPress(
62-
'https://wordpress.dsoft.website',
63-
wp.WordPressAuthenticator.ApplicationPasswords,
64-
wp.WordPressContext.edit);
61+
try {
62+
wordPress = wp.WordPress(
63+
baseUrl: 'http://192.168.6.165',
64+
authenticator: wp.WordPressAuthenticator.ApplicationPasswords,
65+
adminName: 'admin',
66+
adminKey: 'EOjD JsYA hKfM RHNI vufW hyUX',
67+
);
68+
} catch (err) {
69+
print(err);
70+
}
6571

66-
Future<wp.AuthResponse> auth = wordPress.authenticateUser(
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+
});*/
80+
81+
fetchPosts();
82+
83+
/* Future<wp.JWTResponse> auth = wordPress.authenticateUser(
6784
username: 'admin', password: 'mypassword@123');
6885
6986
auth.then((response) {
7087
fetchPosts();
7188
}).catchError((err) {
7289
print(err.message);
73-
});
90+
});*/
7491
}
7592

7693
void fetchPosts() {
7794
setState(() {
78-
posts = wordPress.fetchPosts();
95+
posts = wordPress.fetchPosts(
96+
params:
97+
wp.ParamsPostList(order: wp.Order.asc, includeAuthorIDs: [1, 2]));
7998
});
8099
}
81100

@@ -87,22 +106,35 @@ class PostsBuilderState extends State<PostsBuilder> {
87106

88107
@override
89108
Widget build(BuildContext context) {
90-
return FutureBuilder<List<wp.Posts>>(
109+
return FutureBuilder<List<wp.Post>>(
91110
future: posts,
92111
builder: (context, snapshot) {
93112
if (snapshot.hasData) {
94113
return ListView.separated(
95114
itemBuilder: (context, i) {
96-
return Text(snapshot.data[i].title.rendered);
115+
return Column(
116+
crossAxisAlignment: CrossAxisAlignment.start,
117+
children: <Widget>[
118+
Text(
119+
snapshot.data[i].title.rendered,
120+
style: Theme.of(context).textTheme.title,
121+
),
122+
Text(
123+
snapshot.data[i].content.rendered,
124+
)
125+
],
126+
);
97127
},
98128
separatorBuilder: (context, i) {
99129
return Divider();
100130
},
101131
itemCount: snapshot.data.length,
102132
);
103133
} else if (snapshot.hasError) {
104-
wp.WordPressError err = snapshot.error as wp.WordPressError;
105-
return Text(err.message);
134+
return Text(
135+
snapshot.error.toString(),
136+
style: TextStyle(color: Colors.red),
137+
);
106138
}
107139
return CircularProgressIndicator();
108140
},

lib/constants.dart

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
const URL_WP_BASE = '/wp-json/wp/v2';
2+
const URL_JWT_BASE = '/wp-json/jwt-auth/v1';
3+
4+
const URL_JWT_TOKEN = '$URL_JWT_BASE/token';
5+
const URL_JWT_TOKEN_VALIDATE = '$URL_JWT_BASE/token/validate';
6+
7+
const URL_POSTS = '$URL_WP_BASE/posts';
8+
const URL_USERS = '$URL_WP_BASE/users';
9+
const URL_COMMENTS = '$URL_WP_BASE/comments';
10+
11+
enum WordPressAuthenticator {
12+
JWT,
13+
ApplicationPasswords,
14+
}
15+
enum WordPressContext { view, embed, edit }
16+
17+
enum Order {
18+
asc,
19+
desc,
20+
}
21+
22+
enum PostsOrderBy {
23+
author,
24+
date,
25+
id,
26+
include,
27+
modified,
28+
parent,
29+
relevance,
30+
slug,
31+
title,
32+
}
33+
enum PostStatus {
34+
publish,
35+
future,
36+
draft,
37+
pending,
38+
private,
39+
}
40+
enum PostCommentStatus {
41+
open,
42+
closed,
43+
}
44+
enum PostPingStatus {
45+
open,
46+
closed,
47+
}
48+
enum ObjectFormat {
49+
standard,
50+
aside,
51+
chat,
52+
gallery,
53+
link,
54+
image,
55+
quote,
56+
status,
57+
video,
58+
audio,
59+
}
60+
61+
enum UsersOrderBy {
62+
id,
63+
include,
64+
name,
65+
registered_date,
66+
slug,
67+
email,
68+
url,
69+
}
70+
enum UserRole {
71+
subscriber,
72+
contributor,
73+
author,
74+
editor,
75+
administrator,
76+
}
77+
78+
enum CommentsOrderBy {
79+
date,
80+
date_gmt,
81+
id,
82+
include,
83+
post,
84+
parent,
85+
type,
86+
}
87+
enum CommentStatus {
88+
all,
89+
approve,
90+
hold,
91+
spam,
92+
trash,
93+
}
94+
95+
String enumStringToName(String enumString) {
96+
return enumString.split('.')[1];
97+
}
98+
99+
String listToUrlString<T>(List<T> items) {
100+
if (items == null || items.length == 0) return '';
101+
102+
return items.join(',');
103+
}
104+
105+
String constructUrlParams(Map<String, String> params) {
106+
StringBuffer p = new StringBuffer('/?');
107+
params.forEach((key, value) {
108+
if (value != '') {
109+
p.write('$key=$value');
110+
p.write('&');
111+
}
112+
});
113+
return p.toString();
114+
}

0 commit comments

Comments
 (0)