gapic-generator 0.7.1 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +47 -0
  3. data/lib/gapic/generator/version.rb +1 -1
  4. data/lib/gapic/generators/default_generator.rb +18 -5
  5. data/lib/gapic/generators/default_generator_parameters.rb +8 -4
  6. data/lib/gapic/presenters.rb +4 -0
  7. data/lib/gapic/presenters/field_presenter.rb +44 -0
  8. data/lib/gapic/presenters/gem_presenter.rb +55 -10
  9. data/lib/gapic/presenters/method_presenter.rb +40 -16
  10. data/lib/gapic/presenters/method_rest_presenter.rb +194 -0
  11. data/lib/gapic/presenters/package_presenter.rb +18 -0
  12. data/lib/gapic/presenters/service_config_presenter.rb +48 -0
  13. data/lib/gapic/presenters/service_presenter.rb +60 -4
  14. data/lib/gapic/presenters/service_rest_presenter.rb +139 -0
  15. data/lib/gapic/presenters/snippet_presenter.rb +103 -0
  16. data/lib/gapic/runner.rb +2 -1
  17. data/lib/gapic/schema/api.rb +58 -16
  18. data/lib/gapic/schema/request_param_parser.rb +46 -12
  19. data/lib/google/protobuf/any.pb.rb +1 -1
  20. data/lib/google/protobuf/compiler/plugin.pb.rb +9 -6
  21. data/lib/google/protobuf/descriptor.pb.rb +2 -2
  22. data/lib/google/protobuf/empty.pb.rb +1 -1
  23. data/templates/default/gem/readme.erb +3 -3
  24. data/templates/default/lib/_package.erb +11 -1
  25. data/templates/default/lib/_service.erb +31 -1
  26. data/templates/default/lib/rest/_rest.erb +11 -0
  27. data/templates/default/service/client/_client.erb +1 -1
  28. data/templates/default/service/client/_credentials.erb +2 -0
  29. data/templates/default/service/client/_operations.erb +1 -1
  30. data/templates/default/service/client/_self_configure_defaults.erb +2 -2
  31. data/templates/default/service/rest.erb +6 -0
  32. data/templates/default/service/rest/client.erb +6 -0
  33. data/templates/default/service/rest/client/_client.erb +115 -0
  34. data/templates/default/service/rest/client/_config.erb +74 -0
  35. data/templates/default/service/rest/client/_requires.erb +1 -0
  36. data/templates/default/service/rest/client/method/_def.erb +18 -0
  37. data/templates/default/service/rest/client/method/def/_options_defaults.erb +15 -0
  38. data/templates/default/service/rest/client/method/def/_rescue.erb +3 -0
  39. data/templates/default/service/rest/client/method/def/_response_normal.erb +17 -0
  40. data/templates/default/service/rest/client/method/docs/_error.erb +2 -0
  41. data/templates/default/service/rest/client/method/docs/_request.erb +27 -0
  42. data/templates/default/service/rest/client/method/docs/_result.erb +6 -0
  43. data/templates/default/service/rest/grpc_transcoding.erb +6 -0
  44. data/templates/default/service/rest/grpc_transcoding/_grpc_transcoding.erb +9 -0
  45. data/templates/default/service/rest/grpc_transcoding/method/_def.erb +21 -0
  46. data/templates/default/service/rest/grpc_transcoding/method/def/_query_string_param.erb +8 -0
  47. data/templates/default/service/rest/test/client.erb +18 -0
  48. data/templates/default/service/rest/test/method/_assert_response.erb +2 -0
  49. data/templates/default/service/rest/test/method/_configure.erb +19 -0
  50. data/templates/default/service/rest/test/method/_normal.erb +71 -0
  51. data/templates/default/service/rest/test/method/_setup.erb +38 -0
  52. data/templates/default/snippets/gemfile.erb +17 -0
  53. data/templates/default/snippets/snippet/_structure.erb +71 -0
  54. data/templates/default/snippets/standalone.erb +6 -0
  55. metadata +32 -4
  56. data/templates/default/service/client/_self_configure_retry_policy.erb +0 -15
@@ -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
@@ -104,7 +104,7 @@ class <%= service.client_name %>
104
104
  !@config.endpoint.split(".").first.include?("-")
105
105
  credentials ||= Credentials.default scope: @config.scope,
106
106
  enable_self_signed_jwt: enable_self_signed_jwt
107
- if credentials.is_a?(String) || credentials.is_a?(Hash)
107
+ if credentials.is_a?(::String) || credentials.is_a?(::Hash)
108
108
  credentials = Credentials.new credentials, scope: @config.scope
109
109
  end
110
110
  <%- end -%>
@@ -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
@@ -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 -%>
@@ -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,15 @@
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
+ transports_version_send: [:rest]
13
+
14
+ options.apply_defaults timeout: @config.timeout,
15
+ 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.
@@ -0,0 +1,27 @@
1
+ <%- assert_locals method -%>
2
+ # @overload <%= method.name %>(request, options = nil)
3
+ # Pass arguments to `<%= method.name %>` via a request object, either of type
4
+ # {<%= method.request_type %>} or an equivalent Hash.
5
+ #
6
+ # @param request [<%= method.request_type %>, ::Hash]
7
+ # A request object representing the call parameters. Required. To specify no
8
+ # parameters, or to keep all the default parameter values, pass an empty Hash.
9
+ # @param options [::Gapic::CallOptions, ::Hash]
10
+ # Overrides the default settings for this call, e.g, timeout, retries etc. Optional.
11
+ # Note: currently retry functionality is not implemented. While it is possible
12
+ # to set it using ::Gapic::CallOptions, it will not be applied
13
+ <%-if method.arguments.any?-%>
14
+ #
15
+ <%- arg_list = method.arguments.map { |arg| "#{arg.name}: nil"}.join ", " -%>
16
+ # @overload <%= method.name %>(<%= arg_list %>)
17
+ # Pass arguments to `<%= method.name %>` via keyword arguments. Note that at
18
+ # least one keyword argument is required. To specify no parameters, or to keep all
19
+ # the default parameter values, pass an empty Hash as a request object (see above).
20
+ #
21
+ <%- method.arguments.each do |arg| -%>
22
+ # @param <%= arg.name %> [<%= arg.doc_types %>]
23
+ <%- if arg.doc_description -%>
24
+ <%= indent arg.doc_description, "# " %>
25
+ <%- end -%>
26
+ <%- end -%>
27
+ <%- end -%>
@@ -0,0 +1,6 @@
1
+ <%- assert_locals method -%>
2
+ # @yield [result, env] Access the result along with the Faraday environment object
3
+ # @yieldparam result [<%= method.doc_response_type %>]
4
+ # @yieldparam response [::Faraday::Response]
5
+ #
6
+ # @return [<%= method.doc_response_type %>]
@@ -0,0 +1,6 @@
1
+ <%- assert_locals service -%>
2
+ <%= render partial: "service/rest/grpc_transcoding/grpc_transcoding",
3
+ layout: "layouts/ruby",
4
+ locals: { service: service,
5
+ namespace: service.rest.service_name_full }
6
+ %>
@@ -0,0 +1,9 @@
1
+ <%- assert_locals service -%>
2
+ # GRPC transcoding helper methods for the <%= service.name %> REST API.
3
+ module GrpcTranscoding
4
+ <%- service.methods.each do |method| -%>
5
+
6
+ <%= indent_tail render(partial: "service/rest/grpc_transcoding/method/def", locals: { method: method }), 2 %>
7
+ <%- end %>
8
+ extend self
9
+ end