<commit_before>package de.rheinfabrik.heimdall2

import de.rheinfabrik.heimdall2.grants.OAuth2Grant
import de.rheinfabrik.heimdall2.grants.OAuth2RefreshAccessTokenGrant
import io.reactivex.Single
import java.util.Calendar

open class OAuth2AccessTokenManager(
    private val mStorage: OAuth2AccessTokenStorage
) {

    // Public API

    /**
     * Returns the underlying storage.
     *
     * @return - An OAuth2AccessTokenStorage.
     */
    fun getStorage(): OAuth2AccessTokenStorage = mStorage

    /**
     * Grants a new access token using the given OAuth2 grant.
     *
     * @param grant A class implementing the OAuth2Grant interface.
     * @return - An Single emitting the granted access token.
     */
    fun grantNewAccessToken(
        grant: OAuth2Grant,
        calendar: Calendar = Calendar.getInstance()
    ): Single<OAuth2AccessToken> =
        grant.grantNewAccessToken()
            .doOnSuccess { token ->
                token.expiresIn?.let {
                    val newExpirationDate = (calendar.clone() as Calendar).apply {
                        add(Calendar.SECOND, it)
                    }
                    token.expirationDate = newExpirationDate
                }
                mStorage.storeAccessToken(token)
            }.cache()

    /**
     * Returns an Observable emitting an unexpired access token.
     * NOTE: In order to work, Heimdall needs an access token which has a refresh_token and an
     * expires_in field.
     *
     * @param refreshAccessTokenGrant The refresh grant that will be used if the access token is expired.<commit_msg>Use copy to set new expiration date<commit_after>package de.rheinfabrik.heimdall2

import de.rheinfabrik.heimdall2.grants.OAuth2Grant
import de.rheinfabrik.heimdall2.grants.OAuth2RefreshAccessTokenGrant
import io.reactivex.Single
import java.util.Calendar

open class OAuth2AccessTokenManager(
    private val mStorage: OAuth2AccessTokenStorage
) {

    // Public API

    /**
     * Returns the underlying storage.
     *
     * @return - An OAuth2AccessTokenStorage.
     */
    fun getStorage(): OAuth2AccessTokenStorage = mStorage

    /**
     * Grants a new access token using the given OAuth2 grant.
     *
     * @param grant A class implementing the OAuth2Grant interface.
     * @return - An Single emitting the granted access token.
     */
    fun grantNewAccessToken(
        grant: OAuth2Grant,
        calendar: Calendar = Calendar.getInstance()
    ): Single<OAuth2AccessToken> =
        grant.grantNewAccessToken()
            .doOnSuccess { token ->
                token.expiresIn?.let {
                    val newExpirationDate = (calendar.clone() as Calendar).apply {
                        add(Calendar.SECOND, it)
                    }
                    mStorage.storeAccessToken(
                        token = token.copy(expirationDate = newExpirationDate)
                    )
                }
            }.cache()

    /**
     * Returns an Observable emitting an unexpired access token.
     * NOTE: In order to work, Heimdall needs an access token which has a refresh_token and an
     * expires_in field.
     *
     * @param refreshAccessTokenGrant The refresh grant that will be used if the access token is expired.
{'fraction_non_alphanumeric': 0.0716923076923077, 'fraction_numerical': 0.008, 'mean_word_length': 2.231610337972167, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '420756', 'n_tokens_mistral': 916, 'n_tokens_neox': 863, 'n_words': 246}