Skip to content

germandiagogomez/simple-requests-library

Repository files navigation

Simple requests library

Simple requests library is a simple C++14 library for http get requests. Currently it is so simple that it just includes a single call.

Usage example:

#include "gdg/srl/srl.hpp"
#include "gdg/srl/exceptions.hpp"
#include <iostream>
#include <cassert>

namespace srl = gdg::srl;
using namespace std;

int main() {
    try {
        auto response =
            srl::async_http_get(
                 /*, my_loop, */ //Optional custom loop
                 "www.boost.org",
                 "/LICENSE_1_0.txt"
                 , chrono::seconds(5)); //optional timeout

        auto r = response.get();
        assert(r.first == 200);

        for (char c : r.second)
            cout << c;
        cout << endl;
    }
    catch (srl::timeout_exception) {
        cout << "Timeout";
        return -1;
    }
}

The library lives in namespace gdg. These are the initials for my name. The library is nested into srl (simple requests library) namespace.

Currently, the library is fairly simple:

  • single call library, asynchronous via future result.
  • supports only http (no https).
  • supports timeouts.
  • http get requests, version 1.1. Supports fixed and chunked, but it will only support chunked requests if they do not use any extensions, just plain chunked data (though this could be easily changed).
  • only http 1.1.
  • no redirections. Only will allow 200 return code or will throw gdg::srl::bad_request_exception.
  • depends on boost (this is not a feature, it is a warning :p). I coded the library in a way that the boost dependency can be refactored easily in the future.

How to use

Dependencies

The easiest way to install in Mac OS is through a package manager. The library depends on boost currently, but that dependency might be removed in the future.

You will need:

  • homebrew (actually optional package manager but highly recommended)
  • meson build system (used 0.39.1)
  • an up to date C++14 compiler (tested on clang-700.1.81).

How to compile and use in Mac OS

Assuming you installed homebrew, open a terminal:

brew install git python3 boost ninja
pip3 install meson
git clone https://github.com/germandiagogomez/simple-requests-library.git
git submodule update --init --recursive
mkdir build
export BOOST_ROOT=<your-boost-root>
cd build
meson ..
ninja

This will leave a static library in your directory if everything was ok.

Run tests

You can compile the funtional-tests executable and run it. As of now, remember that some tests make use of the network and access external resources, meaning they could be not deterministic and fail.

ninja functional-tests
./functional-tests

Compile a simple example

You can run the simple example contained in examples/get_urls.cpp doing this:

ninja get_urls
./get_urls

This should display the boost license that was just retrieved through using GET in http.

Roadmap

  • support http other than 1.1.
  • make library ready to able to be used without boost dependency. through asio standalone/network ts and migrating string_view. to std::string_view (partially done).
  • replace coroutines by futures in the implementation (to get rid of boost).
  • lower the C++ version to C++11.

About

Simple C++14 requests library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published