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 "http/HTTPClient.h"
5#include "http/HTTPRequestAuthenticator.h"
6#include "http/HTTPResponseToException.h"
7#include "json/JSONParser.h"
8
9#include <memory>
10
11
12namespace tenduke { namespace se { namespace licensing { namespace rest {
13
21template<class T>
23{
24public:
34 const ::tenduke::net::URL &baseUrl
35 , const std::shared_ptr<const ::tenduke::http::HTTPClient> &httpClient
36 , const std::shared_ptr<const ::tenduke::http::HTTPRequestAuthenticator> &httpRequestAuthenticator
37 , const std::shared_ptr<const ::tenduke::http::HTTPResponseToException> &throwException
38 , const std::shared_ptr<const ::tenduke::json::JSONParser> &jsonParser
39 ) : baseUrl(baseUrl)
40 , http(httpClient)
43 , parseJSON(jsonParser)
44 {}
45
46 virtual ~AbstractAPIRequestMixin() = default;
47
48protected:
53 virtual T executeRequest()
54 {
55 ::tenduke::http::HTTPRequestBuilder request = http->request();
56 ::tenduke::net::URLBuilder urlBuilder = request.createURL();
57 buildUrl(urlBuilder);
58 request.url(urlBuilder.build());
59 setHeaders(request);
60
61 if (httpRequestAuthenticator != nullptr) {
63 }
64
65 auto response = http->call(request.build())->execute();
66
67 if (! response->isSuccessful()) {
68 throwException->basedOn(*response);
69 }
70
71 return fromJSON(response->getPayloadAsString());
72 }
73
74protected:
80 virtual T fromJSON(const std::string &responseBody) const = 0;
81
86 virtual void buildUrl(::tenduke::net::URLBuilder &urlBuilder)
87 const
88 {
89 urlBuilder.baseURL(baseUrl);
90 }
91
97 virtual ::tenduke::http::HTTPRequestBuilder & setHeaders(::tenduke::http::HTTPRequestBuilder &request)
98 const
99 {
100 return request;
101 }
102
103protected:
105 const ::tenduke::net::URL baseUrl;
106
108 const std::shared_ptr<const ::tenduke::http::HTTPClient> http;
109
111 const std::shared_ptr<const ::tenduke::http::HTTPRequestAuthenticator> httpRequestAuthenticator;
112
114 const std::shared_ptr<const ::tenduke::http::HTTPResponseToException> throwException;
115
117 const std::shared_ptr<const ::tenduke::json::JSONParser> parseJSON;
118};
119
120}}}}
121
122#endif //TENDUKE_SE_LICENSING_REST_ABSTRACTAPIREQUEST_H
Builds HTTPRequest.
Definition HTTPRequestBuilder.h:23
std::unique_ptr< HTTPRequest > build()
Builds the request.
Definition HTTPRequestBuilder.cpp:26
HTTPRequestBuilder & authenticateWith(const std::shared_ptr< const tenduke::http::HTTPRequestAuthenticator > &requestAuthenticator)
Sets the request authenticator.
Definition HTTPRequestBuilder.cpp:108
tenduke::net::URLBuilder createURL() const
Starts URL-building.
Definition HTTPRequestBuilder.cpp:158
HTTPRequestBuilder & url(const std::string &url)
Sets the request URL.
Definition HTTPRequestBuilder.cpp:96
Very simple URL-builder.
Definition URLBuilder.h:23
URLBuilder & baseURL(const ::tenduke::net::URL &url)
Sets the base URL.
Definition URLBuilder.cpp:54
::tenduke::net::URL build()
Builds the URL.
Definition URLBuilder.cpp:25
Abstract base-class with generic implementation for 10Duke Scale API requests.
Definition AbstractAPIRequestMixin.h:23
const std::shared_ptr< const ::tenduke::http::HTTPResponseToException > throwException
Service to throw exceptions based on HTTP status codes.
Definition AbstractAPIRequestMixin.h:114
virtual T executeRequest()
Executes the request.
Definition AbstractAPIRequestMixin.h:53
virtual::tenduke::http::HTTPRequestBuilder & setHeaders(::tenduke::http::HTTPRequestBuilder &request) const
Sets additional headers.
Definition AbstractAPIRequestMixin.h:97
const std::shared_ptr< const ::tenduke::http::HTTPClient > http
HTTP-client to execute the request.
Definition AbstractAPIRequestMixin.h:108
virtual T fromJSON(const std::string &responseBody) const =0
Converts the response body from JSON to the response type.
AbstractAPIRequestMixin(const ::tenduke::net::URL &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 a new instance.
Definition AbstractAPIRequestMixin.h:33
const std::shared_ptr< const ::tenduke::http::HTTPRequestAuthenticator > httpRequestAuthenticator
For authenticating the request.
Definition AbstractAPIRequestMixin.h:111
const std::shared_ptr< const ::tenduke::json::JSONParser > parseJSON
For parsing the response payload.
Definition AbstractAPIRequestMixin.h:117
const ::tenduke::net::URL baseUrl
Base-URL for the request.
Definition AbstractAPIRequestMixin.h:105
virtual void buildUrl(::tenduke::net::URLBuilder &urlBuilder) const
Builds the request URL.
Definition AbstractAPIRequestMixin.h:86
Low-level services for performing licensing REST-requests.
Definition DefaultDescribeLicenseConsumerClientBindingsRequest.h:9
Licensing operations for 10Duke Scale.
Definition LicenseConsumerClientBindingStatus.h:8
Classes, functions and globals of 10Duke Scale C++ Client.
Definition AbstractClientFactory.h:16
Root for classes, functions and globals of 10Duke C++ Client.
Definition AbstractClientFactory.h:16