2020-08-17 17:09:03 +02:00
|
|
|
/* API response.
|
|
|
|
*
|
2022-03-15 09:18:01 -04:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
2022-03-15 09:28:57 -04:00
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
2022-07-04 18:20:03 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2020-08-17 17:09:03 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-10-20 22:17:55 +02:00
|
|
|
#include <map>
|
|
|
|
|
2020-08-17 17:09:03 +02:00
|
|
|
#include <jansson.h>
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/api.hpp>
|
2020-10-20 22:17:55 +02:00
|
|
|
#include <villas/buffer.hpp>
|
2020-08-17 17:09:03 +02:00
|
|
|
#include <villas/exceptions.hpp>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/log.hpp>
|
2020-08-17 17:09:03 +02:00
|
|
|
#include <villas/plugin.hpp>
|
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
|
|
|
namespace api {
|
|
|
|
|
|
|
|
// Forward declarations
|
|
|
|
class Session;
|
|
|
|
class Request;
|
|
|
|
|
|
|
|
class Response {
|
|
|
|
|
|
|
|
public:
|
2023-09-07 11:46:39 +02:00
|
|
|
friend Session;
|
2020-08-17 17:09:03 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
Response(Session *s, int c = HTTP_STATUS_OK,
|
|
|
|
const std::string &ct = "text/html; charset=UTF-8",
|
|
|
|
const Buffer &b = Buffer());
|
2020-08-17 17:09:03 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual ~Response() {}
|
2020-10-20 22:17:55 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual void encodeBody() {}
|
2020-08-17 17:09:03 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int writeBody(struct lws *wsi);
|
2020-08-17 17:09:03 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int writeHeaders(struct lws *wsi);
|
2020-10-20 22:17:55 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
void setHeader(const std::string &key, const std::string &value) {
|
|
|
|
headers[key] = value;
|
|
|
|
}
|
2020-10-20 22:17:55 +02:00
|
|
|
|
|
|
|
protected:
|
2023-09-07 11:46:39 +02:00
|
|
|
Session *session;
|
|
|
|
Logger logger;
|
|
|
|
Buffer buffer;
|
2020-10-20 22:17:55 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int code;
|
|
|
|
std::string contentType;
|
|
|
|
std::map<std::string, std::string> headers;
|
2020-08-17 17:09:03 +02:00
|
|
|
};
|
|
|
|
|
2020-10-20 22:17:55 +02:00
|
|
|
class JsonResponse : public Response {
|
2020-08-17 17:09:03 +02:00
|
|
|
|
|
|
|
protected:
|
2023-09-07 11:46:39 +02:00
|
|
|
json_t *response;
|
2020-10-20 22:17:55 +02:00
|
|
|
|
|
|
|
public:
|
2023-09-07 11:46:39 +02:00
|
|
|
JsonResponse(Session *s, int c, json_t *r)
|
|
|
|
: Response(s, c, "application/json"), response(r) {}
|
2020-10-20 22:17:55 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual ~JsonResponse();
|
2020-10-20 22:17:55 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual void encodeBody();
|
2020-10-20 22:17:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class ErrorResponse : public JsonResponse {
|
2020-08-17 17:09:03 +02:00
|
|
|
|
|
|
|
public:
|
2023-09-07 11:46:39 +02:00
|
|
|
ErrorResponse(Session *s, const RuntimeError &e)
|
|
|
|
: JsonResponse(s, HTTP_STATUS_INTERNAL_SERVER_ERROR,
|
|
|
|
json_pack("{ s: s }", "error", e.what())) {}
|
|
|
|
|
|
|
|
ErrorResponse(Session *s, const Error &e)
|
|
|
|
: JsonResponse(s, e.code, json_pack("{ s: s }", "error", e.what())) {
|
|
|
|
if (e.json)
|
|
|
|
json_object_update(response, e.json);
|
|
|
|
}
|
2020-08-17 17:09:03 +02:00
|
|
|
};
|
|
|
|
|
2023-08-28 09:34:02 +02:00
|
|
|
} // namespace api
|
|
|
|
} // namespace node
|
|
|
|
} // namespace villas
|