AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
client.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++23 standard
3 Copyright (C) 2012 Vincent Lachenal
4
5 This file is part of ANCH Framework.
6
7 ANCH Framework is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 ANCH Framework is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with ANCH Framework. If not, see <https://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include "anch-rest-client-export.hpp"
23
24#include <concepts>
25#include <cstdint>
26#include <istream>
27#include <memory>
28#include <optional>
29#include <ostream>
30#include <string>
31#include <string_view>
32#include <type_traits>
33#include <vector>
34
35#include "codec/codec.hpp"
36#include "codec/codecs.hpp"
37
38#include "rest/bodyStream.hpp"
39#include "rest/filter.hpp"
40#include "rest/headers.hpp"
41#include "rest/httpError.hpp"
42#include "rest/method.hpp"
43#include "rest/paramConverter.hpp"
44#include "rest/request.hpp"
45#include "rest/response.hpp"
46#include "rest/transport.hpp"
47
48namespace anch::rest {
49
50 class Client;
51
59 struct ANCH_REST_CLIENT_EXPORT RawBody {
61 std::unique_ptr<std::istream> input;
62
64 std::string contentType = std::string();
65
67 std::optional<std::uint64_t> size = std::nullopt;
68 };
69
83 template<typename... Codecs>
84 class Call {
85
87 template<typename...>
88 friend class Call;
89
90 // Attributes +
91 private:
93 anch::rest::Client* _client;
94
96 anch::rest::Request _request;
97 // Attributes -
98
99 // Constructors +
100 public:
108 // Constructors -
109
110 // Methods +
111 public:
120 Call& header(std::string_view name, std::string_view value) &;
121
130 Call&& header(std::string_view name, std::string_view value) &&;
131
140 template<anch::rest::ParameterValue T>
141 Call& query(std::string_view name, const T& value) &;
142
151 template<anch::rest::ParameterValue T>
152 Call&& query(std::string_view name, const T& value) &&;
153
161 template<typename C>
162 Call<Codecs..., C> accept() &&;
163
173 template<typename C, typename T>
174 Call& body(const T& value) &;
175
185 template<typename C, typename T>
186 Call&& body(const T& value) &&;
187
196 Call& body(std::string content, std::string_view contentType) &;
197
206 Call&& body(std::string content, std::string_view contentType) &&;
207
218 Call& body(std::unique_ptr<std::istream> input, std::string_view contentType, std::optional<std::uint64_t> size = std::nullopt) &;
219
229 Call&& body(std::unique_ptr<std::istream> input, std::string_view contentType, std::optional<std::uint64_t> size = std::nullopt) &&;
230
239
248
257
264 void execute() &&;
265
278 template<typename T>
279 T as() &&;
280
287 // Methods -
288
289 };
290
303 class ANCH_REST_CLIENT_EXPORT Client {
304
305 // Attributes +
306 private:
308 std::unique_ptr<anch::rest::Transport> _transport;
309
311 std::string _baseUrl;
312
314 anch::rest::Headers _headers;
315
317 std::vector<anch::rest::Filter> _filters;
318 // Attributes -
319
320 // Constructors +
321 public:
328 Client(std::unique_ptr<anch::rest::Transport> transport, std::string_view baseUrl);
329 // Constructors -
330
331 // Methods +
332 public:
341 Client& header(std::string_view name, std::string_view value);
342
351
363 template<anch::rest::ParameterValue... P>
364 anch::rest::Call<> request(anch::rest::Method method, std::string_view path, const P&... parameters);
365
374 template<anch::rest::ParameterValue... P>
375 anch::rest::Call<> get(std::string_view path, const P&... parameters);
376
385 template<anch::rest::ParameterValue... P>
386 anch::rest::Call<> post(std::string_view path, const P&... parameters);
387
396 template<anch::rest::ParameterValue... P>
397 anch::rest::Call<> put(std::string_view path, const P&... parameters);
398
407 template<anch::rest::ParameterValue... P>
408 anch::rest::Call<> patch(std::string_view path, const P&... parameters);
409
418 template<anch::rest::ParameterValue... P>
419 anch::rest::Call<> del(std::string_view path, const P&... parameters);
420
429 template<anch::rest::ParameterValue... P>
430 anch::rest::Call<> head(std::string_view path, const P&... parameters);
431
442
450 std::string url(const anch::rest::Request& request) const;
451
462 static std::string formatPath(std::string_view path, const std::vector<std::string>& parameters);
463
464 private:
473 anch::rest::Response next(anch::rest::Request& request, std::size_t index);
474 // Methods -
475
476 };
477
478}
479
480#include "rest/impl/client.hpp"
Definition client.hpp:84
Call && body(const T &value) &&
Call && header(std::string_view name, std::string_view value) &&
Call && body(anch::rest::RawBody raw) &&
Call & header(std::string_view name, std::string_view value) &
anch::rest::Response send() &&
Call & query(std::string_view name, const T &value) &
Call & body(anch::rest::RawBody raw) &
anch::rest::Request & getRequest() noexcept
Call & body(std::unique_ptr< std::istream > input, std::string_view contentType, std::optional< std::uint64_t > size=std::nullopt) &
Call(anch::rest::Client &client, anch::rest::Request request)
Call && query(std::string_view name, const T &value) &&
Call && body(std::unique_ptr< std::istream > input, std::string_view contentType, std::optional< std::uint64_t > size=std::nullopt) &&
Call && body(std::string content, std::string_view contentType) &&
friend class Call
Definition client.hpp:88
Call & body(std::string content, std::string_view contentType) &
void execute() &&
Call & body(const T &value) &
Call< Codecs..., C > accept() &&
Definition client.hpp:303
static std::string formatPath(std::string_view path, const std::vector< std::string > &parameters)
anch::rest::Call request(anch::rest::Method method, std::string_view path, const P &... parameters)
anch::rest::Call head(std::string_view path, const P &... parameters)
anch::rest::Call del(std::string_view path, const P &... parameters)
Client(std::unique_ptr< anch::rest::Transport > transport, std::string_view baseUrl)
Client & filter(anch::rest::Filter filter)
anch::rest::Call get(std::string_view path, const P &... parameters)
anch::rest::Call post(std::string_view path, const P &... parameters)
Client & header(std::string_view name, std::string_view value)
anch::rest::Call patch(std::string_view path, const P &... parameters)
std::string url(const anch::rest::Request &request) const
anch::rest::Response send(anch::rest::Request &request)
anch::rest::Call put(std::string_view path, const P &... parameters)
Definition headers.hpp:62
Definition request.hpp:50
Definition response.hpp:47
Definition paramConverter.hpp:223
AnCH REST library namespace.
Definition auth.hpp:29
Method
Definition method.hpp:36
std::function< anch::rest::Response(anch::rest::Request &, const anch::rest::Next &)> Filter
Definition filter.hpp:36
Definition client.hpp:59
std::optional< std::uint64_t > size
Definition client.hpp:67
std::string contentType
Definition client.hpp:64
std::unique_ptr< std::istream > input
Definition client.hpp:61