Elma
An event loop manager for embedded systems
Public Member Functions | List of all members
elma::Client Class Reference

An HTTP client for connecting to json services. More...

#include <client.h>

Public Member Functions

 Client ()
 
Clientget (std::string url, std::function< void(json &)> handler)
 
Clientprocess_responses ()
 
std::pair< std::string, std::string > url_parts (std::string url)
 
int num_responses () const
 

Detailed Description

An HTTP client for connecting to json services.

An example usage in a process

class GetTester : public Process {
public:
GetTester() : Process("Get Tester") {}
void init() {}
void start() {
got_response = false;
http_get("https://api.github.com/repos/klavins/ecep520", [this](json& response) {
got_response = true;
});
}
void update() {}
void stop() {
ASSERT_EQ(true,got_response);
}
bool got_response;
};

Definition at line 34 of file client.h.

Constructor & Destructor Documentation

◆ Client()

elma::Client::Client ( )
inline

Construct a new client. Only the Manager would normall do this, although a client can work as a standalone object.

Definition at line 40 of file client.h.

Member Function Documentation

◆ get()

Client & elma::Client::get ( std::string  url,
std::function< void(json &)>  handler 
)

Send an HTTP GET request to a specific URL and register a handler to deal with the response. This method assumes the server will respond with a JSON string. This method is asynchronous and returns immediately.

Parameters
urlThe url, preceded by http:// or https://
handlerThe handler, whose argument will be the json received from the request
Returns
A reference to the client, for chaining

Definition at line 33 of file client.cc.

◆ num_responses()

int elma::Client::num_responses ( ) const
inline
Returns
The number of unprocessed responses

Definition at line 60 of file client.h.

◆ process_responses()

Client & elma::Client::process_responses ( )

Process all responses received so far

Returns
A reference to the client, for chaining

Definition at line 39 of file client.cc.

◆ url_parts()

std::pair< std::string, std::string > elma::Client::url_parts ( std::string  url)

Parse a URL into its address and path

Parameters
urlThe url
Returns
A std::pair containing the parts

Definition at line 10 of file client.cc.


The documentation for this class was generated from the following files: