Repository: ClickHouse/ClickHouse
File: src/Databases/DataLake/RestCatalog.cpp (C++)

Code before:
        out_stream_callback = [&](std::ostream & os)
        {
            os << body;
        };

        if (oauth_server_uri.empty())
            url = Poco::URI(base_url / oauth_tokens_endpoint);
        else
            url = Poco::URI(oauth_server_uri);
    }

    const auto & context = getContext();
    auto timeouts = DB::ConnectionTimeouts::getHTTPTimeouts(context->getSettingsRef(), context->getServerSettings());
    auto session = makeHTTPSession(DB::HTTPConnectionGroupType::HTTP, url, timeouts, {});

    Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, url.getPathAndQuery(),
                                Poco::Net::HTTPMessage::HTTP_1_1);
    request.setContentType("application/x-www-form-urlencoded");
    request.setContentLength(body_size);
    request.set("Accept", "application/json");

    std::ostream& os = session->sendRequest(request);
    out_stream_callback(os);

    Poco::Net::HTTPResponse response;
    std::istream& rs = session->receiveResponse(response);

    std::string json_str;
    Poco::StreamCopier::copyToString(rs, json_str);

    Poco::JSON::Parser parser;
    Poco::Dynamic::Var res_json = parser.parse(json_str);
    const Poco::JSON::Object::Ptr & object = res_json.extract<Poco::JSON::Object::Ptr>();

    return object->get("access_token").extract<String>();
}

std::optional<StorageType> RestCatalog::getStorageType() const
{
    if (config.default_base_location.empty())
        return std::nullopt;
    return parseStorageTypeFromLocation(config.default_base_location);
}

DB::ReadWriteBufferFromHTTPPtr RestCatalog::createReadBuffer(
    const std::string & endpoint,
    const Poco::URI::QueryParameters & params,
    const DB::HTTPHeaderEntries & headers) const
{
    const auto & context = getContext();


Review comment (suggestion):
```suggestion
    std::istream & rs = session->receiveResponse(response);
```

Code after:
        Poco::URI::encode(client_id, client_id, encoded_client_id);
        Poco::URI::encode(client_secret, client_secret, encoded_client_secret);

        body = fmt::format(
            "grant_type=client_credentials&scope={}&client_id={}&client_secret={}",
            encoded_auth_scope, encoded_client_id, encoded_client_secret);
        body_size = body.size();
        out_stream_callback = [&](std::ostream & os)
        {
            os << body;
        };

        if (oauth_server_uri.empty())
            url = Poco::URI(base_url / oauth_tokens_endpoint);
        else
            url = Poco::URI(oauth_server_uri);
    }

    const auto & context = getContext();
    auto timeouts = DB::ConnectionTimeouts::getHTTPTimeouts(context->getSettingsRef(), context->getServerSettings());
    auto session = makeHTTPSession(DB::HTTPConnectionGroupType::HTTP, url, timeouts, {});

    Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, url.getPathAndQuery(),
                                Poco::Net::HTTPMessage::HTTP_1_1);
    request.setContentType("application/x-www-form-urlencoded");
    request.setContentLength(body_size);
    request.set("Accept", "application/json");

    std::ostream & os = session->sendRequest(request);
    out_stream_callback(os);

    Poco::Net::HTTPResponse response;
    std::istream & rs = session->receiveResponse(response);

    std::string json_str;
    Poco::StreamCopier::copyToString(rs, json_str);

    Poco::JSON::Parser parser;
    Poco::Dynamic::Var res_json = parser.parse(json_str);
    const Poco::JSON::Object::Ptr & object = res_json.extract<Poco::JSON::Object::Ptr>();

    return object->get("access_token").extract<String>();
}

std::optional<StorageType> RestCatalog::getStorageType() const
{
    if (config.default_base_location.empty())
        return std::nullopt;
    return parseStorageTypeFromLocation(config.default_base_location);
}