grpc-rest 0.1.12 → 0.1.14

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8bfe3edbd1ceece344b2dff703ce619a0d829e40cec4022fe4676fe61ab77940
4
- data.tar.gz: a86fd6b8b0fd11479510e3db9296431d4f36490ec2ad1037ce2a1e80fcac2578
3
+ metadata.gz: 0713304a9f049b5c4cdd88510c9e2b32f9bfd9df2a66987a6b313212be2d6376
4
+ data.tar.gz: 33ca8e2b7014e3abd546967dcd55a8a9c422852026e6dd4275255c66f1c8eed2
5
5
  SHA512:
6
- metadata.gz: c08004595c9f8db0c3e7f6a0f9b15c35fa91f83229ed2741a303633fd174a6f1dba521ba73fb989a17ea901fd5c46177f842449845fd5493c45182bcc232bde4
7
- data.tar.gz: 4010b037a9bd33eb3b5b80896dbc73449f4adb2fccc51771ecec9d0bea1017365181eb9c4e23e87763c8c62cbf15a43da9e5bf2c0b2ef8ae5e6f4f6da870d1ec
6
+ metadata.gz: 8707159314407984f4b83c5ae31247580c12de1958a0775de68e40a615ba083bfca6445070d566109a8b473b1f7bf7a8070fa94769ce5f3e3ff2222fd04ee924
7
+ data.tar.gz: 2ec048673f1f4ae4adbf42a7d61393aeebe9df4c6247535b2250d554d8c46c41576440133074dda527cf4cfb37b18786088399bcb9f027c1b904d7e83736271e
data/CHANGELOG CHANGED
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## UNRELEASED
9
9
 
10
+ # 0.1.14 - 2024-06-25
11
+ - Emit the correct error code on failure.
12
+
13
+ # 0.1.13 - 2024-06-24
14
+ - Use grpc's `to_json` methods to output fields correctly.
15
+
10
16
  # 0.1.12 - 2024-05-15
11
17
  - Parse numbers into Ruby number objects.
12
18
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- grpc-rest (0.1.11)
4
+ grpc-rest (0.1.12)
5
5
  grpc
6
6
  rails (>= 6.0)
7
7
 
@@ -1,3 +1,3 @@
1
1
  module GrpcRest
2
- VERSION = '0.1.12'
2
+ VERSION = '0.1.14'
3
3
  end
data/lib/grpc_rest.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'google/protobuf/well_known_types'
2
2
  require 'grpc'
3
+ require 'grpc/core/status_codes'
3
4
 
4
5
  module GrpcRest
5
6
  class << self
@@ -79,7 +80,7 @@ module GrpcRest
79
80
 
80
81
  def init_request(request_class, params)
81
82
  map_proto_type(request_class.descriptor, params)
82
- request_class.new(params)
83
+ request_class.decode_json(JSON.generate(params))
83
84
  end
84
85
 
85
86
  def assign_params(request, param_hash, body_string, params)
@@ -105,16 +106,60 @@ module GrpcRest
105
106
  end
106
107
  end
107
108
 
109
+ # Ported from https://github.com/grpc-ecosystem/grpc-gateway/blob/main/runtime/errors.go#L36
110
+ def grpc_http_status(code)
111
+ case code
112
+ when GRPC::Core::StatusCodes::OK
113
+ :ok
114
+ when GRPC::Core::StatusCodes::CANCELLED
115
+ 499
116
+ when GRPC::Core::StatusCodes::INVALID_ARGUMENT,
117
+ GRPC::Core::StatusCodes::FAILED_PRECONDITION,
118
+ GRPC::Core::StatusCodes::OUT_OF_RANGE
119
+ :bad_request
120
+ when GRPC::Core::StatusCodes::DEADLINE_EXCEEDED
121
+ :gateway_timeout
122
+ when GRPC::Core::StatusCodes::NOT_FOUND
123
+ :not_found
124
+ when GRPC::Core::StatusCodes::ALREADY_EXISTS, GRPC::Core::StatusCodes::ABORTED
125
+ :conflict
126
+ when GRPC::Core::StatusCodes::PERMISSION_DENIED
127
+ :forbidden
128
+ when GRPC::Core::StatusCodes::UNAUTHENTICATED
129
+ :unauthorized
130
+ when GRPC::Core::StatusCodes::RESOURCE_EXHAUSTED
131
+ :too_many_requests
132
+ when GRPC::Core::StatusCodes::UNIMPLEMENTED
133
+ :not_implemented
134
+ when GRPC::Core::StatusCodes::UNAVAILABLE
135
+ :service_unavailable
136
+ else
137
+ :internal_server_error
138
+ end
139
+ end
140
+
108
141
  def error_msg(error)
142
+ if error.respond_to?(:code)
109
143
  {
110
- code: 3,
111
- message: "InvalidArgument: #{error.message}",
144
+ code: error.code,
145
+ message: error.message,
112
146
  details: [
113
147
  {
114
148
  backtrace: error.backtrace
115
149
  }
116
150
  ]
117
151
  }
152
+ else
153
+ {
154
+ code: 3,
155
+ message: "InvalidArgument: #{error.message}",
156
+ details: [
157
+ {
158
+ backtrace: error.backtrace
159
+ }
160
+ ]
161
+ }
162
+ end
118
163
  end
119
164
 
120
165
  def send_gruf_request(klass, service_obj, method, request)
@@ -141,10 +186,10 @@ module GrpcRest
141
186
  k.bound_service == service_obj
142
187
  end
143
188
  if klass
144
- return send_gruf_request(klass, service_obj, method, request).to_h
189
+ return send_gruf_request(klass, service_obj, method, request)
145
190
  end
146
191
  end
147
- send_grpc_request(service, method, request).to_h
192
+ send_grpc_request(service, method, request)
148
193
  end
149
194
  end
150
195
 
@@ -46,8 +46,14 @@ class {{.ControllerName}}Controller < ActionController::Base
46
46
  protect_from_forgery with: :null_session
47
47
 
48
48
  rescue_from StandardError do |e|
49
- render json: GrpcRest.error_msg(e)
49
+ render json: GrpcRest.error_msg(e), status: :internal_server_error
50
50
  end
51
+ rescue_from GRPC::BadStatus do |e|
52
+ render json: GrpcRest.error_msg(e), status: :internal_server_error
53
+ end
54
+ rescue_from Google::Protobuf::TypeError do |e|
55
+ render json: GrpcRest.error_msg(e), status: :bad_request
56
+ end
51
57
  METHOD_PARAM_MAP = {
52
58
  {{range .Methods }}
53
59
  "{{.Name}}" => [
@@ -6,8 +6,14 @@ class MyServiceController < ActionController::Base
6
6
  protect_from_forgery with: :null_session
7
7
 
8
8
  rescue_from StandardError do |e|
9
- render json: GrpcRest.error_msg(e)
9
+ render json: GrpcRest.error_msg(e), status: :internal_server_error
10
10
  end
11
+ rescue_from GRPC::BadStatus do |e|
12
+ render json: GrpcRest.error_msg(e), status: :internal_server_error
13
+ end
14
+ rescue_from Google::Protobuf::TypeError do |e|
15
+ render json: GrpcRest.error_msg(e), status: :bad_request
16
+ end
11
17
  METHOD_PARAM_MAP = {
12
18
 
13
19
  "test" => [
@@ -28,8 +28,8 @@ RSpec.describe MyServiceController, type: :request do
28
28
  get "/test/blah/xyz?test_id=abc"
29
29
  expect(response).to be_successful
30
30
  expect(response.parsed_body).to eq({
31
- 'some_int' => 1,
32
- 'full_response' => %({"testId":"abc","foobar":"xyz"})
31
+ 'someInt' => 1,
32
+ 'fullResponse' => %({"testId":"abc","foobar":"xyz"})
33
33
  })
34
34
  end
35
35
  end
@@ -44,8 +44,8 @@ RSpec.describe MyServiceController, type: :request do
44
44
  post '/test2?test_id=abc&foobar=xyz&timestamp_field=2024-04-03+01:02:03+UTC', params: params, as: :json
45
45
  expect(response).to be_successful
46
46
  expect(response.parsed_body).to eq({
47
- 'some_int' => 2,
48
- 'full_response' => %({"testId":"abc","foobar":"xyz","secondRecord":{"subId":"id1","anotherId":"id2"},"timestampField":"2024-04-03T01:02:03Z"})
47
+ 'someInt' => 2,
48
+ 'fullResponse' => %({"testId":"abc","foobar":"xyz","secondRecord":{"subId":"id1","anotherId":"id2"},"timestampField":"2024-04-03T01:02:03Z"})
49
49
  })
50
50
  end
51
51
 
@@ -56,8 +56,8 @@ RSpec.describe MyServiceController, type: :request do
56
56
  post '/test3/xyz?test_id=abc'
57
57
  expect(response).to be_successful
58
58
  expect(response.parsed_body).to eq({
59
- 'some_int' => 3,
60
- 'full_response' => %({"testId":"abc","subRecord":{"subId":"xyz"}})
59
+ 'someInt' => 3,
60
+ 'fullResponse' => %({"testId":"abc","subRecord":{"subId":"xyz"}})
61
61
  })
62
62
  end
63
63
  end
@@ -95,8 +95,8 @@ RSpec.describe MyServiceController, type: :request do
95
95
  post '/test4', params: params, as: :json
96
96
  expect(response).to be_successful
97
97
  expect(response.parsed_body).to eq({
98
- 'some_int' => 4,
99
- 'full_response' => %({"testId":"abc","foobar":"xyz","repeatedString":["W","T","F"],"subRecord":{"subId":"id1","anotherId":"id2"},"secondRecord":{"subId":"id3","anotherId":"id4"},"structField":{"bool_key":true,"str_key":"val","nil_key":null,"list_key":[{"inner_key":"inner_val"}],"int_key":123},"timestampField":"2024-04-03T01:02:03Z","listValue":["F","Y","I"],"bareValue":45,\"someInt\":65})
98
+ 'someInt' => 4,
99
+ 'fullResponse' => %({"testId":"abc","foobar":"xyz","repeatedString":["W","T","F"],"subRecord":{"subId":"id1","anotherId":"id2"},"secondRecord":{"subId":"id3","anotherId":"id4"},"structField":{"bool_key":true,"str_key":"val","nil_key":null,"list_key":[{"inner_key":"inner_val"}],"int_key":123},"timestampField":"2024-04-03T01:02:03Z","listValue":["F","Y","I"],"bareValue":45,\"someInt\":65})
100
100
  })
101
101
  end
102
102
  end
@@ -109,8 +109,8 @@ RSpec.describe MyServiceController, type: :request do
109
109
  post "/test4", params: params, as: :json
110
110
  expect(response).to be_successful
111
111
  expect(response.parsed_body).to eq({
112
- 'some_int' => 4,
113
- 'full_response' => %({"timestampField":"2024-04-09T19:54:12Z"})
112
+ 'someInt' => 4,
113
+ 'fullResponse' => %({"timestampField":"2024-04-09T19:54:12Z"})
114
114
  })
115
115
  end
116
116
  end
@@ -127,8 +127,8 @@ RSpec.describe MyServiceController, type: :request do
127
127
  post '/test4', params:, as: :json
128
128
  expect(response).to be_successful
129
129
  expect(response.parsed_body).to eq({
130
- 'some_int' => 4,
131
- 'full_response' => %({"subRecords":[{"subId":"id1","anotherId":"id2"}]})
130
+ 'someInt' => 4,
131
+ 'fullResponse' => %({"subRecords":[{"subId":"id1","anotherId":"id2"}]})
132
132
  })
133
133
  end
134
134
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grpc-rest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.12
4
+ version: 0.1.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Orner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-15 00:00:00.000000000 Z
11
+ date: 2024-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grpc