Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 113/stl naming #129

Merged
merged 6 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ message("${PROJECT_NAME} LIFT_CODE_COVERAGE = ${LIFT_CODE_COVERAGE}")
message("${PROJECT_NAME} LIFT_USER_LINK_LIBRARIES = ${LIFT_USER_LINK_LIBRARIES}")

set(LIBLIFT_SOURCE_FILES
inc/lift/Const.hpp
inc/lift/Escape.hpp src/Escape.cpp
inc/lift/EventLoop.hpp src/EventLoop.cpp
inc/lift/Executor.hpp src/Executor.cpp
inc/lift/Header.hpp src/Header.cpp
inc/lift/Http.hpp src/Http.cpp
inc/lift/Init.hpp src/Init.cpp
inc/lift/Lift.hpp
inc/lift/LiftStatus.hpp src/LiftStatus.cpp
inc/lift/MimeField.hpp src/MimeField.cpp
inc/lift/QueryBuilder.hpp src/QueryBuilder.cpp
inc/lift/Request.hpp src/Request.cpp
inc/lift/ResolveHost.hpp src/ResolveHost.cpp
inc/lift/Response.hpp src/Response.cpp
inc/lift/Share.hpp src/Share.cpp
inc/lift/const.hpp
inc/lift/escape.hpp src/escape.cpp
inc/lift/event_loop.hpp src/event_loop.cpp
inc/lift/executor.hpp src/executor.cpp
inc/lift/header.hpp src/header.cpp
inc/lift/http.hpp src/http.cpp
inc/lift/init.hpp src/init.cpp
inc/lift/lift_status.hpp src/lift_status.cpp
inc/lift/lift.hpp
inc/lift/mime_field.hpp src/mime_field.cpp
inc/lift/query_builder.hpp src/query_builder.cpp
inc/lift/request.hpp src/request.cpp
inc/lift/resolve_host.hpp src/resolve_host.cpp
inc/lift/response.hpp src/response.cpp
inc/lift/share.hpp src/share.cpp
)

add_library(${PROJECT_NAME} STATIC ${LIBLIFT_SOURCE_FILES})
Expand Down
36 changes: 13 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,50 +29,40 @@ to get your started on using liblifthttp with both the synchronous and asynchron
#### Synchronous and Asynchronous Requests
```C++
#include <iostream>
#include <lift/Lift.hpp>
#include <lift/lift.hpp>

int main()
{
// Synchronous requests can be created on the stack.
lift::Request request{"http://www.example.com"};
lift::request request{"http://www.example.com"};
// This is the blocking synchronous HTTP call.
auto response = request.Perform();
std::cout << "LiftStatus: " << lift::to_string(response.LiftStatus()) << "\n";
std::cout << "HTTP Status Code: " << lift::http::to_string(response.StatusCode()) << "\n";
for (const auto& header : response.Headers())
{
std::cout << header.Name() << ": " << header.Value() << "\n";
}
std::cout << response.Data();
auto response = request.perform();
std::cout << "Lift status: " << lift::to_string(response.lift_status()) << "\n";
std::cout << response << "\n"; // Will print the raw http response.

// Creating the event loop starts it immediately, it spawns a background thread for executing requests.
lift::EventLoop loop{};
lift::event_loop loop{};

// Create the request just like we did in the sync version, but now provide a lambda for on completion.
// NOTE: that the Lambda is executed ON the Lift event loop background thread. If you want to handle
// on completion processing on this main thread you need to std::move() it back via a queue or inter-thread
// communication. This is imporant if any resources are shared between the threads.
// NOTE: The request is created on the heap so ownership can be passed easily via an std::unique_ptr
// to the lift::EventLoop! lift::Request::make_unique() is a handy function to easily do so.
auto request_ptr = lift::Request::make_unique(
// to the lift::event_loop! lift::request::make_unique() is a handy function to easily do so.
auto request_ptr = lift::request::make_unique(
"http://www.example.com",
std::chrono::seconds{10}, // Give the request 10 seconds to complete or timeout.
[](lift::RequestPtr req_ptr, lift::Response response) {
std::cout << "LiftStatus: " << lift::to_string(response.LiftStatus()) << "\n";
std::cout << "HTTP Status Code: " << lift::http::to_string(response.StatusCode()) << "\n";
for (const auto& header : response.Headers())
{
std::cout << header.Name() << ": " << header.Value() << "\n";
}
std::cout << response.Data();
[](lift::request_ptr req_ptr, lift::response response) {
std::cout << "Lift status: " << lift::to_string(response.lift_status()) << "\n";
std::cout << response << "\n";
});

// Now inject the request into the event to be executed. Moving into the event loop is required,
// this passes ownership of the request to the event loop background worker thread.
loop.StartRequest(std::move(request_ptr));
loop.start_request(std::move(request_ptr));

// Block on this main thread until the lift event loop background thread has completed the request, or timed out.
while (loop.ActiveRequestCount() > 0)
while (!loop.empty())
{
std::this_thread::sleep_for(std::chrono::milliseconds{10});
}
Expand Down
18 changes: 9 additions & 9 deletions examples/async_simple.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#include <lift/Lift.hpp>
#include <lift/lift.hpp>

#include <atomic>
#include <chrono>
#include <iostream>
#include <string>
#include <thread>

static auto on_complete(std::unique_ptr<lift::Request> request_ptr, lift::Response response) -> void
static auto on_complete(lift::request_ptr request_ptr, lift::response response) -> void
{
if (response.LiftStatus() == lift::LiftStatus::SUCCESS)
if (response.lift_status() == lift::lift_status::success)
{
std::cout << "Completed " << request_ptr->Url() << " ms:" << response.TotalTime().count() << std::endl;
std::cout << "Completed " << request_ptr->url() << " ms:" << response.total_time().count() << std::endl;
}
else
{
std::cout << "Error: " << request_ptr->Url() << " : " << lift::to_string(response.LiftStatus()) << std::endl;
std::cout << "Error: " << request_ptr->url() << " : " << lift::to_string(response.lift_status()) << std::endl;
}
}

Expand All @@ -24,7 +24,7 @@ int main()

std::vector<std::string> urls = {"http://www.example.com", "http://www.google.com", "http://www.reddit.com"};

lift::EventLoop event_loop{};
lift::event_loop el{};

/**
* Create asynchronous requests for each url and inject them into
Expand All @@ -35,14 +35,14 @@ int main()
for (auto& url : urls)
{
std::cout << "Requesting " << url << std::endl;
auto request_ptr = lift::Request::make_unique(url, timeout, on_complete);
event_loop.StartRequest(std::move(request_ptr));
auto request_ptr = lift::request::make_unique(url, timeout, on_complete);
el.start_request(std::move(request_ptr));
timeout += 250ms;
std::this_thread::sleep_for(50ms);
}

// Now wait for all the requests to finish before cleaning up.
while (event_loop.ActiveRequestCount() > 0)
while (!el.empty())
{
std::this_thread::sleep_for(100ms);
}
Expand Down
22 changes: 11 additions & 11 deletions examples/benchmark.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <lift/Lift.hpp>
#include <lift/lift.hpp>

#include <atomic>
#include <chrono>
Expand Down Expand Up @@ -99,18 +99,18 @@ int main(int argc, char* argv[])
std::atomic<uint64_t> error{0};

{
std::vector<std::unique_ptr<lift::EventLoop>> loops;
std::vector<std::unique_ptr<lift::event_loop>> loops;
for (uint64_t i = 0; i < threads; ++i)
{
auto event_loop_ptr = std::make_unique<lift::EventLoop>();
auto event_loop_ptr = std::make_unique<lift::event_loop>();

for (uint64_t j = 0; j < connections; ++j)
{
auto& event_loop = *event_loop_ptr;

auto request_ptr = lift::Request::make_unique(
url, 30s, [&event_loop, &success, &error](lift::RequestPtr req_ptr, lift::Response response) {
if (response.LiftStatus() == lift::LiftStatus::SUCCESS)
auto request_ptr = lift::request::make_unique(
url, 30s, [&event_loop, &success, &error](lift::request_ptr req_ptr, lift::response response) {
if (response.lift_status() == lift::lift_status::success)
{
success.fetch_add(1, std::memory_order_relaxed);
}
Expand All @@ -120,12 +120,12 @@ int main(int argc, char* argv[])
}

// And request again until we are shutting down.
event_loop.StartRequest(std::move(req_ptr));
event_loop.start_request(std::move(req_ptr));
});

request_ptr->FollowRedirects(false);
request_ptr->Header("Connection", "Keep-Alive");
event_loop_ptr->StartRequest(std::move(request_ptr));
request_ptr->follow_redirects(false);
request_ptr->header("Connection", "Keep-Alive");
event_loop_ptr->start_request(std::move(request_ptr));
}

loops.emplace_back(std::move(event_loop_ptr));
Expand All @@ -135,7 +135,7 @@ int main(int argc, char* argv[])

for (auto& thread : loops)
{
thread->Stop();
thread->stop();
}
}

Expand Down
36 changes: 13 additions & 23 deletions examples/readme.cpp
Original file line number Diff line number Diff line change
@@ -1,48 +1,38 @@
#include <iostream>
#include <lift/Lift.hpp>
#include <lift/lift.hpp>

int main()
{
// Synchronous requests can be created on the stack.
lift::Request request{"http://www.example.com"};
lift::request request{"http://www.example.com"};
// This is the blocking synchronous HTTP call.
auto response = request.Perform();
std::cout << "LiftStatus: " << lift::to_string(response.LiftStatus()) << "\n";
std::cout << "HTTP Status Code: " << lift::http::to_string(response.StatusCode()) << "\n";
for (const auto& header : response.Headers())
{
std::cout << header.Name() << ": " << header.Value() << "\n";
}
std::cout << response.Data();
auto response = request.perform();
std::cout << "Lift status: " << lift::to_string(response.lift_status()) << "\n";
std::cout << response << "\n"; // Will print the raw http response.

// Creating the event loop starts it immediately, it spawns a background thread for executing requests.
lift::EventLoop loop{};
lift::event_loop loop{};

// Create the request just like we did in the sync version, but now provide a lambda for on completion.
// NOTE: that the Lambda is executed ON the Lift event loop background thread. If you want to handle
// on completion processing on this main thread you need to std::move() it back via a queue or inter-thread
// communication. This is imporant if any resources are shared between the threads.
// NOTE: The request is created on the heap so ownership can be passed easily via an std::unique_ptr
// to the lift::EventLoop! lift::Request::make_unique() is a handy function to easily do so.
auto request_ptr = lift::Request::make_unique(
// to the lift::event_loop! lift::request::make_unique() is a handy function to easily do so.
auto request_ptr = lift::request::make_unique(
"http://www.example.com",
std::chrono::seconds{10}, // Give the request 10 seconds to complete or timeout.
[](lift::RequestPtr req_ptr, lift::Response response) {
std::cout << "LiftStatus: " << lift::to_string(response.LiftStatus()) << "\n";
std::cout << "HTTP Status Code: " << lift::http::to_string(response.StatusCode()) << "\n";
for (const auto& header : response.Headers())
{
std::cout << header.Name() << ": " << header.Value() << "\n";
}
std::cout << response.Data();
[](lift::request_ptr req_ptr, lift::response response) {
std::cout << "Lift status: " << lift::to_string(response.lift_status()) << "\n";
std::cout << response << "\n";
});

// Now inject the request into the event to be executed. Moving into the event loop is required,
// this passes ownership of the request to the event loop background worker thread.
loop.StartRequest(std::move(request_ptr));
loop.start_request(std::move(request_ptr));

// Block on this main thread until the lift event loop background thread has completed the request, or timed out.
while (loop.ActiveRequestCount() > 0)
while (!loop.empty())
{
std::this_thread::sleep_for(std::chrono::milliseconds{10});
}
Expand Down
18 changes: 9 additions & 9 deletions examples/synch_simple.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
#include <lift/Lift.hpp>
#include <lift/lift.hpp>

#include <iostream>

int main()
{
{
lift::Request request{"http://www.example.com"};
lift::request request{"http://www.example.com"};
std::cout << "Requesting http://www.example.com" << std::endl;
const auto& response = request.Perform();
std::cout << response.Data() << std::endl;
const auto& response = request.perform();
std::cout << response.data() << std::endl;
}

{
lift::Request request{"http://www.google.com"};
lift::request request{"http://www.google.com"};
std::cout << "Requesting http://www.google.com" << std::endl;
const auto& response = request.Perform();
std::cout << response.Data() << std::endl;
const auto& response = request.perform();
std::cout << response.data() << std::endl;

for (const auto& header : response.Headers())
for (const auto& header : response.headers())
{
std::cout << header.Name() << ": " << header.Value() << "\n";
std::cout << header.name() << ": " << header.value() << "\n";
}
}

Expand Down
10 changes: 0 additions & 10 deletions inc/lift/Const.hpp

This file was deleted.

Loading