package com.huimv.video.dhicc.test.request;

import com.alibaba.fastjson.JSON;
import com.dahuatech.icc.oauth.model.v202010.OauthRefreshTokenResponse;
import com.dahuatech.icc.oauth.profile.GrantType;
import com.huimv.video.dhicc.test.config.OauthConfigBaseInfo;
import com.huimv.video.dhicc.test.constant.Constant;
import com.huimv.video.dhicc.test.utils.HttpURLConnectionUtil;
import com.huimv.video.dhicc.test.utils.UrlUtil;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * 刷新token请求
 */
@Data
public class OauthRefreshTokenRequest extends BaseRequest<OauthRefreshTokenResponse> {
    private String grant_type;
    private String client_id;
    private String client_secret;
    private String refresh_token;

    OauthRefreshTokenRequest(){}

    public OauthRefreshTokenRequest(OauthConfigBaseInfo oauthConfigBaseInfo, String refresh_token){
        super(oauthConfigBaseInfo, Constant.HTTP_POST, Constant.REFRESH_TOKEN, true);
        this.grant_type = GrantType.refresh_token.name();
        this.client_id = oauthConfigBaseInfo.getClientId();
        this.client_secret = oauthConfigBaseInfo.getClientSecret();
        this.refresh_token = refresh_token;
        setBody();
    }

    public OauthRefreshTokenResponse doAction(){
        //重置url信息，替换版本号
        this.setUrl(UrlUtil.dealVersion(this.getUrl()));
        //设置请求参数
        setBody();
        //发送请求
        String res = HttpURLConnectionUtil.doPost(this.getBody(), this.getUrl(), this.getHeader());
        //获取响应
        OauthRefreshTokenResponse oauthRefreshTokenResponse = JSON.parseObject(res, OauthRefreshTokenResponse.class);
        return oauthRefreshTokenResponse;
    }

    /**
     * 设置请求参数
     * **/
    public void setBody(){
        //设置请求参数
        Map<String, String> paramsMap = new HashMap();
        paramsMap.put("grant_type", this.grant_type);
        paramsMap.put("client_id", this.client_id);
        paramsMap.put("client_secret", this.client_secret);
        paramsMap.put("refresh_token", this.refresh_token);
        this.setBody(JSON.toJSONString(paramsMap));
    }
}
