protected static OpenSearchResult fromHttpResult()

in aliyun-sdk-opensearch/src/main/java/com/aliyun/opensearch/OpenSearchClient.java [364:415]


    protected static OpenSearchResult fromHttpResult(HttpResult httpResult)
        throws OpenSearchException, OpenSearchClientException {
        OpenSearchResponse openSearchResponse;
        if (httpResult.getCode() >= HttpStatus.SC_BAD_REQUEST) {
            OpenSearchException openSearchException = new OpenSearchException();
            String resultString = httpResult.getResult();
            if (StringUtils.isEmpty(resultString)) {
                openSearchException.setCode(ERROR_INTERNAL);
                openSearchException.setMessage(httpResult.getReason());
                throw openSearchException;
            }
            try {
                openSearchResponse = JsonUtilWrapper.fromJson(httpResult.getResult());
            } catch (JSONException e) {
                openSearchException.setCode(ERROR_INTERNAL);
                openSearchException.setMessage(httpResult.toString());
                throw openSearchException;
            }
            openSearchException.setRequestId(getRequestId(openSearchResponse, httpResult));
            List<ErrorResult> errorResults = openSearchResponse.getErrors();
            if (errorResults == null || errorResults.size() == 0) {
                openSearchException.setCode(ERROR_INTERNAL);
                openSearchException.setMessage(httpResult.toString());
                throw openSearchException;
            }
            ErrorResult errorResult = errorResults.get(0);
            openSearchException.setCode(errorResult.getCode());
            openSearchException.setMessage(errorResult.getMessage());
            throw openSearchException;
        }

        // 流式输出最后以 [done] 结束
        if (END_OF_STREAM.equals(httpResult.getResult())) {
            return new OpenSearchResult().setResult(END_OF_STREAM);
        }

        try {
            openSearchResponse = JsonUtilWrapper.fromJson(httpResult.getResult());
        } catch (JSONException e) {
            throw new OpenSearchClientException(String.format("parse result failed: %s", httpResult.toString()), e);
        }
        OpenSearchResult openSearchResult = new OpenSearchResult();
        TraceInfo traceInfo = new TraceInfo();
        traceInfo.setRequestId(getRequestId(openSearchResponse, httpResult));
        traceInfo.setTracer(openSearchResponse.getTracer());
        openSearchResult.setTraceInfo(traceInfo);
        openSearchResult.setResult(openSearchResponse.getResultString());
        if (!StringUtils.isEmpty(openSearchResponse.getChatString())) {
            openSearchResult.setChat(openSearchResponse.getChatString());
        }
        return openSearchResult;
    }