gapic-generator 0.8.0 → 0.10.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +37 -0
  3. data/lib/gapic/generator/version.rb +1 -1
  4. data/lib/gapic/generators/default_generator.rb +1 -1
  5. data/lib/gapic/generators/default_generator_parameters.rb +3 -1
  6. data/lib/gapic/presenters/gem_presenter.rb +25 -6
  7. data/lib/gapic/presenters/method/rest_pagination_info.rb +246 -0
  8. data/lib/gapic/presenters/method_presenter.rb +17 -2
  9. data/lib/gapic/presenters/method_rest_presenter.rb +75 -2
  10. data/lib/gapic/presenters/resource_presenter.rb +8 -0
  11. data/lib/gapic/presenters/service_config_presenter.rb +48 -0
  12. data/lib/gapic/presenters/service_presenter.rb +69 -0
  13. data/lib/gapic/presenters/service_rest_presenter.rb +42 -16
  14. data/lib/gapic/presenters/snippet_presenter.rb +1 -1
  15. data/lib/gapic/presenters.rb +1 -0
  16. data/lib/gapic/schema/api.rb +17 -7
  17. data/lib/gapic/schema/wrappers.rb +26 -0
  18. data/templates/default/gem/readme.erb +3 -3
  19. data/templates/default/lib/rest/_rest.erb +0 -2
  20. data/templates/default/service/client/_client.erb +18 -20
  21. data/templates/default/service/client/_config.erb +13 -14
  22. data/templates/default/service/client/_credentials.erb +2 -0
  23. data/templates/default/service/client/_operations.erb +1 -1
  24. data/templates/default/service/client/_paths.erb +1 -1
  25. data/templates/default/service/client/_self_configure_defaults.erb +2 -2
  26. data/templates/default/service/client/method/_def.erb +2 -0
  27. data/templates/default/service/client/method/def/_options_defaults.erb +3 -1
  28. data/templates/default/service/client/method/docs/_deprecated.erb +5 -0
  29. data/templates/default/service/client/method/docs/_snippets.erb +6 -0
  30. data/templates/default/service/rest/client/_client.erb +15 -24
  31. data/templates/default/service/rest/client/_config.erb +48 -0
  32. data/templates/default/service/rest/client/method/_def.erb +1 -1
  33. data/templates/default/service/rest/client/method/def/_options_defaults.erb +5 -2
  34. data/templates/default/service/rest/client/method/def/_response.erb +8 -0
  35. data/templates/default/service/rest/client/method/def/_response_lro.erb +7 -0
  36. data/templates/default/service/rest/client/method/def/_response_normal.erb +4 -15
  37. data/templates/default/service/rest/client/method/def/_response_paged.erb +7 -0
  38. data/templates/default/service/rest/client/method/docs/_result.erb +3 -3
  39. data/templates/default/service/rest/service_stub/_service_stub.erb +25 -0
  40. data/templates/default/service/rest/{grpc_transcoding/method → service_stub/grpc_transcoding_method}/_def.erb +4 -1
  41. data/templates/default/service/rest/{grpc_transcoding/method → service_stub/grpc_transcoding_method}/def/_query_string_param.erb +0 -0
  42. data/templates/default/service/rest/service_stub/method/_def.erb +20 -0
  43. data/templates/default/service/rest/service_stub/method/def/_request.erb +2 -0
  44. data/templates/default/service/rest/service_stub/method/def/_response.erb +17 -0
  45. data/templates/default/service/rest/service_stub.erb +6 -0
  46. data/templates/default/service/rest/test/client.erb +1 -1
  47. data/templates/default/service/test/client_paths.erb +1 -1
  48. metadata +17 -8
  49. data/templates/default/service/client/_self_configure_retry_policy.erb +0 -15
  50. data/templates/default/service/rest/client/_requires.erb +0 -1
  51. data/templates/default/service/rest/grpc_transcoding/_grpc_transcoding.erb +0 -9
@@ -14,22 +14,21 @@
14
14
  # on construction.
15
15
  #
16
16
  <%- unless method_service.methods.empty? -%>
17
- # # Examples
17
+ # @example
18
18
  #
19
- # To modify the global config, setting the timeout for <%= method_service.methods.first.name %>
20
- # to 20 seconds, and all remaining timeouts to 10 seconds:
19
+ # # Modify the global config, setting the timeout for
20
+ # # <%= method_service.methods.first.name %> to 20 seconds,
21
+ # # and all remaining timeouts to 10 seconds.
22
+ # <%= service.client_name_full %>.configure do |config|
23
+ # config.timeout = 10.0
24
+ # config.rpcs.<%= method_service.methods.first.name %>.timeout = 20.0
25
+ # end
21
26
  #
22
- # <%= service.client_name_full %>.configure do |config|
23
- # config.timeout = 10.0
24
- # config.rpcs.<%= method_service.methods.first.name %>.timeout = 20.0
25
- # end
26
- #
27
- # To apply the above configuration only to a new client:
28
- #
29
- # client = <%= service.client_name_full %>.new do |config|
30
- # config.timeout = 10.0
31
- # config.rpcs.<%= method_service.methods.first.name %>.timeout = 20.0
32
- # end
27
+ # # Apply the above configuration only to a new client.
28
+ # client = <%= service.client_name_full %>.new do |config|
29
+ # config.timeout = 10.0
30
+ # config.rpcs.<%= method_service.methods.first.name %>.timeout = 20.0
31
+ # end
33
32
  #
34
33
  <%- end -%>
35
34
  # @!attribute [rw] endpoint
@@ -12,10 +12,12 @@ class <%= service.credentials_name %> < ::Google::Auth::Credentials
12
12
  <%- end -%>
13
13
  ]
14
14
  <%- end -%>
15
+ <%- if service.gem.env_prefix -%>
15
16
  self.env_vars = [
16
17
  "<%= service.gem.env_prefix %>_CREDENTIALS",
17
18
  "<%= service.gem.env_prefix %>_KEYFILE",
18
19
  "<%= service.gem.env_prefix %>_CREDENTIALS_JSON",
19
20
  "<%= service.gem.env_prefix %>_KEYFILE_JSON"
20
21
  ]
22
+ <%- end -%>
21
23
  end
@@ -61,7 +61,7 @@ class <%= service.operations_name %>
61
61
  # Create credentials
62
62
  credentials = @config.credentials
63
63
  credentials ||= Credentials.default scope: @config.scope
64
- if credentials.is_a?(String) || credentials.is_a?(Hash)
64
+ if credentials.is_a?(::String) || credentials.is_a?(::Hash)
65
65
  credentials = Credentials.new credentials, scope: @config.scope
66
66
  end
67
67
  @quota_project_id = @config.quota_project
@@ -1,7 +1,7 @@
1
1
  <%- assert_locals service -%>
2
2
  # Path helper methods for the <%= service.name %> API.
3
3
  module Paths
4
- <%- service.references.each do |resource| -%>
4
+ <%- service.deduped_references.each do |resource| -%>
5
5
  <%= indent render(partial: "service/client/resource", locals: { resource: resource }), 2 %>
6
6
 
7
7
  <%- end %>
@@ -6,7 +6,7 @@
6
6
  default_config.timeout = <%= format_number service.grpc_service_config.timeout_seconds %>
7
7
  <%- end -%>
8
8
  <%- if service.grpc_service_config.retry_policy -%>
9
- default_config.retry_policy = <%= indent_tail render(partial: "service/client/self_configure_retry_policy", locals: { retry_policy: service.grpc_service_config.retry_policy }), 2 %>
9
+ default_config.retry_policy = <%= indent_tail service.service_config_presenter.retry_policy_fields, 2 %>
10
10
  <%- end -%>
11
11
  <%- end -%>
12
12
  <%- method_service.methods.each do |method| -%>
@@ -16,7 +16,7 @@
16
16
  default_config.rpcs.<%= method.name %>.timeout = <%= format_number method.grpc_service_config.timeout_seconds %>
17
17
  <%- end -%>
18
18
  <%- if method.grpc_service_config.retry_policy -%>
19
- default_config.rpcs.<%= method.name %>.retry_policy =<%= indent_tail render(partial: "service/client/self_configure_retry_policy", locals: { retry_policy: method.grpc_service_config.retry_policy }), 2 %>
19
+ default_config.rpcs.<%= method.name %>.retry_policy =<%= indent_tail method.service_config_presenter.retry_policy_fields, 2 %>
20
20
  <%- end -%>
21
21
  <%- end -%>
22
22
  <%- end -%>
@@ -4,12 +4,14 @@
4
4
  <%= indent method.doc_description, "# " %>
5
5
  #
6
6
  <%- end -%>
7
+ <%= render partial: "service/client/method/docs/deprecated", locals: { method: method } -%>
7
8
  <%= render partial: "service/client/method/docs/request", locals: { method: method } -%>
8
9
  #
9
10
  <%= render partial: "service/client/method/docs/response", locals: { method: method } -%>
10
11
  #
11
12
  <%= render partial: "service/client/method/docs/error", locals: { method: method } -%>
12
13
  #
14
+ <%= render partial: "service/client/method/docs/snippets", locals: { method: method } -%>
13
15
  <%= render partial: "service/client/method/docs/samples", locals: { method: method } -%>
14
16
  def <%= method.name %> request, options = nil
15
17
  <%= indent render(partial: "service/client/method/def/request", locals: { method: method }), 2 %>
@@ -25,5 +25,7 @@ metadata[:"x-goog-request-params"] ||= request_params_header
25
25
  options.apply_defaults timeout: @config.rpcs.<%= method.name %>.timeout,
26
26
  metadata: metadata,
27
27
  retry_policy: @config.rpcs.<%= method.name %>.retry_policy
28
- options.apply_defaults metadata: @config.metadata,
28
+
29
+ options.apply_defaults timeout: @config.timeout,
30
+ metadata: @config.metadata,
29
31
  retry_policy: @config.retry_policy
@@ -0,0 +1,5 @@
1
+ <%- assert_locals method -%>
2
+ <%- if method.is_deprecated? -%>
3
+ # @deprecated This method is deprecated and may be removed in the next major version update.
4
+ #
5
+ <%- end -%>
@@ -0,0 +1,6 @@
1
+ <%- assert_locals method -%>
2
+ <%- if method.generate_yardoc_snippets? -%>
3
+ # @example Basic example
4
+ <%= indent render(partial: "snippets/snippet/structure", locals: { snippet: method.snippet }), "# " %>
5
+ #
6
+ <%- end -%>
@@ -2,6 +2,7 @@
2
2
  <% @requires = capture do %>
3
3
  <%= render partial: "service/client/requires", locals: { service: service} -%>
4
4
  require "<%= service.proto_service_require %>"
5
+ require "<%= service.rest.service_stub_require %>"
5
6
  <% end %>
6
7
  ##
7
8
  # REST client for the <%= service.name %> service.
@@ -11,7 +12,6 @@
11
12
  #
12
13
  <%- end -%>
13
14
  class <%= service.rest.client_name %>
14
- include <%= service.rest.transcoding_helper_name %>
15
15
  <%- if service.paths? -%>
16
16
  include <%= service.paths_name %>
17
17
  <%- end -%>
@@ -25,13 +25,12 @@ class <%= service.rest.client_name %>
25
25
  # See {<%= service.rest.client_name_full %>::Configuration}
26
26
  # for a description of the configuration fields.
27
27
  #
28
- # ## Example
28
+ # @example
29
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
30
+ # # Modify the configuration for all <%= service.name %> clients
31
+ # <%= service.rest.client_name_full %>.configure do |config|
32
+ # config.timeout = 10.0
33
+ # end
35
34
  #
36
35
  # @yield [config] Configure the <%= service.rest.client_name %> client.
37
36
  # @yieldparam config [<%= service.rest.client_name %>::Configuration]
@@ -65,28 +64,20 @@ class <%= service.rest.client_name %>
65
64
  ##
66
65
  # Create a new <%= service.name %> REST client object.
67
66
  #
68
- # ## Examples
69
- #
70
- # To create a new <%= service.name %> REST client with the default
71
- # configuration:
67
+ # @example
72
68
  #
73
- # client = <%= service.rest.client_name_full %>.new
69
+ # # Create a client using the default configuration
70
+ # client = <%= service.rest.client_name_full %>.new
74
71
  #
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
72
+ # # Create a client using a custom configuration
73
+ # client = <%= service.rest.client_name_full %>.new do |config|
74
+ # config.timeout = 10.0
75
+ # end
81
76
  #
82
77
  # @yield [config] Configure the <%= service.name %> client.
83
78
  # @yieldparam config [<%= service.rest.client_name %>::Configuration]
84
79
  #
85
80
  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
81
  # Create the configuration object
91
82
  @config = Configuration.new <%= service.rest.client_name %>.configure
92
83
 
@@ -97,12 +88,12 @@ class <%= service.rest.client_name %>
97
88
  credentials = @config.credentials
98
89
  <%- unless service.generic_endpoint? -%>
99
90
  credentials ||= Credentials.default scope: @config.scope
100
- if credentials.is_a?(String) || credentials.is_a?(Hash)
91
+ if credentials.is_a?(::String) || credentials.is_a?(::Hash)
101
92
  credentials = Credentials.new credentials, scope: @config.scope
102
93
  end
103
94
  <%- end -%>
104
95
 
105
- @client_stub = ::Gapic::Rest::ClientStub.new endpoint: @config.endpoint, credentials: credentials
96
+ @<%= service.stub_name %> = <%= service.rest.service_stub_name_full %>.new endpoint: @config.endpoint, credentials: credentials
106
97
  end
107
98
 
108
99
  # Service calls
@@ -51,6 +51,9 @@
51
51
  # @!attribute [rw] timeout
52
52
  # The call timeout in seconds.
53
53
  # @return [::Numeric]
54
+ # @!attribute [rw] metadata
55
+ # Additional REST headers to be sent with the call.
56
+ # @return [::Hash{::Symbol=>::String}]
54
57
  #
55
58
  class Configuration
56
59
  extend ::Gapic::Config
@@ -64,6 +67,7 @@ class Configuration
64
67
  config_attr :lib_name, nil, ::String, nil
65
68
  config_attr :lib_version, nil, ::String, nil
66
69
  config_attr :timeout, nil, ::Numeric, nil
70
+ config_attr :metadata, nil, ::Hash, nil
67
71
 
68
72
  # @private
69
73
  def initialize parent_config = nil
@@ -71,4 +75,48 @@ class Configuration
71
75
 
72
76
  yield self if block_given?
73
77
  end
78
+
79
+ ##
80
+ # Configurations for individual RPCs
81
+ # @return [Rpcs]
82
+ #
83
+ def rpcs
84
+ @rpcs ||= begin
85
+ parent_rpcs = nil
86
+ parent_rpcs = @parent_config.rpcs if defined?(@parent_config) && @parent_config.respond_to?(:rpcs)
87
+ Rpcs.new parent_rpcs
88
+ end
89
+ end
90
+
91
+ ##
92
+ # Configuration RPC class for the <%= service.name %> API.
93
+ #
94
+ # Includes fields providing the configuration for each RPC in this service.
95
+ # Each configuration object is of type `Gapic::Config::Method` and includes
96
+ # the following configuration fields:
97
+ #
98
+ # * `timeout` (*type:* `Numeric`) - The call timeout in seconds
99
+ #
100
+ # there is one other field (`retry_policy`) that can be set
101
+ # but is currently not supported for REST Gapic libraries.
102
+ #
103
+ class Rpcs
104
+ <%- method_service.methods.each do |method| -%>
105
+ ##
106
+ # RPC-specific configuration for `<%= method.name %>`
107
+ # @return [::Gapic::Config::Method]
108
+ #
109
+ attr_reader :<%= method.name %>
110
+ <%- end -%>
111
+
112
+ # @private
113
+ def initialize parent_rpcs = nil
114
+ <%- method_service.methods.each do |method| -%>
115
+ <%= method.name %>_config = parent_rpcs.<%= method.name %> if parent_rpcs.respond_to? :<%= method.name %>
116
+ @<%= method.name %> = ::Gapic::Config::Method.new <%= method.name %>_config
117
+ <%- end -%>
118
+
119
+ yield self if block_given?
120
+ end
121
+ end
74
122
  end
@@ -13,6 +13,6 @@ def <%= method.name %> request, options = nil
13
13
 
14
14
  <%= indent render(partial: "service/rest/client/method/def/options_defaults", locals: { method: method }), 2 %>
15
15
 
16
- <%= indent render(partial: "service/rest/client/method/def/response_normal", locals: { method: method }), 2 %>
16
+ <%= indent render(partial: "service/rest/client/method/def/response", locals: { method: method }), 2 %>
17
17
  <%= render partial: "service/rest/client/method/def/rescue", locals: { method: method } -%>
18
18
  end
@@ -3,7 +3,7 @@
3
3
  options = ::Gapic::CallOptions.new(**options.to_h) if options.respond_to? :to_h
4
4
 
5
5
  # Customize the options with defaults
6
- call_metadata = {}
6
+ call_metadata = @config.rpcs.<%= method.name %>.metadata.to_h
7
7
 
8
8
  # Set x-goog-api-client header
9
9
  call_metadata[:"x-goog-api-client"] ||= ::Gapic::Headers.x_goog_api_client \
@@ -11,5 +11,8 @@ call_metadata[:"x-goog-api-client"] ||= ::Gapic::Headers.x_goog_api_client \
11
11
  gapic_version: ::<%= method.service.gem.version_name_full %>,
12
12
  transports_version_send: [:rest]
13
13
 
14
- options.apply_defaults timeout: @config.timeout,
14
+ options.apply_defaults timeout: @config.rpcs.<%= method.name %>.timeout,
15
15
  metadata: call_metadata
16
+
17
+ options.apply_defaults timeout: @config.timeout,
18
+ metadata: @config.metadata
@@ -0,0 +1,8 @@
1
+ <%- assert_locals method -%>
2
+ <%- if method.rest.paged? -%>
3
+ <%= render partial: "service/rest/client/method/def/response_paged", locals: { method: method } -%>
4
+ <%- elsif method.rest.lro? -%>
5
+ <%= render partial: "service/rest/client/method/def/response_lro", locals: { method: method } -%>
6
+ <%- else -%>
7
+ <%= render partial: "service/rest/client/method/def/response_normal", locals: { method: method } -%>
8
+ <%- end -%>
@@ -0,0 +1,7 @@
1
+ <%- assert_locals method -%>
2
+
3
+ @<%= method.service.stub_name %>.<%= method.name %> request, options do |result, response|
4
+ result = ::Gapic::Rest::BaseOperation.new result
5
+ yield result, response if block_given?
6
+ return result
7
+ end
@@ -1,17 +1,6 @@
1
1
  <%- assert_locals method -%>
2
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
3
+ @<%= method.service.stub_name %>.<%= method.name %> request, options do |result, response|
4
+ yield result, response if block_given?
5
+ return result
6
+ end
@@ -0,0 +1,7 @@
1
+ <%- assert_locals method -%>
2
+
3
+ @<%= method.service.stub_name %>.<%= method.name %> request, options do |result, response|
4
+ result = ::Gapic::Rest::PagedEnumerable.new @<%= method.service.stub_name %>, :<%= method.name %>, "<%= method.rest.pagination.response_repeated_field_name %>", request, result, options
5
+ yield result, response if block_given?
6
+ return result
7
+ end
@@ -1,6 +1,6 @@
1
1
  <%- assert_locals method -%>
2
- # @yield [result, env] Access the result along with the Faraday environment object
3
- # @yieldparam result [<%= method.doc_response_type %>]
2
+ # @yield [result, response] Access the result along with the Faraday response object
3
+ # @yieldparam result [<%= method.rest.doc_response_type %>]
4
4
  # @yieldparam response [::Faraday::Response]
5
5
  #
6
- # @return [<%= method.doc_response_type %>]
6
+ # @return [<%= method.rest.doc_response_type %>]
@@ -0,0 +1,25 @@
1
+ <%- assert_locals service -%>
2
+ <% @requires = capture do %>
3
+ require "<%= service.proto_service_require %>"
4
+ <% end %>
5
+ ##
6
+ # REST service stub for the <%= service.name %> service.
7
+ # service stub contains baseline method implementations
8
+ # including transcoding, making the REST call and deserialing the response
9
+ #
10
+ class <%= service.rest.service_stub_name %>
11
+ def initialize endpoint:, credentials:
12
+ # These require statements are intentionally placed here to initialize
13
+ # the REST modules only when it's required.
14
+ require "gapic/rest"
15
+
16
+ @client_stub = ::Gapic::Rest::ClientStub.new endpoint: endpoint, credentials: credentials
17
+ end
18
+
19
+ <%- service.methods.each do |method| -%>
20
+
21
+ <%= indent_tail render(partial: "service/rest/service_stub/method/def", locals: { method: method }), 2 %>
22
+
23
+ <%= indent_tail render(partial: "service/rest/service_stub/grpc_transcoding_method/def", locals: { method: method }), 2 %>
24
+ <%- end %>
25
+ end
@@ -1,4 +1,7 @@
1
1
  <%- assert_locals method -%>
2
+ ##
3
+ # GRPC transcoding helper method for the <%= method.name %> REST call
4
+ #
2
5
  # @param request_pb [<%= method.request_type %>]
3
6
  # A request object representing the call parameters. Required.
4
7
  # @return [Array(String, [String, nil], Hash{String => String})]
@@ -13,7 +16,7 @@ def <%= method.rest.transcoding_helper_name %> request_pb
13
16
  query_string_params = {}
14
17
  <%- if method.rest.query_string_params? -%>
15
18
  <%- method.rest.query_string_params.each do |field| -%>
16
- <%= render partial: "service/rest/grpc_transcoding/method/def/query_string_param", locals: { field: field } -%>
19
+ <%= render partial: "service/rest/service_stub/grpc_transcoding_method/def/query_string_param", locals: { field: field } -%>
17
20
  <%- end -%>
18
21
  <%- end -%>
19
22
 
@@ -0,0 +1,20 @@
1
+ <%- assert_locals method -%>
2
+ ##
3
+ # Baseline implementation for the <%= method.name %> REST call
4
+ #
5
+ # @param request_pb [<%= method.rest.request_type %>]
6
+ # A request object representing the call parameters. Required.
7
+ # @param options [::Gapic::CallOptions]
8
+ # Overrides the default settings for this call, e.g, timeout, retries etc. Optional.
9
+ #
10
+ # @yield [result, response] Access the result along with the Faraday response object
11
+ # @yieldparam result [<%= method.rest.return_type %>]
12
+ # @yieldparam response [::Faraday::Response]
13
+ #
14
+ # @return [<%= method.rest.return_type %>]
15
+ # A result object deserialized from the server's reply
16
+ def <%= method.name %> request_pb, options = nil
17
+ <%= indent render(partial: "service/rest/service_stub/method/def/request", locals: { method: method }), 2 %>
18
+
19
+ <%= indent render(partial: "service/rest/service_stub/method/def/response", locals: { method: method }), 2 %>
20
+ end
@@ -0,0 +1,2 @@
1
+ <%- assert_locals method -%>
2
+ raise ::ArgumentError, "request must be provided" if request_pb.nil?