package com.example.defenseapplication.utils

import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Response
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

//网络工具类
object HttpUtils {
    val baseURL = "https://stem-boozy-margarine.ngrok-free.dev"
    val okHttpClient = OkHttpClient.Builder()
        .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
        .writeTimeout(3, TimeUnit.MINUTES)
        .readTimeout(3, TimeUnit.MINUTES)
        .addInterceptor(object : Interceptor {
            override fun intercept(chain: Interceptor.Chain): Response {
                val s = UserinfoUtil.getUserInfo()
                val aa = chain.request().newBuilder()
                    .addHeader("token", s?.token ?: "")
                    .addHeader("userId", "${s?.userId ?: 0}")
                    .build()
                return chain.proceed(aa)
            }
        })
        .build()

    val retrofit = Retrofit.Builder()
        .addConverterFactory(GsonConverterFactory.create())
        .client(okHttpClient)
        .baseUrl(baseURL)
        .build()


    val api = retrofit.create(ApiService::class.java)





}