SopUtil.java 1.9 KB
package cn.com.glsx.entmicro.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

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

@Slf4j
@Component
public class SopUtil {

    private static String GENERATE_SHORT_URL_HTTP;

    @Value("${sop.cdp.customer.shortUrl}")
    public void setGenerateShortUrlApi(String api) {
        GENERATE_SHORT_URL_HTTP = api;
    }

    private static final HttpHeaders headers = GetHeaders();

    private static final RestTemplate restTemplate = new RestTemplate();

    public static HttpHeaders GetHeaders() {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        return headers;
    }

    public static String GenerateShortUrl(String url, boolean isThroughAuth){
        Map<String, Object> map = new HashMap<>();
        map.put("url",url);
        map.put("isThroughAuth",isThroughAuth);
        HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(map, headers);
        String shortUrl = restTemplate.postForObject(GENERATE_SHORT_URL_HTTP, requestEntity, String.class);
        log.info("sop GenerateShortUrl shortUrl:{}",shortUrl);
        return shortUrl;
    }

    public static String GenerateShortUrl(List<String> urlList){
        Map<String, Object> map = new HashMap<>();
        map.put("urlList",urlList);
        HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(map, headers);
        String shortUrl = restTemplate.postForObject(GENERATE_SHORT_URL_HTTP + "/batch", requestEntity, String.class);
        log.info("sop GenerateShortUrl shortUrl:{}",shortUrl);
        return shortUrl;
    }
}