respondo-grpc 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +22 -0
- data/LICENSE +201 -0
- data/README.md +200 -0
- data/lib/respondo/v1/response_pb.rb +23 -0
- data/lib/respondo-grpc.rb +5 -0
- data/lib/respondo_grpc/client_helpers.rb +54 -0
- data/lib/respondo_grpc/configuration.rb +58 -0
- data/lib/respondo_grpc/proto_mapper.rb +132 -0
- data/lib/respondo_grpc/response_builder.rb +114 -0
- data/lib/respondo_grpc/serializer.rb +86 -0
- data/lib/respondo_grpc/service_helpers.rb +232 -0
- data/lib/respondo_grpc/short_helpers.rb +33 -0
- data/lib/respondo_grpc/status_map.rb +110 -0
- data/lib/respondo_grpc/version.rb +5 -0
- data/lib/respondo_grpc.rb +74 -0
- data/proto/respondo/v1/response.proto +78 -0
- data/respondo-grpc.gemspec +45 -0
- metadata +159 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 20ddd7ac619ee059822fee6c9bbf1c7fc18e18c3df7ba2c3da5b59aece58384e
|
|
4
|
+
data.tar.gz: 83995bb81bf937e308e8cdebe9112fff5f010f352416c2c801232b03f71730a6
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: f79102dc1bdffc70993c3007186fc84b6b4c2eabc0b43ace8fc83d34922c625cfa0ca4b770172e421cdd8ffb72fbbe3611b25f3cf61ec20b238e4674739f55a6
|
|
7
|
+
data.tar.gz: e77ab17701a23da42c6d97396bc86cff1ac367427f239ceeeab33f4573657f4f2103dd657b7924d4fb572205c54109429a8d1256c40d77c056b32d5630b389c4
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [1.0.0] — Initial release
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
- `respondo.v1.Response` protobuf envelope (`proto/respondo/v1/response.proto`):
|
|
7
|
+
`success`, `message`, `data` (`google.protobuf.Value`), `payload` (`google.protobuf.Any`),
|
|
8
|
+
`errors` (`FieldError`), `meta` (`request_id`, `timestamp`, `code`, `status`, `grpc_code`,
|
|
9
|
+
`error_code`, `pagination`, `extra`).
|
|
10
|
+
- `RespondoGrpc::ServiceHelpers`: `render_grpc_*` helpers for every 2xx / 4xx / 5xx situation
|
|
11
|
+
(same names and codes as the respondo-graphql helpers, with `call:` for the request id).
|
|
12
|
+
Success helpers return the envelope; error helpers raise `GRPC::BadStatus` with the mapped
|
|
13
|
+
gRPC status and the envelope in the `respondo-response-bin` trailing metadata.
|
|
14
|
+
- `RespondoGrpc::ShortHelpers`: opt-in short names (`render_ok`, `render_not_found`, …).
|
|
15
|
+
- `RespondoGrpc::StatusMap`: HTTP-style code → gRPC status table, overridable with
|
|
16
|
+
`config.status_map`.
|
|
17
|
+
- `RespondoGrpc::ClientHelpers`: `to_h`, `error_response` and `error_hash` for reading replies
|
|
18
|
+
and failures on the client.
|
|
19
|
+
- Protobuf messages passed as `data:` travel as typed payloads (`Any`); hashes, arrays and
|
|
20
|
+
ActiveRecord records travel as JSON-like `data`.
|
|
21
|
+
- Configuration: `default_success_message`, `default_error_message`, `include_request_id`,
|
|
22
|
+
`request_id_key`, `serializer`, `default_meta`, `always_ok`, `status_map`.
|
data/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Shailendra Kumar
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# Respondo gRPC 🛰️
|
|
4
|
+
|
|
5
|
+
**Consistent response envelopes for gRPC services — real status codes, structured errors, one line per reply.**
|
|
6
|
+
|
|
7
|
+
[](https://github.com/spatelpatidar/respondo-grpc/actions/workflows/ci.yml)
|
|
8
|
+
[](https://rubygems.org/gems/respondo-grpc)
|
|
9
|
+
[](LICENSE)
|
|
10
|
+
[](https://www.ruby-lang.org/)
|
|
11
|
+
|
|
12
|
+
</div>
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
`respondo-grpc` is the gRPC sibling of [`respondo`](https://github.com/spatelpatidar/respondo) (JSON) and [`respondo-graphql`](https://github.com/spatelpatidar/respondo-graphql). It is a **standalone gem** with its own namespace (`RespondoGrpc`), config and protobuf message. Install any of the three, or all of them.
|
|
17
|
+
|
|
18
|
+
Same idea, same helper names and codes: one expressive helper per situation (`render_grpc_ok`, `render_grpc_not_found`, `render_grpc_unprocessable`, …). What changes is the transport:
|
|
19
|
+
|
|
20
|
+
| | respondo (JSON) | respondo-graphql | respondo-grpc |
|
|
21
|
+
|---|---|---|---|
|
|
22
|
+
| Envelope | `success, message, data, errors, meta` | `data, errors, extensions` | `respondo.v1.Response` message |
|
|
23
|
+
| Failure signal | HTTP status | `errors[]` (+ HTTP status) | **gRPC status code** + envelope |
|
|
24
|
+
| Typed payloads | no | no | yes (`google.protobuf.Any`) |
|
|
25
|
+
|
|
26
|
+
## Install
|
|
27
|
+
|
|
28
|
+
```ruby
|
|
29
|
+
# Gemfile
|
|
30
|
+
gem "respondo-grpc"
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
bundle install
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Use the envelope in your `.proto`
|
|
38
|
+
|
|
39
|
+
The gem ships `proto/respondo/v1/response.proto`. Import it and return it from any rpc:
|
|
40
|
+
|
|
41
|
+
```proto
|
|
42
|
+
syntax = "proto3";
|
|
43
|
+
package example.users.v1;
|
|
44
|
+
|
|
45
|
+
import "respondo/v1/response.proto";
|
|
46
|
+
|
|
47
|
+
service UserService {
|
|
48
|
+
rpc GetUser(GetUserRequest) returns (respondo.v1.Response);
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Compile your protos with the gem's `proto/` directory on the include path:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
GEM_PROTO="$(bundle exec ruby -e 'puts File.join(Gem.loaded_specs["respondo-grpc"].full_gem_path, "proto")')"
|
|
56
|
+
grpc_tools_ruby_protoc -I proto -I "$GEM_PROTO" --ruby_out=gen --grpc_out=gen proto/users/v1/users.proto
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
The compiled `Respondo::V1::Response` class comes with the gem; you do not compile it yourself.
|
|
60
|
+
|
|
61
|
+
## Usage
|
|
62
|
+
|
|
63
|
+
```ruby
|
|
64
|
+
class UserService < Example::Users::V1::UserService::Service
|
|
65
|
+
include RespondoGrpc::ServiceHelpers
|
|
66
|
+
|
|
67
|
+
def get_user(request, call)
|
|
68
|
+
user = User.find_by(id: request.id)
|
|
69
|
+
return render_grpc_not_found(message: "User not found", call: call) unless user
|
|
70
|
+
|
|
71
|
+
render_grpc_ok(data: user, message: "User fetched", call: call)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def create_user(request, call)
|
|
75
|
+
user = User.new(name: request.name, email: request.email)
|
|
76
|
+
if user.save
|
|
77
|
+
render_grpc_created(data: user, message: "Account created", call: call)
|
|
78
|
+
else
|
|
79
|
+
render_grpc_unprocessable(message: "Validation failed", errors: user.errors, call: call)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
* **Success helpers return** the `Respondo::V1::Response`. Make the call the last expression of the rpc.
|
|
86
|
+
* **Error helpers raise** `GRPC::BadStatus` with the mapped gRPC status (`NOT_FOUND`, `INVALID_ARGUMENT`, …). The message is the status detail, and the whole envelope rides in the `respondo-response-bin` trailing metadata. `return render_grpc_not_found(...)` reads naturally and works in both modes (see `always_ok`).
|
|
87
|
+
* `call:` is optional. Pass the rpc's second argument to let `include_request_id` copy the client's `x-request-id` into `meta`.
|
|
88
|
+
|
|
89
|
+
**What the client receives** for `render_grpc_ok(data: { id: 1 }, message: "User fetched")` (shown as JSON):
|
|
90
|
+
|
|
91
|
+
```json
|
|
92
|
+
{
|
|
93
|
+
"success": true,
|
|
94
|
+
"message": "User fetched",
|
|
95
|
+
"data": { "id": 1 },
|
|
96
|
+
"meta": { "timestamp": "2026-10-26T10:30:00Z", "code": 200, "status": "ok" }
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
and for `render_grpc_unprocessable(message: "Validation failed", errors: { email: ["is invalid"] })`, a call that finishes with `INVALID_ARGUMENT` (3) and this envelope in its trailers:
|
|
101
|
+
|
|
102
|
+
```json
|
|
103
|
+
{
|
|
104
|
+
"message": "Validation failed",
|
|
105
|
+
"errors": [{ "field": "email", "messages": ["is invalid"] }],
|
|
106
|
+
"meta": { "timestamp": "2026-10-26T10:30:00Z", "code": 422, "status": "unprocessable_content", "grpcCode": 3 }
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### What goes where
|
|
111
|
+
|
|
112
|
+
| You pass | Where it ends up |
|
|
113
|
+
|---|---|
|
|
114
|
+
| `data:` hash / array / scalar / ActiveRecord | `Response.data` (`google.protobuf.Value`) |
|
|
115
|
+
| `data:` a **protobuf message** | `Response.payload` (`google.protobuf.Any`), so the client can unpack a real class |
|
|
116
|
+
| `message:` | `Response.message`, and the gRPC status detail on failure |
|
|
117
|
+
| `errors: { field: [...] }` / `ActiveModel::Errors` | `Response.errors[]` (`field`, `messages`) |
|
|
118
|
+
| `pagination: { current_page:, per_page:, total_pages:, total_count:, next_page:, prev_page: }` | `Response.meta.pagination` (other keys are ignored) |
|
|
119
|
+
| `meta: { ... }` and `config.default_meta` | `Response.meta.extra` (`google.protobuf.Struct`) |
|
|
120
|
+
|
|
121
|
+
`google.protobuf.Value` has a single number type, so numbers inside `data` come back as floats (`1.0`). Use a typed payload when exact integers matter.
|
|
122
|
+
|
|
123
|
+
## Reading replies on the client
|
|
124
|
+
|
|
125
|
+
```ruby
|
|
126
|
+
stub = Example::Users::V1::UserService::Stub.new("localhost:50051", :this_channel_is_insecure)
|
|
127
|
+
|
|
128
|
+
begin
|
|
129
|
+
response = stub.get_user(Example::Users::V1::GetUserRequest.new(id: 42),
|
|
130
|
+
metadata: { "x-request-id" => "abc-123" })
|
|
131
|
+
|
|
132
|
+
user = response.payload.unpack(Example::Users::V1::User) # typed payload
|
|
133
|
+
RespondoGrpc::ClientHelpers.to_h(response)[:data] # JSON-like data, as plain Ruby
|
|
134
|
+
rescue GRPC::BadStatus => e
|
|
135
|
+
failure = RespondoGrpc::ClientHelpers.error_hash(e)
|
|
136
|
+
failure[:message] # => "User not found"
|
|
137
|
+
failure[:errors] # => { email: ["is invalid"] }
|
|
138
|
+
failure[:meta][:grpc_code] # => 5
|
|
139
|
+
end
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
`error_hash` falls back to the status details when the server sent no envelope, so there is always a message to show.
|
|
143
|
+
|
|
144
|
+
## Helpers
|
|
145
|
+
|
|
146
|
+
Every helper takes `message:`, `meta:` and `call:`. Success helpers also take `data:` and `pagination:`; error helpers take `errors:`.
|
|
147
|
+
|
|
148
|
+
| Kind | Helpers |
|
|
149
|
+
|---|---|
|
|
150
|
+
| Success (2xx) | `render_grpc_ok`, `_created`, `_accepted`, `_non_authoritative`, `_partial_content`, `_multi_status`, `_already_reported`, `_im_used`; without `data:`: `_no_content`, `_reset_content` |
|
|
151
|
+
| Client errors (4xx) | `render_grpc_bad_request`, `_unauthorized`, `_forbidden`, `_not_found`, `_conflict`, `_gone`, `_unprocessable`, `_too_many_requests`, … (all 29) |
|
|
152
|
+
| Server errors (5xx) | `render_grpc_server_error`, `_not_implemented`, `_bad_gateway`, `_service_unavailable`, `_gateway_timeout`, … (all 11) |
|
|
153
|
+
|
|
154
|
+
The names and numeric codes match `respondo-graphql`, so `meta.code` means the same thing in every envelope. 1xx and 3xx helpers are not provided: they are HTTP transport concepts with no meaning in gRPC.
|
|
155
|
+
|
|
156
|
+
Prefer shorter names? `include RespondoGrpc::ShortHelpers` gives you `render_ok`, `render_not_found`, … as aliases. They are never mixed in automatically, because those are the names the JSON gem uses in controllers.
|
|
157
|
+
|
|
158
|
+
### HTTP code → gRPC status
|
|
159
|
+
|
|
160
|
+
| HTTP-style code | gRPC status | | HTTP-style code | gRPC status |
|
|
161
|
+
|---|---|---|---|---|
|
|
162
|
+
| 400, 406, 411, 414, 415, 422 | `INVALID_ARGUMENT` (3) | | 409 | `ALREADY_EXISTS` (6) |
|
|
163
|
+
| 401, 407, 511 | `UNAUTHENTICATED` (16) | | 402, 412, 417, 421, 423–426, 428 | `FAILED_PRECONDITION` (9) |
|
|
164
|
+
| 403, 451 | `PERMISSION_DENIED` (7) | | 413, 429, 431, 507 | `RESOURCE_EXHAUSTED` (8) |
|
|
165
|
+
| 404, 410 | `NOT_FOUND` (5) | | 416 | `OUT_OF_RANGE` (11) |
|
|
166
|
+
| 408, 504 | `DEADLINE_EXCEEDED` (4) | | 405, 501, 505, 510 | `UNIMPLEMENTED` (12) |
|
|
167
|
+
| 500, 506, 508 | `INTERNAL` (13) | | 502, 503 | `UNAVAILABLE` (14) |
|
|
168
|
+
| 418 | `UNKNOWN` (2) | | | |
|
|
169
|
+
|
|
170
|
+
Disagree with one? `config.status_map = { 409 => :aborted }`.
|
|
171
|
+
|
|
172
|
+
## Configuration
|
|
173
|
+
|
|
174
|
+
```ruby
|
|
175
|
+
RespondoGrpc.configure do |config|
|
|
176
|
+
config.default_success_message = "Success"
|
|
177
|
+
config.default_error_message = "An error occurred"
|
|
178
|
+
config.include_request_id = false # copy x-request-id into meta.request_id
|
|
179
|
+
config.request_id_key = "x-request-id" # metadata key to read it from
|
|
180
|
+
config.always_ok = false # see below
|
|
181
|
+
config.status_map = {} # e.g. { 409 => :aborted }
|
|
182
|
+
config.default_meta = { api_version: "v1" }
|
|
183
|
+
config.serializer = ->(obj) { MySerializer.new(obj).as_json }
|
|
184
|
+
end
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
**`always_ok`** — by default failures finish the call with a real gRPC status code, which is what interceptors, retries, load balancers and monitoring understand. Set `config.always_ok = true` to answer failures as a normal reply (status `OK`) with `success: false` and `meta.grpc_code` in the message, if your clients only look at the message body.
|
|
188
|
+
|
|
189
|
+
## Development
|
|
190
|
+
|
|
191
|
+
```bash
|
|
192
|
+
bundle install
|
|
193
|
+
bundle exec rspec # specs
|
|
194
|
+
bundle exec rake proto # recompile the .proto files (needs grpc-tools)
|
|
195
|
+
bundle exec rake publish # spec → build → push
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
## License
|
|
199
|
+
|
|
200
|
+
Apache-2.0
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# Descriptor compiled from respondo/v1/response.proto with protoc 35.1; same layout as `protoc --ruby_out`.
|
|
3
|
+
# Regenerate with grpc_tools_ruby_protoc (see README), it produces an equivalent file.
|
|
4
|
+
# source: respondo/v1/response.proto
|
|
5
|
+
|
|
6
|
+
require 'google/protobuf'
|
|
7
|
+
|
|
8
|
+
require 'google/protobuf/any_pb'
|
|
9
|
+
require 'google/protobuf/struct_pb'
|
|
10
|
+
|
|
11
|
+
descriptor_data = "\n\x1arespondo/v1/response.proto\x12\x0brespondo.v1\x1a\x19google/protobuf/any.proto\x1a\x1cgoogle/protobuf/struct.proto\"\xf2\x01\n\x08Response\x12\x18\n\x07success\x18\x01 \x01(\x08R\x07success\x12\x18\n\x07message\x18\x02 \x01(\x09R\x07message\x12*\n\x04data\x18\x03 \x01(\x0b2\x16.google.protobuf.ValueR\x04data\x12.\n\x07payload\x18\x04 \x01(\x0b2\x14.google.protobuf.AnyR\x07payload\x12/\n\x06errors\x18\x05 \x03(\x0b2\x17.respondo.v1.FieldErrorR\x06errors\x12%\n\x04meta\x18\x06 \x01(\x0b2\x11.respondo.v1.MetaR\x04meta\">\n\nFieldError\x12\x14\n\x05field\x18\x01 \x01(\x09R\x05field\x12\x1a\n\x08messages\x18\x02 \x03(\x09R\x08messages\"\x93\x02\n\x04Meta\x12\x1d\n\nrequest_id\x18\x01 \x01(\x09R\x09requestId\x12\x1c\n\x09timestamp\x18\x02 \x01(\x09R\x09timestamp\x12\x12\n\x04code\x18\x03 \x01(\x05R\x04code\x12\x16\n\x06status\x18\x04 \x01(\x09R\x06status\x12\x1b\n\x09grpc_code\x18\x05 \x01(\x05R\x08grpcCode\x12\x1d\n\nerror_code\x18\x06 \x01(\x09R\x09errorCode\x127\n\npagination\x18\x07 \x01(\x0b2\x17.respondo.v1.PaginationR\npagination\x12-\n\x05extra\x18\x08 \x01(\x0b2\x17.google.protobuf.StructR\x05extra\"\xec\x01\n\nPagination\x12!\n\x0ccurrent_page\x18\x01 \x01(\x05R\x0bcurrentPage\x12\x19\n\x08per_page\x18\x02 \x01(\x05R\x07perPage\x12\x1f\n\x0btotal_pages\x18\x03 \x01(\x05R\ntotalPages\x12\x1f\n\x0btotal_count\x18\x04 \x01(\x05R\ntotalCount\x12 \n\x09next_page\x18\x05 \x01(\x05H\x00R\x08nextPage\x88\x01\x01\x12 \n\x09prev_page\x18\x06 \x01(\x05H\x01R\x08prevPage\x88\x01\x01B\x0c\n\n_next_pageB\x0c\n\n_prev_pageb\x06proto3".b
|
|
12
|
+
|
|
13
|
+
pool = Google::Protobuf::DescriptorPool.generated_pool
|
|
14
|
+
pool.add_serialized_file(descriptor_data)
|
|
15
|
+
|
|
16
|
+
module Respondo
|
|
17
|
+
module V1
|
|
18
|
+
Response = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("respondo.v1.Response").msgclass
|
|
19
|
+
FieldError = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("respondo.v1.FieldError").msgclass
|
|
20
|
+
Meta = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("respondo.v1.Meta").msgclass
|
|
21
|
+
Pagination = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("respondo.v1.Pagination").msgclass
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RespondoGrpc
|
|
4
|
+
# Client-side helpers: read a respondo.v1.Response, or the one a failed call
|
|
5
|
+
# carries in its trailing metadata.
|
|
6
|
+
#
|
|
7
|
+
# begin
|
|
8
|
+
# response = stub.get_user(Example::Users::V1::GetUserRequest.new(id: 42))
|
|
9
|
+
# result = RespondoGrpc::ClientHelpers.to_h(response)
|
|
10
|
+
# result[:data] # plain Ruby, for hash-style payloads
|
|
11
|
+
# response.payload.unpack(Example::Users::V1::User) # typed payloads
|
|
12
|
+
# rescue GRPC::BadStatus => e
|
|
13
|
+
# failure = RespondoGrpc::ClientHelpers.error_hash(e)
|
|
14
|
+
# failure[:message] # => "User not found"
|
|
15
|
+
# failure[:errors] # => { email: ["is invalid"] }
|
|
16
|
+
# failure[:meta][:grpc_code]
|
|
17
|
+
# end
|
|
18
|
+
module ClientHelpers
|
|
19
|
+
module_function
|
|
20
|
+
|
|
21
|
+
# @param response [Respondo::V1::Response]
|
|
22
|
+
# @return [Hash] { success:, message:, data:, payload:, errors:, meta: }
|
|
23
|
+
def to_h(response)
|
|
24
|
+
ProtoMapper.to_h(response)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# The envelope a failed call carried, or nil when the server did not send one
|
|
28
|
+
# (a plain gRPC error, a transport failure, ...).
|
|
29
|
+
#
|
|
30
|
+
# @param bad_status [GRPC::BadStatus]
|
|
31
|
+
# @return [Respondo::V1::Response, nil]
|
|
32
|
+
def error_response(bad_status)
|
|
33
|
+
metadata = bad_status.respond_to?(:metadata) ? bad_status.metadata : nil
|
|
34
|
+
bytes = metadata && metadata[RespondoGrpc::METADATA_KEY]
|
|
35
|
+
return nil if bytes.nil? || bytes.empty?
|
|
36
|
+
|
|
37
|
+
ProtoMapper.decode(bytes)
|
|
38
|
+
rescue ::Google::Protobuf::ParseError
|
|
39
|
+
nil
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Same as error_response, as a Hash. Falls back to the status details when
|
|
43
|
+
# there is no envelope, so there is always something to show.
|
|
44
|
+
#
|
|
45
|
+
# @param bad_status [GRPC::BadStatus]
|
|
46
|
+
# @return [Hash]
|
|
47
|
+
def error_hash(bad_status)
|
|
48
|
+
response = error_response(bad_status)
|
|
49
|
+
return ProtoMapper.to_h(response) if response
|
|
50
|
+
|
|
51
|
+
{ success: false, message: bad_status.details.to_s, meta: { grpc_code: bad_status.code } }
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RespondoGrpc
|
|
4
|
+
# Global configuration for RespondoGrpc.
|
|
5
|
+
#
|
|
6
|
+
# @example
|
|
7
|
+
# RespondoGrpc.configure do |config|
|
|
8
|
+
# config.default_success_message = "OK"
|
|
9
|
+
# config.include_request_id = true
|
|
10
|
+
# config.always_ok = false
|
|
11
|
+
# config.default_meta = { api_version: "v1" }
|
|
12
|
+
# end
|
|
13
|
+
class Configuration
|
|
14
|
+
# Message used when none is supplied to a success helper
|
|
15
|
+
attr_accessor :default_success_message
|
|
16
|
+
|
|
17
|
+
# Message used when none is supplied to an error helper
|
|
18
|
+
attr_accessor :default_error_message
|
|
19
|
+
|
|
20
|
+
# When true, copies the request id from the call's metadata into every
|
|
21
|
+
# response's meta block (see request_id_key). Needs `call:` on the helper.
|
|
22
|
+
attr_accessor :include_request_id
|
|
23
|
+
|
|
24
|
+
# Request metadata key the request id is read from (gRPC keys are lowercase).
|
|
25
|
+
attr_accessor :request_id_key
|
|
26
|
+
|
|
27
|
+
# Custom serializer callable — receives (object) and returns a Hash/Array.
|
|
28
|
+
# Defaults to nil (built-in serialization strategy is used).
|
|
29
|
+
# @example config.serializer = ->(obj) { MySerializer.new(obj).as_json }
|
|
30
|
+
attr_accessor :serializer
|
|
31
|
+
|
|
32
|
+
# Static key-value pairs merged into every response's `meta.extra` block.
|
|
33
|
+
# @example config.default_meta = { api_version: "v1" }
|
|
34
|
+
attr_accessor :default_meta
|
|
35
|
+
|
|
36
|
+
# By default an error helper raises GRPC::BadStatus, so the call finishes
|
|
37
|
+
# with a real gRPC status code (NOT_FOUND, INVALID_ARGUMENT, ...) and the
|
|
38
|
+
# envelope rides in the trailing metadata. When true, error helpers return
|
|
39
|
+
# the envelope as a normal reply and the call finishes with status OK;
|
|
40
|
+
# clients then read `success` / `meta.grpc_code` from the message.
|
|
41
|
+
attr_accessor :always_ok
|
|
42
|
+
|
|
43
|
+
# Overrides for the HTTP-code -> gRPC-status table, e.g. { 409 => :aborted }.
|
|
44
|
+
# Values are status names (see StatusMap::CODES) or integers.
|
|
45
|
+
attr_accessor :status_map
|
|
46
|
+
|
|
47
|
+
def initialize
|
|
48
|
+
@default_success_message = "Success"
|
|
49
|
+
@default_error_message = "An error occurred"
|
|
50
|
+
@include_request_id = false
|
|
51
|
+
@request_id_key = "x-request-id"
|
|
52
|
+
@serializer = nil
|
|
53
|
+
@default_meta = {}
|
|
54
|
+
@always_ok = false
|
|
55
|
+
@status_map = {}
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|