package com.xunmei.common.core.http.service.params;

import com.alibaba.fastjson2.JSON;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;


public class HttpParamsPostJson extends HttpParams {

    private static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");

    private final Object object;


    public HttpParamsPostJson(final String url, final Object params) {
        this.setUrl(url);
        this.object = params;
    }

    @Override
    protected Request.Builder toBuild() {
        final String jsonString = JSON.toJSONString(this.object);
        final RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, jsonString);
        return new Request.Builder().url(this.getUrl())
                                    .post(body);
    }

    @Override
    public HttpParams put(final String key, final Object page) {
        return this;
    }

    @Override
    public String toString() {
        return this.getUrl() + " " + JSON.toJSONString(this.object);
    }
}
