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

import okhttp3.HttpUrl;
import okhttp3.Request;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;


public class HttpParamsGet extends HttpParams {


    private final Map<String, Object> params;

    public HttpParamsGet(final String url, final Map<String, Object> params) {
        this.setUrl(url);
        this.params = Optional.ofNullable(params)
                              .orElse(Collections.emptyMap());
    }

    @Override
    protected Request.Builder toBuild() {
        final HttpUrl.Builder builder = HttpUrl.parse(this.getUrl())
                                               .newBuilder();
        this.params.forEach((k, v) -> {
            if (v == null) {
                return;
            }
            builder.addEncodedQueryParameter(k, v.toString());
        });
        return new Request.Builder().url(builder.build());
    }

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

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