twirbet 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (136) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/.rubocop.yml +20 -0
  4. data/CHANGELOG.md +5 -0
  5. data/Gemfile +28 -0
  6. data/Gemfile.lock +127 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +144 -0
  9. data/Rakefile +17 -0
  10. data/examples/clientcompat/client +28 -0
  11. data/examples/clientcompat/clientcompat.proto +29 -0
  12. data/examples/clientcompat/clientcompat_pb.rb +36 -0
  13. data/examples/clientcompat/clientcompat_twirbet.rb +57 -0
  14. data/examples/ping/Gemfile +11 -0
  15. data/examples/ping/Gemfile.lock +69 -0
  16. data/examples/ping/bin/puma +27 -0
  17. data/examples/ping/bin/pumactl +27 -0
  18. data/examples/ping/bin/srb +27 -0
  19. data/examples/ping/bin/srb-rbi +27 -0
  20. data/examples/ping/bin/tapioca +27 -0
  21. data/examples/ping/client.rb +14 -0
  22. data/examples/ping/config/application.rb +13 -0
  23. data/examples/ping/config/environment.rb +6 -0
  24. data/examples/ping/config.ru +8 -0
  25. data/examples/ping/proto/ping.proto +15 -0
  26. data/examples/ping/proto/ping_pb.rb +20 -0
  27. data/examples/ping/proto/ping_twirbet.rb +47 -0
  28. data/examples/ping/sorbet/config +4 -0
  29. data/examples/ping/sorbet/rbi/dsl/google/protobuf/descriptor_proto/extension_range.rbi +34 -0
  30. data/examples/ping/sorbet/rbi/dsl/google/protobuf/descriptor_proto/reserved_range.rbi +22 -0
  31. data/examples/ping/sorbet/rbi/dsl/google/protobuf/descriptor_proto.rbi +83 -0
  32. data/examples/ping/sorbet/rbi/dsl/google/protobuf/enum_descriptor_proto/enum_reserved_range.rbi +22 -0
  33. data/examples/ping/sorbet/rbi/dsl/google/protobuf/enum_descriptor_proto.rbi +48 -0
  34. data/examples/ping/sorbet/rbi/dsl/google/protobuf/enum_options.rbi +34 -0
  35. data/examples/ping/sorbet/rbi/dsl/google/protobuf/enum_value_descriptor_proto.rbi +34 -0
  36. data/examples/ping/sorbet/rbi/dsl/google/protobuf/enum_value_options.rbi +27 -0
  37. data/examples/ping/sorbet/rbi/dsl/google/protobuf/extension_range_options.rbi +20 -0
  38. data/examples/ping/sorbet/rbi/dsl/google/protobuf/field_descriptor_proto/label.rbi +22 -0
  39. data/examples/ping/sorbet/rbi/dsl/google/protobuf/field_descriptor_proto/type.rbi +37 -0
  40. data/examples/ping/sorbet/rbi/dsl/google/protobuf/field_descriptor_proto.rbi +90 -0
  41. data/examples/ping/sorbet/rbi/dsl/google/protobuf/field_options/c_type.rbi +22 -0
  42. data/examples/ping/sorbet/rbi/dsl/google/protobuf/field_options/js_type.rbi +22 -0
  43. data/examples/ping/sorbet/rbi/dsl/google/protobuf/field_options.rbi +69 -0
  44. data/examples/ping/sorbet/rbi/dsl/google/protobuf/file_descriptor_proto.rbi +97 -0
  45. data/examples/ping/sorbet/rbi/dsl/google/protobuf/file_descriptor_set.rbi +20 -0
  46. data/examples/ping/sorbet/rbi/dsl/google/protobuf/file_options/optimize_mode.rbi +22 -0
  47. data/examples/ping/sorbet/rbi/dsl/google/protobuf/file_options.rbi +160 -0
  48. data/examples/ping/sorbet/rbi/dsl/google/protobuf/generated_code_info/annotation.rbi +41 -0
  49. data/examples/ping/sorbet/rbi/dsl/google/protobuf/generated_code_info.rbi +20 -0
  50. data/examples/ping/sorbet/rbi/dsl/google/protobuf/map.rbi +12 -0
  51. data/examples/ping/sorbet/rbi/dsl/google/protobuf/message_options.rbi +48 -0
  52. data/examples/ping/sorbet/rbi/dsl/google/protobuf/method_descriptor_proto.rbi +55 -0
  53. data/examples/ping/sorbet/rbi/dsl/google/protobuf/method_options/idempotency_level.rbi +22 -0
  54. data/examples/ping/sorbet/rbi/dsl/google/protobuf/method_options.rbi +34 -0
  55. data/examples/ping/sorbet/rbi/dsl/google/protobuf/oneof_descriptor_proto.rbi +22 -0
  56. data/examples/ping/sorbet/rbi/dsl/google/protobuf/oneof_options.rbi +20 -0
  57. data/examples/ping/sorbet/rbi/dsl/google/protobuf/repeated_field.rbi +11 -0
  58. data/examples/ping/sorbet/rbi/dsl/google/protobuf/service_descriptor_proto.rbi +34 -0
  59. data/examples/ping/sorbet/rbi/dsl/google/protobuf/service_options.rbi +27 -0
  60. data/examples/ping/sorbet/rbi/dsl/google/protobuf/source_code_info/location.rbi +48 -0
  61. data/examples/ping/sorbet/rbi/dsl/google/protobuf/source_code_info.rbi +20 -0
  62. data/examples/ping/sorbet/rbi/dsl/google/protobuf/uninterpreted_option/name_part.rbi +22 -0
  63. data/examples/ping/sorbet/rbi/dsl/google/protobuf/uninterpreted_option.rbi +62 -0
  64. data/examples/ping/sorbet/rbi/dsl/ping/ping_request.rbi +16 -0
  65. data/examples/ping/sorbet/rbi/dsl/ping/ping_response.rbi +16 -0
  66. data/examples/ping/sorbet/rbi/gems/ast@2.4.2.rbi +584 -0
  67. data/examples/ping/sorbet/rbi/gems/diff-lcs@1.5.0.rbi +8 -0
  68. data/examples/ping/sorbet/rbi/gems/google-protobuf@3.21.12.rbi +1645 -0
  69. data/examples/ping/sorbet/rbi/gems/netrc@0.11.0.rbi +158 -0
  70. data/examples/ping/sorbet/rbi/gems/nio4r@2.5.8.rbi +8 -0
  71. data/examples/ping/sorbet/rbi/gems/parallel@1.22.1.rbi +277 -0
  72. data/examples/ping/sorbet/rbi/gems/parser@3.1.3.0.rbi +5076 -0
  73. data/examples/ping/sorbet/rbi/gems/puma@6.0.0.rbi +4177 -0
  74. data/examples/ping/sorbet/rbi/gems/rack@3.0.2.rbi +5016 -0
  75. data/examples/ping/sorbet/rbi/gems/rbi@0.0.16.rbi +3008 -0
  76. data/examples/ping/sorbet/rbi/gems/spoom@1.1.15.rbi +2383 -0
  77. data/examples/ping/sorbet/rbi/gems/tapioca@0.10.3.rbi +3032 -0
  78. data/examples/ping/sorbet/rbi/gems/thor@1.2.1.rbi +3919 -0
  79. data/examples/ping/sorbet/rbi/gems/twirbet@0.1.0.rbi +528 -0
  80. data/examples/ping/sorbet/rbi/gems/unparser@0.6.5.rbi +8 -0
  81. data/examples/ping/sorbet/rbi/gems/webrick@1.7.0.rbi +2498 -0
  82. data/examples/ping/sorbet/rbi/gems/yard-sorbet@0.7.0.rbi +391 -0
  83. data/examples/ping/sorbet/rbi/gems/yard@0.9.28.rbi +17022 -0
  84. data/examples/ping/sorbet/tapioca/config.yml +13 -0
  85. data/examples/ping/sorbet/tapioca/require.rb +5 -0
  86. data/lib/twirbet/client.rb +45 -0
  87. data/lib/twirbet/dsl.rb +84 -0
  88. data/lib/twirbet/encoding.rb +52 -0
  89. data/lib/twirbet/errors.rb +375 -0
  90. data/lib/twirbet/method.rb +34 -0
  91. data/lib/twirbet/service.rb +85 -0
  92. data/lib/twirbet/transport.rb +55 -0
  93. data/lib/twirbet/transports/fake_transport.rb +32 -0
  94. data/lib/twirbet/transports/net_http_transport.rb +20 -0
  95. data/lib/twirbet/version.rb +6 -0
  96. data/lib/twirbet.rb +11 -0
  97. data/sorbet/config +5 -0
  98. data/sorbet/rbi/annotations/rainbow.rbi +269 -0
  99. data/sorbet/rbi/custom/ping.rbi +23 -0
  100. data/sorbet/rbi/gems/ast@2.4.2.rbi +584 -0
  101. data/sorbet/rbi/gems/diff-lcs@1.5.0.rbi +1064 -0
  102. data/sorbet/rbi/gems/google-protobuf@3.21.12.rbi +1645 -0
  103. data/sorbet/rbi/gems/json@2.6.3.rbi +1541 -0
  104. data/sorbet/rbi/gems/netrc@0.11.0.rbi +158 -0
  105. data/sorbet/rbi/gems/parallel@1.22.1.rbi +277 -0
  106. data/sorbet/rbi/gems/parser@3.1.3.0.rbi +6878 -0
  107. data/sorbet/rbi/gems/rack@3.0.2.rbi +5163 -0
  108. data/sorbet/rbi/gems/rainbow@3.1.1.rbi +397 -0
  109. data/sorbet/rbi/gems/rake@13.0.6.rbi +2946 -0
  110. data/sorbet/rbi/gems/rbi@0.0.16.rbi +3008 -0
  111. data/sorbet/rbi/gems/regexp_parser@2.6.1.rbi +3126 -0
  112. data/sorbet/rbi/gems/rexml@3.2.5.rbi +4660 -0
  113. data/sorbet/rbi/gems/rspec-core@3.12.0.rbi +10492 -0
  114. data/sorbet/rbi/gems/rspec-expectations@3.12.1.rbi +7817 -0
  115. data/sorbet/rbi/gems/rspec-mocks@3.12.1.rbi +4994 -0
  116. data/sorbet/rbi/gems/rspec-support@3.12.0.rbi +1477 -0
  117. data/sorbet/rbi/gems/rspec@3.12.0.rbi +10 -0
  118. data/sorbet/rbi/gems/rubocop-ast@1.24.0.rbi +6790 -0
  119. data/sorbet/rbi/gems/rubocop-rake@0.6.0.rbi +354 -0
  120. data/sorbet/rbi/gems/rubocop-rspec@2.16.0.rbi +7650 -0
  121. data/sorbet/rbi/gems/rubocop-shopify@2.10.1.rbi +8 -0
  122. data/sorbet/rbi/gems/rubocop-sorbet@0.6.11.rbi +1014 -0
  123. data/sorbet/rbi/gems/rubocop@1.40.0.rbi +51560 -0
  124. data/sorbet/rbi/gems/ruby-progressbar@1.11.0.rbi +1212 -0
  125. data/sorbet/rbi/gems/spoom@1.1.15.rbi +2383 -0
  126. data/sorbet/rbi/gems/tapioca@0.10.3.rbi +3032 -0
  127. data/sorbet/rbi/gems/thor@1.2.1.rbi +3950 -0
  128. data/sorbet/rbi/gems/unicode-display_width@2.3.0.rbi +46 -0
  129. data/sorbet/rbi/gems/unparser@0.6.5.rbi +4265 -0
  130. data/sorbet/rbi/gems/webrick@1.7.0.rbi +2498 -0
  131. data/sorbet/rbi/gems/yard-sorbet@0.7.0.rbi +391 -0
  132. data/sorbet/rbi/gems/yard@0.9.28.rbi +17033 -0
  133. data/sorbet/tapioca/config.yml +13 -0
  134. data/sorbet/tapioca/require.rb +4 -0
  135. data/twirbet.gemspec +36 -0
  136. metadata +223 -0
@@ -0,0 +1,13 @@
1
+ gem:
2
+ # Add your `gem` command parameters here:
3
+ #
4
+ # exclude:
5
+ # - gem_name
6
+ # doc: true
7
+ # workers: 5
8
+ dsl:
9
+ # Add your `dsl` command parameters here:
10
+ #
11
+ # exclude:
12
+ # - SomeGeneratorName
13
+ # workers: 5
@@ -0,0 +1,5 @@
1
+ # typed: true
2
+ # frozen_string_literal: true
3
+
4
+ require "google/protobuf"
5
+ require "twirbet"
@@ -0,0 +1,45 @@
1
+ # typed: true
2
+ # frozen_string_literal: true
3
+
4
+ require "twirbet/dsl"
5
+ require "twirbet/encoding"
6
+ require "twirbet/transport"
7
+ require "twirbet/transports/net_http_transport"
8
+
9
+ module Twirbet
10
+ class Client
11
+ extend T::Sig
12
+ include DSL
13
+
14
+ sig { returns(String) }
15
+ attr_reader :base_url
16
+
17
+ sig { returns(String) }
18
+ attr_reader :prefix
19
+
20
+ sig { returns(Transport) }
21
+ attr_reader :transport
22
+
23
+ sig { params(base_url: String, prefix: String, transport: Transport).void }
24
+ def initialize(base_url, prefix: "/twirp", transport: Transports::NetHTTPTransport.new)
25
+ @base_url = base_url
26
+ @prefix = prefix
27
+ @transport = transport
28
+ end
29
+
30
+ sig { params(method_name: String, request: T.untyped, headers: T::Hash[String, String]).returns(T.untyped) }
31
+ def call(method_name, request, headers = {})
32
+ method = rpc(method_name)
33
+ raise ArgumentError, "Unknown method: #{method_name}" unless method
34
+
35
+ url = "#{base_url}#{prefix}/#{full_name}/#{method_name}"
36
+ body = Encoding.encode_request(request, method.request)
37
+ headers = headers.merge({ "Content-Type" => "application/protobuf" })
38
+
39
+ response = transport.call(Transport::Request.new(url, body, headers))
40
+ raise Twirbet::Error.from_response(response) if response.status != 200
41
+
42
+ Encoding.decode(response.body, method.response, "application/protobuf")
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,84 @@
1
+ # typed: true
2
+ # frozen_string_literal: true
3
+
4
+ require "twirbet/method"
5
+
6
+ module Twirbet
7
+ module DSL
8
+ extend T::Sig
9
+ extend T::Helpers
10
+
11
+ requires_ancestor { Kernel }
12
+
13
+ module ClassMethods
14
+ extend T::Sig
15
+ extend T::Helpers
16
+
17
+ requires_ancestor { Kernel }
18
+
19
+ sig { params(name: T.nilable(String)).void }
20
+ def package(name)
21
+ @package = name
22
+ end
23
+
24
+ sig { returns(T.nilable(String)) }
25
+ def package_name
26
+ @package
27
+ end
28
+
29
+ sig { params(name: String).void }
30
+ def service(name)
31
+ @service = name
32
+ end
33
+
34
+ sig { returns(String) }
35
+ def service_name
36
+ raise "Unknown service name. Did you forget to call `service`?" if @service.nil?
37
+
38
+ @service
39
+ end
40
+
41
+ sig { returns(String) }
42
+ def full_name
43
+ if package_name.nil?
44
+ service_name
45
+ else
46
+ "#{package_name}.#{service_name}"
47
+ end
48
+ end
49
+
50
+ sig { params(name: String, request: Class, response: Class, ruby_method: Symbol).void }
51
+ def rpc(name, request, response, ruby_method:)
52
+ method = Method.new(name, request, response, ruby_method: ruby_method)
53
+ rpcs[method.name] = method
54
+ end
55
+
56
+ sig { returns(T::Hash[String, Method]) }
57
+ def rpcs
58
+ @rpcs ||= {}
59
+ end
60
+ end
61
+
62
+ mixes_in_class_methods(ClassMethods)
63
+
64
+ sig { returns(T.nilable(String)) }
65
+ def package_name
66
+ self.class.package_name
67
+ end
68
+
69
+ sig { returns(String) }
70
+ def service_name
71
+ self.class.service_name
72
+ end
73
+
74
+ sig { returns(String) }
75
+ def full_name
76
+ self.class.full_name
77
+ end
78
+
79
+ sig { params(name: String).returns(T.nilable(Method)) }
80
+ def rpc(name)
81
+ self.class.rpcs[name]
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,52 @@
1
+ # typed: true
2
+ # frozen_string_literal: true
3
+
4
+ module Twirbet
5
+ module Encoding
6
+ class << self
7
+ extend T::Sig
8
+
9
+ sig { params(content_type: T.nilable(String)).returns(T::Boolean) }
10
+ def supported?(content_type)
11
+ content_type == "application/protobuf" || content_type == "application/json"
12
+ end
13
+
14
+ sig { params(request: T.untyped, klass: Class).returns(T.untyped) }
15
+ def encode_request(request, klass)
16
+ T.unsafe(klass).encode(request)
17
+ end
18
+
19
+ sig { params(request: Rack::Request).returns(T.untyped) }
20
+ def decode_request(request)
21
+ method = request.env["twirp.method"]
22
+ raise "Missing `twirp.method` in request environment." if method.nil?
23
+
24
+ decode(request.body.read, method.request, request.content_type)
25
+ end
26
+
27
+ sig { params(object: T.untyped, content_type: String).returns(T.untyped) }
28
+ def encode(object, content_type)
29
+ case content_type
30
+ when "application/protobuf"
31
+ T.unsafe(object).to_proto
32
+ when "application/json"
33
+ T.unsafe(object).to_json
34
+ else
35
+ raise "Unsupported content type: #{content_type}"
36
+ end
37
+ end
38
+
39
+ sig { params(object: T.untyped, klass: Class, content_type: String).returns(T.untyped) }
40
+ def decode(object, klass, content_type)
41
+ case content_type
42
+ when "application/protobuf"
43
+ T.unsafe(klass).decode(object)
44
+ when "application/json"
45
+ T.unsafe(klass).decode_json(object)
46
+ else
47
+ raise "Unsupported content type: #{content_type}"
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,375 @@
1
+ # typed: true
2
+ # frozen_string_literal: true
3
+
4
+ require "json"
5
+ require "rack"
6
+ require "twirbet/transport"
7
+
8
+ module Twirbet
9
+ class BaseError < StandardError
10
+ extend T::Sig
11
+ extend T::Helpers
12
+
13
+ abstract!
14
+
15
+ sig { returns(T::Hash[String, String]) }
16
+ attr_reader :metadata
17
+
18
+ sig { params(message: String, metadata: T::Hash[String, String]).void }
19
+ def initialize(message, metadata = {})
20
+ super(message)
21
+ @metadata = metadata
22
+ end
23
+
24
+ sig { abstract.returns(String) }
25
+ def code
26
+ end
27
+
28
+ sig { abstract.returns(Integer) }
29
+ def status
30
+ end
31
+
32
+ sig(:final) { returns(Rack::Response) }
33
+ def to_rack_response
34
+ Rack::Response.new(to_json, status, "Content-Type" => "application/json")
35
+ end
36
+
37
+ sig(:final) { returns(String) }
38
+ def to_json
39
+ T.unsafe(to_hash).to_json
40
+ end
41
+
42
+ sig(:final) { returns(T::Hash[String, String]) }
43
+ def to_hash
44
+ { "code" => code, "msg" => message, "meta" => metadata }
45
+ end
46
+ end
47
+
48
+ class CanceledError < BaseError
49
+ extend T::Sig
50
+
51
+ sig { override.returns(String) }
52
+ def code
53
+ "canceled"
54
+ end
55
+
56
+ sig { override.returns(Integer) }
57
+ def status
58
+ 400
59
+ end
60
+ end
61
+
62
+ class UnknownError < BaseError
63
+ extend T::Sig
64
+
65
+ sig { override.returns(String) }
66
+ def code
67
+ "unknown"
68
+ end
69
+
70
+ sig { override.returns(Integer) }
71
+ def status
72
+ 400
73
+ end
74
+ end
75
+
76
+ class InvalidArgumentError < BaseError
77
+ extend T::Sig
78
+
79
+ sig { override.returns(String) }
80
+ def code
81
+ "invalid_argument"
82
+ end
83
+
84
+ sig { override.returns(Integer) }
85
+ def status
86
+ 400
87
+ end
88
+ end
89
+
90
+ class MalformedError < BaseError
91
+ extend T::Sig
92
+
93
+ sig { override.returns(String) }
94
+ def code
95
+ "malformed"
96
+ end
97
+
98
+ sig { override.returns(Integer) }
99
+ def status
100
+ 400
101
+ end
102
+ end
103
+
104
+ class DeadlineExceededError < BaseError
105
+ extend T::Sig
106
+
107
+ sig { override.returns(String) }
108
+ def code
109
+ "deadline_exceeded"
110
+ end
111
+
112
+ sig { override.returns(Integer) }
113
+ def status
114
+ 400
115
+ end
116
+ end
117
+
118
+ class NotFoundError < BaseError
119
+ extend T::Sig
120
+
121
+ sig { override.returns(String) }
122
+ def code
123
+ "not_found"
124
+ end
125
+
126
+ sig { override.returns(Integer) }
127
+ def status
128
+ 404
129
+ end
130
+ end
131
+
132
+ class BadRouteError < BaseError
133
+ extend T::Sig
134
+
135
+ sig { override.returns(String) }
136
+ def code
137
+ "bad_route"
138
+ end
139
+
140
+ sig { override.returns(Integer) }
141
+ def status
142
+ 404
143
+ end
144
+ end
145
+
146
+ class AlreadyExistsError < BaseError
147
+ extend T::Sig
148
+
149
+ sig { override.returns(String) }
150
+ def code
151
+ "already_exists"
152
+ end
153
+
154
+ sig { override.returns(Integer) }
155
+ def status
156
+ 409
157
+ end
158
+ end
159
+
160
+ class PermissionDeniedError < BaseError
161
+ extend T::Sig
162
+
163
+ sig { override.returns(String) }
164
+ def code
165
+ "permission_denied"
166
+ end
167
+
168
+ sig { override.returns(Integer) }
169
+ def status
170
+ 403
171
+ end
172
+ end
173
+
174
+ class UnauthenticatedError < BaseError
175
+ extend T::Sig
176
+
177
+ sig { override.returns(String) }
178
+ def code
179
+ "unauthenticated"
180
+ end
181
+
182
+ sig { override.returns(Integer) }
183
+ def status
184
+ 401
185
+ end
186
+ end
187
+
188
+ class ResourceExhaustedError < BaseError
189
+ extend T::Sig
190
+
191
+ sig { override.returns(String) }
192
+ def code
193
+ "resource_exhausted"
194
+ end
195
+
196
+ sig { override.returns(Integer) }
197
+ def status
198
+ 429
199
+ end
200
+ end
201
+
202
+ class FailedPreconditionError < BaseError
203
+ extend T::Sig
204
+
205
+ sig { override.returns(String) }
206
+ def code
207
+ "failed_precondition"
208
+ end
209
+
210
+ sig { override.returns(Integer) }
211
+ def status
212
+ 400
213
+ end
214
+ end
215
+
216
+ class AbortedError < BaseError
217
+ extend T::Sig
218
+
219
+ sig { override.returns(String) }
220
+ def code
221
+ "aborted"
222
+ end
223
+
224
+ sig { override.returns(Integer) }
225
+ def status
226
+ 409
227
+ end
228
+ end
229
+
230
+ class OutOfRangeError < BaseError
231
+ extend T::Sig
232
+
233
+ sig { override.returns(String) }
234
+ def code
235
+ "out_of_range"
236
+ end
237
+
238
+ sig { override.returns(Integer) }
239
+ def status
240
+ 400
241
+ end
242
+ end
243
+
244
+ class UnimplementedError < BaseError
245
+ extend T::Sig
246
+
247
+ sig { override.returns(String) }
248
+ def code
249
+ "unimplemented"
250
+ end
251
+
252
+ sig { override.returns(Integer) }
253
+ def status
254
+ 501
255
+ end
256
+ end
257
+
258
+ class InternalError < BaseError
259
+ extend T::Sig
260
+
261
+ sig { override.returns(String) }
262
+ def code
263
+ "internal"
264
+ end
265
+
266
+ sig { override.returns(Integer) }
267
+ def status
268
+ 500
269
+ end
270
+ end
271
+
272
+ class UnavailableError < BaseError
273
+ extend T::Sig
274
+
275
+ sig { override.returns(String) }
276
+ def code
277
+ "unavailable"
278
+ end
279
+
280
+ sig { override.returns(Integer) }
281
+ def status
282
+ 503
283
+ end
284
+ end
285
+
286
+ class DataLossError < BaseError
287
+ extend T::Sig
288
+
289
+ sig { override.returns(String) }
290
+ def code
291
+ "data_loss"
292
+ end
293
+
294
+ sig { override.returns(Integer) }
295
+ def status
296
+ 500
297
+ end
298
+ end
299
+
300
+ module Error
301
+ extend T::Sig
302
+
303
+ CODE_MAP = T.let({
304
+ "canceled" => CanceledError,
305
+ "unknown" => UnknownError,
306
+ "invalid_argument" => InvalidArgumentError,
307
+ "malformed" => MalformedError,
308
+ "deadline_exceeded" => DeadlineExceededError,
309
+ "not_found" => NotFoundError,
310
+ "bad_route" => BadRouteError,
311
+ "already_exists" => AlreadyExistsError,
312
+ "permission_denied" => PermissionDeniedError,
313
+ "unauthenticated" => UnauthenticatedError,
314
+ "resource_exhausted" => ResourceExhaustedError,
315
+ "failed_precondition" => FailedPreconditionError,
316
+ "aborted" => AbortedError,
317
+ "out_of_range" => OutOfRangeError,
318
+ "unimplemented" => UnimplementedError,
319
+ "internal" => InternalError,
320
+ "unavailable" => UnavailableError,
321
+ "data_loss" => DataLossError,
322
+ }.freeze, T::Hash[String, T.class_of(BaseError)])
323
+
324
+ class << self
325
+ extend T::Sig
326
+
327
+ sig { params(response: Transport::Response).returns(BaseError) }
328
+ def from_response(response)
329
+ from_json(response.body)
330
+ rescue JSON::ParserError
331
+ from_intermidate(response.status, "Response is not JSON.", response.body)
332
+ end
333
+
334
+ sig { params(status: Integer, reason: String, body: String).returns(BaseError) }
335
+ def from_intermidate(status, reason, body)
336
+ code = case status
337
+ when 400 then "internal"
338
+ when 401 then "unauthenticated"
339
+ when 403 then "permission_denied"
340
+ when 404 then "bad_route"
341
+ when 429, 502, 503, 504 then "unavailable"
342
+ else "unknown"
343
+ end
344
+
345
+ build(code, code)
346
+ end
347
+
348
+ sig { params(exception: Exception).returns(BaseError) }
349
+ def from_exception(exception)
350
+ case exception
351
+ when BaseError
352
+ exception
353
+ else
354
+ InternalError.new(exception.message)
355
+ end
356
+ end
357
+
358
+ sig { params(json: String).returns(BaseError) }
359
+ def from_json(json)
360
+ hash = JSON.parse(json)
361
+ from_hash(hash)
362
+ end
363
+
364
+ sig { params(hash: T::Hash[String, String]).returns(BaseError) }
365
+ def from_hash(hash)
366
+ build(hash.fetch("code", "unknown"), hash.fetch("msg", ""))
367
+ end
368
+
369
+ sig { params(code: String, message: String).returns(BaseError) }
370
+ def build(code, message)
371
+ CODE_MAP.fetch(code, UnknownError).new(message)
372
+ end
373
+ end
374
+ end
375
+ end
@@ -0,0 +1,34 @@
1
+ # typed: true
2
+ # frozen_string_literal: true
3
+
4
+ module Twirbet
5
+ class Method
6
+ extend T::Sig
7
+ include Comparable
8
+
9
+ sig { returns(String) }
10
+ attr_reader :name
11
+
12
+ sig { returns(Class) }
13
+ attr_reader :request
14
+
15
+ sig { returns(Class) }
16
+ attr_reader :response
17
+
18
+ sig { returns(Symbol) }
19
+ attr_reader :ruby_method
20
+
21
+ sig { params(name: String, request: Class, response: Class, ruby_method: Symbol).void }
22
+ def initialize(name, request, response, ruby_method:)
23
+ @name = name
24
+ @request = request
25
+ @response = response
26
+ @ruby_method = ruby_method
27
+ end
28
+
29
+ sig { params(other: T.untyped).returns(Integer) }
30
+ def <=>(other)
31
+ [name, request, response, ruby_method] <=> [other.name, other.request, other.response, other.ruby_method]
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,85 @@
1
+ # typed: true
2
+ # frozen_string_literal: true
3
+
4
+ require "rack"
5
+ require "twirbet/dsl"
6
+ require "twirbet/encoding"
7
+ require "twirbet/errors"
8
+
9
+ module Twirbet
10
+ class Service
11
+ extend T::Sig
12
+ include DSL
13
+
14
+ sig { returns(T.untyped) }
15
+ attr_reader :handler
16
+
17
+ def initialize(handler)
18
+ @handler = handler
19
+ end
20
+
21
+ sig { params(env: T.untyped).returns(T.untyped) }
22
+ def call(env)
23
+ request = Rack::Request.new(env)
24
+ validate_request(request)
25
+
26
+ method = request.env["twirp.method"]
27
+ response = invoke_method(method, request)
28
+
29
+ rack_response = Rack::Response.new(response, 200, "Content-Type" => request.content_type)
30
+ rack_response.finish
31
+ rescue => e
32
+ Error.from_exception(e).to_rack_response.finish
33
+ end
34
+
35
+ private
36
+
37
+ sig { params(method: Method, request: Rack::Request).returns(T.untyped) }
38
+ def invoke_method(method, request)
39
+ begin
40
+ decoded_request = Encoding.decode_request(request)
41
+ rescue => e
42
+ raise MalformedError, "Error decoding request: #{e.message}"
43
+ end
44
+ response = handler.public_send(method.ruby_method, decoded_request)
45
+ Encoding.encode(response, request.content_type)
46
+ end
47
+
48
+ sig { params(request: Rack::Request).void }
49
+ def validate_request(request)
50
+ validate_method(request)
51
+ validate_content_type(request)
52
+ validate_path(request)
53
+ end
54
+
55
+ sig { params(request: Rack::Request).void }
56
+ def validate_method(request)
57
+ return if request.post?
58
+
59
+ raise BadRouteError, "Invalid HTTP method: #{request.request_method}. Only POST is allowed."
60
+ end
61
+
62
+ sig { params(request: Rack::Request).void }
63
+ def validate_content_type(request)
64
+ return if Encoding.supported?(request.content_type)
65
+
66
+ raise BadRouteError, "Unsupported content type: #{request.content_type}."
67
+ end
68
+
69
+ sig { params(request: Rack::Request).void }
70
+ def validate_path(request)
71
+ path_parts = request.path_info.split("/")
72
+
73
+ if path_parts.length < 3 || path_parts[-2] != full_name
74
+ raise BadRouteError, "Invalid path: #{request.path_info}."
75
+ end
76
+
77
+ method_name = path_parts[-1]
78
+
79
+ method = rpc(method_name)
80
+ raise BadRouteError, "Invalid method: #{method_name}." if method.nil?
81
+
82
+ request.env["twirp.method"] = method
83
+ end
84
+ end
85
+ end