From 4e30d6fdd15b21052e4ed75e12b84fbbaec4c1e7 Mon Sep 17 00:00:00 2001 From: Afiq Date: Tue, 23 Jun 2020 00:29:07 +0800 Subject: [PATCH 1/2] Fixed createUser request, fix user toJson deserialization in User model --- .../ios/Flutter/flutter_export_environment.sh | 8 +++---- example/lib/display_posts.dart | 21 ++++++++++++++++--- example/lib/login.dart | 2 +- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/example/ios/Flutter/flutter_export_environment.sh b/example/ios/Flutter/flutter_export_environment.sh index 89441eb..182db67 100755 --- a/example/ios/Flutter/flutter_export_environment.sh +++ b/example/ios/Flutter/flutter_export_environment.sh @@ -1,12 +1,12 @@ #!/bin/sh # This is a generated file; do not edit or check into version control. -export "FLUTTER_ROOT=/Users/sachin/Documents/devtools/flutter" -export "FLUTTER_APPLICATION_PATH=/Users/sachin/Projects/2019/Flutter/Plugins/flutter_wordpress/example" -export "FLUTTER_TARGET=/Users/sachin/Projects/2019/Flutter/Plugins/flutter_wordpress/example/lib/main.dart" +export "FLUTTER_ROOT=/Users/afiqhamdan/Development/flutter" +export "FLUTTER_APPLICATION_PATH=/Users/afiqhamdan/Desktop/flutter_wordpress/example" +export "FLUTTER_TARGET=/Users/afiqhamdan/Desktop/flutter_wordpress/example/lib/main.dart" export "FLUTTER_BUILD_DIR=build" export "SYMROOT=${SOURCE_ROOT}/../build/ios" export "OTHER_LDFLAGS=$(inherited) -framework Flutter" -export "FLUTTER_FRAMEWORK_DIR=/Users/sachin/Documents/devtools/flutter/bin/cache/artifacts/engine/ios" +export "FLUTTER_FRAMEWORK_DIR=/Users/afiqhamdan/Development/flutter/bin/cache/artifacts/engine/ios" export "FLUTTER_BUILD_NAME=1.0.0" export "FLUTTER_BUILD_NUMBER=1" export "TRACK_WIDGET_CREATION=true" diff --git a/example/lib/display_posts.dart b/example/lib/display_posts.dart index 076b254..985f287 100644 --- a/example/lib/display_posts.dart +++ b/example/lib/display_posts.dart @@ -73,19 +73,33 @@ class PostsBuilderState extends State { // yahya - Future createUser({@required String email, @required String username, @required String password, @required List roles}) async { + Future createUser({@required String email, @required String username, @required String password, List roles}) async { + await widget.wordPress.createUser( user: wp.User( email: email, password: password, username: username, - roles: roles + // roles: roles ) ).then((p) { print('User created successfully ${p}'); }).catchError((err) { print('Failed to create user: $err'); }); + + // await widget.wordPress.createUser2( + // user: wp.User( + // email: email, + // password: password, + // username: username, + // roles: roles + // ) + // ).then((p) { + // print('User created successfully ${p}'); + // }).catchError((err) { + // print('Failed to create user: $err'); + // }); } // ===================== @@ -93,6 +107,7 @@ class PostsBuilderState extends State { // ===================== Future updatePost({@required int id, @required int userId}) async { + await widget.wordPress.updatePost( post: new wp.Post( title: 'First post as a Chief Editor', @@ -361,7 +376,7 @@ class PostsBuilderState extends State { RaisedButton.icon( color: Colors.blueAccent, onPressed: () { - createUser(roles: ["subscriber"], username: "myUserName", password: "123", email: "myEmail@domain.com"); + createUser(username: "myUserName", password: "123", email: "myEmail@domain.com"); }, icon: Icon(Icons.add_circle, color: Colors.white,), label: Text( diff --git a/example/lib/login.dart b/example/lib/login.dart index 1ed8d43..645a6cf 100644 --- a/example/lib/login.dart +++ b/example/lib/login.dart @@ -148,7 +148,7 @@ class LoginFieldsState extends State { }); wp.WordPress wordPress = new wp.WordPress( - baseUrl: 'YOUR WEBSITE URL', + baseUrl: 'https://bmf.marketplaced.my/map/', authenticator: wp.WordPressAuthenticator.JWT, adminName: '', adminKey: '', From 1fc0196d47d564f7d9570a711e14893ed83e2222 Mon Sep 17 00:00:00 2001 From: Afiq Date: Tue, 23 Jun 2020 00:34:47 +0800 Subject: [PATCH 2/2] Fix createUser request, fix user toJson deserialization in User model --- example/lib/login.dart | 2 +- lib/flutter_wordpress.dart | 44 ++++++++++++++++----------------- lib/schemas/user.dart | 50 +++++++++++++++++++++++--------------- 3 files changed, 54 insertions(+), 42 deletions(-) diff --git a/example/lib/login.dart b/example/lib/login.dart index 645a6cf..dfcd131 100644 --- a/example/lib/login.dart +++ b/example/lib/login.dart @@ -148,7 +148,7 @@ class LoginFieldsState extends State { }); wp.WordPress wordPress = new wp.WordPress( - baseUrl: 'https://bmf.marketplaced.my/map/', + baseUrl: '', authenticator: wp.WordPressAuthenticator.JWT, adminName: '', adminKey: '', diff --git a/lib/flutter_wordpress.dart b/lib/flutter_wordpress.dart index 51d8a14..9c4694b 100644 --- a/lib/flutter_wordpress.dart +++ b/lib/flutter_wordpress.dart @@ -257,7 +257,8 @@ class WordPress { bool fetchAttachments = false, String postType = "posts", }) async { - final StringBuffer url = new StringBuffer(_baseUrl + URL_WP_BASE + "/" + postType); + final StringBuffer url = + new StringBuffer(_baseUrl + URL_WP_BASE + "/" + postType); url.write(postParams.toString()); @@ -651,33 +652,32 @@ class WordPress { } } -// yahya - @mymakarim + // Map UserJson(@required User user) => { + // 'username': user.username, + // 'email': user.email, + // 'password': user.password, + // // 'Roles': _improvement, + // }; - async.Future createUser({@required User user}) async { + async.Future createUser({@required User user}) async { final StringBuffer url = new StringBuffer(_baseUrl + URL_USERS); - HttpClient httpClient = new HttpClient(); - HttpClientRequest request = - await httpClient.postUrl(Uri.parse(url.toString())); - request.headers - .set(HttpHeaders.contentTypeHeader, "application/json; charset=UTF-8"); - request.headers.set(HttpHeaders.acceptHeader, "application/json"); - request.headers.set('Authorization', "${_urlHeader['Authorization']}"); - - request.add(utf8.encode(json.encode(user.toJson()))); - HttpClientResponse response = await request.close(); + final response = await http.post( + url.toString(), + headers: _urlHeader, + body: user.toJson(), + ); if (response.statusCode >= 200 && response.statusCode < 300) { - return true; + return User.fromJson(json.decode(response.body)); } else { - response.transform(utf8.decoder).listen((contents) { - try { - WordPressError err = WordPressError.fromJson(json.decode(contents)); - throw err; - } catch (e) { - throw new WordPressError(message: contents); - } - }); + try { + WordPressError err = + WordPressError.fromJson(json.decode(response.body)); + throw err; + } catch (e) { + throw new WordPressError(message: response.body); + } } } diff --git a/lib/schemas/user.dart b/lib/schemas/user.dart index 389d4d6..02658bd 100644 --- a/lib/schemas/user.dart +++ b/lib/schemas/user.dart @@ -84,20 +84,27 @@ class User { Map toJson() { final Map data = new Map(); - data['id'] = this.id; - data['username'] = this.username; - data['name'] = this.name; - data['first_name'] = this.firstName; - data['last_name'] = this.lastName; - data['email'] = this.email; - data['url'] = this.url; - data['description'] = this.description; - data['link'] = this.link; - data['locale'] = this.locale; - data['nickname'] = this.nickname; - data['slug'] = this.slug; - data['roles'] = this.roles; - data['registered_date'] = this.registeredDate; + + // afiq @afiqnymous + if (this.id != null) data['id'] = this.id; + if (this.username != null) data['username'] = this.username; + if (this.password != null) data['password'] = this.password; + if (this.name != null) data['name'] = this.name; + if (this.firstName != null) data['first_name'] = this.firstName; + if (this.lastName != null) data['last_name'] = this.lastName; + if (this.email != null) data['email'] = this.email; + if (this.url != null) data['url'] = this.url; + if (this.description != null) data['description'] = this.description; + if (this.link != null) data['link'] = this.link; + if (this.locale != null) data['locale'] = this.locale; + if (this.nickname != null) data['nickname'] = this.nickname; + if (this.link != null) data['link'] = this.link; + if (this.slug != null) data['slug'] = this.slug; + // if (this.roles != null) data['roles'] = this.roles; + // if (json['roles'] != null) roles = json['roles'].cast(); + + if (this.registeredDate != null) data['registered_date'] = this.registeredDate; + if (this.capabilities != null) { data['capabilities'] = this.capabilities.toJson(); } @@ -113,14 +120,19 @@ class User { if (this.lLinks != null) { data['_links'] = this.lLinks.toJson(); } -// yahya - @mymakarim - if (this.password != null) { - data['password'] = this.password; - } -// end yahya - @mymakarim + return data; } + // Map toJson() { + // final Map data = new Map(); + // if (this.username != null) data['username'] = this.username; + // if (this.password != null) data['password'] = this.password; + // if (this.email != null) data['email'] = this.email; + + // return data; + // } + @override String toString() { return 'id: $id, name: $name';