package com.kaixed.kchat.network

import com.kaixed.kchat.network.OkhttpHelper.getInstance
import okhttp3.Callback
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody


/**
 * @Author: kaixed
 * @Date: 2024/10/14 13:52
 */

class NetworkRequest {

    companion object {
        const val TAG = "NetworkRequest"
    }

    private val client: OkHttpClient = getInstance()

    fun postAsync(url: String, formBody: RequestBody, callback: Callback) {
        val request = Request.Builder()
            .url(url)
            .post(formBody)
            .build()

        val call = client.newCall(request)
        call.enqueue(callback)
    }

    fun postAsync(url: String, json: String, callback: Callback) {
        val requestBody = json.toRequestBody("application/json".toMediaType())

        val request = Request.Builder()
            .url(url)
            .post(requestBody)
            .build()

        val call = client.newCall(request)
        call.enqueue(callback)
    }


    fun getAsync(url: String, callback: Callback) {
        val request = Request.Builder()
            .url(url)
            .get()
            .build()

        val call = client.newCall(request)
        call.enqueue(callback)
    }
}
