Skip to content

Commit

Permalink
Move runtime class to runtime.h (#55)
Browse files Browse the repository at this point in the history
Enables integration with other runtimes.
  • Loading branch information
bmoffatt authored and marcomagdy committed Aug 27, 2019
1 parent 681652d commit 66b5af9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 40 deletions.
8 changes: 7 additions & 1 deletion CMakeLists.txt
Expand Up @@ -82,7 +82,13 @@ configure_file(
NEWLINE_STYLE LF)

# installation
install(FILES "include/aws/lambda-runtime/runtime.h" "include/aws/lambda-runtime/version.h"
install(FILES "include/aws/http/response.h"
DESTINATION "include/aws/http")

install(FILES
"include/aws/lambda-runtime/runtime.h"
"include/aws/lambda-runtime/version.h"
"include/aws/lambda-runtime/outcome.h"
DESTINATION "include/aws/lambda-runtime")

install(FILES "include/aws/logging/logging.h"
Expand Down
43 changes: 43 additions & 0 deletions include/aws/lambda-runtime/runtime.h
Expand Up @@ -14,9 +14,13 @@
* permissions and limitations under the License.
*/

#include <array>
#include <chrono>
#include <string>
#include <functional>
#include <curl/curl.h>
#include "aws/lambda-runtime/outcome.h"
#include "aws/http/response.h"

namespace aws {
namespace lambda_runtime {
Expand Down Expand Up @@ -115,6 +119,45 @@ class invocation_response {
bool is_success() const { return m_success; }
};

struct no_result {
};

class runtime {
public:
using next_outcome = aws::lambda_runtime::outcome<invocation_request, aws::http::response_code>;
using post_outcome = aws::lambda_runtime::outcome<no_result, aws::http::response_code>;

runtime(std::string const& endpoint);
~runtime();

/**
* Ask lambda for an invocation.
*/
next_outcome get_next();

/**
* Tells lambda that the function has succeeded.
*/
post_outcome post_success(std::string const& request_id, invocation_response const& handler_response);

/**
* Tells lambda that the function has failed.
*/
post_outcome post_failure(std::string const& request_id, invocation_response const& handler_response);

private:
void set_curl_next_options();
void set_curl_post_result_options();
post_outcome do_post(
std::string const& url,
std::string const& request_id,
invocation_response const& handler_response);

private:
std::array<std::string const, 3> const m_endpoints;
CURL* const m_curl_handle;
};

inline std::chrono::milliseconds invocation_request::get_time_remaining() const
{
using namespace std::chrono;
Expand Down
39 changes: 0 additions & 39 deletions src/runtime.cpp
Expand Up @@ -145,45 +145,6 @@ static int rt_curl_debug_callback(CURL* handle, curl_infotype type, char* data,
}
#endif

struct no_result {
};

class runtime {
public:
using next_outcome = aws::lambda_runtime::outcome<invocation_request, aws::http::response_code>;
using post_outcome = aws::lambda_runtime::outcome<no_result, aws::http::response_code>;

runtime(std::string const& endpoint);
~runtime();

/**
* Ask lambda for an invocation.
*/
next_outcome get_next();

/**
* Tells lambda that the function has succeeded.
*/
post_outcome post_success(std::string const& request_id, invocation_response const& handler_response);

/**
* Tells lambda that the function has failed.
*/
post_outcome post_failure(std::string const& request_id, invocation_response const& handler_response);

private:
void set_curl_next_options();
void set_curl_post_result_options();
post_outcome do_post(
std::string const& url,
std::string const& request_id,
invocation_response const& handler_response);

private:
std::array<std::string const, 3> const m_endpoints;
CURL* const m_curl_handle;
};

runtime::runtime(std::string const& endpoint)
: m_endpoints{{endpoint + "/2018-06-01/runtime/init/error",
endpoint + "/2018-06-01/runtime/invocation/next",
Expand Down

0 comments on commit 66b5af9

Please sign in to comment.