Skip to content

Commit 1a84829

Browse files
committed
Remove kotlinx-serialization dependency, implement UserDetails and PasteDetails serializers using regular expressions
1 parent 10ec83d commit 1a84829

File tree

11 files changed

+79
-130
lines changed

11 files changed

+79
-130
lines changed

build.gradle.kts

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

88
val ktorVersion: String by project
9-
val xmlutilVersion: String by project
10-
val kotlinxSerializationVersion: String by project
119

1210
val pearxRepoUsername: String? by project
1311
val pearxRepoPassword: String? by project
@@ -20,7 +18,6 @@ val devBuildNumber: String? by project
2018
plugins {
2119
id("net.pearx.multigradle.simple.project")
2220
kotlin("multiplatform") apply (false)
23-
kotlin("plugin.serialization")
2421
id("com.github.breadmoirai.github-release")
2522
`maven-publish`
2623
signing
@@ -48,8 +45,6 @@ kotlinMpp {
4845
val commonMain by getting {
4946
dependencies {
5047
implementation("io.ktor:ktor-client-core:$ktorVersion")
51-
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:$kotlinxSerializationVersion")
52-
implementation("net.devrieze:xmlutil-serialization:$xmlutilVersion")
5348
}
5449
}
5550

gradle.properties

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,4 @@ compileSdkVersion=android-29
2929
junitVersion=4.13
3030

3131
#Dependencies
32-
ktorVersion=1.4.0
33-
xmlutilVersion=0.20.0.1
34-
kotlinxSerializationVersion=1.0.0-rc
32+
ktorVersion=1.4.0

settings.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ pluginManagement {
77

88
plugins {
99
kotlin("multiplatform") version kotlinVersion
10-
kotlin("plugin.serialization") version kotlinVersion
1110
id("net.pearx.multigradle.simple.project") version multigradleVersion
1211
id("net.pearx.multigradle.simple.settings") version multigradleVersion
1312
id("com.github.breadmoirai.github-release") version githubReleaseVersion

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

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ import io.ktor.client.HttpClient
44
import io.ktor.client.features.defaultRequest
55
import io.ktor.client.request.post
66
import io.ktor.http.*
7-
import kotlinx.serialization.SerialName
8-
import kotlinx.serialization.Serializable
9-
import net.pearx.kpastebin.internal.*
7+
import net.pearx.kpastebin.internal.API_URL_LOGIN
8+
import net.pearx.kpastebin.internal.API_URL_POST
9+
import net.pearx.kpastebin.internal.API_URL_RAW
10+
import net.pearx.kpastebin.internal.checkPastebinResponse
1011
import net.pearx.kpastebin.model.ExpireDate
1112
import net.pearx.kpastebin.model.PasteDetails
1213
import net.pearx.kpastebin.model.Privacy
1314
import net.pearx.kpastebin.model.UserDetails
14-
import nl.adaptivity.xmlutil.serialization.XML
15-
import nl.adaptivity.xmlutil.serialization.XmlSerialName
1615

1716
public class PastebinClient(private val devKey: String) {
1817
private val http = HttpClient {
@@ -21,8 +20,6 @@ public class PastebinClient(private val devKey: String) {
2120
}
2221
}
2322

24-
private val xml = XML()
25-
2623
private suspend fun sendRequest(url: String, parameters: Parameters): String {
2724
val out = http.post<String> {
2825
this.url.takeFrom(url)
@@ -50,13 +47,13 @@ public class PastebinClient(private val devKey: String) {
5047
append("api_paste_code", text)
5148
if (userKey != null)
5249
append("api_user_key", userKey)
53-
if(name != null)
50+
if (name != null)
5451
append("api_paste_name", name)
55-
if(format != null)
52+
if (format != null)
5653
append("api_paste_format", format)
57-
if(privacy != null)
54+
if (privacy != null)
5855
append("api_privacy", privacy.ordinal.toString())
59-
if(expireDate != null)
56+
if (expireDate != null)
6057
append("api_paste_expire_date", expireDate.code)
6158
}
6259
}
@@ -72,18 +69,14 @@ public class PastebinClient(private val devKey: String) {
7269
val out = sendRequest(API_URL_POST) {
7370
append("api_user_key", userKey)
7471
append("api_option", "list")
75-
if(resultsLimit != null)
72+
if (resultsLimit != null)
7673
append("api_results_limit", resultsLimit.toString())
7774
}
7875
if (out == "No pastes found.")
7976
return listOf()
80-
return xml.parse(ListPastesResponse.serializer(), "<root>$out</root>").data
77+
return PasteDetails.parseList(out)
8178
}
8279

83-
@Serializable
84-
@SerialName("root")
85-
private data class ListPastesResponse(@XmlSerialName("paste", "", "") val data: List<PasteDetails>)
86-
8780
public suspend fun deletePaste(userKey: String, pasteKey: String) {
8881
val out = sendRequest(API_URL_POST) {
8982
append("api_option", "delete")
@@ -95,11 +88,12 @@ public class PastebinClient(private val devKey: String) {
9588
}
9689

9790
public suspend fun getUserDetails(userKey: String): UserDetails {
98-
val out = sendRequest(API_URL_POST) {
99-
append("api_option", "userdetails")
100-
append("api_user_key", userKey)
101-
}
102-
return xml.parse(UserDetails.serializer(), out)
91+
return UserDetails.parse(
92+
sendRequest(API_URL_POST) {
93+
append("api_option", "userdetails")
94+
append("api_user_key", userKey)
95+
}
96+
)
10397
}
10498

10599
public suspend fun getPaste(userKey: String, pasteKey: String): String {
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package net.pearx.kpastebin.internal
22

3-
internal const val MODEL_PACKAGE = "net.pearx.kpastebin.model"
4-
53
internal const val API_URL = "https://pastebin.com/api"
64
internal const val API_URL_POST = "$API_URL/api_post.php"
75
internal const val API_URL_LOGIN = "$API_URL/api_login.php"
86
internal const val API_URL_RAW = "$API_URL/api_raw.php"
9-
internal const val URL_RAW = "https://pastebin.com/raw"
7+
internal const val URL_RAW = "https://pastebin.com/raw"
8+
9+
internal val XML_PROPERTY_REGEX = Regex("<([a-zA-Z_]+)>(.*)</\\1>")
10+
internal val XML_PARENT_REGEX = Regex("<([a-zA-Z]+)>((?:.|\r\n|\r|\n)+?)</\\1>")

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

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
package net.pearx.kpastebin.model
22

3-
import kotlinx.serialization.Serializable
4-
import net.pearx.kpastebin.internal.EnumIntSerializer
5-
import net.pearx.kpastebin.internal.MODEL_PACKAGE
6-
7-
@Serializable(with = AccountType.Ser::class)
83
public enum class AccountType
94
{
105
NORMAL,
116
PRO;
12-
13-
internal companion object Ser : EnumIntSerializer<AccountType>("$MODEL_PACKAGE.AccountType", values())
147
}
Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package net.pearx.kpastebin.model
22

3-
import kotlinx.serialization.*
4-
import net.pearx.kpastebin.internal.MODEL_PACKAGE
5-
6-
@Serializable
73
public enum class ExpireDate(internal val code: String) {
84
NEVER("N"),
95
TEN_MINUTES("10M"),
@@ -15,17 +11,7 @@ public enum class ExpireDate(internal val code: String) {
1511
SIX_MONTHS("6M"),
1612
ONE_YEAR("1Y");
1713

18-
@Serializer(forClass = ExpireDate::class)
19-
internal companion object Ser : KSerializer<ExpireDate> {
20-
override val descriptor: SerialDescriptor = PrimitiveDescriptor("$MODEL_PACKAGE.ExpireDate", PrimitiveKind.STRING)
21-
22-
override fun deserialize(decoder: Decoder): ExpireDate {
23-
val value = decoder.decodeString()
24-
return values().firstOrNull { it.code == value } ?: throw NoSuchElementException("Can't find ExpireDate with key $value")
25-
}
26-
27-
override fun serialize(encoder: Encoder, value: ExpireDate) {
28-
encoder.encodeString(value.code)
29-
}
14+
internal companion object {
15+
fun forCode(code: String) = values().first { it.code == code }
3016
}
3117
}
Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,44 @@
11
package net.pearx.kpastebin.model
22

3-
import kotlinx.serialization.SerialName
4-
import kotlinx.serialization.Serializable
5-
import nl.adaptivity.xmlutil.serialization.XmlElement
3+
import net.pearx.kpastebin.internal.XML_PARENT_REGEX
4+
import net.pearx.kpastebin.internal.XML_PROPERTY_REGEX
65

7-
@SerialName("paste")
8-
@Serializable
96
public data class PasteDetails(
10-
@SerialName("paste_key")
11-
@XmlElement(true)
127
val key: String,
13-
@SerialName("paste_date")
14-
@XmlElement(true)
158
val date: Int,
16-
@SerialName("paste_title")
17-
@XmlElement(true)
189
val title: String,
19-
@SerialName("paste_size")
20-
@XmlElement(true)
2110
val size: Int,
22-
@SerialName("paste_expire_date")
23-
@XmlElement(true)
2411
val expireDate: Int,
25-
@SerialName("paste_private")
26-
@XmlElement(true)
2712
val privacy: Privacy,
28-
@SerialName("paste_format_long")
29-
@XmlElement(true)
3013
val formatLong: String,
31-
@SerialName("paste_format_short")
32-
@XmlElement(true)
3314
val formatShort: String,
34-
@SerialName("paste_url")
35-
@XmlElement(true)
3615
val url: String,
37-
@SerialName("paste_hits")
38-
@XmlElement(true)
3916
val hist: Int
40-
)
17+
) {
18+
internal companion object {
19+
fun parseList(input: String): List<PasteDetails> {
20+
val lst = mutableListOf<PasteDetails>()
21+
// it's a hack because currently there's no multiplatform API to parse XML with Kotlin/Native support
22+
for (paste in XML_PARENT_REGEX.findAll(input)) {
23+
if (paste.groupValues[1] == "paste") {
24+
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.
25+
lst.add(PasteDetails(
26+
map.getValue("key"),
27+
map.getValue("date").toInt(),
28+
map.getValue("title"),
29+
map.getValue("size").toInt(),
30+
map.getValue("expire_date").toInt(),
31+
Privacy.values()[map.getValue("private").toInt()],
32+
map.getValue("format_long"),
33+
map.getValue("format_short"),
34+
map.getValue("url"),
35+
map.getValue("hits").toInt()
36+
))
37+
}
38+
else
39+
throw IllegalArgumentException(input)
40+
}
41+
return lst
42+
}
43+
}
44+
}
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
package net.pearx.kpastebin.model
22

3-
import kotlinx.serialization.*
4-
import net.pearx.kpastebin.internal.EnumIntSerializer
5-
import net.pearx.kpastebin.internal.MODEL_PACKAGE
6-
7-
@Serializable(with = Privacy.Ser::class)
83
public enum class Privacy {
94
PUBLIC,
105
UNLISTED,
116
PRIVATE;
12-
13-
internal companion object Ser : EnumIntSerializer<Privacy>("$MODEL_PACKAGE.Privacy", values())
147
}

0 commit comments

Comments
 (0)