PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
rest_types.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
18#pragma once
19
20#include <cstdint>
21#include <string>
22#include <string_view>
23
24namespace kcenon::pacs::web {
25
30enum class http_status : std::uint16_t {
31 // Success
32 ok = 200,
33 created = 201,
34 accepted = 202,
35 no_content = 204,
36
37 // Client errors
38 bad_request = 400,
39 unauthorized = 401,
40 forbidden = 403,
41 not_found = 404,
43 conflict = 409,
45
46 // Server errors
48 not_implemented = 501,
50};
51
56struct api_error {
57 std::string code;
58 std::string message;
59 std::string details;
60};
61
67[[nodiscard]] inline std::string to_json(const api_error &error) {
68 std::string json = R"({"error":{"code":")" + error.code + R"(","message":")" +
69 error.message + R"("})";
70 return json;
71}
72
79[[nodiscard]] inline std::string make_error_json(std::string_view code,
80 std::string_view message) {
81 return std::string(R"({"error":{"code":")") + std::string(code) +
82 R"(","message":")" + std::string(message) + R"("}})";
83}
84
90[[nodiscard]] inline std::string
91make_success_json(std::string_view message = "OK") {
92 return std::string(R"({"status":"success","message":")") +
93 std::string(message) + R"("})";
94}
95
101[[nodiscard]] inline std::string json_escape(std::string_view s) {
102 std::string result;
103 result.reserve(s.size() + 10);
104 for (char c : s) {
105 switch (c) {
106 case '"':
107 result += "\\\"";
108 break;
109 case '\\':
110 result += "\\\\";
111 break;
112 case '\b':
113 result += "\\b";
114 break;
115 case '\f':
116 result += "\\f";
117 break;
118 case '\n':
119 result += "\\n";
120 break;
121 case '\r':
122 result += "\\r";
123 break;
124 case '\t':
125 result += "\\t";
126 break;
127 default:
128 result += c;
129 break;
130 }
131 }
132 return result;
133}
134
135} // namespace kcenon::pacs::web
std::string to_json(const api_error &error)
Create JSON error response body.
Definition rest_types.h:67
std::string make_error_json(std::string_view code, std::string_view message)
Create JSON error response body with details.
Definition rest_types.h:79
std::string json_escape(std::string_view s)
Escape a string for JSON.
Definition rest_types.h:101
http_status
Common HTTP status codes.
Definition rest_types.h:30
std::string make_success_json(std::string_view message="OK")
Create success response with optional message.
Definition rest_types.h:91
std::string_view code
Standard API error structure.
Definition rest_types.h:56