Home

Awesome

日本語

kbsky

<!-- ![Maven metadata URL](https://img.shields.io/maven-metadata/v?metadataUrl=https%3A%2F%2Frepo.repsy.io%2Fmvn%2Fuakihir0%2Fpublic%2Fwork%2Fsocialhub%2Fkbsky%2Fcore%2Fmaven-metadata.xml) -->

Maven Central Version

badge badge badge

This library is a Bluesky/ATProtocol client library compatible with Kotlin Multiplatform. It depends on khttpclient and uses Ktor Client internally. Therefore, this library can be used on any platform supported by Kotlin Multiplatform and Ktor Client. The behavior on each platform depends on khttpclient.

Usage

Below is how to use it with Kotlin on the supported platforms using Gradle.
If you are using it on an Apple platform, please refer to kbsky-cocoapods.
Additionally, please check the test code as well.

Stable (Maven Central)

repositories {
    mavenCentral()
}

dependencies {
+   implementation("work.socialhub.kbsky:core:0.2.0")
+   implementation("work.socialhub.kbsky:stream:0.2.0")
}

Snapshot

repositories {
+   maven { url = uri("https://repo.repsy.io/mvn/uakihir0/public") }
}

dependencies {
+   implementation("work.socialhub.kbsky:core:0.3.0-SNAPSHOT")
+   implementation("work.socialhub.kbsky:stream:0.3.0-SNAPSHOT")
}

Starting a Session

Authentication with Password

To start a session by specifying a handle and password, do as follows:

val response = BlueskyFactory
    .instance(BSKY_SOCIAL.uri)
    .server()
    .createSession(
        ServerCreateSessionRequest().also {
            it.identifier = HANDLE
            it.password = PASSWORD
        }
    )

println(response.data.accessJwt)

To access various resources with the obtained access token, execute the following:

val auth = BearerTokenAuthProvider(accessJwt)

BlueskyFactory
    .instance(BSKY_SOCIAL.uri)
    .feed()
    .post(
        FeedPostRequest(auth).also {
            it.text = "Hello World!"
        }
    )

Authentication with OAuth

Authentication using a password is gradually being replaced with OAuth. For more details on OAuth authentication, please refer to Authentication via OAuth.

PLC Directory

val response = PLCDirectoryFactory
    .instance()
    .DIDDetails(did)

println(checkNotNull(response.data.alsoKnownAs)[0])

SubscribeRepos (stream)

val stream = ATProtocolStreamFactory
    .instance(
        apiUri = BSKY_SOCIAL.uri,
        streamUri = BSKY_NETWORK.uri
    )
    .sync()
    .subscribeRepos(
        SyncSubscribeReposRequest().also {
            it.filter = listOf(
                "app.bsky.feed.post"
            )
        }
    )

stream.eventCallback(
    object : EventCallback {
        override fun onEvent(
            cid: String?,
            uri: String?,
            record: RecordUnion
        ) {
            print(record)
        }
    })

License

MIT License

Author

Akihiro Urushihara