CCF
Loading...
Searching...
No Matches
types.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
5#include "ccf/http_consts.h"
7#include "status.h"
8
9#include <google/protobuf/empty.pb.h>
10#include <optional>
11#include <string>
12#include <variant>
13
14namespace ccf::grpc
15{
16 template <typename T>
18 {
20 ccf::protobuf::Status status;
21
22 SuccessResponse(const T& body_, ccf::protobuf::Status status_) :
23 body(body_),
24 status(status_)
25 {}
26 };
28 {
29 ccf::protobuf::Status status;
30 ErrorResponse(ccf::protobuf::Status status_) : status(status_) {}
31 };
32
33 template <typename T>
34 using GrpcAdapterResponse = std::variant<ErrorResponse, SuccessResponse<T>>;
35
36 // Used for server streaming endpoints. Successful response bodies are
37 // streamed, not returned by the handler.
39 {};
40
42 std::variant<ErrorResponse, PendingResponse>;
43
44 using EmptyResponse = google::protobuf::Empty;
47
48 template <typename T>
49 static GrpcAdapterResponse<T> make_success(const T& t)
50 {
51 return SuccessResponse(t, make_grpc_status_ok());
52 }
53
55 {
56 return SuccessResponse(EmptyResponse{}, make_grpc_status_ok());
57 }
58
59 static PendingResponse make_pending()
60 {
61 return PendingResponse{};
62 }
63
64 static ErrorResponse make_error(grpc_status code, const std::string& msg)
65 {
66 return ErrorResponse(make_grpc_status(code, msg));
67 }
68
69 template <typename T>
70 static GrpcAdapterResponse<T> make_error(
71 grpc_status code, const std::string& msg)
72 {
73 return ErrorResponse(make_grpc_status(code, msg));
74 }
75}
grpc_status
Definition grpc_status.h:30
Definition grpc_status.h:98
google::protobuf::Empty EmptyResponse
Definition types.h:44
std::variant< ErrorResponse, SuccessResponse< T > > GrpcAdapterResponse
Definition types.h:34
std::variant< ErrorResponse, PendingResponse > GrpcAdapterStreamingResponse
Definition types.h:42
GrpcAdapterResponse< EmptyResponse > GrpcAdapterEmptyResponse
Definition types.h:46
SuccessResponse< EmptyResponse > EmptySuccessResponse
Definition types.h:45
jsonhandler::JsonAdapterResponse make_success()
Definition json_handler.cpp:102
jsonhandler::JsonAdapterResponse make_error(http_status status, const std::string &code, const std::string &msg)
Definition json_handler.cpp:118
Definition types.h:28
ccf::protobuf::Status status
Definition types.h:29
ErrorResponse(ccf::protobuf::Status status_)
Definition types.h:30
Definition types.h:39
Definition types.h:18
SuccessResponse(const T &body_, ccf::protobuf::Status status_)
Definition types.h:22
ccf::protobuf::Status status
Definition types.h:20
T body
Definition types.h:19