File: repe-jsonrpc-conversion.cpp

package info (click to toggle)
glaze 6.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,948 kB
  • sloc: cpp: 121,839; sh: 99; ansic: 26; makefile: 13
file content (173 lines) | stat: -rw-r--r-- 6,519 bytes parent folder | download
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Example demonstrating REPE to JSON-RPC conversion
// Compile: g++ -std=c++20 -I../include repe-jsonrpc-conversion.cpp -o repe-jsonrpc-conversion

#include <iostream>

#include "glaze/rpc/repe/repe_to_jsonrpc.hpp"

namespace repe = glz::repe;

void print_separator(const std::string& title) { std::cout << "\n========== " << title << " ==========\n"; }

int main()
{
   print_separator("REPE Request to JSON-RPC Request");
   {
      // Create a REPE request
      repe::message repe_request{};
      repe_request.query = "/calculate";
      repe_request.body = R"({"x":10,"y":20,"operation":"add"})";
      repe_request.header.id = 12345;
      repe_request.header.body_format = repe::body_format::JSON;
      repe_request.header.notify = false;

      // Convert to JSON-RPC
      std::string jsonrpc_request = repe::to_jsonrpc_request(repe_request);

      std::cout << "REPE Request:\n";
      std::cout << "  Query: " << repe_request.query << "\n";
      std::cout << "  Body: " << repe_request.body << "\n";
      std::cout << "  ID: " << repe_request.header.id << "\n";
      std::cout << "\nJSON-RPC Request:\n";
      std::cout << "  " << jsonrpc_request << "\n";
   }

   print_separator("REPE Response to JSON-RPC Response (Success)");
   {
      // Create a REPE success response
      repe::message repe_response{};
      repe_response.body = R"({"result":30})";
      repe_response.header.id = 12345;
      repe_response.header.body_format = repe::body_format::JSON;
      repe_response.header.ec = glz::error_code::none;

      // Convert to JSON-RPC
      std::string jsonrpc_response = repe::to_jsonrpc_response(repe_response);

      std::cout << "REPE Response:\n";
      std::cout << "  Body: " << repe_response.body << "\n";
      std::cout << "  ID: " << repe_response.header.id << "\n";
      std::cout << "  Error Code: " << static_cast<int>(repe_response.header.ec) << "\n";
      std::cout << "\nJSON-RPC Response:\n";
      std::cout << "  " << jsonrpc_response << "\n";
   }

   print_separator("REPE Response to JSON-RPC Response (Error)");
   {
      // Create a REPE error response
      repe::message repe_error{};
      repe_error.body = "Invalid operation specified";
      repe_error.header.id = 12345;
      repe_error.header.body_format = repe::body_format::UTF8;
      repe_error.header.ec = glz::error_code::parse_error;

      // Convert to JSON-RPC
      std::string jsonrpc_error = repe::to_jsonrpc_response(repe_error);

      std::cout << "REPE Error Response:\n";
      std::cout << "  Body: " << repe_error.body << "\n";
      std::cout << "  ID: " << repe_error.header.id << "\n";
      std::cout << "  Error Code: " << static_cast<int>(repe_error.header.ec) << "\n";
      std::cout << "\nJSON-RPC Error Response:\n";
      std::cout << "  " << jsonrpc_error << "\n";
   }

   print_separator("JSON-RPC Request to REPE Request");
   {
      std::string jsonrpc_request = R"({"jsonrpc":"2.0","method":"multiply","params":{"a":5,"b":7},"id":99})";

      // Convert to REPE
      auto repe_request = repe::from_jsonrpc_request(jsonrpc_request);

      std::cout << "JSON-RPC Request:\n";
      std::cout << "  " << jsonrpc_request << "\n";

      if (repe_request.has_value()) {
         std::cout << "\nREPE Request:\n";
         std::cout << "  Query: " << repe_request->query << "\n";
         std::cout << "  Body: " << repe_request->body << "\n";
         std::cout << "  ID: " << repe_request->header.id << "\n";
         std::cout << "  Notify: " << (repe_request->header.notify ? "true" : "false") << "\n";
      }
      else {
         std::cout << "\nConversion failed: " << repe_request.error() << "\n";
      }
   }

   print_separator("JSON-RPC Response to REPE Response");
   {
      std::string jsonrpc_response = R"({"jsonrpc":"2.0","result":{"value":35},"id":99})";

      // Convert to REPE
      auto repe_response = repe::from_jsonrpc_response(jsonrpc_response);

      std::cout << "JSON-RPC Response:\n";
      std::cout << "  " << jsonrpc_response << "\n";

      if (repe_response.has_value()) {
         std::cout << "\nREPE Response:\n";
         std::cout << "  Body: " << repe_response->body << "\n";
         std::cout << "  ID: " << repe_response->header.id << "\n";
         std::cout << "  Error Code: " << static_cast<int>(repe_response->header.ec) << "\n";
      }
      else {
         std::cout << "\nConversion failed: " << repe_response.error() << "\n";
      }
   }

   print_separator("Notification Handling");
   {
      // REPE notification (notify=true)
      repe::message notification{};
      notification.query = "/log";
      notification.body = R"({"level":"info","message":"System started"})";
      notification.header.notify = true;
      notification.header.body_format = repe::body_format::JSON; // Must set format!

      std::string jsonrpc_notification = repe::to_jsonrpc_request(notification);

      std::cout << "REPE Notification (notify=true):\n";
      std::cout << "  Query: " << notification.query << "\n";
      std::cout << "  Body: " << notification.body << "\n";
      std::cout << "\nJSON-RPC Notification (id=null):\n";
      std::cout << "  " << jsonrpc_notification << "\n";
   }

   print_separator("Roundtrip Conversion");
   {
      // Create original REPE request
      repe::message original{};
      original.query = "/divide";
      original.body = R"({"dividend":100,"divisor":5})";
      original.header.id = 777;
      original.header.body_format = repe::body_format::JSON;
      original.header.notify = false;

      // Convert to JSON-RPC
      std::string jsonrpc = repe::to_jsonrpc_request(original);

      // Convert back to REPE
      auto roundtrip = repe::from_jsonrpc_request(jsonrpc);

      std::cout << "Original REPE Request:\n";
      std::cout << "  Query: " << original.query << "\n";
      std::cout << "  Body: " << original.body << "\n";
      std::cout << "  ID: " << original.header.id << "\n";

      std::cout << "\nAfter Roundtrip:\n";
      if (roundtrip.has_value()) {
         std::cout << "  Query: " << roundtrip->query << "\n";
         std::cout << "  Body: " << roundtrip->body << "\n";
         std::cout << "  ID: " << roundtrip->header.id << "\n";
         std::cout << "  Match: "
                   << (original.query == roundtrip->query && original.header.id == roundtrip->header.id ? "✓" : "✗")
                   << "\n";
      }
      else {
         std::cout << "  Conversion failed!\n";
      }
   }

   std::cout << "\n";
   return 0;
}