-
Notifications
You must be signed in to change notification settings - Fork 14
Usage
Easily integrate WordPress functionalities into your Dart project with the wordpress_client
package.
-
Dependency Installation: Add
wordpress_client
to your project'spubspec.yaml
file. Remember to always check the Package Page to grab the most recent version.dependencies: wordpress_client: {latest}
-
Library Import: Integrate the library into the desired Dart class.
import 'package:wordpress_client/wordpress_client.dart';
-
Client Initialization: There are two methods for initializing the
WordpressClient
. Whichever method you choose, it's advised to initialize the client once and store its instance for subsequent usage.- π’ Simple Method: Quick initialization using default values.
- π΅ Advanced Method: Configurable initialization with the Bootstrapper for advanced settings (e.g., User Agent, Authorization).
final baseUrl = Uri.parse('https://example.com/wp-json/wp/v2');
final client = WordpressClient(baseUrl: baseUrl);
// Typically invoked during app initialization (e.g., on a splash screen)
client.initialize();
final baseUrl = Uri.parse('https://example.com/wp-json/wp/v2');
final client = WordpressClient(
baseUrl: baseUrl,
bootstrapper: (bootstrapper) => bootstrapper
.withStatisticDelegate((baseUrl, requestCount) {
print('$baseUrl -> $requestCount');
})
.withDebugMode(true)
.withDefaultAuthorization(
UsefulJwtAuth(
userName: 'username',
password: 'password',
),
)
.build(),
);
client.initialize();
Once set up, you're ready to interact with the WordPress REST API. For instance, to fetch the latest 20 posts from your WordPress site in ascending order:
final request = ListPostRequest(
page: 1,
perPage: 20,
order: Order.asc,
);
final wpResponse = await client.posts.list(request);
// Dart 3 switch style
switch (wpResponse) {
case WordpressSuccessResponse():
final data = wpResponse.data; // List<Post>
break;
case WordpressFailureResponse():
final error = wpResponse.error; // WordpressError
break;
}
// Alternatively, using the wordpress_client style
final result = wpResponse.map(
onSuccess: (response) {
print(response.message);
return response.data;
},
onFailure: (response) {
print(response.error.toString());
return <Post>[];
},
);
The WordpressResponse
class encapsulates the result object (either success or error) and offers access to metadata related to the response, such as request duration, status codes, and total pages.
Once you have a WordpressResponse
, it's crucial to differentiate between WordpressSuccessResponse
and WordpressFailureResponse
. There are methods and getters in the class to assist with this.
- For a
WordpressSuccessResponse
, the response's data is found in thedata
property. - For a
WordpressFailureResponse
, error details are available in theerror
property.
In the traditional approach of using model classes from the library, it may not be possible to integrate the package with database solutions to either cache or store the received response. Raw requests helps in this sector where it simplifies adding a new custom model class for the requests. It does so by not parsing the received response inside the library but offload it to the developer.
To send a raw request, you can use the method which ends with Raw
in respective interface. for example, on posts
, simply call posts.listRaw(...)
with the associated request class to get a raw response. The raw response contains the body which is received from the API, and other details such as duration, headers, extra data etc. It also contains many helpful methods to map the response to the required model class.
final WordpressRawResponse response = await client.posts.listRaw(
ListPostRequest(
perPage: 5,
order: Order.asc,
),
);