void HttpRequestJob::Process()

in Gems/AWSCore/Code/Source/Framework/HttpRequestJob.cpp [356:432]


    void HttpRequestJob::Process()
    {
        // Someday the AWS Http client may support real async I/O. The 
        // GetRequest and OnResponse methods are designed with that in 
        ///mind. When that feature is available, we can use the AZ::Job 
        // defined IncrementDependentCount method, start the async i/o, 
        // and call WaitForChildren. When the i/o completes, we would call
        // DecrementDependentCount, which would cause WaitForChildren to 
        // return. We would then call OnResponse.

        // Create the request
        std::shared_ptr<Aws::Http::HttpRequest> request = InitializeRequest();
        std::shared_ptr<Aws::Http::HttpResponse> httpResponse;

        if (request)
        {
            // Populate headers
            for (const auto& header : m_requestHeaders)
            {
                request->SetHeaderValue(Util::ToAwsString(header.first), Util::ToAwsString(header.second));
            }

            // Populate the body
            if (!m_requestBody.empty())
            {
                auto body = std::make_shared<Aws::StringStream>();
                body->write(m_requestBody.c_str(), m_requestBody.length());
                request->AddContentBody(body);
            }

            // Allow descendant classes to modify the request if desired
            this->CustomizeRequest(request);

            // Sign the request
            if (m_awsAuthSigner)
            {
                m_awsAuthSigner->SignRequest(*request);
            }

            httpResponse = m_httpClient->MakeRequest(request, m_readRateLimiter.get(), m_writeRateLimiter.get());
        }

        // Allow descendant classes to process the response
        this->ProcessResponse(httpResponse);

        // Configure and deliver our response
        auto callbackResponse = AZStd::make_shared<Response>();
        callbackResponse->m_response = httpResponse;
        bool failure = true;

        if (httpResponse)
        {
            Aws::IOStream& responseBody = httpResponse->GetResponseBody();
            std::istreambuf_iterator<AZStd::string::value_type> eos;
            callbackResponse->m_responseBody = AZStd::string{ std::istreambuf_iterator<AZStd::string::value_type>(responseBody), eos };
            callbackResponse->m_responseCode = static_cast<int>(httpResponse->GetResponseCode());

            if (callbackResponse->m_responseCode >= 200 && callbackResponse->m_responseCode <= 299)
            {
                failure = false;

                if (m_successCallback)
                {
                    auto callback = AZStd::make_shared<SuccessFn>(AZStd::move(m_successCallback));
                    auto fn = AZStd::function<void()>([callbackResponse, callback]() { (*callback)(callbackResponse); });
                    AZ::TickBus::QueueFunction(fn);
                }
            }
        }

        if (failure && m_failureCallback)
        {
            auto callback = AZStd::make_shared<SuccessFn>(AZStd::move(m_failureCallback));
            auto fn = AZStd::function<void()>([callbackResponse, callback]() { (*callback)(callbackResponse); });
            AZ::TickBus::QueueFunction(fn);
        }
    }