Initial commit

This commit is contained in:
alexpete
2021-03-05 11:26:34 -08:00
commit a10351f38d
27091 changed files with 5521199 additions and 0 deletions
+12
View File
@@ -0,0 +1,12 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
add_subdirectory(Code)
+73
View File
@@ -0,0 +1,73 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
ly_get_list_relative_pal_filename(source_pal_dir ${CMAKE_CURRENT_LIST_DIR}/Source/Platform/${PAL_PLATFORM_NAME})
ly_add_target(
NAME HttpRequestor.Static STATIC
NAMESPACE Gem
FILES_CMAKE
httprequestor_files.cmake
INCLUDE_DIRECTORIES
PRIVATE
Source
PUBLIC
Include
BUILD_DEPENDENCIES
PRIVATE
AZ::AzCore
AZ::AzFramework
AZ::AWSNativeSDKInit
PUBLIC
3rdParty::AWSNativeSDK::Core
)
ly_add_target(
NAME HttpRequestor ${PAL_TRAIT_MONOLITHIC_DRIVEN_MODULE_TYPE}
NAMESPACE Gem
OUTPUT_NAME Gem.HttpRequestor.28479e255bde466e91fc34eec808d9c7.v1.0.0
FILES_CMAKE
httprequestor_shared_files.cmake
PLATFORM_INCLUDE_FILES
${source_pal_dir}/httprequestor_${PAL_PLATFORM_NAME_LOWERCASE}.cmake
INCLUDE_DIRECTORIES
PRIVATE
Source
PUBLIC
Include
BUILD_DEPENDENCIES
PRIVATE
AZ::AzCore
Gem::HttpRequestor.Static
)
################################################################################
# Tests
################################################################################
if(PAL_TRAIT_BUILD_TESTS_SUPPORTED)
ly_add_target(
NAME HttpRequestor.Tests ${PAL_TRAIT_TEST_TARGET_TYPE}
NAMESPACE Gem
FILES_CMAKE
httprequestor_tests_files.cmake
INCLUDE_DIRECTORIES
PRIVATE
Source
Tests
BUILD_DEPENDENCIES
PRIVATE
AZ::AzTest
Gem::HttpRequestor.Static
)
ly_add_googletest(
NAME Gem::HttpRequestor.Tests
)
endif()
@@ -0,0 +1,92 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
#include "HttpTypes.h"
namespace HttpRequestor
{
/*
**
** The Parameters needed to make a HTTP call and then receive the
** returned JSON in a meaningful place. Examples of use are in the
** HttpRequestCaller class.
**
*/
class Parameters
{
public:
// Initializing ctor
Parameters(const AZStd::string& URI, Aws::Http::HttpMethod method, const Callback& callback);
Parameters(const AZStd::string& URI, Aws::Http::HttpMethod method, const Headers& headers, const Callback& callback);
Parameters(const AZStd::string& URI, Aws::Http::HttpMethod method, const Headers& headers, const AZStd::string& body, const Callback& callback);
// Defaults
virtual ~Parameters() = default;
Parameters(const Parameters&) = default;
Parameters& operator=(const Parameters&) = default;
Parameters(Parameters&&) = default;
Parameters& operator=(Parameters&&) = default;
//returns the URI in string form as an recipient of the HTTP connection
const Aws::String& GetURI() const { return m_URI; }
//returns the method of which the HTTP request will take. GET, POST, DELETE, PUT, or HEAD
Aws::Http::HttpMethod GetMethod() const { return m_method; }
//returns the list of extra headers to include in the request
const Headers & GetHeaders() const { return m_headers; }
//returns the stream for the body of the request
const std::shared_ptr<std::stringstream> & GetBodyStream() const { return m_bodyStream; }
//returns the function of which to feed back the JSON that the HTTP call resulted in. The function also requires the HTTPResponseCode indicating if the call was successful or failed
const Callback & GetCallback() const { return m_callback; }
private:
Aws::String m_URI;
Aws::Http::HttpMethod m_method;
Headers m_headers;
std::shared_ptr<std::stringstream> m_bodyStream; // required by Aws::Http::HttpRequest
Callback m_callback;
};
inline Parameters::Parameters(const AZStd::string& URI, Aws::Http::HttpMethod method, const Callback& callback)
: m_URI(URI.c_str())
, m_method(method)
, m_callback(callback)
{
}
inline Parameters::Parameters(const AZStd::string& URI, Aws::Http::HttpMethod method, const Headers& headers, const Callback& callback)
: m_URI(URI.c_str())
, m_method(method)
, m_headers(headers)
, m_callback(callback)
{
}
inline Parameters::Parameters(const AZStd::string& URI, Aws::Http::HttpMethod method, const Headers& headers, const AZStd::string& body, const Callback& callback)
: m_URI(URI.c_str())
, m_method(method)
, m_headers(headers)
, m_bodyStream(std::make_shared<std::stringstream>(body.c_str()))
, m_callback(callback)
{
}
}
@@ -0,0 +1,40 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
#include <AzCore/EBus/EBus.h>
#include "HttpTypes.h"
namespace HttpRequestor
{
class HttpRequestorRequests
: public AZ::EBusTraits
{
public:
static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Single;
static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single;
// Public functions
virtual void AddRequest(const AZStd::string& URI, Aws::Http::HttpMethod method, const Callback& callback) = 0;
virtual void AddRequestWithHeaders(const AZStd::string& URI, Aws::Http::HttpMethod method, const Headers & headers, const Callback& callback) = 0;
virtual void AddRequestWithHeadersAndBody(const AZStd::string& URI, Aws::Http::HttpMethod method, const Headers & headers, const AZStd::string& body, const Callback& callback) = 0;
virtual void AddTextRequest(const AZStd::string& URI, Aws::Http::HttpMethod method, const TextCallback& callback) = 0;
virtual void AddTextRequestWithHeaders(const AZStd::string& URI, Aws::Http::HttpMethod method, const Headers & headers, const TextCallback& callback) = 0;
virtual void AddTextRequestWithHeadersAndBody(const AZStd::string& URI, Aws::Http::HttpMethod method, const Headers & headers, const AZStd::string& body, const TextCallback& callback) = 0;
};
using HttpRequestorRequestBus = AZ::EBus<HttpRequestorRequests>;
} // namespace HttpRequestor
@@ -0,0 +1,87 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "HttpTypes.h"
#include <sstream>
namespace HttpRequestor
{
/*
**
** The Parameters needed to make a HTTP call and then receive the
** returned TEXT from the web request without parsing it.
**
*/
class TextParameters
{
public:
// Initializing ctor
TextParameters(const AZStd::string& URI, Aws::Http::HttpMethod method, const TextCallback& callback);
TextParameters(const AZStd::string& URI, Aws::Http::HttpMethod method, const Headers& headers, const TextCallback& callback);
TextParameters(const AZStd::string& URI, Aws::Http::HttpMethod method, const Headers& headers, const AZStd::string& body, const TextCallback& callback);
// Defaults
~TextParameters() = default;
TextParameters(const TextParameters&) = default;
TextParameters& operator=(const TextParameters&) = default;
TextParameters(TextParameters&&) = default;
TextParameters& operator=(TextParameters&&) = default;
//returns the URI in string form as an recipient of the HTTP connection
const Aws::String& GetURI() const { return m_URI; }
//returns the method of which the HTTP request will take. GET, POST, DELETE, PUT, or HEAD
Aws::Http::HttpMethod GetMethod() const { return m_method; }
//returns the list of extra headers to include in the request
const Headers & GetHeaders() const { return m_headers; }
//returns the stream for the body of the request
const std::shared_ptr<std::stringstream> & GetBodyStream() const { return m_bodyStream; }
//returns the function of which to feed back the TEXT that the HTTP call resulted in. The function also requires the HTTPResponseCode indicating if the call was successful or failed
const TextCallback & GetCallback() const { return m_callback; }
private:
Aws::String m_URI;
Aws::Http::HttpMethod m_method;
Headers m_headers;
std::shared_ptr<std::stringstream> m_bodyStream;
TextCallback m_callback;
};
inline TextParameters::TextParameters(const AZStd::string& URI, Aws::Http::HttpMethod method, const TextCallback& callback)
: m_URI(URI.c_str())
, m_method(method)
, m_callback(callback)
{
}
inline TextParameters::TextParameters(const AZStd::string& URI, Aws::Http::HttpMethod method, const Headers& headers, const TextCallback& callback)
: m_URI(URI.c_str())
, m_method(method)
, m_headers(headers)
, m_callback(callback)
{
}
inline TextParameters::TextParameters(const AZStd::string& URI, Aws::Http::HttpMethod method, const Headers& headers, const AZStd::string& body, const TextCallback& callback)
: m_URI(URI.c_str())
, m_method(method)
, m_headers(headers)
, m_bodyStream( std::make_shared<std::stringstream>(body.c_str()) )
, m_callback(callback)
{
}
}
@@ -0,0 +1,46 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
// The AWS Native SDK AWSAllocator triggers a warning due to accessing members of std::allocator directly.
// AWSAllocator.h(70): warning C4996: 'std::allocator<T>::pointer': warning STL4010: Various members of std::allocator are deprecated in C++17.
// Use std::allocator_traits instead of accessing these members directly.
// You can define _SILENCE_CXX17_OLD_ALLOCATOR_MEMBERS_DEPRECATION_WARNING or _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS to acknowledge that you have received this warning.
AZ_PUSH_DISABLE_WARNING(4251 4996, "-Wunknown-warning-option")
#include <aws/core/http/HttpTypes.h>
#include <aws/core/http/HttpResponse.h>
#include <aws/core/utils/json/JsonSerializer.h>
AZ_POP_DISABLE_WARNING
#include <AzCore/std/string/string.h>
#include <AzCore/std/containers/map.h>
#include <AzCore/std/functional.h>
namespace HttpRequestor
{
//
// the call back function for http requests.
//
using Callback = AZStd::function<void(const Aws::Utils::Json::JsonView&, Aws::Http::HttpResponseCode)>;
//
// the call back function for any http text requests.
//
using TextCallback = AZStd::function<void(const AZStd::string&, Aws::Http::HttpResponseCode)>;
//
// a map of REST headers.
//
using Headers = AZStd::map<AZStd::string, AZStd::string>;
}
@@ -0,0 +1,16 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "HttpRequestor_precompiled.h"
#include <AzCore/Module/Module.h>
AZ_DECLARE_MODULE_CLASS(Gem_HttpRequestor, AZ::Module)
@@ -0,0 +1,201 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "HttpRequestor_precompiled.h"
#include <AzFramework/AzFramework_Traits_Platform.h>
// The AWS Native SDK AWSAllocator triggers a warning due to accessing members of std::allocator directly.
// AWSAllocator.h(70): warning C4996: 'std::allocator<T>::pointer': warning STL4010: Various members of std::allocator are deprecated in C++17.
// Use std::allocator_traits instead of accessing these members directly.
// You can define _SILENCE_CXX17_OLD_ALLOCATOR_MEMBERS_DEPRECATION_WARNING or _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS to acknowledge that you have received this warning.
AZ_PUSH_DISABLE_WARNING(4251 4996, "-Wunknown-warning-option")
#include <aws/core/http/HttpClient.h>
#include <aws/core/http/HttpClientFactory.h>
#include <aws/core/http/HttpRequest.h>
#include <aws/core/http/HttpResponse.h>
#include <aws/core/client/ClientConfiguration.h>
AZ_POP_DISABLE_WARNING
#include <AWSNativeSDKInit/AWSNativeSDKInit.h>
#include <AzCore/std/string/conversions.h>
#include "HttpRequestManager.h"
namespace HttpRequestor
{
const char* Manager::s_loggingName = "GemHttpRequestManager";
Manager::Manager()
{
AZStd::thread_desc desc;
desc.m_name = s_loggingName;
desc.m_cpuId = AFFINITY_MASK_USERTHREADS;
m_runThread = true;
// Shutdown will be handled by the InitializationManager - no need to call in the destructor
AWSNativeSDKInit::InitializationManager::InitAwsApi();
auto function = AZStd::bind(&Manager::ThreadFunction, this);
m_thread = AZStd::thread(function, &desc);
}
Manager::~Manager()
{
// NativeSDK Shutdown does not need to be called here - will be taken care of by the InitializationManager
m_runThread = false;
m_requestConditionVar.notify_all();
if (m_thread.joinable())
{
m_thread.join();
}
}
void Manager::AddRequest(Parameters && httpRequestParameters)
{
{
AZStd::lock_guard<AZStd::mutex> lock(m_requestMutex);
m_requestsToHandle.push(AZStd::move(httpRequestParameters));
}
m_requestConditionVar.notify_all();
}
void Manager::AddTextRequest(TextParameters && httpTextRequestParameters)
{
{
AZStd::lock_guard<AZStd::mutex> lock(m_requestMutex);
m_textRequestsToHandle.push(AZStd::move(httpTextRequestParameters));
}
m_requestConditionVar.notify_all();
}
void Manager::ThreadFunction()
{
// Run the thread as long as directed
while (m_runThread)
{
HandleRequestBatch();
}
}
void Manager::HandleRequestBatch()
{
// Lock mutex and wait for work to be signalled via the condition variable
AZStd::unique_lock<AZStd::mutex> lock(m_requestMutex);
m_requestConditionVar.wait(lock, [&] { return !m_runThread || !m_requestsToHandle.empty() || !m_textRequestsToHandle.empty(); });
// Swap queues
AZStd::queue<Parameters> requestsToHandle;
requestsToHandle.swap(m_requestsToHandle);
AZStd::queue<TextParameters> textRequestsToHandle;
textRequestsToHandle.swap(m_textRequestsToHandle);
// Release lock
lock.unlock();
// Handle requests
while (!requestsToHandle.empty())
{
HandleRequest(requestsToHandle.front());
requestsToHandle.pop();
}
while (!textRequestsToHandle.empty())
{
HandleTextRequest(textRequestsToHandle.front());
textRequestsToHandle.pop();
}
}
void Manager::HandleRequest(const Parameters& httpRequestParameters)
{
Aws::Client::ClientConfiguration config;
config.enableTcpKeepAlive = AZ_TRAIT_AZFRAMEWORK_AWS_ENABLE_TCP_KEEP_ALIVE_SUPPORTED;
std::shared_ptr<Aws::Http::HttpClient> httpClient = Aws::Http::CreateHttpClient(config);
auto httpRequest = Aws::Http::CreateHttpRequest(httpRequestParameters.GetURI(), httpRequestParameters.GetMethod(), Aws::Utils::Stream::DefaultResponseStreamFactoryMethod);
AZ_Assert(httpRequest, "HttpRequest not created!");
for (const auto & it : httpRequestParameters.GetHeaders())
{
httpRequest->SetHeaderValue(it.first.c_str(), it.second.c_str());
}
if( httpRequestParameters.GetBodyStream() != nullptr)
{
httpRequest->AddContentBody(httpRequestParameters.GetBodyStream());
httpRequest->SetContentLength(AZStd::to_string(httpRequestParameters.GetBodyStream()->str().length()).c_str());
}
auto httpResponse = httpClient->MakeRequest(httpRequest);
if (!httpResponse)
{
httpRequestParameters.GetCallback()(Aws::Utils::Json::JsonValue(), Aws::Http::HttpResponseCode::INTERNAL_SERVER_ERROR);
return;
}
if (httpResponse->GetResponseCode() != Aws::Http::HttpResponseCode::OK)
{
httpRequestParameters.GetCallback()(Aws::Utils::Json::JsonValue(), httpResponse->GetResponseCode());
return;
}
Aws::Utils::Json::JsonValue json(httpResponse->GetResponseBody());
if (json.WasParseSuccessful())
{
httpRequestParameters.GetCallback()(AZStd::move(json), httpResponse->GetResponseCode());
}
else
{
httpRequestParameters.GetCallback()(Aws::Utils::Json::JsonValue(), Aws::Http::HttpResponseCode::INTERNAL_SERVER_ERROR);
}
}
void Manager::HandleTextRequest(const TextParameters & httpRequestParameters)
{
Aws::Client::ClientConfiguration config;
config.enableTcpKeepAlive = AZ_TRAIT_AZFRAMEWORK_AWS_ENABLE_TCP_KEEP_ALIVE_SUPPORTED;
std::shared_ptr<Aws::Http::HttpClient> httpClient = Aws::Http::CreateHttpClient(config);
auto httpRequest = Aws::Http::CreateHttpRequest(httpRequestParameters.GetURI(), httpRequestParameters.GetMethod(), Aws::Utils::Stream::DefaultResponseStreamFactoryMethod);
for (const auto & it : httpRequestParameters.GetHeaders())
{
httpRequest->SetHeaderValue(it.first.c_str(), it.second.c_str());
}
if (httpRequestParameters.GetBodyStream() != nullptr)
{
httpRequest->AddContentBody(httpRequestParameters.GetBodyStream());
}
auto httpResponse = httpClient->MakeRequest(httpRequest);
if (!httpResponse)
{
httpRequestParameters.GetCallback()(AZStd::string(), Aws::Http::HttpResponseCode::INTERNAL_SERVER_ERROR);
return;
}
if (httpResponse->GetResponseCode() != Aws::Http::HttpResponseCode::OK)
{
httpRequestParameters.GetCallback()(AZStd::string(), httpResponse->GetResponseCode());
return;
}
// load up the raw output into a string
// TODO(aaj): it feels like there should be some limit maybe 1 MB?
std::istreambuf_iterator<char> eos;
AZStd::string data(std::istreambuf_iterator<char>(httpResponse->GetResponseBody()), eos);
httpRequestParameters.GetCallback()(AZStd::move(data), httpResponse->GetResponseCode());
}
}
@@ -0,0 +1,63 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
#include <AzCore/std/containers/queue.h>
#include <AzCore/std/smart_ptr/make_shared.h>
#include <AzCore/std/parallel/atomic.h>
#include <AzCore/std/parallel/mutex.h>
#include <AzCore/std/parallel/condition_variable.h>
#include <AzCore/std/parallel/thread.h>
#include <HttpRequestor/HttpRequestParameters.h>
#include <HttpRequestor/HttpTextRequestParameters.h>
namespace HttpRequestor
{
class Manager
{
public:
Manager();
virtual ~Manager();
// Add these parameters to a queue of request parameters to send off as an HTTP request as soon as they reach the head of the queue
void AddRequest(Parameters && httpRequestParameters);
// Add these parameters to a queue of request parameters to send off as an HTTP TEXT request as soon as they reach the head of the queue
void AddTextRequest(TextParameters && httpTextRequestParameters);
private:
// RequestManager thread loop.
void ThreadFunction();
// Called by ThreadFunction. Waits for timeout or until notified and processes any requests queued up.
void HandleRequestBatch();
// Perform an HTTP request, block until a response is received, then give the returned JSON to the callback to parse. Returns the HTTPResponseCode to the callback to handle any errors.
void HandleRequest(const Parameters & httpRequestParameters);
// Perform an HTTP request, block until a response is received, then give the returned TEXT to the callback to parse. Returns the HTTPResponseCode to the callback to handle any errors.
void HandleTextRequest(const TextParameters & httpTextRequestParameters);
private:
AZStd::queue<Parameters> m_requestsToHandle; // Queue of requests that will be made in order of time received
AZStd::queue<TextParameters> m_textRequestsToHandle; // Queue of requests for TEXT blobs that will be made in order of time received
AZStd::mutex m_requestMutex; // Member variables for synchronization
AZStd::condition_variable m_requestConditionVar;
AZStd::atomic<bool> m_runThread; // Run flag used to signal the worker thread
AZStd::thread m_thread; // This is the thread that will be used for all async operations
static const char* s_loggingName; // Name to use for log messages etc...
};
using ManagerPtr = AZStd::shared_ptr<Manager>;
}
@@ -0,0 +1,49 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "HttpRequestor_precompiled.h"
#include "HttpRequestorSystemComponent.h"
#include <AzCore/Module/Module.h>
namespace HttpRequestor
{
class HttpRequestorModule
: public AZ::Module
{
public:
AZ_RTTI(HttpRequestorModule, "{FD411E40-AF83-4F6B-A5A3-F59AB71150BF}", AZ::Module);
HttpRequestorModule()
: AZ::Module()
{
// Push results of [MyComponent]::CreateDescriptor() into m_descriptors here.
m_descriptors.insert(m_descriptors.end(), {
HttpRequestorSystemComponent::CreateDescriptor(),
});
}
/**
* Add required SystemComponents to the SystemEntity.
*/
AZ::ComponentTypeList GetRequiredSystemComponents() const override
{
return AZ::ComponentTypeList{
azrtti_typeid<HttpRequestorSystemComponent>(),
};
}
};
}
// DO NOT MODIFY THIS LINE UNLESS YOU RENAME THE GEM
// The first parameter should be GemName_GemIdLower
// The second should be the fully qualified name of the class above
AZ_DECLARE_MODULE_CLASS(Gem_HttpRequestor, HttpRequestor::HttpRequestorModule)
@@ -0,0 +1,126 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "HttpRequestor_precompiled.h"
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/Serialization/EditContext.h>
#include "HttpRequestorSystemComponent.h"
namespace HttpRequestor
{
void HttpRequestorSystemComponent::AddRequest(const AZStd::string& URI, Aws::Http::HttpMethod method, const Callback& callback)
{
if(m_httpManager != nullptr)
{
m_httpManager->AddRequest( Parameters(URI, method, callback) );
}
}
void HttpRequestorSystemComponent::AddRequestWithHeaders(const AZStd::string& URI, Aws::Http::HttpMethod method, const Headers & headers, const Callback& callback)
{
if (m_httpManager != nullptr)
{
m_httpManager->AddRequest(Parameters(URI, method, headers, callback));
}
}
void HttpRequestorSystemComponent::AddRequestWithHeadersAndBody(const AZStd::string& URI, Aws::Http::HttpMethod method, const Headers & headers, const AZStd::string& body, const Callback& callback)
{
if (m_httpManager != nullptr)
{
m_httpManager->AddRequest(Parameters(URI, method, headers, body, callback));
}
}
void HttpRequestorSystemComponent::AddTextRequest(const AZStd::string& URI, Aws::Http::HttpMethod method, const TextCallback& callback)
{
if (m_httpManager != nullptr)
{
m_httpManager->AddTextRequest( TextParameters(URI, method, callback));
}
}
void HttpRequestorSystemComponent::AddTextRequestWithHeaders(const AZStd::string& URI, Aws::Http::HttpMethod method, const Headers & headers, const TextCallback& callback)
{
if (m_httpManager != nullptr)
{
m_httpManager->AddTextRequest(TextParameters(URI, method, headers, callback));
}
}
void HttpRequestorSystemComponent::AddTextRequestWithHeadersAndBody(const AZStd::string& URI, Aws::Http::HttpMethod method, const Headers & headers, const AZStd::string& body, const TextCallback& callback)
{
if (m_httpManager != nullptr)
{
m_httpManager->AddTextRequest(TextParameters(URI, method, headers, body, callback));
}
}
void HttpRequestorSystemComponent::Reflect(AZ::ReflectContext* context)
{
if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
{
serialize->Class<HttpRequestorSystemComponent, AZ::Component>()
->Version(1);
;
if (AZ::EditContext* ec = serialize->GetEditContext())
{
ec->Class<HttpRequestorSystemComponent>("HttpRequestor", "Will make HTTP Rest calls")
->ClassElement(AZ::Edit::ClassElements::EditorData, "")
// ->Attribute(AZ::Edit::Attributes::Category, "") Set a category
->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("System"))
->Attribute(AZ::Edit::Attributes::AutoExpand, true)
;
}
}
}
void HttpRequestorSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
{
provided.push_back(AZ_CRC("HttpRequestorService"));
}
void HttpRequestorSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
{
incompatible.push_back(AZ_CRC("HttpRequestorService"));
}
void HttpRequestorSystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
{
(void)required;
}
void HttpRequestorSystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
{
(void)dependent;
}
void HttpRequestorSystemComponent::Init()
{
}
void HttpRequestorSystemComponent::Activate()
{
m_httpManager = AZStd::make_shared<Manager>();
HttpRequestorRequestBus::Handler::BusConnect();
}
void HttpRequestorSystemComponent::Deactivate()
{
HttpRequestorRequestBus::Handler::BusDisconnect();
m_httpManager = nullptr;
}
}
@@ -0,0 +1,58 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
#include <AzCore/Component/Component.h>
#include <HttpRequestor/HttpRequestorBus.h>
#include "HttpRequestManager.h"
namespace HttpRequestor
{
class HttpRequestorSystemComponent
: public AZ::Component
, protected HttpRequestorRequestBus::Handler
{
public:
AZ_COMPONENT(HttpRequestorSystemComponent, "{CF29468F-1F67-497F-B4FF-C0F123584864}");
static void Reflect(AZ::ReflectContext* context);
static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent);
protected:
////////////////////////////////////////////////////////////////////////
// HttpRequestorRequestBus interface implementation
////////////////////////////////////////////////////////////////////////
void AddRequest(const AZStd::string& URI, Aws::Http::HttpMethod method, const Callback& callback) override;
void AddRequestWithHeaders(const AZStd::string& URI, Aws::Http::HttpMethod method, const Headers & headers, const Callback& callback) override;
void AddRequestWithHeadersAndBody(const AZStd::string& URI, Aws::Http::HttpMethod method, const Headers & headers, const AZStd::string& body, const Callback& callback) override;
void AddTextRequest(const AZStd::string& URI, Aws::Http::HttpMethod method, const TextCallback& callback) override;
void AddTextRequestWithHeaders(const AZStd::string& URI, Aws::Http::HttpMethod method, const Headers & headers, const TextCallback& callback) override;
void AddTextRequestWithHeadersAndBody(const AZStd::string& URI, Aws::Http::HttpMethod method, const Headers & headers, const AZStd::string& body, const TextCallback& callback) override;
////////////////////////////////////////////////////////////////////////
// AZ::Component interface implementation
void Init() override;
void Activate() override;
void Deactivate() override;
////////////////////////////////////////////////////////////////////////
private:
ManagerPtr m_httpManager;
};
}
@@ -0,0 +1,13 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "HttpRequestor_precompiled.h"
@@ -0,0 +1,15 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
#include <AzCore/PlatformDef.h>
@@ -0,0 +1,11 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
@@ -0,0 +1,11 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
@@ -0,0 +1,10 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
@@ -0,0 +1,10 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
@@ -0,0 +1,10 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
@@ -0,0 +1,64 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "HttpRequestor_precompiled.h"
#include <AzTest/AzTest.h>
#include <AzCore/std/smart_ptr/shared_ptr.h>
#include <AzCore/std/parallel/atomic.h>
#include <AzCore/std/parallel/condition_variable.h>
#include "HttpRequestManager.h"
class Integ_HttpTest
: public ::testing::Test
{
public:
HttpRequestor::ManagerPtr m_httpRequestManager;
// to wait for test to complete
AZStd::mutex m_requestMutex;
AZStd::condition_variable m_requestConditionVar;
AZStd::string resultData;
AZStd::atomic<Aws::Http::HttpResponseCode> resultCode;
Integ_HttpTest()
{
m_httpRequestManager = AZStd::make_shared<HttpRequestor::Manager>();
resultCode = Aws::Http::HttpResponseCode::REQUEST_NOT_MADE;
resultData = "{}";
AZStd::unique_lock<AZStd::mutex> lock(m_requestMutex);
m_requestConditionVar.wait_for(lock, AZStd::chrono::milliseconds(10));
}
virtual ~Integ_HttpTest()
{
m_httpRequestManager.reset();
}
};
TEST_F(Integ_HttpTest, HttpRequesterTest)
{
m_httpRequestManager->AddTextRequest(HttpRequestor::TextParameters("https://httpbin.org/ip", Aws::Http::HttpMethod::HTTP_GET, [this](const AZStd::string & data, Aws::Http::HttpResponseCode code)
{
resultData = data;
resultCode = code;
m_requestConditionVar.notify_all();
}));
AZStd::unique_lock<AZStd::mutex> lock(m_requestMutex);
m_requestConditionVar.wait_for(lock, AZStd::chrono::milliseconds(5000));
EXPECT_NE(Aws::Http::HttpResponseCode::REQUEST_NOT_MADE, resultCode);
}
AZ_UNIT_TEST_HOOK(DEFAULT_UNIT_TEST_ENV);
@@ -0,0 +1,23 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
set(FILES
Source/HttpRequestor_precompiled.cpp
Source/HttpRequestor_precompiled.h
Source/HttpRequestManager.cpp
Source/HttpRequestManager.h
Include/HttpRequestor/HttpRequestorBus.h
Include/HttpRequestor/HttpTextRequestParameters.h
Include/HttpRequestor/HttpRequestParameters.h
Include/HttpRequestor/HttpTypes.h
Source/HttpRequestorSystemComponent.cpp
Source/HttpRequestorSystemComponent.h
)
@@ -0,0 +1,15 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
set(FILES
Source/HttpRequestorModule.cpp
)
@@ -0,0 +1,14 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
set(FILES
Tests/HttpRequestorTest.cpp
)
@@ -0,0 +1,16 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
set(FILES
Source/HttpRequestor_precompiled.cpp
Source/HttpRequestor_precompiled.h
Source/ComponentStub.cpp
)
+11
View File
@@ -0,0 +1,11 @@
{
"GemFormatVersion": 3,
"Uuid": "28479e255bde466e91fc34eec808d9c7",
"Name": "HttpRequestor",
"DisplayName": "HttpRequestor",
"Version": "1.0.0",
"LinkType": "Dynamic",
"Summary": "A Gem to handle HTTP/HTTPs requests.",
"Tags": ["http", "https", "rest"],
"IconPath": "preview.png"
}
+3
View File
@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:82adcf8e7b72de929fd89f5a677d6786b2dc749541bc5ee792551d50c43dd6dc
size 2296