src/HTTPClient.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 15 Mar 2022 23:22:00 +0100
branchv_0
changeset 9 9fdbfbe24161
parent 6 59c9ca066322
child 11 6b913e82f52a
permissions -rw-r--r--
throw and report exception on error (e.g. 'Couldn't connect to server')
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <string>
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <vector>
9
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    21
#include <stdexcept>
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/common/type/typedefs.h>
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
namespace relpipe {
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
namespace tr {
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace http {
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
/**
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
 * Simple synchronous client for the HTTP and HTTPS protocol.
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
 * Is not thread-safe – must not be called from multiple threads simultaneously.
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
 */
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
class HTTPClient {
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
private:
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
	class HTTPClientImpl;
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	HTTPClientImpl* impl;
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	HTTPClient(HTTPClientImpl* impl) : impl(impl) {
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	}
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
public:
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	enum class Method {
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
		GET,
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
		HEAD,
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
		POST,
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
		PUT,
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
		DELETE,
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
		// CONNECT,
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
		// OPTIONS,
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
		// TRACE,
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
		PATCH,
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	};
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	struct Request {
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
		Method method = Method::GET;
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		std::string url;
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		std::vector<std::string> headers;
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		std::string body;
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	};
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
	struct Response {
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
		int responseCode = 0;
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		std::vector<std::string> headers;
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
		std::string body;
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	};
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
9
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    70
	class Exception : public std::runtime_error {
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    71
	public:
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    72
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    73
		Exception(const std::string& message) : runtime_error(message) {
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    74
		}
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    75
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    76
		Exception(const char* message) : runtime_error(message) {
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    77
		}
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    78
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    79
	};
9fdbfbe24161 throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    80
5
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
	virtual ~HTTPClient();
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
	HTTPClient(const HTTPClient&) = delete;
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
	HTTPClient& operator=(const HTTPClient&) = delete;
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
	static HTTPClient* open();
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
	const Response exchange(const Request& request);
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
};
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
}
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
}
165f6162524d introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
}