aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/jamesbarnett/redditlite/service
diff options
context:
space:
mode:
authorJames Barnett <noreply@jamesbarnett.xyz>2020-04-10 13:34:23 +0100
committerJames Barnett <noreply@jamesbarnett.xyz>2020-04-10 13:34:23 +0100
commit78400d587ea5367d3424333913ff4f94ca3f1908 (patch)
tree2cf5f5ff8069740b0b7dd00853a4ea8c13d6e05c /src/main/kotlin/io/jamesbarnett/redditlite/service
parentd5a608143ad2250d8b35b9e4a488d39faaf5a021 (diff)
downloadreddit-lite-78400d587ea5367d3424333913ff4f94ca3f1908.tar.xz
reddit-lite-78400d587ea5367d3424333913ff4f94ca3f1908.zip
Reimplement in Kotlin
Diffstat (limited to 'src/main/kotlin/io/jamesbarnett/redditlite/service')
-rw-r--r--src/main/kotlin/io/jamesbarnett/redditlite/service/SubredditService.kt62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/main/kotlin/io/jamesbarnett/redditlite/service/SubredditService.kt b/src/main/kotlin/io/jamesbarnett/redditlite/service/SubredditService.kt
new file mode 100644
index 0000000..8c01c93
--- /dev/null
+++ b/src/main/kotlin/io/jamesbarnett/redditlite/service/SubredditService.kt
@@ -0,0 +1,62 @@
+package io.jamesbarnett.redditlite.service
+
+import com.fasterxml.jackson.databind.ObjectMapper
+import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
+import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
+import com.fasterxml.jackson.module.kotlin.treeToValue
+import io.jamesbarnett.redditlite.model.Comment
+import io.jamesbarnett.redditlite.model.Post
+import io.jamesbarnett.redditlite.model.PostDetail
+import org.springframework.boot.web.client.RestTemplateBuilder
+import org.springframework.stereotype.Service
+import org.springframework.web.client.RestTemplate
+import java.time.Duration
+
+const val REDDIT_API_ROOT_URL = "https://reddit.com/r/"
+
+@Service
+class SubredditService (restTemplateBuilder: RestTemplateBuilder) {
+
+ val objectMapper: ObjectMapper = jacksonObjectMapper()
+ .registerModule(JavaTimeModule())
+
+ val restTemplate: RestTemplate = restTemplateBuilder
+ .setConnectTimeout(Duration.ofSeconds(5))
+ .setReadTimeout(Duration.ofSeconds(10))
+ // Custom UA required to prevent rate limiting: https://github.com/reddit-archive/reddit/wiki/API#rules
+ .defaultHeader("User-Agent", "reddit-lite")
+ .build()
+
+ fun getPosts(subreddit: String, postsAfterId: String?): List<Post> {
+ val jsonResponse = restTemplate.getForObject("${REDDIT_API_ROOT_URL}${subreddit}/.json${if (postsAfterId != null) "?after=${postsAfterId}" else ""}", String::class.java)
+ return objectMapper.readTree(jsonResponse)
+ .path("data")
+ .path("children")
+ .findValues("data")
+ .map { objectMapper.treeToValue(it, Post::class.java) }
+ }
+
+ fun getPostDetail(subreddit: String, postId: String): PostDetail {
+ val jsonResponse = restTemplate.getForObject("${REDDIT_API_ROOT_URL}${subreddit}/comments/${postId}/.json", String::class.java)
+ val rootNode = objectMapper.readTree(jsonResponse)
+
+ val post = objectMapper.treeToValue(
+ rootNode
+ .path(0)
+ .path("data")
+ .path("children")
+ .path(0)
+ .path("data"),
+ Post::class.java)
+
+ val comments = rootNode
+ .path(1)
+ .path("data")
+ .path("children")
+ .findValues("data")
+ .map { Comment(it, objectMapper) }
+
+ return PostDetail(post, comments)
+ }
+
+} \ No newline at end of file