Repository navigation
Expand file tree
/
Copy pathsessions.cpp
More file actions
67 lines (51 loc) · 1.68 KB
/
Copy pathsessions.cpp
File metadata and controls
67 lines (51 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) 2019 Andrey Upadyshev <oliora@gmail.com>
//
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include "ppconsul/sessions.h"
#include "s11n.h"
namespace ppconsul { namespace sessions {
struct CreateResult
{
std::string id;
};
void load(const s11n::Json& src, CreateResult& dst)
{
using s11n::load;
load(src, dst.id, "ID");
}
std::string encodeBehavior(InvalidationBehavior behavior)
{
switch (behavior) {
case InvalidationBehavior::Release:
return "release";
case InvalidationBehavior::Delete:
return "delete";
}
return {};
}
namespace impl {
using s11n::to_json;
std::string createBodyJson(const std::string &name, const std::string &node, std::chrono::seconds lockDelay,
InvalidationBehavior behavior, std::chrono::seconds ttl)
{
s11n::Json::object obj{
{"Behavior", encodeBehavior(behavior)},
};
if (lockDelay.count() >= 0)
obj["LockDelay"] = to_json(lockDelay);
if (!name.empty())
obj["Name"] = name;
if (!node.empty())
obj["Node"] = node;
if (ttl.count() >= 0)
obj["TTL"] = to_json(ttl);
return s11n::Json(std::move(obj)).dump();
}
std::string parseCreateResponse(const std::string &resp)
{
return s11n::parseJson<CreateResult>(resp).id;
}
}
}}