aboutsummaryrefslogtreecommitdiff
path: root/src/zenhttp/include
diff options
context:
space:
mode:
Diffstat (limited to 'src/zenhttp/include')
-rw-r--r--src/zenhttp/include/zenhttp/httpclient.h26
-rw-r--r--src/zenhttp/include/zenhttp/httpclientauth.h4
2 files changed, 25 insertions, 5 deletions
diff --git a/src/zenhttp/include/zenhttp/httpclient.h b/src/zenhttp/include/zenhttp/httpclient.h
index e878c900f..9531b9366 100644
--- a/src/zenhttp/include/zenhttp/httpclient.h
+++ b/src/zenhttp/include/zenhttp/httpclient.h
@@ -68,14 +68,30 @@ struct HttpClientAccessToken
static constexpr int64_t ExpireMarginInSeconds = 60 * 5;
- std::string Value;
- TimePoint ExpireTime;
+ HttpClientAccessToken() {}
- bool IsValid() const
+ HttpClientAccessToken(std::string_view InValue, const TimePoint& InExpireTime) : Value(InValue), ExpireTime(InExpireTime) {}
+
+ std::optional<std::string> GetValue() const
{
- return Value.empty() == false &&
- ExpireMarginInSeconds < std::chrono::duration_cast<std::chrono::seconds>(ExpireTime - Clock::now()).count();
+ if (IsValid())
+ {
+ return Value;
+ }
+ return {};
}
+
+ bool NeedsRefresh() const
+ {
+ return Value.empty() == true || (Clock::now() + std::chrono::seconds(ExpireMarginInSeconds)) >= ExpireTime;
+ }
+
+ bool HasExpired() const { return Clock::now() >= ExpireTime; }
+ bool IsValid() const { return !Value.empty() && !HasExpired(); }
+
+private:
+ std::string Value;
+ TimePoint ExpireTime;
};
struct HttpClientSettings
diff --git a/src/zenhttp/include/zenhttp/httpclientauth.h b/src/zenhttp/include/zenhttp/httpclientauth.h
index 26f31ed2a..f1bccdca6 100644
--- a/src/zenhttp/include/zenhttp/httpclientauth.h
+++ b/src/zenhttp/include/zenhttp/httpclientauth.h
@@ -10,6 +10,10 @@ namespace zen {
class AuthMgr;
namespace httpclientauth {
+
+ // The std::function<HttpClientAccessToken()> instances returned from these functions are not guarateed to
+ // be thread safe so caller must make sure they are not called from multiple threads in parallell
+
std::function<HttpClientAccessToken()> CreateFromStaticToken(HttpClientAccessToken Token);
std::function<HttpClientAccessToken()> CreateFromStaticToken(std::string_view Token);