From 78400d587ea5367d3424333913ff4f94ca3f1908 Mon Sep 17 00:00:00 2001 From: James Barnett Date: Fri, 10 Apr 2020 13:34:23 +0100 Subject: Reimplement in Kotlin --- .../redditlite/service/SubredditService.kt | 62 ++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/main/kotlin/io/jamesbarnett/redditlite/service/SubredditService.kt (limited to 'src/main/kotlin/io/jamesbarnett/redditlite/service') 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 { + 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 -- cgit v1.2.3