~aleteoryx/muditaos

ref: f8c39b30d66bf275994d8c137219de2c9b965226 muditaos/module-services/service-desktop/endpoints/Context.hpp -rw-r--r-- 7.2 KiB
f8c39b30 — Pawel Olejniczak [CP-142] Update tests with 204 response code 4 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 <module-services/service-desktop/parser/ParserUtils.hpp>
#include "ResponseContext.hpp"

namespace parserFSM
{
    constexpr http::Code toCode(bool r)
    {
        return r ? http::Code::OK : http::Code::InternalServerError;
    }

    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

    constexpr int invalidUuid = 0;

    class Context
    {
      protected:
        json11::Json body;
        EndpointType endpoint;
        int uuid;
        http::Method method;
        endpoint::ResponseContext 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, const 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:
        explicit Context(json11::Json &js)
        {
            body     = js[json::body];
            endpoint = static_cast<EndpointType>(js[json::endpoint].int_value());
            uuid     = js[json::uuid].int_value();
            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(const std::string &entryTitle = json::entries) -> std::string
        {
            json11::Json responseJson = json11::Json::object{{json::endpoint, static_cast<int>(getEndpoint())},
                                                             {json::status, static_cast<int>(responseContext.status)},
                                                             {json::uuid, getUuid()},
                                                             {json::body, responseContext.body}};
            return buildResponseStr(responseJson.dump().size(), responseJson.dump());
        }

        auto setResponse(endpoint::ResponseContext r)
        {
            responseContext = r;
        }

        auto setResponseStatus(http::Code status)
        {
            responseContext.status = status;
        }
        auto setEndpoint(EndpointType endpointTypeToSet)
        {
            endpoint = endpointTypeToSet;
        }
        auto setResponseBody(json11::Json respBody)
        {
            responseContext.body = std::move(respBody);
        }
        auto getBody() -> const json11::Json &
        {
            return body;
        }
        auto getEndpoint() -> EndpointType
        {
            return endpoint;
        }
        auto getUuid() -> int
        {
            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(const std::string &entryTitle = json::entries) -> std::string override
        {
            auto elemsCount = responseContext.body.array_items().size();
            auto newBody    = json11::Json::object{{entryTitle, 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;
        inline constexpr std::size_t calendarEventsPageSize = 10;
        inline constexpr std::size_t messagesPageSize       = 4;
    } // namespace endpoint_pageing

    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:
                return std::make_unique<PagedContext>(js, endpoint_pageing::calendarEventsPageSize);
            // case EndpointType::calllog:
            case EndpointType::contacts:
                return std::make_unique<PagedContext>(js, endpoint_pageing::contactsPageSize);
            case EndpointType::messages:
                return std::make_unique<PagedContext>(js, endpoint_pageing::messagesPageSize);
            default:
                return std::make_unique<Context>(js);
            }
        }
    };

} // namespace parserFSM