Skip to content

Commit c8abe9a

Browse files
committed
Change some docs and README, PastebinClient constructors
1 parent 9e250fd commit c8abe9a

File tree

5 files changed

+60
-37
lines changed

5 files changed

+60
-37
lines changed

README.MD

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,18 @@ Multiplatform Kotlin library to interact with the pastebin.com API.
4949
implementation("net.pearx.kpastebin:kpastebin-PLATFORM_YOU_WANT:$kpastebinVersion") // for Native
5050
```
5151

52-
2. Use the library and have fun!
52+
2. Add [kotlinx.coroutines](https://github.com/Kotlin/kotlinx.coroutines) and any [Ktor client engine](https://ktor.io/clients/http-client/engines.html) to your project's dependencies.
53+
54+
3. Use the library and have fun! See [net.pearx.kpastebin.PastebinClient](src/commonMain/kotlin/net/pearx/kpastebin/PastebinClient.kt) class KDoc for more info.
55+
56+
# Examples
57+
```kotlin
58+
runBlocking {
59+
// see KDoc for more info
60+
val client = PastebinClient("your dev key") // create a PastebinClient instance. You can get your dev key here: https://pastebin.com/doc_api#1
61+
client.getPaste("0b42rwhf") // getting a paste
62+
63+
client.login("username", "password") // logging in
64+
client.getUserDetails().accountType // getting current user account type
65+
}
66+
```

src/commonMain/kotlin/net/pearx/kpastebin/PastebinClient.kt

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
package net.pearx.kpastebin
99

1010
import io.ktor.client.HttpClient
11+
import io.ktor.client.HttpClientConfig
1112
import io.ktor.client.engine.HttpClientEngine
13+
import io.ktor.client.engine.HttpClientEngineFactory
1214
import io.ktor.client.features.ClientRequestException
1315
import io.ktor.client.features.HttpResponseValidator
1416
import io.ktor.client.features.RedirectResponseException
@@ -26,30 +28,37 @@ import net.pearx.kpastebin.model.UserDetails
2628
* Pastebin API client with specified unique developer API key.
2729
* You can get your key on [official Pastebin website](https://pastebin.com/doc_api#1).
2830
*
29-
* @param engine Ktor [HttpClientEngine] to use
30-
* @param devKey Unique developer API key
31-
* @param userKey user key used for requests. Use null for guest user.
31+
* @property devKey Unique developer API key
32+
* @property engine Ktor [HttpClientEngine] to use. Use null to detect it automatically.
33+
* @property userKey User key used for requests. Use null for guest user. It also can be set using [login] method.
3234
*/
3335
public class PastebinClient(
34-
private val engine: HttpClientEngine,
3536
private val devKey: String,
36-
/**
37-
* User key used for requests. Use null for guest user. It also can be set using [login] method.
38-
*/
37+
private val engine: HttpClientEngine? = null,
3938
public var userKey: String? = null
4039
) {
41-
private val http = HttpClient(engine) {
42-
HttpResponseValidator { // https://youtrack.jetbrains.com/issue/KTOR-406
43-
validateResponse { response ->
44-
when (response.status.value) {
45-
in 300..399 -> throw RedirectResponseException(response)
46-
in 400..499 -> throw ClientRequestException(response)
47-
in 500..599 -> throw ServerResponseException(response)
40+
/**
41+
* Constructs a new [PastebinClient] instance using specified [engine], [devKey] and [userKey].
42+
* @see PastebinClient
43+
*/
44+
public constructor(devKey: String, engine: HttpClientEngineFactory<*>, userKey: String? = null): this(devKey, engine.create(), userKey)
45+
46+
private val http: HttpClient
47+
init {
48+
val config: HttpClientConfig<*>.() -> Unit = {
49+
HttpResponseValidator { // https://youtrack.jetbrains.com/issue/KTOR-406
50+
validateResponse { response ->
51+
when (response.status.value) {
52+
in 300..399 -> throw RedirectResponseException(response)
53+
in 400..499 -> throw ClientRequestException(response)
54+
in 500..599 -> throw ServerResponseException(response)
55+
}
4856
}
4957
}
50-
}
5158

52-
expectSuccess = false
59+
expectSuccess = false
60+
}
61+
http = if(engine == null) HttpClient(config) else HttpClient(engine, config)
5362
}
5463

5564
private suspend fun sendRequest(url: String, userKeyRequired: Boolean, userKey: String?, parameters: Parameters): String {

src/commonMain/kotlin/net/pearx/kpastebin/model/AccountType.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ public enum class AccountType {
1616

1717
/** Paid pro account. */
1818
PRO;
19-
}
19+
}

src/commonMain/kotlin/net/pearx/kpastebin/model/PasteDetails.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,27 @@ import net.pearx.kpastebin.internal.XML_PROPERTY_REGEX
1212

1313
/**
1414
* A detailed Pastebin paste information.
15+
* @property key Paste key.
16+
* @property date Paste publication date in Unix time format.
17+
* @property title Paste title.
18+
* @property size Paste size in bytes.
19+
* @property expireDate Paste expiration date in Unix time format.
20+
* @property privacy Paste privacy status.
21+
* @property formatLong Paste syntax highlighting language in a user-readable format (e.g., Kotlin or C#).
22+
* @property formatShort Paste syntax highlighting language in a short format (e.g. kotlin or csharp).
23+
* @property url Paste URL.
24+
* @property hits Paste view count.
1525
*/
1626
public data class PasteDetails(
17-
/** Paste key. */
1827
val key: String,
19-
/** Paste publication date in Unix time format. */
2028
val date: ULong,
21-
/** Paste title. */
2229
val title: String,
23-
/** Paste size in bytes. */
2430
val size: Int,
25-
/** Paste expiration date in Unix time format. */
2631
val expireDate: ULong,
27-
/** Paste privacy status. */
2832
val privacy: Privacy,
29-
/** Paste syntax highlighting language in a user-readable format (e.g., Kotlin or C#). */
3033
val formatLong: String,
31-
/** Paste syntax highlighting language in a short format (e.g. kotlin or csharp). */
3234
val formatShort: String,
33-
/** Paste URL. */
3435
val url: String,
35-
/** Paste view count. */
3636
val hits: Int
3737
) {
3838
internal companion object {

src/commonMain/kotlin/net/pearx/kpastebin/model/UserDetails.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,25 @@ import net.pearx.kpastebin.internal.XML_PROPERTY_REGEX
1212

1313
/**
1414
* A detailed Pastebin user information.
15+
* @property name Full user name.
16+
* @property defaultFormatShort Default paste syntax highlighting language for this user in a short format (e.g., kotlin or csharp).
17+
* @property defaultExpiration Default paste expiration duration for this user.
18+
* @property avatarUrl User avatar URL.
19+
* @property defaultPrivacy Default paste privacy status for this user.
20+
* @property website User website.
21+
* @property email User E-Mail.
22+
* @property location User location.
23+
* @property accountType User account type.
1524
*/
1625
public data class UserDetails(
17-
/** Full user name. */
1826
val name: String,
19-
/** Default paste syntax highlighting language for this user in a short format (e.g., kotlin or csharp). */
2027
val defaultFormatShort: String,
21-
/** Default paste expiration duration for this user. */
2228
val defaultExpiration: ExpireDate,
23-
/** User avatar URL. */
2429
val avatarUrl: String,
25-
/** Default paste privacy status for this user. */
2630
val defaultPrivacy: Privacy,
27-
/** User website. */
2831
val website: String,
29-
/** User E-Mail. */
3032
val email: String,
31-
/** User location. */
3233
val location: String,
33-
/** User account type. */
3434
val accountType: AccountType
3535
) {
3636
internal companion object {

0 commit comments

Comments
 (0)