com.itheima.auth.sdk.service.JsonHttpApiService Maven / Gradle / Ivy
package com.itheima.auth.sdk.service;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.itheima.auth.sdk.common.AuthSdkException;
import com.itheima.auth.sdk.common.TokenCheck;
import com.itheima.auth.sdk.config.AuthorityConfig;
import java.util.HashMap;
import java.util.Map;
public class JsonHttpApiService extends HttpApiService {
private AuthorityConfig authorityConfig;
public JsonHttpApiService(AuthorityConfig authorityConfig) {
this.authorityConfig = authorityConfig;
}
@Override
protected void setRequestParam(HttpRequest httpRequest, Map param) {
//设置form表单参数
httpRequest.body(JSONUtil.toJsonStr(param));
}
@Override
public void setRequestHeader(HttpRequest httpRequest) {
httpRequest.header(Header.CONTENT_TYPE, "application/json");
}
@Override
public void addHeaders(HttpRequest httpRequest) {
Map headers = new HashMap<>();
if(authorityConfig.getToken() != null && !"".equals(authorityConfig.getToken())) {
new TokenCheck().parserToken(authorityConfig.getToken());
headers.put("token", authorityConfig.getToken());
}
if(authorityConfig.getApplicationId() != null && !"".equals(authorityConfig.getApplicationId())) {
headers.put("APPLICATIONID", String.valueOf(authorityConfig.getApplicationId()));
}
//默认applicationType为APPLICATION
headers.put("APPLICATIONTYPE","APPLICATION");
httpRequest.addHeaders(headers);
}
@Override
protected int getTimeOut() {
return this.authorityConfig.getTimeout();
}
}