<commit_before>package org.gravidence.lastfm4k

import mu.KotlinLogging
import org.gravidence.lastfm4k.api.LastfmApiClient
import org.gravidence.lastfm4k.api.LastfmApiContext
import org.gravidence.lastfm4k.api.auth.AuthApi
import org.gravidence.lastfm4k.api.auth.Session
import org.gravidence.lastfm4k.api.auth.Token
import org.gravidence.lastfm4k.api.track.TrackApi
import org.gravidence.lastfm4k.exception.LastfmException
import org.http4k.core.*

private val logger = KotlinLogging.logger {}

class LastfmClient(
    val apiRoot: String = "http://ws.audioscrobbler.com/2.0/",
    val apiKey: String,
    val apiSecret: String,
    session: Session? = null
) {

    private val context = LastfmApiContext(
        LastfmApiClient(apiRoot, apiKey, apiSecret),
        session
    )

    val authApi: AuthApi = AuthApi(context)
    val trackApi: TrackApi = TrackApi(context)

    @Throws(LastfmException::class)
    fun authorizeStep1(): Pair<Token, Uri> {
        val token = authApi.getToken()
        val userAuthorizationRequest = authApi.getUserAuthorizationRequest(token)
        return Pair(token, userAuthorizationRequest)
    }

    @Throws(LastfmException::class)
    fun authorizeStep2(token: Token, useNewSession: Boolean = true): Session {
        return authApi.getSession(token).also {
            if (useNewSession) {
                session(it)
            }
        }
    }

    /**
     * Make all APIs use new [session].
     */
    fun session(session: Session?) {
        context.session = session
    }

}<commit_msg>Log session manipulations<commit_after>package org.gravidence.lastfm4k

import mu.KotlinLogging
import org.gravidence.lastfm4k.api.LastfmApiClient
import org.gravidence.lastfm4k.api.LastfmApiContext
import org.gravidence.lastfm4k.api.auth.AuthApi
import org.gravidence.lastfm4k.api.auth.Session
import org.gravidence.lastfm4k.api.auth.Token
import org.gravidence.lastfm4k.api.track.TrackApi
import org.gravidence.lastfm4k.exception.LastfmException
import org.http4k.core.*

private val logger = KotlinLogging.logger {}

class LastfmClient(
    val apiRoot: String = "http://ws.audioscrobbler.com/2.0/",
    val apiKey: String,
    val apiSecret: String,
    session: Session? = null
) {

    private val context = LastfmApiContext(
        LastfmApiClient(apiRoot, apiKey, apiSecret),
        session
    )

    val authApi: AuthApi = AuthApi(context)
    val trackApi: TrackApi = TrackApi(context)

    @Throws(LastfmException::class)
    fun authorizeStep1(): Pair<Token, Uri> {
        val token = authApi.getToken()
        val userAuthorizationRequest = authApi.getUserAuthorizationRequest(token)
        return Pair(token, userAuthorizationRequest)
    }

    @Throws(LastfmException::class)
    fun authorizeStep2(token: Token, useNewSession: Boolean = true): Session {
        return authApi.getSession(token).also {
            if (useNewSession) {
                session(it)
            }
        }
    }

    /**
     * Make all APIs use new [session].
     */
    fun session(session: Session?) {
        if (session != null) {
            logger.info { "Use new session: $session" }
        } else {
            logger.info { "Session cleared" }
        }
        context.session = session
    }

}
{'fraction_non_alphanumeric': 0.1002460024600246, 'fraction_numerical': 0.007995079950799507, 'mean_word_length': 3.0460199004975124, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2142545', 'n_tokens_mistral': 1017, 'n_tokens_neox': 996, 'n_words': 200}