ArunPrakashG / wordpress_client

A powerful and easy-to-use WordPress REST API client for Dart & Flutter.
MIT License
32 stars 12 forks source link
dart dart-library flutter hacktoberfest hacktoberfest2021 wordpress

WordPress Client

Pub Version
Dart Flutter WordPress
A powerful and easy-to-use WordPress REST API client for Dart & Flutter.

✨ Features

Future

If you find any functionality which you require is missing from the package and you are not able to work it out using built in options like raw requests etc, then please share the functionality in details as a comment here: https://github.com/ArunPrakashG/wordpress_client/discussions/55

πŸ“– How to Use

1. Setup

Add wordpress_client in your pubspec.yaml:

dependencies:
 wordpress_client: ^8.4.8

πŸ’‘ Ensure you get the latest version here.

Import the package where you need:

import 'package:wordpress_client/wordpress_client.dart';

2. Initialization

You can initialize WordpressClient in two methods:

Simple Method:

final baseUrl = Uri.parse('https://example.com/wp-json/wp/v2');
final client = WordpressClient(baseUrl: baseUrl);

πŸ“˜ Learn more about the Advanced Method here.

3. Sending Requests

Example to retrieve 20 recent WordPress posts in ascending order:

final request = ListPostRequest(
  page: 1,
  perPage: 20,
  order = Order.asc,
);

final response = await client.posts.list(request);

// Dart 3 style
switch (response) {
    case WordpressSuccessResponse():
      final data = response.data; // List<Post>
      break;
    case WordpressFailureResponse():
      final error = response.error; // WordpressError
      break;
}

// or
// using map method to handle both success and failure
final result = response.map(
    onSuccess: (response) {
      // response is a WordpressSuccessResponse<List<Post>>

      print(response.message);
      return response.data;
    },
    onFailure: (response) {
      // response is a WordpressFailureResponse
      print(response.error.toString());
      return <Post>[];
    },
);

// or
// you can cast to a state directly; this will throw an error if the response is of the wrong type
final result = response.asSuccess(); // or response.asFailure();

Refer to the documentation for more request examples.

πŸ”’ Supported Authorization

1. AppPasswordAuth

By the WordPress Team, this method uses basic HTTP authentication where credentials are passed with every request. Details

2. BasicJwtAuth

Developed by Enrique Chavez, it involves JSON Web Token (JWT) authentication where a token is issued and then used in subsequent requests. Details

3. UsefulJwtAuth

By Useful Team, this is another implementation using JWT for authentication purposes. Details

For custom authorization, check the Authorization Wiki.

πŸ“‹ Supported REST Methods

Endpoint Create Read Update Delete
Posts βœ… βœ… βœ… βœ…
Comments βœ… βœ… βœ… βœ…
Categories βœ… βœ… βœ… βœ…
Tags βœ… βœ… βœ… βœ…
Users βœ… βœ… βœ… βœ…
Me βœ… βœ… βœ… βœ…
Media βœ… βœ… βœ… βœ…
Pages βœ… βœ… βœ… βœ…
Application Passwords βœ… βœ… βœ… βœ…
Search - βœ… - -
Post Revisions ❌ ❌ ❌ ❌
Taxonomies ❌ ❌ ❌ ❌
Post Types ❌ ❌ ❌ ❌
Post Statuses ❌ ❌ ❌ ❌
Settings ❌ ❌ ❌ ❌

πŸ“’ Custom Response Types

Learn how to implement Custom Requests here.

πŸ“£ Feedback

πŸ“œ License

Licensed under MIT.


Support Me: [![Buy Me A Coffee](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/arunprakashg)