Using C++ To Connect To Web Services - Steve Gates - CppCon 2014
Using C++ To Connect To Web Services - Steve Gates - CppCon 2014
in C++
Steve Gates, Microsoft
[email protected]
Overview
• What a typical web service looks like
• Some options for consuming
• Share a library, the C++ Rest SDK, we’ve been building
Walkthrough how to use
How you can get involved if interested
• What about if you care about asynchrony, cross platform, and C++11
style?
The C++ Rest SDK
• The C++ Rest SDK aims to fill these gaps by providing the building
blocks for accessing services with high level APIs covering:
HTTP, URIs, JSON, OAuth, and WebSockets
• Goal is to make it easier to consume web APIs and write client SDK
libraries
The C++ Rest SDK
• Simple APIs
Having every single feature is not as important as a straight forward API
• Asynchronous
All I/O and potentially long running work needs to be asynchronous
• C++11 style
• All the code I show you the today runs on Windows desktop/server
(XP+), Windows Store, Windows Phone, OS X, iOS, Ubuntu, and
Android
Asynchrony
pplx::task
pplx::task
• Cross platform tasks from
Parallel Patterns Library (PPL) task APIs
• Similar to std::future, but with
continuations
• Later could be replaced with scheduler interface
futures (N3970)
• Lots of existing presentations and Windows Grand
resources on task based Boost ASIO
Threadpool Central
programming pthreads
ConcRT Dispatch
pplx::task – continuations
pplx::task<int> intTask = start_op();
intTask.then([](int value)
{
// Execute some work once operation has completed...
});
intTask.then([](pplx::task<int> op)
{
Task based continuation,
try
always executes
{
int value = op.get();
} catch (const std::runtime_error &e)
{ /* Perform error handling... */ }
});
http_client
http_client
• What is the ‘U’? Platform dependent string type, to allow using preferred type:
On Windows UTF-16, std::wstring
Other platforms UTF-8, std::string
http_client – hello world upload
// Manually build up request.
http_request req(methods::POST);
req.set_request_uri(U("mypath"));
req.headers().add(header_names::user_agent, U(“myclient"));
req.set_body(U("Hello World"));
http_response response = client.request(req).get();
• http_client also takes configuration for options like timeouts, chunk size, etc…
http_client – hello world
download
http_client client(U("http://myserver.com"));
http_response response = client.request(methods::GET).get();
if (response.status_code() == status_codes::OK)
{
const utility::string_t body = response.extract_string().get();
}
// Binary
websocket_outgoing_message msg;
streambuf<uint8_t> buffer = file_buffer<uint8_t>::open(U("myfile")).get();
msg.set_binary_message(buffer.create_istream(), 10); // Send only 10 bytes
pplx::task<void> sendTask = client.send(msg);
websocket_client – receiving
websocket_client client;
client.receive().then([](websocket_incoming_message msg)
{
return msg.extract_string(); // Msg body could still be arriving
}).then([](std::string &data)
{
// Use string...
});
• Can also access body as a stream
• Adding option for using callback for repeated message receiving
Cross platform learning
• Test automation infrastructure
Gated, rolling, nightly
http://casablanca.codeplex.com/