twirbet 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +20 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +28 -0
- data/Gemfile.lock +127 -0
- data/LICENSE.txt +21 -0
- data/README.md +144 -0
- data/Rakefile +17 -0
- data/examples/clientcompat/client +28 -0
- data/examples/clientcompat/clientcompat.proto +29 -0
- data/examples/clientcompat/clientcompat_pb.rb +36 -0
- data/examples/clientcompat/clientcompat_twirbet.rb +57 -0
- data/examples/ping/Gemfile +11 -0
- data/examples/ping/Gemfile.lock +69 -0
- data/examples/ping/bin/puma +27 -0
- data/examples/ping/bin/pumactl +27 -0
- data/examples/ping/bin/srb +27 -0
- data/examples/ping/bin/srb-rbi +27 -0
- data/examples/ping/bin/tapioca +27 -0
- data/examples/ping/client.rb +14 -0
- data/examples/ping/config/application.rb +13 -0
- data/examples/ping/config/environment.rb +6 -0
- data/examples/ping/config.ru +8 -0
- data/examples/ping/proto/ping.proto +15 -0
- data/examples/ping/proto/ping_pb.rb +20 -0
- data/examples/ping/proto/ping_twirbet.rb +47 -0
- data/examples/ping/sorbet/config +4 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/descriptor_proto/extension_range.rbi +34 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/descriptor_proto/reserved_range.rbi +22 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/descriptor_proto.rbi +83 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/enum_descriptor_proto/enum_reserved_range.rbi +22 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/enum_descriptor_proto.rbi +48 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/enum_options.rbi +34 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/enum_value_descriptor_proto.rbi +34 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/enum_value_options.rbi +27 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/extension_range_options.rbi +20 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/field_descriptor_proto/label.rbi +22 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/field_descriptor_proto/type.rbi +37 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/field_descriptor_proto.rbi +90 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/field_options/c_type.rbi +22 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/field_options/js_type.rbi +22 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/field_options.rbi +69 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/file_descriptor_proto.rbi +97 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/file_descriptor_set.rbi +20 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/file_options/optimize_mode.rbi +22 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/file_options.rbi +160 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/generated_code_info/annotation.rbi +41 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/generated_code_info.rbi +20 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/map.rbi +12 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/message_options.rbi +48 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/method_descriptor_proto.rbi +55 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/method_options/idempotency_level.rbi +22 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/method_options.rbi +34 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/oneof_descriptor_proto.rbi +22 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/oneof_options.rbi +20 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/repeated_field.rbi +11 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/service_descriptor_proto.rbi +34 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/service_options.rbi +27 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/source_code_info/location.rbi +48 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/source_code_info.rbi +20 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/uninterpreted_option/name_part.rbi +22 -0
- data/examples/ping/sorbet/rbi/dsl/google/protobuf/uninterpreted_option.rbi +62 -0
- data/examples/ping/sorbet/rbi/dsl/ping/ping_request.rbi +16 -0
- data/examples/ping/sorbet/rbi/dsl/ping/ping_response.rbi +16 -0
- data/examples/ping/sorbet/rbi/gems/ast@2.4.2.rbi +584 -0
- data/examples/ping/sorbet/rbi/gems/diff-lcs@1.5.0.rbi +8 -0
- data/examples/ping/sorbet/rbi/gems/google-protobuf@3.21.12.rbi +1645 -0
- data/examples/ping/sorbet/rbi/gems/netrc@0.11.0.rbi +158 -0
- data/examples/ping/sorbet/rbi/gems/nio4r@2.5.8.rbi +8 -0
- data/examples/ping/sorbet/rbi/gems/parallel@1.22.1.rbi +277 -0
- data/examples/ping/sorbet/rbi/gems/parser@3.1.3.0.rbi +5076 -0
- data/examples/ping/sorbet/rbi/gems/puma@6.0.0.rbi +4177 -0
- data/examples/ping/sorbet/rbi/gems/rack@3.0.2.rbi +5016 -0
- data/examples/ping/sorbet/rbi/gems/rbi@0.0.16.rbi +3008 -0
- data/examples/ping/sorbet/rbi/gems/spoom@1.1.15.rbi +2383 -0
- data/examples/ping/sorbet/rbi/gems/tapioca@0.10.3.rbi +3032 -0
- data/examples/ping/sorbet/rbi/gems/thor@1.2.1.rbi +3919 -0
- data/examples/ping/sorbet/rbi/gems/twirbet@0.1.0.rbi +528 -0
- data/examples/ping/sorbet/rbi/gems/unparser@0.6.5.rbi +8 -0
- data/examples/ping/sorbet/rbi/gems/webrick@1.7.0.rbi +2498 -0
- data/examples/ping/sorbet/rbi/gems/yard-sorbet@0.7.0.rbi +391 -0
- data/examples/ping/sorbet/rbi/gems/yard@0.9.28.rbi +17022 -0
- data/examples/ping/sorbet/tapioca/config.yml +13 -0
- data/examples/ping/sorbet/tapioca/require.rb +5 -0
- data/lib/twirbet/client.rb +45 -0
- data/lib/twirbet/dsl.rb +84 -0
- data/lib/twirbet/encoding.rb +52 -0
- data/lib/twirbet/errors.rb +375 -0
- data/lib/twirbet/method.rb +34 -0
- data/lib/twirbet/service.rb +85 -0
- data/lib/twirbet/transport.rb +55 -0
- data/lib/twirbet/transports/fake_transport.rb +32 -0
- data/lib/twirbet/transports/net_http_transport.rb +20 -0
- data/lib/twirbet/version.rb +6 -0
- data/lib/twirbet.rb +11 -0
- data/sorbet/config +5 -0
- data/sorbet/rbi/annotations/rainbow.rbi +269 -0
- data/sorbet/rbi/custom/ping.rbi +23 -0
- data/sorbet/rbi/gems/ast@2.4.2.rbi +584 -0
- data/sorbet/rbi/gems/diff-lcs@1.5.0.rbi +1064 -0
- data/sorbet/rbi/gems/google-protobuf@3.21.12.rbi +1645 -0
- data/sorbet/rbi/gems/json@2.6.3.rbi +1541 -0
- data/sorbet/rbi/gems/netrc@0.11.0.rbi +158 -0
- data/sorbet/rbi/gems/parallel@1.22.1.rbi +277 -0
- data/sorbet/rbi/gems/parser@3.1.3.0.rbi +6878 -0
- data/sorbet/rbi/gems/rack@3.0.2.rbi +5163 -0
- data/sorbet/rbi/gems/rainbow@3.1.1.rbi +397 -0
- data/sorbet/rbi/gems/rake@13.0.6.rbi +2946 -0
- data/sorbet/rbi/gems/rbi@0.0.16.rbi +3008 -0
- data/sorbet/rbi/gems/regexp_parser@2.6.1.rbi +3126 -0
- data/sorbet/rbi/gems/rexml@3.2.5.rbi +4660 -0
- data/sorbet/rbi/gems/rspec-core@3.12.0.rbi +10492 -0
- data/sorbet/rbi/gems/rspec-expectations@3.12.1.rbi +7817 -0
- data/sorbet/rbi/gems/rspec-mocks@3.12.1.rbi +4994 -0
- data/sorbet/rbi/gems/rspec-support@3.12.0.rbi +1477 -0
- data/sorbet/rbi/gems/rspec@3.12.0.rbi +10 -0
- data/sorbet/rbi/gems/rubocop-ast@1.24.0.rbi +6790 -0
- data/sorbet/rbi/gems/rubocop-rake@0.6.0.rbi +354 -0
- data/sorbet/rbi/gems/rubocop-rspec@2.16.0.rbi +7650 -0
- data/sorbet/rbi/gems/rubocop-shopify@2.10.1.rbi +8 -0
- data/sorbet/rbi/gems/rubocop-sorbet@0.6.11.rbi +1014 -0
- data/sorbet/rbi/gems/rubocop@1.40.0.rbi +51560 -0
- data/sorbet/rbi/gems/ruby-progressbar@1.11.0.rbi +1212 -0
- data/sorbet/rbi/gems/spoom@1.1.15.rbi +2383 -0
- data/sorbet/rbi/gems/tapioca@0.10.3.rbi +3032 -0
- data/sorbet/rbi/gems/thor@1.2.1.rbi +3950 -0
- data/sorbet/rbi/gems/unicode-display_width@2.3.0.rbi +46 -0
- data/sorbet/rbi/gems/unparser@0.6.5.rbi +4265 -0
- data/sorbet/rbi/gems/webrick@1.7.0.rbi +2498 -0
- data/sorbet/rbi/gems/yard-sorbet@0.7.0.rbi +391 -0
- data/sorbet/rbi/gems/yard@0.9.28.rbi +17033 -0
- data/sorbet/tapioca/config.yml +13 -0
- data/sorbet/tapioca/require.rb +4 -0
- data/twirbet.gemspec +36 -0
- metadata +223 -0
@@ -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
|
data/lib/twirbet/dsl.rb
ADDED
@@ -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
|