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

Are there any HTTP(s) client examples? #56

Closed
pengweichu opened this issue Dec 30, 2015 · 9 comments
Closed

Are there any HTTP(s) client examples? #56

pengweichu opened this issue Dec 30, 2015 · 9 comments
Assignees
Labels

Comments

@pengweichu
Copy link

I'm sorry but I would like to know where has the example for implement a http/https client ?

Thanks.

@ben-crowhurst
Copy link
Member

If you read the README.md you'll notice its still in development. I'm moments away from checking-in the HTTP client example. HTTPS example will follow tomorrow at some point; logic is already present.

For now explore the restbed::Http class, Regression, Integration, and Acceptance Tests.

@pengweichu
Copy link
Author

Thanks, I have been read the http client example source code, but I have a suggestion: It's possible an option allows set the request timeout.

@ben-crowhurst
Copy link
Member

We'll investigate this proposal shortly.

auto settings = make_shared< Settings >( );
settings->set_connection_limit( milliseconds( 30 ) );

Http::sync( const Request& request, const Settings& settings );

@ben-crowhurst
Copy link
Member

@pengweichu out of curiosity, we'd love to hear about what your building?

@ben-crowhurst ben-crowhurst changed the title Example for http/https client Are there any HTTP(s) client examples? Dec 30, 2015
@pengweichu
Copy link
Author

I use this for the servers communicate, for example, I have some servers, one central server provides the REST API service, another servers use the HTTPS client connect to central server and call the REST API to get/post messages with central server.

For a scenario, if a server call a REST API but the central server doesn't respond(for some reason likes query database takes long time), so the HTTPS client request will failed once the timeout, otherwise the request will blocking...

In the previous version I use the ZMQ to do that, now I want to replaced it by REST API.

Here the code for set the send and receive timeout with asio:

class timeout_op
{
public:
    timeout_op(unsigned int timeout, int _name)
        :timeout_value(timeout),
        op_name(_name) 
    {

    }

    template<class Protocol>
    int level(const Protocol& p) const { return SOL_SOCKET; }

    template<class Protocol>
    int name(const Protocol& p) const { return op_name; }

    template<class Protocol>
    const void* data(const Protocol& p) const { return &timeout_value; }

    template<class Protocol>
    size_t size(const Protocol& p) const { return sizeof(timeout_value); }

private:
    unsigned int timeout_value;
    int op_name; // SO_SNDTIMEO or SO_RCVTIMEO
};

// For Linux  does the time_out need to be /1000 ?
if (send_timeout > 0)
 {
   timeout_op send_op(send_timeout, SO_SNDTIMEO);
   socket->set_option(send_op);
 }

if (recv_timeout > 0)
{
   timeout_op recv_op(recv_timeout, SO_RCVTIMEO);
   socket->set_option(recv_op);
}

We can also use the asio::ip::tcp::socket::native to get the native socket to set the SO_SNDTIMEO and SO_RCVTIMEO if don't use above code.

@pengweichu
Copy link
Author

@ben-crowhurst it's great code, thanks!

auto settings = make_shared< Settings >( );
settings->set_connection_limit( milliseconds( 30 ) );

Http::sync( const Request& request, const Settings& settings );

@ben-crowhurst
Copy link
Member

HTTP(s) client examples are now available at HTTP and HTTPS.

More examples will follow.

@pengweichu
Copy link
Author

Thanks very much, happy new year.

@ben-crowhurst
Copy link
Member

You're welcome, Happy new year :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants