10Duke Scale C++ Client
Loading...
Searching...
No Matches
StatefulAPIRequest.h
1#ifndef TENDUKE_SE_STATEFULAPIREQUEST_H
2#define TENDUKE_SE_STATEFULAPIREQUEST_H
3
4#include "./APIRequest.h"
5#include "http/Unauthorized.h"
6#include "log/log.h"
7#include "oidc/session/OIDCSession.h"
8
9#include <memory>
10
11namespace tenduke { namespace se {
12
25template<class R>
27{
28public:
35 const std::shared_ptr<::tenduke::se::APIRequest<R>> &request,
36 const std::shared_ptr<::tenduke::oidc::OIDCSession> &oidc
37 ) : request(request)
38 , oidc(oidc)
39 {}
40
41public:
42 R execute() override
43 {
44 // Sanity check: We need OIDCSession service.
45 // The client can be bootstrapped without OIDCSession when using only license keys
46 if (oidc == nullptr) {
47 return request->execute();
48 }
49
50 // Check that the local OIDC session is still valid.
51 // If the session has expired, this refreshes or re-logins the user
52 ::tenduke::log::debug("StatefulAPIRequest::execute() ensuring valid session ...");
53 oidc->ensureValidSession();
54
55 ::tenduke::log::debug("StatefulAPIRequest::execute() ...executing the request ...");
56
57 try {
58 R response = request->execute();
59 ::tenduke::log::debug("StatefulAPIRequest::execute() ... success");
60
61 return response;
62 }
63 // The call might fail with HTTP 401 Unauthorized (for example if the admin user has invalidated the
64 // OIDC session in the backend), even if we earlier thought the session was valid.
65 // In that case, we re-establish the session (either with refresh or with re-login) and then
66 // re-execute the request. If the request still fails with HTTP 401, then here is nothing we can do
67 // automatically and the exception is thrown.
69 ::tenduke::log::warning("StatefulAPIRequest::execute() ... got HTTP 401, re-establishing OIDC-session ...");
70 oidc->reEstablish();
71
72 ::tenduke::log::info("StatefulAPIRequest::execute() ... OIDC session re-established. Re-executing original request ...");
73 R response = request->execute();
74
75 ::tenduke::log::info("StatefulAPIRequest::execute() ... success.");
76
77 return response;
78 }
79 }
80
81private:
82 const std::shared_ptr<::tenduke::se::APIRequest<R>> request;
83 const std::shared_ptr<::tenduke::oidc::OIDCSession> oidc;
84};
85
86}}
87#endif //TENDUKE_SE_STATEFULAPIREQUEST_H
Interface for 10Duke Scale API-requests.
Definition APIRequest.h:12
A tenduke::se::APIRequest, which uses tenduke::oidc::OIDCSession to maintain request authorization.
Definition StatefulAPIRequest.h:27
StatefulAPIRequest(const std::shared_ptr<::tenduke::se::APIRequest< R > > &request, const std::shared_ptr<::tenduke::oidc::OIDCSession > &oidc)
Constructs new instance.
Definition StatefulAPIRequest.h:34
R execute() override
Execute the request synchronously.
Definition StatefulAPIRequest.h:42
Thrown when 10Duke Scale backend responds with HTTP 401.
Definition Unauthorized.h:12
void debug(const char *message)
Write message to global logger at DEBUG-level.
Definition log.cpp:15
void warning(const char *message)
Write message to global logger at WARNING-level.
Definition log.cpp:55
void info(const char *message)
Write message to global logger at INFO-level.
Definition log.cpp:35
Root for classes, functions and globals of 10Duke C++ Client.
Definition BackendConfiguration.h:7