YYour NameAccount Kit
c647687bAdd on Jun 10History
/*
 * Copyright (c) 2026 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.huawei.accountkit.userinfo;

import com.huawei.accountkit.userinfo.req.GetUserRiskLevelReq;
import com.huawei.accountkit.userinfo.resp.GetUserRiskLevelResp;
import com.huawei.accountkit.util.CallUtils;

import com.alibaba.fastjson2.JSONObject;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 获取用户风险等级
 *
 * @author accountkit
 * @since 2026-04-14
 */
public class GetUserRiskLevelService {
    private static final String URL = "https://account.cloud.huawei.com/user/getuserrisklevel";

    /**
     * 获取用户风险等级
     * 
     * @param req GetMobileByAccessTokenReq
     * @return GetMobileByAccessTokenResp
     * @throws IOException IOException
     */
    public static GetUserRiskLevelResp call(GetUserRiskLevelReq req) throws IOException {
        HttpPost httpPost =
            new HttpPost(URL + "?" + "clientID=" + req.getClientID() + "&transactionID=" + req.getTransactionID());
        Map<String, String> reqBody = new HashMap<>();
        reqBody.put("accessToken", req.getAccessToken());
        reqBody.put("scene", req.getScene());
        httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
        httpPost.setEntity(CallUtils.wrapJsonEntity(reqBody));

        String res = CallUtils.remoteCall(httpPost, (CloseableHttpResponse response, String rawBody) -> {
            int statusCode = response.getStatusLine().getStatusCode();
            // http状态码不是200,请求失败
            if (statusCode != 200) {
                return new IOException("call failed! http status code: " + statusCode + ", response data: " + rawBody);
            }
            // http状态码为200,解析响应的body,判断业务错误码
            JSONObject errorResponseBody = CallUtils.toJsonObject(rawBody);
            // 错误码
            Integer errCode = errorResponseBody.getInteger("errCode");
            // errCode为0表示成功,非0表示失败
            if (Objects.nonNull(errCode) && errCode != 0) {
                return new IOException("call failed! http status code: " + statusCode + ", response data: " + rawBody);
            }
            return null;
        });

        return CallUtils.parseObject(res, GetUserRiskLevelResp.class);
    }
}