10Duke Scale C++ Client
Loading...
Searching...
No Matches
AbstractAPIRequestMixin.h
1#ifndef TENDUKE_SE_LICENSING_REST_ABSTRACTAPIREQUEST_H
2#define TENDUKE_SE_LICENSING_REST_ABSTRACTAPIREQUEST_H
3
4#include "./APIRequest.h"
5#include "../config/BackendConfiguration.h"
6#include "http/HTTPClient.h"
7#include "http/HTTPRequestAuthenticator.h"
8#include "http/HTTPResponseToException.h"
9#include "json/JSONParser.h"
10
11#include <memory>
12#include <utility>
13
14
15namespace tenduke { namespace se { namespace licensing { namespace rest {
16
24template<class T>
26{
27public:
37 std::string baseUrl
38 , const std::shared_ptr<const ::tenduke::http::HTTPClient> &httpClient
39 , const std::shared_ptr<const ::tenduke::http::HTTPRequestAuthenticator> &httpRequestAuthenticator
40 , const std::shared_ptr<const ::tenduke::http::HTTPResponseToException> &throwException
41 , const std::shared_ptr<const ::tenduke::json::JSONParser> &jsonParser
42 ) : baseUrl(std::move(baseUrl))
43 , http(httpClient)
46 , parseJSON(jsonParser)
47 {}
48
49 virtual ~AbstractAPIRequestMixin() = default;
50
51protected:
56 virtual T executeRequest()
57 {
58 ::tenduke::http::HTTPRequestBuilder request = http->request();
61 request.url(urlBuilder.buildString());
62 setHeaders(request);
63
64 if (httpRequestAuthenticator != nullptr) {
66 }
67
68 auto response = http->call(request.build())->execute();
69
70 if (! response->isSuccessful()) {
71 throwException->basedOn(*response);
72 }
73
74 return fromJSON(response->getPayloadAsString());
75 }
76
77protected:
83 virtual T fromJSON(const std::string &responseBody) const = 0;
84
90 const
91 {
92 urlBuilder.baseURL(baseUrl);
93 }
94
100 virtual ::tenduke::http::HTTPRequestBuilder & setHeaders(::tenduke::http::HTTPRequestBuilder &request)
101 const
102 {
103 return request;
104 }
105
106protected:
108 const std::string baseUrl;
109
111 const std::shared_ptr<const ::tenduke::http::HTTPClient> http;
112
114 const std::shared_ptr<const ::tenduke::http::HTTPRequestAuthenticator> httpRequestAuthenticator;
115
117 const std::shared_ptr<const ::tenduke::http::HTTPResponseToException> throwException;
118
120 const std::shared_ptr<const ::tenduke::json::JSONParser> parseJSON;
121};
122
123}}}}
124
125#endif //TENDUKE_SE_LICENSING_REST_ABSTRACTAPIREQUEST_H
Builds HTTPRequest.
Definition HTTPRequestBuilder.h:22
std::unique_ptr< HTTPRequest > build()
Builds the request.
Definition HTTPRequestBuilder.cpp:21
HTTPRequestBuilder & authenticateWith(const std::shared_ptr< const tenduke::http::HTTPRequestAuthenticator > &requestAuthenticator)
Sets the request authenticator.
Definition HTTPRequestBuilder.cpp:97
HTTPRequestBuilder & url(const std::string &url)
Sets the request URL.
Definition HTTPRequestBuilder.cpp:91
Very simple URL-builder.
Definition URLBuilder.h:19
A tenduke::se::APIRequest, which uses tenduke::oidc::OIDCSession to maintain request authorization.
Definition StatefulAPIRequest.h:27
R execute() override
Execute the request synchronously.
Definition StatefulAPIRequest.h:42
Abstract base-class with generic implementation for 10Duke Scale API requests.
Definition AbstractAPIRequestMixin.h:26
const std::shared_ptr< const ::tenduke::http::HTTPResponseToException > throwException
Service to throw exceptions based on HTTP status codes.
Definition AbstractAPIRequestMixin.h:117
virtual T executeRequest()
Executes the request.
Definition AbstractAPIRequestMixin.h:56
const std::string baseUrl
Base-URL for the request.
Definition AbstractAPIRequestMixin.h:108
const std::shared_ptr< const ::tenduke::http::HTTPClient > http
HTTP-client to execute the request.
Definition AbstractAPIRequestMixin.h:111
virtual T fromJSON(const std::string &responseBody) const =0
Converts the response body from JSON to the response type.
const std::shared_ptr< const ::tenduke::http::HTTPRequestAuthenticator > httpRequestAuthenticator
For authenticating the request.
Definition AbstractAPIRequestMixin.h:114
const std::shared_ptr< const ::tenduke::json::JSONParser > parseJSON
For parsing the response payload.
Definition AbstractAPIRequestMixin.h:120
virtual ::tenduke::http::HTTPRequestBuilder & setHeaders(::tenduke::http::HTTPRequestBuilder &request) const
Sets additional headers.
Definition AbstractAPIRequestMixin.h:100
AbstractAPIRequestMixin(std::string baseUrl, const std::shared_ptr< const ::tenduke::http::HTTPClient > &httpClient, const std::shared_ptr< const ::tenduke::http::HTTPRequestAuthenticator > &httpRequestAuthenticator, const std::shared_ptr< const ::tenduke::http::HTTPResponseToException > &throwException, const std::shared_ptr< const ::tenduke::json::JSONParser > &jsonParser)
Constructs new instance.
Definition AbstractAPIRequestMixin.h:36
virtual void buildUrl(::tenduke::net::URLBuilder &urlBuilder) const
Builds the request URL.
Definition AbstractAPIRequestMixin.h:89
Root for classes, functions and globals of 10Duke C++ Client.
Definition BackendConfiguration.h:7