webapi/src/main/kotlin/LoggedRequestInterceptorFactory.kt
author Da Risk <da_risk@geekorum.com>
Mon, 15 Sep 2025 14:00:07 -0400
changeset 1370 13e39ef920a8
parent 1174 731f6ee517b6
permissions -rw-r--r--
update license headers
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
137
5464f07a306c Update copyright headers for 2019
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
     1
/*
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     2
 * Geekttrss is a RSS feed reader application on the Android Platform.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     3
 *
1370
13e39ef920a8 update license headers
Da Risk <da_risk@geekorum.com>
parents: 1174
diff changeset
     4
 * Copyright (C) 2017-2025 by Frederic-Charles Barthelery.
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     5
 *
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     6
 * This file is part of Geekttrss.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     7
 *
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     8
 * Geekttrss is free software: you can redistribute it and/or modify
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     9
 * it under the terms of the GNU General Public License as published by
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    10
 * the Free Software Foundation, either version 3 of the License, or
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    11
 * (at your option) any later version.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    12
 *
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    13
 * Geekttrss is distributed in the hope that it will be useful,
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    16
 * GNU General Public License for more details.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    17
 *
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    18
 * You should have received a copy of the GNU General Public License
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    19
 * along with Geekttrss.  If not, see <http://www.gnu.org/licenses/>.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    20
 */
310
bd0611482474 Extract tinyrss web api to a new module "webapi"
Da Risk <da_risk@geekorum.com>
parents: 271
diff changeset
    21
package com.geekorum.ttrss.webapi
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    22
310
bd0611482474 Extract tinyrss web api to a new module "webapi"
Da Risk <da_risk@geekorum.com>
parents: 271
diff changeset
    23
import com.geekorum.ttrss.webapi.model.LoggedRequestPayload
580
a184a66bc77b Update geekdroid, and make TokenRetriever functions suspendable
Da Risk <da_risk@geekorum.com>
parents: 310
diff changeset
    24
import kotlinx.coroutines.runBlocking
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    25
import okhttp3.RequestBody
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    26
import retrofit2.Converter
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    27
import retrofit2.Retrofit
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    28
import java.lang.reflect.Type
310
bd0611482474 Extract tinyrss web api to a new module "webapi"
Da Risk <da_risk@geekorum.com>
parents: 271
diff changeset
    29
import java.util.logging.Level
bd0611482474 Extract tinyrss web api to a new module "webapi"
Da Risk <da_risk@geekorum.com>
parents: 271
diff changeset
    30
import java.util.logging.Logger
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    31
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    32
/**
271
b4ca3126dc2d TinyRssApi: add unsubscribeFromFeed() endpoint method
Da Risk <da_risk@geekorum.com>
parents: 137
diff changeset
    33
 * This [Converter.Factory] intercept all RequestBody conversion for [LoggedRequestPayload] and its subclasses
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    34
 * and set the [LoggedRequestPayload.sessionId] .
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    35
 */
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    36
// Don't @Inject it otherwise it can't be an Optional binding
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    37
class LoggedRequestInterceptorFactory(
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    38
    private val tokenRetriever: TokenRetriever
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    39
) : Converter.Factory() {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    40
310
bd0611482474 Extract tinyrss web api to a new module "webapi"
Da Risk <da_risk@geekorum.com>
parents: 271
diff changeset
    41
bd0611482474 Extract tinyrss web api to a new module "webapi"
Da Risk <da_risk@geekorum.com>
parents: 271
diff changeset
    42
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    43
    override fun requestBodyConverter(
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    44
        type: Type, parameterAnnotations: Array<out Annotation>, methodAnnotations: Array<out Annotation>,
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    45
        retrofit: Retrofit
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    46
    ): Converter<*, RequestBody>? {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    47
        return when (type) {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    48
            is Class<*> -> {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    49
                if (LoggedRequestPayload::class.java.isAssignableFrom(type)) {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    50
                    return Converter<LoggedRequestPayload, RequestBody> {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    51
                        val delegate: Converter<LoggedRequestPayload, RequestBody> =
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    52
                            retrofit.nextRequestBodyConverter(this, type, parameterAnnotations, methodAnnotations)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    53
                        try {
580
a184a66bc77b Update geekdroid, and make TokenRetriever functions suspendable
Da Risk <da_risk@geekorum.com>
parents: 310
diff changeset
    54
                            runBlocking {
a184a66bc77b Update geekdroid, and make TokenRetriever functions suspendable
Da Risk <da_risk@geekorum.com>
parents: 310
diff changeset
    55
                                it.sessionId = tokenRetriever.getToken()
a184a66bc77b Update geekdroid, and make TokenRetriever functions suspendable
Da Risk <da_risk@geekorum.com>
parents: 310
diff changeset
    56
                            }
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    57
                        } catch (e: TokenRetriever.RetrieverException) {
310
bd0611482474 Extract tinyrss web api to a new module "webapi"
Da Risk <da_risk@geekorum.com>
parents: 271
diff changeset
    58
                            logger.log(Level.CONFIG, "unable to retrieve token", e)
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    59
                        }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    60
                        delegate.convert(it)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    61
                    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    62
                }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    63
                return null
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    64
            }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    65
            else -> null
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    66
        }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    67
    }
310
bd0611482474 Extract tinyrss web api to a new module "webapi"
Da Risk <da_risk@geekorum.com>
parents: 271
diff changeset
    68
bd0611482474 Extract tinyrss web api to a new module "webapi"
Da Risk <da_risk@geekorum.com>
parents: 271
diff changeset
    69
    companion object {
bd0611482474 Extract tinyrss web api to a new module "webapi"
Da Risk <da_risk@geekorum.com>
parents: 271
diff changeset
    70
        private val logger by lazy {
bd0611482474 Extract tinyrss web api to a new module "webapi"
Da Risk <da_risk@geekorum.com>
parents: 271
diff changeset
    71
            Logger.getLogger(LoggedRequestInterceptorFactory::class.java.name)
bd0611482474 Extract tinyrss web api to a new module "webapi"
Da Risk <da_risk@geekorum.com>
parents: 271
diff changeset
    72
        }
bd0611482474 Extract tinyrss web api to a new module "webapi"
Da Risk <da_risk@geekorum.com>
parents: 271
diff changeset
    73
    }
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    74
}