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 const std::shared_ptr<::tenduke::log::Logger> &logger
39 ) : http(httpClient)
40 , httpRequestAuthenticator(httpRequestAuthenticator)
41 , throwException(throwException)
42 , log(logger)
43 {}
44
45public:
50 T execute() override
51 {
52 // Build the HTTP-request
53 auto request = http->request();
54 auto urlBuilder = request.url();
55 buildRequestUrl(urlBuilder);
56 std::string url = urlBuilder.buildString();
57
58 if (this->log->isDebugEnabled()) {
59 this->log->debug("AbstractAuthzApiRequest::execute() Executing HTTP request with URL " + url + " ...");
60 }
61
62 // Build and execute the request:
63 auto httpCall = request.get()
64 .authenticateWith(httpRequestAuthenticator)
65 .url(url)
66 .call();
67 auto httpResponse = httpCall->execute();
68
69 if (! httpResponse->isSuccessful()) {
70 this->log->warning("AbstractAuthzApiRequest::execute() ...HTTP request failed with status code " + std::to_string(httpResponse->getStatusCode()));
71 throwException->basedOn(*httpResponse);
72 }
73
74 this->log->debug("AbstractAuthzApiRequest::execute() ...HTTP request successfully executed");
75
76 return parseResponsePayload(httpResponse->getPayloadAsString());
77 }
78
79protected:
85 virtual T parseResponsePayload(const std::string &payload) const = 0;
86
91 virtual void buildRequestUrl(::tenduke::net::URLBuilder &url) const = 0;
92
93private:
94 const std::shared_ptr<::tenduke::log::Logger> log;
95 const std::shared_ptr<const ::tenduke::http::HTTPClient> http;
96 const std::shared_ptr<const ::tenduke::http::HTTPRequestAuthenticator> httpRequestAuthenticator;
97 const std::shared_ptr<const ::tenduke::http::HTTPResponseToException> throwException;
98};
99
100
101}}}}
102
103#endif // TENDUKE_EE_LICENSING_AUTZHAPI_ABSTRACTAUTHZAPIREQUEST_H
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.
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, const std::shared_ptr<::tenduke::log::Logger > &logger)
Constructs new instance.
Definition AbstractAuthzApiRequest.h:34
T execute() override
Executes the request.
Definition AbstractAuthzApiRequest.h:50
Licensing-related request.
Definition LicensingRequest.h:13
Very simple URL-builder.
Definition URLBuilder.h:19
Low-level client for licensing operations using 10Duke authz-API.
Definition AuthzLicensingClient.h:13
Classes, functions and globals related to 10Duke Enterprise licensing.
Definition AuthzLicensingClient.h:13
Classes, functions and globals of 10Duke Enterprise C++ Client.
Definition APIRequest.h:4
Root for classes, functions and globals of 10Duke C++ Client.
Definition APIRequest.h:4