~aleteoryx/muditaos

ref: 1a165ee797c3be5e37162259c670b7cb0e51b1a5 muditaos/module-services/service-desktop/endpoints/Context.hpp -rw-r--r-- 6.9 KiB
1a165ee7 — breichel [EGD-5692] Change URC parsing mechanism 5 years ago
                                                                                
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <json/json11.hpp>
#include <module-services/service-desktop/parser/ParserUtils.hpp>

namespace parserFSM
{

    namespace json
    {
        inline constexpr auto method   = "method";
        inline constexpr auto endpoint = "endpoint";
        inline constexpr auto uuid     = "uuid";
        inline constexpr auto status   = "status";
        inline constexpr auto totalCount = "totalCount";
        inline constexpr auto body     = "body";
        inline constexpr auto offset     = "offset";
        inline constexpr auto limit      = "limit";
        inline constexpr auto nextPage   = "nextPage";
        inline constexpr auto entries    = "entries";
    } // namespace json

    struct endpointResponseContext
    {
        http::Code status = http::Code::OK;
        json11::Json body = json11::Json();
    };

    constexpr int invalidUuid = 0;

    class Context
    {
      protected:
        json11::Json body;
        EndpointType endpoint;
        uint32_t uuid;
        http::Method method;
        endpointResponseContext responseContext;

        auto validate() -> void
        {
            if (body.is_object() == false) {
                body = json11::Json();
            }
            if (static_cast<int>(endpoint) > lastEndpoint) {
                endpoint = EndpointType::invalid;
            }
            if (method > http::Method::del) {
                method = http::Method::get;
            }
        }
        auto buildResponseStr(std::size_t responseSize, std::string responsePayloadString) -> std::string
        {
            constexpr auto pos                 = 0;
            constexpr auto count               = 1;
            std::string responsePayloadSizeStr = std::to_string(responseSize);
            while (responsePayloadSizeStr.length() < message::size_length) {
                responsePayloadSizeStr.insert(pos, count, '0');
            }

            std::string responseStr = message::endpointChar + responsePayloadSizeStr + responsePayloadString;
            return responseStr;
        }

      public:
        Context(json11::Json &js)
        {
            body     = js[json::body];
            endpoint = static_cast<EndpointType>(js[json::endpoint].int_value());
            uuid     = js[json::uuid].int_value();
            if (uuid == invalidUuid) {
                try {
                    uuid = stoi(js[json::uuid].string_value());
                }
                catch (...) {
                    uuid = invalidUuid;
                }
            }
            method = static_cast<http::Method>(js[json::method].int_value());
            validate();
        }
        Context()
        {
            body     = json11::Json();
            endpoint = EndpointType::invalid;
            uuid     = invalidUuid;
            method   = http::Method::get;
        }
        virtual ~Context() noexcept = default;

        virtual auto createSimpleResponse() -> std::string
        {
            json11::Json responseJson = json11::Json::object{{json::endpoint, static_cast<int>(getEndpoint())},
                                                             {json::status, static_cast<int>(responseContext.status)},
                                                             {json::uuid, std::to_string(getUuid())},
                                                             {json::body, responseContext.body}};
            return buildResponseStr(responseJson.dump().size(), responseJson.dump());
        }
        auto setResponseStatus(http::Code status)
        {
            responseContext.status = status;
        }
        auto setEndpoint(EndpointType endpointTypeToSet)
        {
            endpoint = endpointTypeToSet;
        }
        auto setResponseBody(json11::Json respBody)
        {
            responseContext.body = respBody;
        }
        auto getBody() -> json11::Json
        {
            return body;
        }
        auto getEndpoint() -> EndpointType
        {
            return endpoint;
        }
        auto getUuid() -> uint32_t
        {
            return uuid;
        }
        auto getMethod() -> http::Method
        {
            return method;
        }
    };

    class PagedContext : public Context
    {
      private:
        // from request
        std::size_t requestedLimit, requestedOffset;
        // set by query (during helper run)
        std::size_t totalCount;
        // set it before calling handle on helper
        std::size_t pageSize;

      public:
        explicit PagedContext(json11::Json &js, size_t pageSize) : Context(js), pageSize(pageSize)
        {}
        PagedContext() = default;
        void setRequestedLimit(std::size_t limit)
        {
            requestedLimit = limit;
        }
        void setRequestedOffset(std::size_t offset)
        {
            requestedOffset = offset;
        }
        void setTotalCount(std::size_t count)
        {
            totalCount = count;
        }
        std::size_t getPageSize() const
        {
            return pageSize;
        }

        auto createSimpleResponse() -> std::string override
        {
            auto elemsCount = responseContext.body.array_items().size();
            auto newBody    = json11::Json::object{{json::entries, responseContext.body},
                                                {json::totalCount, static_cast<int>(totalCount)}};
            if (requestedLimit > elemsCount) {
                std::size_t offset = requestedOffset + elemsCount;
                if (offset < totalCount) {
                    auto lastTableIndex = std::min(totalCount, offset + requestedLimit - elemsCount);
                    std::size_t limit   = std::min(pageSize, lastTableIndex - offset);
                    auto nextPageParams = json11::Json::object{{json::offset, static_cast<int>(offset)},
                                                               {json::limit, static_cast<int>(limit)}};
                    newBody.insert({json::nextPage, nextPageParams});
                }
            }

            setResponseBody(newBody);
            return Context::createSimpleResponse();
        }
    };

    namespace endpoint_pageing
    {
        inline constexpr std::size_t contactsPageSize = 10;
    }

    class ContextFactory
    {
      public:
        static auto create(json11::Json &js) -> std::unique_ptr<parserFSM::Context>
        {
            switch (static_cast<EndpointType>(js[json::endpoint].int_value())) {
            // enable for pagination in other endpoints
            // case EndpointType::calendarEvents:
            // case EndpointType::calllog:
            case EndpointType::contacts:
                // case EndpointType::messages:
                return std::make_unique<PagedContext>(js, endpoint_pageing::contactsPageSize);
            default:
                return std::make_unique<Context>(js);
            }
        }
    };

} // namespace parserFSM