~aleteoryx/muditaos

ref: afe316f82510f10aa88ecaba4c3173291613dccb muditaos/module-services/service-desktop/endpoints/BaseHelper.hpp -rw-r--r-- 2.5 KiB
afe316f8 — Marcin Smoczyński Merge branch 'stable' - release v0.74.1 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
// 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 <parser/HttpEnums.hpp>
#include <endpoints/ResponseContext.hpp>

namespace sys
{
    class Service;
} // namespace sys

namespace parserFSM
{
    class Context;
    enum class sent
    {
        /// response was sent from Helper class
        /// we should avoid that
        yes,
        /// response wasn't sent from Helper class
        /// we should send it from endpoint based on processing result
        no,
        /// works on global context
        /// mechanism:
        /// - Send message which is delayed
        /// - send the actual response form ServiceDesktop on `sdesktop::developerMode::DeveloperModeRequest`
        delayed,
    };

    /// base helper class to avoid copies
    class BaseHelper
    {
      public:
        using ProcessResult = std::pair<sent, std::optional<endpoint::ResponseContext>>;

      protected:
        sys::Service *owner = nullptr;
        /// by default - result = not sent
        [[nodiscard]] virtual auto processPut(Context &) -> ProcessResult;
        /// by default - result = not sent
        [[nodiscard]] virtual auto processGet(Context &) -> ProcessResult;
        /// by default - result = not sent
        [[nodiscard]] virtual auto processPost(Context &) -> ProcessResult;
        /// by default - result = not sent
        [[nodiscard]] virtual auto processDelete(Context &) -> ProcessResult;
        /// pre process action foo - in case we want to do something before processing request
        virtual void preProcess(http::Method method, Context &context){};
        /// post processing action - in case we want to do something after processing request
        virtual void postProcess(http::Method method, Context &context){};

      public:
        explicit BaseHelper(sys::Service *p) : owner(p)
        {}

        /// generall processing function
        ///
        /// we should define processing functions, not copy switch cases
        /// as we are super ambiguous how we should really handle responses
        /// here we can either:
        /// return true - to mark that we responded on this request
        /// return fale - and optionally respond that we didn't handle the request
        /// pre and post processing is available on pre/post process method override
        [[nodiscard]] auto process(http::Method method, Context &context) -> ProcessResult;
    };
}; // namespace parserFSM