Skip to content

Latest commit

 

History

History
136 lines (115 loc) · 3.53 KB

README_EN.md

File metadata and controls

136 lines (115 loc) · 3.53 KB
📖 English Documentation | 📖 中文文档

WordPress SDK

GitHub license GitHub release

WP-SDK library built in Kotlin, only supported Application Passwords authentication.

Dependency

Maven:

<dependency>
    <groupId>com.xzakota.wordpress</groupId>
    <artifactId>sdk-kt</artifactId>
    <version>${version}</version>
</dependency>

Gradle:

dependencies {
    implementation 'com.xzakota.wordpress:sdk-kt:${version}'
}

Usage

Java:

WPClient client = new WPClient(server, new Authentication(username, appPwd));
// connect
client.connect();
// connect and test, failure to pass the test will throw an exception
// client.connectTest();

// Send request
// ...

// 断开连接
client.disconnect();

Kotlin:

val client = WPClient(server, Authentication(username, appPwd))
// connect
client.connect()
// connect and test, failure to pass the test will throw an exception
// client.connectTest()

// send request
// ...

// disconnect
client.disconnect()

Page action

Java:

client.request().pages(router -> {
    List<Page> pages = router.list();
    println("List of Page: ");
    println(pages);

    Page page = new Page();
    page.setTitle(new RenderedField("New page title"));
    page.setContent(new RenderedField("New page content"));

    Page createdPage = router.create(page);
    if (createdPage != null) {
        println("Created page id: " + createdPage.getId());
    }
});

Kotlin:

client.request().pages { router ->
    val pages = router.list()
    println("List of Page: ")
    println(pages)

    router.create(Page().apply {
        title = RenderedField("New page title")
        content = RenderedField("New page content")
    })?.let { createdPage ->
        println("Created page id: " + createdPage.id)
    }
}

Post action

Java:

client.request().posts(router -> {
    Post post = router.retrieveById(1L);
    if (post != null) {
        println("Post with ID 1: $post");

        post.setPassword("123456");
        if (post.getId() != null) {
            router.updateById(post.getId(), post);
        }
    } else {
        println("No post with ID 1.");
    }
});

Kotlin:

client.request().posts { router ->
    val post = router.retrieveById(1L)
    if (post != null) {
        println("Post with ID 1: $post")

        post.password = "123456"
        if (post.id != null) {
            router.updateById(post.id!!, post)
        }
    } else {
        println("No post with ID 1.")
    }
}

Other action

For other uses, please check out the test unit or browse the source code:

  1. Java Test
  2. Kotlin Test

Thanks