Skip to content

Commit 126caaa

Browse files
committed
Some minor library changes, the HttpClient now should be provided to PastebinClient
1 parent 0f6fa82 commit 126caaa

File tree

9 files changed

+45
-79
lines changed

9 files changed

+45
-79
lines changed

build.gradle.kts

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ val projectChangelog: String by project
66
val projectDescription: String by project
77

88
val ktorVersion: String by project
9+
val coroutinesVersion: String by project
910

1011
val pearxRepoUsername: String? by project
1112
val pearxRepoPassword: String? by project
@@ -32,12 +33,6 @@ configure<MultiGradleExtension> {
3233
}
3334
}
3435

35-
repositories {
36-
maven {
37-
url = uri("https://dl.bintray.com/pdvrieze/maven")
38-
}
39-
}
40-
4136
kotlinMpp {
4237
explicitApi()
4338

@@ -48,39 +43,10 @@ kotlinMpp {
4843
}
4944
}
5045

51-
val androidMain by getting {
52-
dependencies {
53-
implementation("io.ktor:ktor-client-android:$ktorVersion")
54-
}
55-
}
56-
57-
val jvmMain by getting {
58-
dependencies {
59-
implementation("io.ktor:ktor-client-cio:$ktorVersion")
60-
}
61-
}
62-
63-
val jsMain by getting {
64-
dependencies {
65-
implementation("io.ktor:ktor-client-js:$ktorVersion")
66-
}
67-
}
68-
69-
val posixMain by getting {
70-
dependencies {
71-
implementation("io.ktor:ktor-client-curl:$ktorVersion")
72-
}
73-
}
74-
75-
val appleMobileMain by getting {
76-
dependencies {
77-
implementation("io.ktor:ktor-client-ios:$ktorVersion")
78-
}
79-
}
80-
81-
val jvmTest by getting {
46+
val commonTest by getting {
8247
dependencies {
83-
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.8")
48+
implementation("io.ktor:ktor-client-mock:$ktorVersion")
49+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
8450
}
8551
}
8652
}

gradle.properties

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ projectChangelog=
1010
projectDescription=Multiplatform Kotlin library to interact with the pastebin.com API.
1111
kotlinVersion=1.4.0
1212
githubReleaseVersion=2.2.12
13-
multigradleVersion=1.9.1
13+
multigradleVersion=1.9.2
1414

1515
#Java Stuff
1616
javaVersion=8
@@ -23,4 +23,5 @@ compileSdkVersion=android-29
2323
junitVersion=4.13
2424

2525
#Dependencies
26-
ktorVersion=1.4.0
26+
ktorVersion=1.4.0
27+
coroutinesVersion=1.3.8-native-mt-1.4.0-rc

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

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package net.pearx.kpastebin
22

3+
import io.ktor.client.HttpClient
4+
import io.ktor.client.features.ClientRequestException
5+
import io.ktor.client.request.get
36
import io.ktor.client.request.post
47
import io.ktor.http.*
58
import net.pearx.kpastebin.internal.*
@@ -12,35 +15,37 @@ import net.pearx.kpastebin.model.UserDetails
1215
* Pastebin API client with specified unique developer API key.
1316
* You can get your key on [official Pastebin website](https://pastebin.com/doc_api#1).
1417
*
18+
* @param http Ktor [HttpClient] to use
1519
* @param devKey Unique developer API key
1620
* @param userKey user key used for requests. Use null for guest user.
1721
*/
1822
public class PastebinClient(
23+
private val http: HttpClient,
1924
private val devKey: String,
2025
/**
2126
* User key used for requests. Use null for guest user. It also can be set using [login] method.
2227
*/
2328
public var userKey: String? = null
2429
) {
25-
private suspend fun sendRequest(url: String, userKeyRequired: Boolean, parameters: Parameters): String {
26-
val userKey = userKey
27-
if (userKeyRequired && userKey == null)
30+
private suspend fun sendRequest(url: String, userKeyRequired: Boolean, userKey: String?, parameters: Parameters): String {
31+
val usrKey = userKey ?: this.userKey
32+
if (userKeyRequired && usrKey == null)
2833
throw InvalidUserKeyException("The 'userKey' property is null. It can be initialized by setting it directly or using the 'login' function.")
29-
val out = Http.post<String> {
34+
val out = http.post<String> {
3035
contentType(ContentType.Application.FormUrlEncoded)
3136
this.url.takeFrom(url)
3237
body = Parameters.build {
3338
append("api_dev_key", devKey)
34-
if (userKey != null)
35-
append("api_user_key", userKey)
39+
if (usrKey != null)
40+
append("api_user_key", usrKey)
3641
appendAll(parameters)
3742
}.formUrlEncode()
3843
}
3944
checkPastebinResponse(out)
4045
return out
4146
}
4247

43-
private suspend inline fun sendRequest(url: String, userKeyRequired: Boolean, parametersBuilder: ParametersBuilder.() -> Unit) = sendRequest(url, userKeyRequired, Parameters.build(parametersBuilder))
48+
private suspend inline fun sendRequest(url: String, userKeyRequired: Boolean, userKey: String? = null, parametersBuilder: ParametersBuilder.() -> Unit) = sendRequest(url, userKeyRequired, userKey, Parameters.build(parametersBuilder))
4449

4550
/**
4651
* Publishes a new paste with specified [text].
@@ -69,7 +74,7 @@ public class PastebinClient(
6974
append("api_paste_code", text)
7075
append("api_paste_name", name)
7176
append("api_paste_format", format)
72-
append("api_privacy", privacy.ordinal.toString())
77+
append("api_paste_private", privacy.ordinal.toString())
7378
append("api_paste_expire_date", expireDate.code)
7479
}
7580
}
@@ -135,16 +140,26 @@ public class PastebinClient(
135140

136141
/**
137142
* Gets text of a paste by its [pasteKey].
143+
* If [userKey] is null, you can get only public pastes.
138144
*
139-
* @see net.pearx.kpastebin.getPaste
140-
*
141-
* @throws InvalidUserKeyException when [userKey] is null, expired or invalid.
145+
* @throws InvalidUserKeyException when [userKey] is expired or invalid.
142146
* @throws PasteNotFoundException when no paste with such [pasteKey] is found.
143147
*/
144148
public suspend fun getPaste(pasteKey: String): String {
145-
return sendRequest(API_URL_RAW, true) {
146-
append("api_option", "show_paste")
147-
append("api_paste_key", pasteKey)
148-
}
149+
val userKey = userKey // cache userKey because it can change
150+
return if (userKey == null)
151+
try {
152+
http.get { url.takeFrom("$URL_RAW/$pasteKey") }
153+
} catch(ex: ClientRequestException) {
154+
if(ex.response?.status == HttpStatusCode.NotFound)
155+
throw PasteNotFoundException(ex.message, ex)
156+
else
157+
throw ex
158+
}
159+
else
160+
sendRequest(API_URL_RAW, false, userKey) {
161+
append("api_option", "show_paste")
162+
append("api_paste_key", pasteKey)
163+
}
149164
}
150165
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package net.pearx.kpastebin
22

33
import net.pearx.kpastebin.model.Privacy
44

5+
/** Thrown by [PastebinClient] when provided dev key is invalid. */
6+
public class InvalidDevKeyException(message: String, cause: Throwable? = null) : PastebinException(message, cause)
7+
58
/** Thrown by [PastebinClient] when provided user key is invalid, expired or null. */
69
public class InvalidUserKeyException(message: String, cause: Throwable? = null) : PastebinException(message, cause)
710

@@ -27,10 +30,10 @@ public class InvalidLoginException(message: String, cause: Throwable? = null) :
2730
public class AccountNotActiveException(message: String, cause: Throwable? = null) : PastebinException(message, cause)
2831

2932
/** Thrown by [PastebinClient] when paste with specified key doesn't exist or you don't have permissions to view or delete it. */
30-
public class PasteNotFoundException(message: String, cause: Throwable? = null) : PastebinException(message, cause)
33+
public class PasteNotFoundException(message: String?, cause: Throwable? = null) : PastebinException(message, cause)
3134

3235
/** Thrown by [PastebinClient] when paste publication limit for current IP per day is exceeded. */
3336
public class PastePerDayLimitException(message: String, cause: Throwable? = null) : PastebinException(message, cause)
3437

3538
/** Generic Pastebin exception thrown by [PastebinClient]. */
36-
public open class PastebinException(message: String, cause: Throwable? = null) : RuntimeException(message, cause)
39+
public open class PastebinException(message: String?, cause: Throwable? = null) : RuntimeException(message, cause)

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

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/commonMain/kotlin/net/pearx/kpastebin/internal/ExceptionHelper.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ internal fun checkPastebinResponse(response: String) {
2121
"account not active" -> AccountNotActiveException(sub)
2222
"invalid permission to remove paste" -> PasteNotFoundException(sub)
2323
"invalid permission to view this paste or invalid api_paste_key" -> PasteNotFoundException(sub)
24+
"invalid api_dev_key" -> InvalidDevKeyException(sub)
2425
else -> PastebinException(sub)
2526
}
2627
}

src/commonMain/kotlin/net/pearx/kpastebin/internal/HttpClient.kt

Lines changed: 0 additions & 5 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public data class PasteDetails(
3131
internal companion object {
3232
fun parseList(input: String): List<PasteDetails> {
3333
val lst = mutableListOf<PasteDetails>()
34-
// it's a hack because currently there's no multiplatform API to parse XML with Kotlin/Native support
34+
// it's a hack because currently there's no multiplatform API to parse XML with Kotlin/Native support. xmlutil doesn't support Kotlin/Native :(.
3535
for (paste in XML_PARENT_REGEX.findAll(input)) {
3636
if (paste.groupValues[1] == "paste") {
3737
val map = XML_PROPERTY_REGEX.findAll(paste.groupValues[2]).associate { it.groupValues[1].substring(6) to it.groupValues[2] } // .substring(6) is here to cut the 'paste_' part of each property.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public data class UserDetails(
2828
) {
2929
internal companion object {
3030
fun parse(input: String): UserDetails {
31-
// it's a hack because currently there's no multiplatform API to parse XML with Kotlin/Native support
31+
// it's a hack because currently there's no multiplatform API to parse XML with Kotlin/Native support. xmlutil doesn't support Kotlin/Native :(.
3232
val user = XML_PARENT_REGEX.matchEntire(input)
3333
if (user != null && user.groupValues[1] == "user") {
3434
val map = XML_PROPERTY_REGEX.findAll(user.groupValues[2]).associate { it.groupValues[1].substring(5) to it.groupValues[2] } // .substring(5) is here to cut the 'user_' part of each property.

0 commit comments

Comments
 (0)