10Duke Enterprise C++ Client
Loading...
Searching...
No Matches
AbstractLicenseTokenAuthzApiRequest.h
1#ifndef TENDUKE_EE_LICENSING_AUTZHAPI_ABSTRACTLICENSETOKENAUTHZAPIREQUEST_H
2#define TENDUKE_EE_LICENSING_AUTZHAPI_ABSTRACTLICENSETOKENAUTHZAPIREQUEST_H
3
4#include "./AbstractAuthzApiRequest.h"
5#include "../../LicenseTokenParser.h"
6
7#include "http/HTTPClient.h"
8#include "http/HTTPRequestAuthenticator.h"
9#include "http/HTTPResponseToException.h"
10
11#include <memory>
12#include <string>
13
14
15namespace tenduke { namespace ee { namespace licensing { namespace authzapi {
16
17
23template<class T>
25{
26public:
27 virtual ~AbstractLicenseTokenAuthzApiRequest() = default;
28
37 const std::shared_ptr<const ::tenduke::http::HTTPClient> &httpClient,
38 const std::shared_ptr<const ::tenduke::http::HTTPRequestAuthenticator> &httpRequestAuthenticator,
39 const std::shared_ptr<const ::tenduke::http::HTTPResponseToException> &throwException,
40 const std::shared_ptr<const tenduke::ee::licensing::impl::LicenseTokenParser> &licenseTokenParser
41 ) : ::tenduke::ee::licensing::authzapi::AbstractAuthzApiRequest<T>(httpClient, httpRequestAuthenticator, throwException)
42 , parseLicenseToken(licenseTokenParser)
43 {}
44
45protected:
53 T parseResponsePayload(const std::string &payload) const override
54 {
55 std::map<std::string, ::tenduke::ee::licensing::Lease> items;
56 std::map<std::string, ::tenduke::ee::licensing::LicensingError> failedItems;
57 int startOfToken = 0;
58
59 // If the response contains multiple licensed items, each of them are
60 // encoded separately as JWT and the JWTs are concatenated together. This parses those out.
61 std::size_t payloadLength = payload.length();
62 for (int i = 0; i < payloadLength; i++) {
63 if (payload.at(i) == '&') {
64 handleParsedResult(
65 parseLicenseToken->from(payload.substr(startOfToken, i - startOfToken)),
66 items,
67 failedItems
68 );
69
70 startOfToken = i + 1;
71 }
72 }
73 // Handle last token:
74 if (startOfToken < payloadLength) {
75 handleParsedResult(
76 parseLicenseToken->from(payload.substr(startOfToken)),
77 items,
78 failedItems
79 );
80 }
81 return {items, failedItems};
82 }
83
84private:
85 void handleParsedResult(
86 const ::tenduke::ee::licensing::impl::LeaseOrError &leaseOrError,
87 std::map<std::string, ::tenduke::ee::licensing::Lease> &items,
88 std::map<std::string, ::tenduke::ee::licensing::LicensingError> &failedItems
89 ) const
90 {
91 if (leaseOrError.isLease()) {
92 items.emplace(leaseOrError.lease.name, leaseOrError.lease);
93 }
94 else {
95 failedItems.emplace(leaseOrError.error.getItemName(), leaseOrError.error);
96 }
97 }
98
99private:
100 const std::shared_ptr<const ::tenduke::ee::licensing::impl::LicenseTokenParser> parseLicenseToken;
101};
102
103
104}}}}
105
106#endif // TENDUKE_EE_LICENSING_AUTZHAPI_ABSTRACTLICENSETOKENAUTHZAPIREQUEST_H
Base class for authz-API HTTP requests.
Definition AbstractAuthzApiRequest.h:24
Base class for licensing requests, which return tokens.
Definition AbstractLicenseTokenAuthzApiRequest.h:25
T parseResponsePayload(const std::string &payload) const override
Parses the response payload.
Definition AbstractLicenseTokenAuthzApiRequest.h:53
AbstractLicenseTokenAuthzApiRequest(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::ee::licensing::impl::LicenseTokenParser > &licenseTokenParser)
Constructs new instance.g.
Definition AbstractLicenseTokenAuthzApiRequest.h:36
Root for classes, functions and globals of 10Duke C++ Client.
Definition APIRequest.h:4