aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/net/http/json_get_handler.h
blob: 6a49618ed37ae2ccf5603cd1219f7b51249284e6 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <vespa/vespalib/stllike/string.h>
#include <map>

namespace vespalib::net { class ConnectionAuthContext; }

namespace vespalib {

struct JsonGetHandler {
    class Response {
        int              _status_code;
        vespalib::string _status_or_payload;

        Response(int status_code, vespalib::string status_or_payload);
    public:
        Response(); // By default, 500 Internal Server Error
        ~Response();
        Response(const Response&);
        Response& operator=(const Response&);
        Response(Response&&) noexcept;
        Response& operator=(Response&&) noexcept;

        [[nodiscard]] int status_code() const noexcept { return _status_code; }
        [[nodiscard]] bool ok() const noexcept { return _status_code == 200; }
        [[nodiscard]] bool failed() const noexcept { return _status_code != 200; }
        [[nodiscard]] vespalib::stringref status_message() const noexcept {
            if (_status_code == 200) {
                return "OK";
            } else {
                return _status_or_payload;
            }
        }
        [[nodiscard]] vespalib::stringref payload() const noexcept {
            if (_status_code == 200) {
                return _status_or_payload;
            } else {
                return {};
            }
        }

        [[nodiscard]] static Response make_ok_with_json(vespalib::string json);
        [[nodiscard]] static Response make_failure(int status_code, vespalib::string status_message);
        [[nodiscard]] static Response make_not_found();
    };

    virtual Response get(const vespalib::string &host,
                         const vespalib::string &path,
                         const std::map<vespalib::string,vespalib::string> &params,
                         const net::ConnectionAuthContext &auth_ctx) const = 0;
    virtual ~JsonGetHandler() = default;
};

} // namespace vespalib