Skip to content

Commit b173dd6

Browse files
committed
Add GraphQL LeetCode client
1 parent 96180e8 commit b173dd6

File tree

6 files changed

+100
-1
lines changed

6 files changed

+100
-1
lines changed

.github/CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
Before you begin:
66

7-
- This project is powered by [Kotlin](https://kotlinlang.org), [TelegramBotAPI](https://github.com/InsanusMokrassar/TelegramBotAPI), [Koin](https://insert-koin.io), and [Skija](https://github.com/JetBrains/skija) libraries, [AWS](https://aws.amazon.com) services and [CDK](https://aws.amazon.com/cdk) with [TypeScript](https://www.typescriptlang.org), and [GitHub Actions](https://github.com/features/actions).
7+
- This project is powered by [Kotlin](https://kotlinlang.org), [TelegramBotAPI](https://github.com/InsanusMokrassar/TelegramBotAPI), [Koin](https://insert-koin.io), [Skija](https://github.com/JetBrains/skija), and [GraphQL](https://graphql.org) technologies, [AWS](https://aws.amazon.com) services and [CDK](https://aws.amazon.com/cdk) with [TypeScript](https://www.typescriptlang.org), and [GitHub Actions](https://github.com/features/actions).
88
- Have you read the [code of conduct](CODE_OF_CONDUCT.md)?
99
- Check out the [existing issues](https://github.com/JavaBy/jprof_by_bot/issues).
1010
- Discuss your plans in [the chat](https://t.me/jprof_by) or in an issue.

gradle/libs.versions.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ koin = "3.1.2"
1010

1111
ktor = "1.6.8"
1212

13+
graphql-kotlin = "5.5.0"
14+
1315
kotlinx-serialization = "1.3.0"
1416
jackson = "2.13.0"
1517
kaml = "0.43.0"
@@ -43,6 +45,8 @@ ktor-bom = { group = "io.ktor", name = "ktor-bom", version.ref = "ktor" }
4345
ktor-client-apache = { group = "io.ktor", name = "ktor-client-apache" }
4446
ktor-client-serialization = { group = "io.ktor", name = "ktor-client-serialization" }
4547

48+
graphql-kotlin-ktor-client = { group = "com.expediagroup", name = "graphql-kotlin-ktor-client", version.ref = "graphql-kotlin" }
49+
4650
kotlinx-serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinx-serialization" }
4751
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" }
4852
jackson-databind = { group = "com.fasterxml.jackson.core", name = "jackson-databind", version.ref = "jackson" }

leetcode/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
plugins {
22
kotlin("jvm")
3+
kotlin("plugin.serialization")
34
}
45

56
dependencies {
67
api(project.projects.core)
78
api(libs.tgbotapi.core)
89
implementation(libs.tgbotapi.extensions.api)
910
implementation(libs.log4j.api)
11+
implementation(libs.graphql.kotlin.ktor.client)
12+
implementation(libs.kotlinx.serialization.core)
1013

1114
testImplementation(libs.junit.jupiter.api)
1215
testImplementation(libs.junit.jupiter.params)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package by.jprof.telegram.bot.leetcode
2+
3+
import com.expediagroup.graphql.client.ktor.GraphQLKtorClient
4+
import com.expediagroup.graphql.client.types.GraphQLClientRequest
5+
import kotlinx.serialization.Required
6+
import kotlinx.serialization.Serializable
7+
import org.apache.logging.log4j.LogManager
8+
import java.io.Closeable
9+
import java.net.URL
10+
import kotlin.reflect.KClass
11+
12+
class GraphQLLeetCodeClient : LeetCodeClient, Closeable {
13+
companion object {
14+
private val logger = LogManager.getLogger(GraphQLLeetCodeClient::class.java)!!
15+
}
16+
17+
private val client = GraphQLKtorClient(
18+
url = URL("https://leetcode.com/graphql"),
19+
)
20+
21+
override suspend fun questionData(slug: String): Question? {
22+
val request = QuestionDataQuery(QuestionDataQuery.Variables(slug))
23+
val response = client.execute(request)
24+
25+
response.errors?.let { errors ->
26+
errors.forEach { error -> logger.error(error.message) }
27+
return null
28+
}
29+
30+
return response.data?.question
31+
}
32+
33+
override fun close() {
34+
client.close()
35+
}
36+
}
37+
38+
@Serializable
39+
private class QuestionDataQuery(
40+
override val variables: Variables,
41+
) : GraphQLClientRequest<QuestionData> {
42+
@Required
43+
override val query: String = """
44+
query questionData(${"$"}slug: String!) {
45+
question(titleSlug: ${"$"}slug) {
46+
title
47+
titleSlug
48+
content
49+
isPaidOnly
50+
difficulty
51+
likes
52+
dislikes
53+
categoryTitle
54+
}
55+
}
56+
""".trimIndent()
57+
58+
@Required
59+
override val operationName: String = "questionData"
60+
61+
override fun responseType(): KClass<QuestionData> = QuestionData::class
62+
63+
@Serializable
64+
data class Variables(
65+
val slug: String
66+
)
67+
}
68+
69+
@Serializable
70+
private data class QuestionData(
71+
val question: Question?,
72+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package by.jprof.telegram.bot.leetcode
2+
3+
interface LeetCodeClient {
4+
suspend fun questionData(slug: String): Question?
5+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package by.jprof.telegram.bot.leetcode
2+
3+
import kotlinx.serialization.Serializable
4+
5+
@Serializable
6+
data class Question(
7+
val title: String,
8+
val titleSlug: String,
9+
val content: String,
10+
val isPaidOnly: Boolean,
11+
val difficulty: String,
12+
val likes: Int,
13+
val dislikes: Int,
14+
val categoryTitle: String,
15+
)

0 commit comments

Comments
 (0)