gapic-generator 0.6.15 → 0.7.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +33 -0
  3. data/bin/protoc-gen-ruby_gapic +5 -5
  4. data/lib/gapic/file_formatter.rb +2 -1
  5. data/lib/gapic/formatting_utils.rb +4 -4
  6. data/lib/gapic/generator/version.rb +1 -1
  7. data/lib/gapic/generators/default_generator.rb +8 -5
  8. data/lib/gapic/generators/default_generator_parameters.rb +8 -4
  9. data/lib/gapic/grpc_service_config/method_config.rb +2 -1
  10. data/lib/gapic/grpc_service_config/retry_policy.rb +4 -1
  11. data/lib/gapic/grpc_service_config/service_config.rb +2 -1
  12. data/lib/gapic/path_pattern/parser.rb +1 -1
  13. data/lib/gapic/path_pattern/pattern.rb +2 -1
  14. data/lib/gapic/path_pattern/segment.rb +10 -3
  15. data/lib/gapic/presenters.rb +2 -0
  16. data/lib/gapic/presenters/field_presenter.rb +44 -0
  17. data/lib/gapic/presenters/gem_presenter.rb +4 -1
  18. data/lib/gapic/presenters/method_presenter.rb +32 -16
  19. data/lib/gapic/presenters/method_rest_presenter.rb +186 -0
  20. data/lib/gapic/presenters/package_presenter.rb +21 -3
  21. data/lib/gapic/presenters/resource_presenter.rb +2 -1
  22. data/lib/gapic/presenters/sample_presenter.rb +6 -1
  23. data/lib/gapic/presenters/service_presenter.rb +48 -10
  24. data/lib/gapic/presenters/service_rest_presenter.rb +157 -0
  25. data/lib/gapic/runner.rb +2 -1
  26. data/lib/gapic/schema/api.rb +48 -19
  27. data/lib/gapic/schema/parameter_schema.rb +4 -1
  28. data/lib/gapic/schema/request_param_parser.rb +44 -10
  29. data/lib/gapic/schema/request_parameter.rb +5 -1
  30. data/lib/gapic/schema/wrappers.rb +31 -18
  31. data/lib/gapic/uri_template/parser.rb +4 -6
  32. data/lib/google/protobuf/any.pb.rb +1 -1
  33. data/lib/google/protobuf/compiler/plugin.pb.rb +9 -6
  34. data/lib/google/protobuf/descriptor.pb.rb +2 -2
  35. data/lib/google/protobuf/empty.pb.rb +1 -1
  36. data/templates/default/gem/gemspec.erb +2 -2
  37. data/templates/default/gem/readme.erb +2 -2
  38. data/templates/default/gem/rubocop.erb +2 -12
  39. data/templates/default/lib/_package.erb +11 -1
  40. data/templates/default/lib/_service.erb +31 -1
  41. data/templates/default/lib/rest/_rest.erb +11 -0
  42. data/templates/default/service/client/_config.erb +2 -2
  43. data/templates/default/service/rest.erb +6 -0
  44. data/templates/default/service/rest/client.erb +6 -0
  45. data/templates/default/service/rest/client/_client.erb +115 -0
  46. data/templates/default/service/rest/client/_config.erb +74 -0
  47. data/templates/default/service/rest/client/_requires.erb +1 -0
  48. data/templates/default/service/rest/client/method/_def.erb +18 -0
  49. data/templates/default/service/rest/client/method/def/_options_defaults.erb +14 -0
  50. data/templates/default/service/rest/client/method/def/_rescue.erb +3 -0
  51. data/templates/default/service/rest/client/method/def/_response_normal.erb +17 -0
  52. data/templates/default/service/rest/client/method/docs/_error.erb +2 -0
  53. data/templates/default/service/rest/client/method/docs/_request.erb +27 -0
  54. data/templates/default/service/rest/client/method/docs/_result.erb +6 -0
  55. data/templates/default/service/rest/grpc_transcoding.erb +6 -0
  56. data/templates/default/service/rest/grpc_transcoding/_grpc_transcoding.erb +9 -0
  57. data/templates/default/service/rest/grpc_transcoding/method/_def.erb +21 -0
  58. data/templates/default/service/rest/grpc_transcoding/method/def/_query_string_param.erb +8 -0
  59. data/templates/default/service/test/method/_normal.erb +1 -1
  60. data/templates/default/service/test/method/_server.erb +1 -1
  61. metadata +27 -8
@@ -21,13 +21,13 @@ Gem::Specification.new do |gem|
21
21
  <%= gem.extra_files.inspect %>
22
22
  gem.require_paths = ["lib"]
23
23
 
24
- gem.required_ruby_version = ">= 2.4"
24
+ gem.required_ruby_version = ">= 2.5"
25
25
 
26
26
  <%- gem.dependency_list.each do |name, requirements| -%>
27
27
  gem.add_dependency <%= name.inspect %>, <%= requirements.map { |v| v.inspect }.join ", " %>
28
28
  <%- end -%>
29
29
 
30
- gem.add_development_dependency "google-style", "~> 1.24.0"
30
+ gem.add_development_dependency "google-style", "~> 1.25.1"
31
31
  gem.add_development_dependency "minitest", "~> 5.14"
32
32
  gem.add_development_dependency "minitest-focus", "~> 1.1"
33
33
  gem.add_development_dependency "minitest-rg", "~> 5.2"
@@ -28,11 +28,11 @@ response = client.<%= method.name %> request
28
28
 
29
29
  ## Supported Ruby Versions
30
30
 
31
- This library is supported on Ruby 2.4+.
31
+ This library is supported on Ruby 2.5+.
32
32
 
33
33
  Google provides official support for Ruby versions that are actively supported
34
34
  by Ruby Core—that is, Ruby versions that are either in normal maintenance or
35
- in security maintenance, and not end of life. Currently, this means Ruby 2.4
35
+ in security maintenance, and not end of life. Currently, this means Ruby 2.5
36
36
  and later. Older versions of Ruby _may_ still work, but are unsupported and not
37
37
  recommended. See https://www.ruby-lang.org/en/downloads/branches/ for details
38
38
  about the Ruby support schedule.
@@ -8,14 +8,14 @@ AllCops:
8
8
  - "proto_docs/**/*"
9
9
  - "test/**/*"
10
10
 
11
+ Layout/LineLength:
12
+ Enabled: false
11
13
  Metrics/AbcSize:
12
14
  Enabled: false
13
15
  Metrics/ClassLength:
14
16
  Enabled: false
15
17
  Metrics/CyclomaticComplexity:
16
18
  Enabled: false
17
- Metrics/LineLength:
18
- Enabled: false
19
19
  Metrics/MethodLength:
20
20
  Enabled: false
21
21
  Metrics/ModuleLength:
@@ -25,13 +25,3 @@ Metrics/PerceivedComplexity:
25
25
  Naming/FileName:
26
26
  Exclude:
27
27
  - "lib/<%= gem.name %>.rb"
28
- Naming/PredicateName:
29
- Enabled: false
30
- Style/AsciiComments:
31
- Enabled: false
32
- Style/CaseEquality:
33
- Enabled: false
34
- Style/IfUnlessModifier:
35
- Enabled: false
36
- Style/ModuleFunction:
37
- Enabled: false
@@ -6,13 +6,23 @@ require "<%= service.service_require %>"
6
6
  require "<%= package.gem.version_require %>"
7
7
  <% end %>
8
8
  <%- unless package.empty? -%>
9
+ <%- if package.services.first.generate_grpc_clients? -%>
9
10
  ##
10
- # To load this package, including all its services, and instantiate a client:
11
+ # To load this package, including all its services, and instantiate a <%= package.grpc_client_designation %>:
11
12
  #
12
13
  # require "<%= package.package_require %>"
13
14
  # client = <%= package.services.first.create_client_call %>
14
15
  #
15
16
  <%- end -%>
17
+ <%- if package.services.first.generate_rest_clients? -%>
18
+ ##
19
+ # To load this package, including all its services, and instantiate a REST client:
20
+ #
21
+ # require "<%= package.package_require %>"
22
+ # client = <%= package.services.first.rest.create_client_call %>
23
+ #
24
+ <%- end -%>
25
+ <%- end -%>
16
26
  module <%= package.module_name %>
17
27
  end
18
28
  <% @footer = capture do %>
@@ -1,4 +1,21 @@
1
1
  <%- assert_locals service -%>
2
+
3
+ <%- if service.generate_rest_clients? -%>
4
+ <% @requires = capture do %>
5
+ require "gapic/config"
6
+ require "gapic/config/method"
7
+
8
+ require "<%= service.gem.version_require %>"
9
+
10
+ <%- unless service.generic_endpoint? -%>
11
+ require "<%= service.credentials_require %>"
12
+ <%- end -%>
13
+ <%- if service.paths? -%>
14
+ require "<%= service.paths_require %>"
15
+ <%- end -%>
16
+ require "<%= service.rest.service_rest_require %>"
17
+ <% end %>
18
+ <%- else -%>
2
19
  <% @requires = capture do %>
3
20
  require "gapic/common"
4
21
  require "gapic/config"
@@ -22,13 +39,26 @@ require "<%= service.client_require %>"
22
39
  <%= indent service.doc_description, "# " %>
23
40
  #
24
41
  <%- end -%>
25
- # To load this service and instantiate a client:
42
+ <%- end -%>
43
+ <%- if service.generate_grpc_clients? -%>
44
+ # To load this service and instantiate a <%= service.grpc_client_designation %>:
26
45
  #
27
46
  # require "<%= service.service_require %>"
28
47
  # client = <%= service.create_client_call %>
29
48
  #
49
+ <%- end -%>
50
+ <%- if service.generate_rest_clients? -%>
51
+ # To load this service and instantiate a REST client:
52
+ #
53
+ # require "<%= service.service_require %>"
54
+ # client = <%= service.rest.create_client_call %>
55
+ #
56
+ <%- end -%>
30
57
  module <%= service.module_name %>
31
58
  end
59
+
60
+
61
+
32
62
  <% @footer = capture do %>
33
63
  <%= render partial: "service/helpers", locals: { service: service} -%>
34
64
  <% end %>
@@ -0,0 +1,11 @@
1
+ <%- assert_locals service -%>
2
+ <% @requires = capture do %>
3
+ require "gapic/rest"
4
+ require "<%= service.rest.transcoding_helper_require %>"
5
+ require "<%= service.rest.client_require %>"
6
+ <% end %>
7
+ module <%= service.module_name %>
8
+ # Client for the REST transport
9
+ module Rest
10
+ end
11
+ end
@@ -115,7 +115,7 @@ class Configuration
115
115
  def rpcs
116
116
  @rpcs ||= begin
117
117
  parent_rpcs = nil
118
- parent_rpcs = @parent_config.rpcs if defined?(@parent_config) && @parent_config&.respond_to?(:rpcs)
118
+ parent_rpcs = @parent_config.rpcs if defined?(@parent_config) && @parent_config.respond_to?(:rpcs)
119
119
  Rpcs.new parent_rpcs
120
120
  end
121
121
  end
@@ -149,7 +149,7 @@ class Configuration
149
149
  # @private
150
150
  def initialize parent_rpcs = nil
151
151
  <%- method_service.methods.each do |method| -%>
152
- <%= method.name %>_config = parent_rpcs&.<%= method.name %> if parent_rpcs&.respond_to? :<%= method.name %>
152
+ <%= method.name %>_config = parent_rpcs.<%= method.name %> if parent_rpcs.respond_to? :<%= method.name %>
153
153
  @<%= method.name %> = ::Gapic::Config::Method.new <%= method.name %>_config
154
154
  <%- end -%>
155
155
 
@@ -0,0 +1,6 @@
1
+ <%- assert_locals service -%>
2
+ <%= render partial: "lib/rest/rest",
3
+ layout: "layouts/ruby",
4
+ locals: { service: service,
5
+ namespace: service.namespace }
6
+ %>
@@ -0,0 +1,6 @@
1
+ <%- assert_locals service -%>
2
+ <%= render partial: "service/rest/client/client",
3
+ layout: "layouts/ruby",
4
+ locals: { service: service,
5
+ namespace: service.rest.service_name_full }
6
+ %>
@@ -0,0 +1,115 @@
1
+ <%- assert_locals service -%>
2
+ <% @requires = capture do %>
3
+ <%= render partial: "service/client/requires", locals: { service: service} -%>
4
+ require "<%= service.proto_service_require %>"
5
+ <% end %>
6
+ ##
7
+ # REST client for the <%= service.name %> service.
8
+ #
9
+ <%- if service.doc_description -%>
10
+ <%= indent service.doc_description, "# " %>
11
+ #
12
+ <%- end -%>
13
+ class <%= service.rest.client_name %>
14
+ include <%= service.rest.transcoding_helper_name %>
15
+ <%- if service.paths? -%>
16
+ include <%= service.paths_name %>
17
+ <%- end -%>
18
+
19
+ # @private
20
+ attr_reader :<%= service.stub_name %>
21
+
22
+ ##
23
+ # Configure the <%= service.name %> <%= service.rest.client_name %> class.
24
+ #
25
+ # See {<%= service.rest.client_name_full %>::Configuration}
26
+ # for a description of the configuration fields.
27
+ #
28
+ # ## Example
29
+ #
30
+ # To modify the configuration for all <%= service.name %> clients:
31
+ #
32
+ # <%= service.rest.client_name_full %>.configure do |config|
33
+ # config.timeout = 10.0
34
+ # end
35
+ #
36
+ # @yield [config] Configure the <%= service.rest.client_name %> client.
37
+ # @yieldparam config [<%= service.rest.client_name %>::Configuration]
38
+ #
39
+ # @return [<%= service.rest.client_name %>::Configuration]
40
+ #
41
+ def self.configure
42
+ <%= indent render(partial: "service/client/self_configure", locals: { service: service }), 4 %>
43
+ end
44
+
45
+ ##
46
+ # Configure the <%= service.name %> <%= service.rest.client_name %> instance.
47
+ #
48
+ # The configuration is set to the derived mode, meaning that values can be changed,
49
+ # but structural changes (adding new fields, etc.) are not allowed. Structural changes
50
+ # should be made on {<%= service.rest.client_name %>.configure}.
51
+ #
52
+ # See {<%= service.rest.client_name_full %>::Configuration}
53
+ # for a description of the configuration fields.
54
+ #
55
+ # @yield [config] Configure the <%= service.rest.client_name %> client.
56
+ # @yieldparam config [<%= service.rest.client_name %>::Configuration]
57
+ #
58
+ # @return [<%= service.rest.client_name %>::Configuration]
59
+ #
60
+ def configure
61
+ yield @config if block_given?
62
+ @config
63
+ end
64
+
65
+ ##
66
+ # Create a new <%= service.name %> REST client object.
67
+ #
68
+ # ## Examples
69
+ #
70
+ # To create a new <%= service.name %> REST client with the default
71
+ # configuration:
72
+ #
73
+ # client = <%= service.rest.client_name_full %>.new
74
+ #
75
+ # To create a new <%= service.name %> REST client with a custom
76
+ # configuration:
77
+ #
78
+ # client = <%= service.rest.client_name_full %>.new do |config|
79
+ # config.timeout = 10.0
80
+ # end
81
+ #
82
+ # @yield [config] Configure the <%= service.name %> client.
83
+ # @yieldparam config [<%= service.rest.client_name %>::Configuration]
84
+ #
85
+ def initialize
86
+ # These require statements are intentionally placed here to initialize
87
+ # the REST modules only when it's required.
88
+ require "gapic/rest"
89
+
90
+ # Create the configuration object
91
+ @config = Configuration.new <%= service.rest.client_name %>.configure
92
+
93
+ # Yield the configuration if needed
94
+ yield @config if block_given?
95
+
96
+ # Create credentials
97
+ credentials = @config.credentials
98
+ <%- unless service.generic_endpoint? -%>
99
+ credentials ||= Credentials.default scope: @config.scope
100
+ if credentials.is_a?(String) || credentials.is_a?(Hash)
101
+ credentials = Credentials.new credentials, scope: @config.scope
102
+ end
103
+ <%- end -%>
104
+
105
+ @client_stub = ::Gapic::Rest::ClientStub.new endpoint: @config.endpoint, credentials: credentials
106
+ end
107
+
108
+ # Service calls
109
+ <%- service.methods.each do |method| -%>
110
+
111
+ <%= indent_tail render(partial: "service/rest/client/method/def", locals: { method: method }), 2 %>
112
+ <%- end %>
113
+
114
+ <%= indent_tail render(partial: "service/rest/client/config", locals: { service: service }), 2 %>
115
+ end
@@ -0,0 +1,74 @@
1
+ <%- assert_locals service -%>
2
+ <%- method_service ||= service -%>
3
+ ##
4
+ # Configuration class for the <%= service.name %> REST API.
5
+ #
6
+ # This class represents the configuration for <%= service.name %> REST,
7
+ # providing control over credentials, timeouts, retry behavior, logging.
8
+ #
9
+ # Configuration can be applied globally to all clients, or to a single client
10
+ # on construction.
11
+ #
12
+ <%- unless method_service.methods.empty? -%>
13
+ # # Examples
14
+ #
15
+ # To modify the global config, setting the timeout for all calls to 10 seconds:
16
+ #
17
+ # <%= service.client_name_full %>.configure do |config|
18
+ # config.timeout = 10.0
19
+ # end
20
+ #
21
+ # To apply the above configuration only to a new client:
22
+ #
23
+ # client = <%= service.client_name_full %>.new do |config|
24
+ # config.timeout = 10.0
25
+ # end
26
+ #
27
+ <%- end -%>
28
+ # @!attribute [rw] endpoint
29
+ # The hostname or hostname:port of the service endpoint.
30
+ # Defaults to `<%= service.client_endpoint.inspect %>`.
31
+ # @return [::String]
32
+ # @!attribute [rw] credentials
33
+ # Credentials to send with calls. You may provide any of the following types:
34
+ # * (`String`) The path to a service account key file in JSON format
35
+ # * (`Hash`) A service account key as a Hash
36
+ # * (`Google::Auth::Credentials`) A googleauth credentials object
37
+ # (see the [googleauth docs](https://googleapis.dev/ruby/googleauth/latest/index.html))
38
+ # * (`Signet::OAuth2::Client`) A signet oauth2 client object
39
+ # (see the [signet docs](https://googleapis.dev/ruby/signet/latest/Signet/OAuth2/Client.html))
40
+ # * (`nil`) indicating no credentials
41
+ # @return [::Object]
42
+ # @!attribute [rw] scope
43
+ # The OAuth scopes
44
+ # @return [::Array<::String>]
45
+ # @!attribute [rw] lib_name
46
+ # The library name as recorded in instrumentation and logging
47
+ # @return [::String]
48
+ # @!attribute [rw] lib_version
49
+ # The library version as recorded in instrumentation and logging
50
+ # @return [::String]
51
+ # @!attribute [rw] timeout
52
+ # The call timeout in seconds.
53
+ # @return [::Numeric]
54
+ #
55
+ class Configuration
56
+ extend ::Gapic::Config
57
+
58
+ config_attr :endpoint, <%= service.client_endpoint.inspect %>, ::String
59
+ config_attr :credentials, nil do |value|
60
+ allowed = [::String, ::Hash, ::Proc, ::Symbol, ::Google::Auth::Credentials, ::Signet::OAuth2::Client, nil]
61
+ allowed.any? { |klass| klass === value }
62
+ end
63
+ config_attr :scope, nil, ::String, ::Array, nil
64
+ config_attr :lib_name, nil, ::String, nil
65
+ config_attr :lib_version, nil, ::String, nil
66
+ config_attr :timeout, nil, ::Numeric, nil
67
+
68
+ # @private
69
+ def initialize parent_config = nil
70
+ @parent_config = parent_config unless parent_config.nil?
71
+
72
+ yield self if block_given?
73
+ end
74
+ end
@@ -0,0 +1 @@
1
+ <%- assert_locals service -%>
@@ -0,0 +1,18 @@
1
+ <%- assert_locals method -%>
2
+ ##
3
+ <%- if method.doc_description -%>
4
+ <%= indent method.doc_description, "# " %>
5
+ #
6
+ <%- end -%>
7
+ <%= render partial: "service/rest/client/method/docs/request", locals: { method: method } -%>
8
+ <%= render partial: "service/rest/client/method/docs/result", locals: { method: method } -%>
9
+ #
10
+ <%= render partial: "service/rest/client/method/docs/error", locals: { method: method } -%>
11
+ def <%= method.name %> request, options = nil
12
+ <%= indent render(partial: "service/client/method/def/request", locals: { method: method }), 2 %>
13
+
14
+ <%= indent render(partial: "service/rest/client/method/def/options_defaults", locals: { method: method }), 2 %>
15
+
16
+ <%= indent render(partial: "service/rest/client/method/def/response_normal", locals: { method: method }), 2 %>
17
+ <%= render partial: "service/rest/client/method/def/rescue", locals: { method: method } -%>
18
+ end
@@ -0,0 +1,14 @@
1
+ <%- assert_locals method -%>
2
+ # Converts hash and nil to an options object
3
+ options = ::Gapic::CallOptions.new(**options.to_h) if options.respond_to? :to_h
4
+
5
+ # Customize the options with defaults
6
+ call_metadata = {}
7
+
8
+ # Set x-goog-api-client header
9
+ call_metadata[:"x-goog-api-client"] ||= ::Gapic::Headers.x_goog_api_client \
10
+ lib_name: @config.lib_name, lib_version: @config.lib_version,
11
+ gapic_version: ::<%= method.service.gem.version_name_full %>
12
+
13
+ options.apply_defaults timeout: @config.timeout,
14
+ metadata: call_metadata
@@ -0,0 +1,3 @@
1
+ <%- assert_locals method -%>
2
+ rescue ::Faraday::Error => e
3
+ raise ::Gapic::Rest::Error.wrap_faraday_error e
@@ -0,0 +1,17 @@
1
+ <%- assert_locals method -%>
2
+
3
+ uri, <%= method.rest.body_var_name %>, <%= method.rest.query_string_params_var_name %> = <%= method.rest.transcoding_helper_name %> request
4
+ response = @client_stub.make_<%= method.rest.verb %>_request(
5
+ uri: uri,
6
+ <%- if method.rest.body? -%>
7
+ body: body,
8
+ <%- end -%>
9
+ <%- if method.rest.query_string_params? -%>
10
+ params: query_string_params,
11
+ <%- end -%>
12
+ options: options
13
+ )
14
+ result = <%= method.return_type %>.decode_json response.body, ignore_unknown_fields: true
15
+
16
+ yield result, response if block_given?
17
+ result
@@ -0,0 +1,2 @@
1
+ <%- assert_locals method -%>
2
+ # @raise [::Gapic::Rest::Error] if the REST call is aborted.