<commit_before>package pl.temomuko.autostoprace.domain.repository

import android.util.Base64
import pl.temomuko.autostoprace.data.local.PrefsHelper
import pl.temomuko.autostoprace.data.remote.ApiException
import pl.temomuko.autostoprace.data.remote.AsrService
import pl.temomuko.autostoprace.data.remote.ErrorHandler
import pl.temomuko.autostoprace.data.remote.TokenAuthenticationInterceptor
import pl.temomuko.autostoprace.data.remote.model.UserEntity
import pl.temomuko.autostoprace.domain.model.User
import retrofit2.Response
import rx.Completable
import rx.Single
import javax.inject.Inject

class Authenticator @Inject constructor(
    private val asrService: AsrService,
    private val prefsHelper: PrefsHelper
) {
    val token: String
        get() = prefsHelper.authAccessToken

    fun authorize(email: String, password: String): Single<User> {
        val encodedCredentials =
            Base64.encodeToString("$email:$password".toByteArray(), Base64.NO_WRAP)
        val basicAuthHeaderValue = "Basic $encodedCredentials"
        return asrService.authorize(basicAuthHeaderValue)
            .doOnSuccess { saveAuthToken(it) }
            .map {
                if (it.isSuccessful) {
                    it.body()!!.toUser()
                } else {
                    throw ApiException(it.code())
                    //todo handle errors in rx java adapter
                }
            }
    }

    private fun saveAuthToken(response: Response<UserEntity>) {
        val authTokenHeader = response.headers()[TokenAuthenticationInterceptor.TOKEN_HEADER]
        authTokenHeader?.let { prefsHelper.authAccessToken = it }
    }

    fun resetPassword(email: String): Completable {<commit_msg>Update comment<commit_after>package pl.temomuko.autostoprace.domain.repository

import android.util.Base64
import pl.temomuko.autostoprace.data.local.PrefsHelper
import pl.temomuko.autostoprace.data.remote.ApiException
import pl.temomuko.autostoprace.data.remote.AsrService
import pl.temomuko.autostoprace.data.remote.ErrorHandler
import pl.temomuko.autostoprace.data.remote.TokenAuthenticationInterceptor
import pl.temomuko.autostoprace.data.remote.model.UserEntity
import pl.temomuko.autostoprace.domain.model.User
import retrofit2.Response
import rx.Completable
import rx.Single
import javax.inject.Inject

class Authenticator @Inject constructor(
    private val asrService: AsrService,
    private val prefsHelper: PrefsHelper
) {
    val token: String
        get() = prefsHelper.authAccessToken

    fun authorize(email: String, password: String): Single<User> {
        val encodedCredentials =
            Base64.encodeToString("$email:$password".toByteArray(), Base64.NO_WRAP)
        val basicAuthHeaderValue = "Basic $encodedCredentials"
        return asrService.authorize(basicAuthHeaderValue)
            .doOnSuccess { saveAuthToken(it) }
            .map {
                if (it.isSuccessful) {
                    it.body()!!.toUser()
                } else {
                    throw ApiException(it.code())
                    //todo handle errors in rx java call adapter
                }
            }
    }

    private fun saveAuthToken(response: Response<UserEntity>) {
        val authTokenHeader = response.headers()[TokenAuthenticationInterceptor.TOKEN_HEADER]
        authTokenHeader?.let { prefsHelper.authAccessToken = it }
    }

    fun resetPassword(email: String): Completable {
{'fraction_non_alphanumeric': 0.08644859813084112, 'fraction_numerical': 0.0040887850467289715, 'mean_word_length': 3.1116446578631454, 'pattern_counts': {'":': 0, '<': 7, '<?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': '142203', 'n_tokens_mistral': 1015, 'n_tokens_neox': 980, 'n_words': 180}