123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License.
523b3eb3cSopenharmony_ci * You may obtain a copy of the License at
623b3eb3cSopenharmony_ci *
723b3eb3cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
823b3eb3cSopenharmony_ci *
923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and
1323b3eb3cSopenharmony_ci * limitations under the License.
1423b3eb3cSopenharmony_ci */
1523b3eb3cSopenharmony_ci
1623b3eb3cSopenharmony_ci#include "adapter/preview/osal/response_data.h"
1723b3eb3cSopenharmony_ci
1823b3eb3cSopenharmony_cinamespace OHOS::Ace {
1923b3eb3cSopenharmony_ci
2023b3eb3cSopenharmony_cistd::unique_ptr<JsonValue> ResponseData::GetResultString() const
2123b3eb3cSopenharmony_ci{
2223b3eb3cSopenharmony_ci    auto resultJson = JsonUtil::Create(true);
2323b3eb3cSopenharmony_ci    if (code_ == HTTP_OK) {
2423b3eb3cSopenharmony_ci        resultJson->Put(std::string("code").c_str(), ACTION_SUCCESS);
2523b3eb3cSopenharmony_ci        resultJson->Put(std::string("data").c_str(), GetStringValue());
2623b3eb3cSopenharmony_ci    } else {
2723b3eb3cSopenharmony_ci        resultJson->Put(std::string("code").c_str(), COMMON_ERROR_CODE);
2823b3eb3cSopenharmony_ci        resultJson->Put(std::string("data").c_str(), GetStringValue());
2923b3eb3cSopenharmony_ci    }
3023b3eb3cSopenharmony_ci
3123b3eb3cSopenharmony_ci    return resultJson;
3223b3eb3cSopenharmony_ci}
3323b3eb3cSopenharmony_ci
3423b3eb3cSopenharmony_cistd::unique_ptr<JsonValue> ResponseData::GetStringValue() const
3523b3eb3cSopenharmony_ci{
3623b3eb3cSopenharmony_ci    auto responseJson = JsonUtil::Create(true);
3723b3eb3cSopenharmony_ci    responseJson->Put(std::string("code").c_str(), code_);
3823b3eb3cSopenharmony_ci    responseJson->Put(std::string("data").c_str(), data_.c_str());
3923b3eb3cSopenharmony_ci
4023b3eb3cSopenharmony_ci    if (code_ == HTTP_OK) {
4123b3eb3cSopenharmony_ci        std::string headersStr = "{";
4223b3eb3cSopenharmony_ci        for (auto&& [key, value] : headers_) {
4323b3eb3cSopenharmony_ci            headersStr += key + ":" + value + ",";
4423b3eb3cSopenharmony_ci        }
4523b3eb3cSopenharmony_ci        headersStr[headersStr.size() - 1] = '}';
4623b3eb3cSopenharmony_ci        responseJson->Put(std::string("headers").c_str(), headersStr.c_str());
4723b3eb3cSopenharmony_ci    }
4823b3eb3cSopenharmony_ci    return responseJson;
4923b3eb3cSopenharmony_ci}
5023b3eb3cSopenharmony_ci
5123b3eb3cSopenharmony_civoid ResponseData::SetHeaders(std::string headersStr)
5223b3eb3cSopenharmony_ci{
5323b3eb3cSopenharmony_ci    const char separator = '\n';
5423b3eb3cSopenharmony_ci    size_t posSeparator = headersStr.find(separator);
5523b3eb3cSopenharmony_ci    while (std::string::npos != posSeparator) {
5623b3eb3cSopenharmony_ci        std::string header = headersStr.substr(0, posSeparator - 1);
5723b3eb3cSopenharmony_ci        if (header == "") {
5823b3eb3cSopenharmony_ci            break;
5923b3eb3cSopenharmony_ci        }
6023b3eb3cSopenharmony_ci        size_t posColon = header.find(':');
6123b3eb3cSopenharmony_ci        if (std::string::npos == posColon) {
6223b3eb3cSopenharmony_ci            headers_["null"] = "[\"" + header + "\"]";
6323b3eb3cSopenharmony_ci        } else {
6423b3eb3cSopenharmony_ci            headers_["\"" + header.substr(0, posColon) + "\""] = "[\"" + header.substr(posColon + 2) + "\"]";
6523b3eb3cSopenharmony_ci        }
6623b3eb3cSopenharmony_ci        headersStr = headersStr.substr(posSeparator + 1);
6723b3eb3cSopenharmony_ci        posSeparator = headersStr.find(separator);
6823b3eb3cSopenharmony_ci    }
6923b3eb3cSopenharmony_ci}
7023b3eb3cSopenharmony_ci
7123b3eb3cSopenharmony_ciint ResponseData::GetActionCode() const
7223b3eb3cSopenharmony_ci{
7323b3eb3cSopenharmony_ci    if (code_ == HTTP_OK) {
7423b3eb3cSopenharmony_ci        return ACTION_SUCCESS;
7523b3eb3cSopenharmony_ci    } else {
7623b3eb3cSopenharmony_ci        return ACTION_FAIL;
7723b3eb3cSopenharmony_ci    }
7823b3eb3cSopenharmony_ci}
7923b3eb3cSopenharmony_ci} // namespace OHOS::Ace
80