package cn.com.poc.user.wecom.api;

import cn.com.poc.common.utils.http.LocalHttpClient;
import cn.com.poc.user.wecom.domain.WecomProviderAccessToken;
import cn.com.poc.user.wecom.domain.WecomSuiteAccessToken;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.StringEntity;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;

@Service
public class WecomTokenApi implements WecomBaseApi {

    public WecomProviderAccessToken getProviderToken(String corpId, String providerSecret) {
        String jsonData = String.format("{\"corpid\":\"%1$s\",\"provider_secret\":\"%2$s\"}", corpId, providerSecret);
        HttpUriRequest httpUriRequest = RequestBuilder.post()
                .setHeader(JSON_HEADER)
                .setUri(QY_BASE_URI + "/cgi-bin/service/get_provider_token")
                .setEntity(new StringEntity(jsonData, StandardCharsets.UTF_8))
                .build();
        return LocalHttpClient.executeJsonResult(httpUriRequest, WecomProviderAccessToken.class);
    }

    public WecomSuiteAccessToken getSuiteToken(String suiteId, String suiteSecret, String suiteTicket) {
        String jsonData = String.format("{\"suite_id\":\"%1$s\",\"suite_secret\":\"%2$s\",\"suite_ticket\":\"%3$s\"}",
                suiteId, suiteSecret, suiteTicket);
        HttpUriRequest httpUriRequest = RequestBuilder.post()
                .setHeader(JSON_HEADER)
                .setUri(QY_BASE_URI + "/cgi-bin/service/get_suite_token")
                .setEntity(new StringEntity(jsonData, StandardCharsets.UTF_8))
                .build();
        return LocalHttpClient.executeJsonResult(httpUriRequest, WecomSuiteAccessToken.class);
    }
}
