src/HTTPClient.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 12 Mar 2022 20:48:25 +0100
branchv_0
changeset 5 165f6162524d
child 6 59c9ca066322
permissions -rw-r--r--
introduce HTTPClient wrapper around CURL

/**
 * Relational pipes
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#pragma once

#include <string>
#include <vector>

#include <relpipe/common/type/typedefs.h>


namespace relpipe {
namespace tr {
namespace http {

/**
 * Simple synchronous client for the HTTP and HTTPS protocol.
 * Is not thread-safe – must not be called from multiple threads simultaneously.
 */
class HTTPClient {
private:
	class HTTPClientImpl;
	HTTPClientImpl* impl;

	HTTPClient(HTTPClientImpl* impl) : impl(impl) {
	}


public:

	enum class Method {
		GET,
		HEAD,
		POST,
		PUT,
		DELETE,
		// CONNECT,
		// OPTIONS,
		// TRACE,
		PATCH,
	};

	struct Request {
		Method method = Method::GET;
		std::string url;
		std::vector<std::string> headers;
		std::string body;
	};

	struct Response {
		bool success = false;
		int responseCode = 0;
		std::vector<std::string> headers;
		std::string body;
	};

	virtual ~HTTPClient();
	HTTPClient(const HTTPClient&) = delete;
	HTTPClient& operator=(const HTTPClient&) = delete;

	static HTTPClient* open();

	const Response exchange(const Request& request);

};

}
}
}