티스토리 뷰

공부

[kotlin] jsoup & retries

승가비 2023. 2. 26. 12:34
728x90
object RestUtil : Loggable {
    const val RETRIES = 3
    const val TIMEOUT = 5 * 60 * 1000

    private const val MAX_BODY_SIZE = 0
    private const val IGNORE_CONTENT_TYPE = true

    fun connection(
        url: String,
        json: String,
        headers: Map<String, String> = emptyMap(),
        data: Map<String, String>? = emptyMap(),
        timeout: Int? = TIMEOUT
    ): Connection {
        var connection = Jsoup.connect(url)

        headers.forEach {
            connection = connection.header(it.key, it.value)
        }

        data!!.forEach {
            connection = connection.data(it.key, it.value)
        }

        return connection
            .timeout(timeout!!)
            .maxBodySize(MAX_BODY_SIZE)
            .ignoreContentType(IGNORE_CONTENT_TYPE)
            .requestBody(json)
    }

    inline fun <reified T> get(
        url: String,
        json: String,
        headers: Map<String, String> = emptyMap(),
        data: Map<String, String>? = emptyMap(),
        retries: Int? = RETRIES,
        timeout: Int? = TIMEOUT,
    ): T {
        var text: String? = null

        run loop@{
            repeat(retries!!) {
                try {
                    text = connection(url, json, headers, data, timeout = timeout)
                        .get()
                        .text()
                    return@loop
                } catch (expected: Exception) {
                    logger.error(expected.message, expected)
                }
            }
        }

        if (text == null) {
            throw NotFoundException()
        }

        return try {
            text!!.toObject()
        } catch (expected: Exception) {
            text as T
        }
    }

    inline fun <reified T> post(
        url: String,
        json: String,
        headers: Map<String, String> = emptyMap(),
        data: Map<String, String>? = emptyMap(),
        retries: Int? = RETRIES,
        timeout: Int? = TIMEOUT,
    ): T {
        var text: String? = null

        run loop@{
            repeat(retries!!) {
                try {
                    text = connection(url, json, headers, data, timeout = timeout)
                        .post()
                        .text()
                    return@loop
                } catch (expected: Exception) {
                    logger.error(expected.message, expected)
                }
            }
        }

        if (text == null) {
            throw NotFoundException()
        }

        return try {
            text!!.toObject()
        } catch (expected: Exception) {
            text as T
        }
    }
}

https://stackoverflow.com/questions/10506577/jsoup-connections-and-retries

 

Jsoup connections and retries

I'm using JSoup to connect to a website. I sometimes find that JSoupwill have a connection timeout, when this happens I want JSoup to retry the connection and when it fails on the 3rd time it sha...

stackoverflow.com

https://stackoverflow.com/questions/37700926/using-jsoup-to-access-html-but-receives-error-code-503

 

Using Jsoup to access HTML but receives error code 503

I have been trying to access KissAnime however, i kept receiving this error: org.jsoup.HttpStatusException: HTTP error fetching URL. Status=503 When i tried another URL eg. https://www.google.c...

stackoverflow.com

https://jsoup.org/apidocs/org/jsoup/Connection.html

 

Connection (jsoup Java HTML Parser 1.15.4 API)

ignoreContentType Connection ignoreContentType(boolean ignoreContentType) Ignore the document's Content-Type when parsing the response. By default this is false, an unrecognised content-type will cause an IOException to be thrown. (This is to prevent pro

jsoup.org

 

728x90

'공부' 카테고리의 다른 글

[spark] zipWithIndex  (0) 2023.02.26
[spark] broadcast nested loop join  (0) 2023.02.26
[terraform] command (init, apply, plan)  (0) 2023.02.26
[hive] `VARCHAR` vs `STRING` -> STRING is winner  (0) 2023.02.26
[aws] install `Amazon Corretto 11`  (0) 2023.02.26
댓글