package com.hfkj.common.alipay;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

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

/**
 * 阿里云业务服务
 * @className: AliyunService
 * @author: HuRui
 * @date: 2024/4/3
 **/
public class AliyunService {

    /**
     * 查询ip地址
     * @param ip  ip地址
     * @return
     */
    public static JSONObject queryAddress(String ip) {
        try {
            String host = "https://ipaddquery.market.alicloudapi.com";
            String path = "/ip/address-query";
            String method = "POST";
            String appcode = "f9ace4c915054ca697a76fb9a4e1e8c0";

            Map<String, String> headers = new HashMap<>();
            headers.put("Authorization", "APPCODE " + appcode);
            headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

            Map<String, String> bodys = new HashMap<>();
            bodys.put("ip", ip);

            HttpResponse response = HttpUtils.doPost(host, path, method, headers, new HashMap<>(), bodys);
            JSONObject resObj = JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
            if (resObj.getString("code").equals("200")) {
                return resObj.getJSONObject("data");
            }
            return null;

        } catch (Exception e) {
            return null;
        }
    }

    public static void main(String[] args) {
        try {
            System.out.println(queryAddress("123.147.76.209"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
