<commit_before>package de.ironjan.mensaupb.api

import android.content.Context
import android.util.Log
import arrow.core.Either
import com.koushikdutta.ion.Ion
import com.koushikdutta.ion.bitmap.LocallyCachedStatus.CACHED
import com.koushikdutta.ion.builder.Builders.Any.B
import de.ironjan.mensaupb.api.model.Menu
import de.ironjan.mensaupb.api.model.Menu.ArrayDeserializer
import de.ironjan.mensaupb.api.model.Menu.Deserializer
import org.slf4j.LoggerFactory
import java.io.PrintWriter
import java.io.StringWriter
import java.util.concurrent.TimeUnit.MILLISECONDS

class ContextBoundClient(val context: Context) : ClientV2 {

    private val menusUri = ClientV2Implementation.baseUrl + ClientV2Implementation.menusPath

    private val logger = LoggerFactory.getLogger("ContextBoundClient")

    override fun getMenus(): Either<String, Array<Menu>> = getMenus("", "", false)

    fun getMenus(noCache: Boolean): Either<String, Array<Menu>> = getMenus("", "", noCache)

    override fun getMenus(restaurant: String, date: String): Either<String, Array<Menu>> =
            getMenus(restaurant, date, false)

    private fun getMenus(restaurant: String, date: String, noCache: Boolean): Either<String, Array<Menu>> {
        val url = constructMenusUriWithParams(restaurant, date)

        val preparedRequest = prepareRequest(url, noCache)

        return try {
            Either.right(tryMenusRequestExecution(preparedRequest))
        } catch (e: Exception) {
            wrapException(e)
        }
    }

    private fun wrapException(e: Exception): Either<String, Nothing> {
        val sw = StringWriter()
        val pw = PrintWriter(sw)
        e.printStackTrace(pw)

        return Either.left(sw.toString())
    }

    private fun tryMenusRequestExecution(request: B): Array<Menu> {
        val response =
                request.asString()
                        .withResponse()
                        .get(ClientV2Implementation.REQUEST_TIMEOUT_30_SECONDS.toLong(), MILLISECONDS)


        return ArrayDeserializer().deserialize(response.result) ?: arrayOf()
    }

    private fun prepareRequest(url: String, forceReload: Boolean): B {
        val requestBuilder = Ion.with(context).load(url)
//                .setLogging("ContextBoundClient", Log.DEBUG)
        return if (forceReload) {
            requestBuilder.noCache()
        } else {
            requestBuilder
        }
    }

    override fun getMenu(key: String): Either<String, Menu> {
        val splitKey = key.split("_")
        val date = splitKey[0]
        val restaurant = splitKey[1]

        val allMenusRequest = prepareRequest(constructMenusUriWithParams("", ""), false)

        val cachedStatus = allMenusRequest.isLocallyCached

        return try {
            val resp = tryMenusRequestExecution(allMenusRequest)
            val right = resp.first { it.key == key }
            return Either.right(right)
        } catch (e: java.lang.Exception){
            wrapException(e)
        }

        if (cachedStatus == CACHED) {
            val resp = tryMenusRequestExecution(allMenusRequest)
            val right = resp.first { it.key == key }
            return Either.right(right)
        }


        val menusRequest = prepareRequest(constructMenusUriWithParams(restaurant, date), false)
        val locallyCached = menusRequest.isLocallyCached

        if (locallyCached == CACHED) {
            val resp = tryMenusRequestExecution(menusRequest)
            val right = resp.first { it.key == key }
            return Either.right(right)
        }
        val menuRequest = prepareRequest(constructMenuUriWithKey(key), false)
       return try {
            Either.right(tryMenuRequestExecution(menuRequest))
        } catch (e: Exception) {
            wrapException(e)
        }
    }

    private fun tryMenuRequestExecution(request: B): Menu {
        val response =
                request.asString()
                        .withResponse()
                        .get(ClientV2Implementation.REQUEST_TIMEOUT_30_SECONDS.toLong(), MILLISECONDS)


        return Deserializer().deserialize(response.result)
    }

    private fun constructMenusUriWithParams(restaurant: String = "", date: String = ""): String {
        val paramList = mutableListOf<Pair<String, String>>()
        if (restaurant.isNotBlank()) paramList.add(Pair("restaurant", restaurant))
        if (date.isNotBlank()) paramList.add(Pair("date", date))

        val paramsAsString =
                if (paramList.isEmpty()) ""
                else paramList.joinToString(prefix = "?", separator = "&", transform = { "${it.first}=${it.second}" })

        return "$menusUri$paramsAsString"
    }

    private fun constructMenuUriWithKey(key: String): String = "$menusUri/$key"

}<commit_msg>Cleaned up ContextBoundClient<commit_after>package de.ironjan.mensaupb.api

import android.content.Context
import android.util.Log
import arrow.core.Either
import com.koushikdutta.ion.Ion
import com.koushikdutta.ion.bitmap.LocallyCachedStatus.CACHED
import com.koushikdutta.ion.builder.Builders.Any.B
import de.ironjan.mensaupb.api.model.Menu
import de.ironjan.mensaupb.api.model.Menu.ArrayDeserializer
import de.ironjan.mensaupb.api.model.Menu.Deserializer
import org.slf4j.LoggerFactory
import java.io.PrintWriter
import java.io.StringWriter
import java.util.concurrent.TimeUnit.MILLISECONDS

class ContextBoundClient(val context: Context) : ClientV2 {

    private val menusUri = ClientV2Implementation.baseUrl + ClientV2Implementation.menusPath


    override fun getMenus(): Either<String, Array<Menu>> = getMenus("", "", false)

    fun getMenus(noCache: Boolean): Either<String, Array<Menu>> = getMenus("", "", noCache)

    override fun getMenus(restaurant: String, date: String): Either<String, Array<Menu>> =
            getMenus(restaurant, date, false)

    private fun getMenus(restaurant: String, date: String, noCache: Boolean): Either<String, Array<Menu>> {
        val url = constructMenusUriWithParams(restaurant, date)

        val preparedRequest = prepareRequest(url, noCache)

        return try {
            Either.right(tryMenusRequestExecution(preparedRequest))
        } catch (e: Exception) {
            wrapException(e)
        }
    }

    private fun wrapException(e: Exception): Either<String, Nothing> {
        val sw = StringWriter()
        val pw = PrintWriter(sw)
        e.printStackTrace(pw)

        return Either.left(sw.toString())
    }

    private fun tryMenusRequestExecution(request: B): Array<Menu> {
        val response =
                request.asString()
                        .withResponse()
                        .get(ClientV2Implementation.REQUEST_TIMEOUT_30_SECONDS.toLong(), MILLISECONDS)


        return ArrayDeserializer().deserialize(response.result) ?: arrayOf()
    }

    private fun prepareRequest(url: String, forceReload: Boolean): B {
        val requestBuilder = Ion.with(context).load(url)
//                .setLogging("ContextBoundClient", Log.DEBUG)
        return if (forceReload) {
            requestBuilder.noCache()
        } else {
            requestBuilder
        }
    }

    override fun getMenu(key: String): Either<String, Menu> {
        val allMenusRequest = prepareRequest(constructMenusUriWithParams("", ""), false)

        return try {
            val resp = tryMenusRequestExecution(allMenusRequest)
            val right = resp.first { it.key == key }
            return Either.right(right)
        } catch (e: java.lang.Exception){
            wrapException(e)
        }
    }

    private fun constructMenusUriWithParams(restaurant: String = "", date: String = ""): String {
        val paramList = mutableListOf<Pair<String, String>>()
        if (restaurant.isNotBlank()) paramList.add(Pair("restaurant", restaurant))
        if (date.isNotBlank()) paramList.add(Pair("date", date))

        val paramsAsString =
                if (paramList.isEmpty()) ""
                else paramList.joinToString(prefix = "?", separator = "&", transform = { "${it.first}=${it.second}" })

        return "$menusUri$paramsAsString"
    }

}
{'fraction_non_alphanumeric': 0.10726643598615918, 'fraction_numerical': 0.0023479980227385073, 'mean_word_length': 2.8337280909521554, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '7589638', 'n_tokens_mistral': 2375, 'n_tokens_neox': 2282, 'n_words': 464}