package com.swoosh.microblog.data.api

import okhttp3.Interceptor
import okhttp3.Response

class GhostAuthInterceptor(
    private val apiKeyProvider: () -> String?
) : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
        val apiKey = apiKeyProvider()
        val request = if (apiKey != null) {
            val token = GhostJwtGenerator.generateToken(apiKey)
            chain.request().newBuilder()
                .addHeader("Authorization", "Ghost $token")
                .build()
        } else {
            chain.request()
        }
        return chain.proceed(request)
    }
}
