10Duke Enterprise C++ Client
Loading...
Searching...
No Matches
AbstractAuthzApiRequest.h
1#ifndef TENDUKE_EE_LICENSING_AUTZHAPI_ABSTRACTAUTHZAPIREQUEST_H
2#define TENDUKE_EE_LICENSING_AUTZHAPI_ABSTRACTAUTHZAPIREQUEST_H
3
4#include "../LicensingRequest.h"
5#include "http/HTTPClient.h"
6#include "http/HTTPRequestAuthenticator.h"
7#include "http/HTTPResponseToException.h"
8#include "log/log.h"
9
10#include <memory>
11#include <string>
12
13
14namespace tenduke { namespace ee { namespace licensing { namespace authzapi {
15
16
22template<class T>
24{
25public:
26 virtual ~AbstractAuthzApiRequest() = default;
27
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 ) : http(httpClient)
39 , httpRequestAuthenticator(httpRequestAuthenticator)
40 , throwException(throwException)
41 {}
42
43public:
48 virtual T execute() override
49 {
50 // Build the HTTP-request
51 auto request = http->request();
52 auto urlBuilder = request.url();
53 buildRequestUrl(urlBuilder);
54 std::string url = urlBuilder.buildString();
55
56 log::trace("authz-api request: " + url);
57
58 // Build and execute the request:
59 auto httpCall = request.get()
60 .authenticateWith(httpRequestAuthenticator)
61 .url(url)
62 .call();
63 auto httpResponse = httpCall->execute();
64
65 if (! httpResponse->isSuccessful()) {
66 throwException->basedOn(*httpResponse);
67 }
68
69 return parseResponsePayload(httpResponse->getPayloadAsString());
70 }
71
72protected:
78 virtual T parseResponsePayload(const std::string &payload) const = 0;
79
84 virtual void buildRequestUrl(::tenduke::net::URLBuilder &url) const = 0;
85
86private:
87 const std::shared_ptr<const ::tenduke::http::HTTPClient> http;
88 const std::shared_ptr<const ::tenduke::http::HTTPRequestAuthenticator> httpRequestAuthenticator;
89 const std::shared_ptr<const ::tenduke::http::HTTPResponseToException> throwException;
90};
91
92
93}}}}
94
95#endif // TENDUKE_EE_LICENSING_AUTZHAPI_ABSTRACTAUTHZAPIREQUEST_H
Base class for authz-API HTTP requests.
Definition AbstractAuthzApiRequest.h:24
virtual T execute() override
Executes the request.
Definition AbstractAuthzApiRequest.h:48
AbstractAuthzApiRequest(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)
Constructs new instance.
Definition AbstractAuthzApiRequest.h:34
virtual T parseResponsePayload(const std::string &payload) const =0
Parses the response payload to tenduke::ee::licensing::LicenseCheckoutResponse.
virtual void buildRequestUrl(::tenduke::net::URLBuilder &url) const =0
Builds request URL.
Licensing-related request.
Definition LicensingRequest.h:13
Very simple URL-builder.
Definition URLBuilder.h:19
void trace(const char *message)
Write message to global logger at TRACE-level.
Definition log.cpp:56
Root for classes, functions and globals of 10Duke C++ Client.
Definition APIRequest.h:4