-
Notifications
You must be signed in to change notification settings - Fork 33
feat: new overridable routing headers implementation #910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a93ac58
61651af
e562e97
077ba32
e634eb0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Copyright 2023 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| require "gapic/routing_headers/headers_extractor" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # Copyright 2023 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| module Gapic | ||
| module RoutingHeaders | ||
| class HeadersExtractor | ||
| ## | ||
| # @private | ||
| class HeaderBinding | ||
| attr_reader :field | ||
| attr_reader :header_name | ||
| attr_reader :regex | ||
|
|
||
| def initialize field, header_name=nil, regex=nil | ||
| @field = field | ||
| @header_name = header_name | ||
| @regex = regex | ||
| end | ||
|
|
||
| ## | ||
| # @private | ||
| # Creates a new HeaderBinding. | ||
| # | ||
| def self.create field:, header_name: nil, regex: nil | ||
| HeaderBinding.new field, header_name, regex | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| # Copyright 2023 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| require "gapic/routing_headers/header_binding" | ||
|
|
||
| module Gapic | ||
| module RoutingHeaders | ||
| class HeadersExtractor | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless this class is
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will be |
||
| attr_accessor :bindings | ||
|
|
||
| def initialize bindings = nil | ||
| @bindings = bindings || [] | ||
| end | ||
|
|
||
| def with_bindings(field:, header_name: nil, regex: nil) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be okay without the parens
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| binding = HeaderBinding.create(field: field, header_name: header_name, regex: regex) | ||
| HeadersExtractor.new @bindings + [binding] | ||
| end | ||
|
|
||
| def extract_headers request | ||
| header_params = {} | ||
|
|
||
| unless request | ||
| err_msg = "Incorrect header extraction request: request is nil." | ||
| raise ::Gapic::Common::Error, err_msg | ||
| end | ||
|
|
||
| @bindings.each do |header_binding| | ||
| field_value = get_field_val request, header_binding.field | ||
| next unless field_value | ||
|
|
||
| if header_binding.regex && !field_value.is_a?(::String) | ||
| err_msg = "Header binding configuration is incorrect: regex" \ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you probably want a space after
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| "is given with a non-string field #{field}.\n" \ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you mean
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, good catch |
||
| "When the regex is present the field in the regex must be a ::String." | ||
| raise ::Gapic::Common::Error, err_msg | ||
| end | ||
|
|
||
| header_name = header_binding.header_name || header_binding.field | ||
|
|
||
| header_value = if header_binding.regex | ||
| match = header_binding.regex.match field_value | ||
| match[1] if match | ||
| else | ||
| field_value.to_s | ||
| end | ||
|
|
||
| header_params[header_name] = header_value if header_value && !header_value.empty? | ||
| end | ||
|
|
||
| header_params | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def get_field_val request, field | ||
| field_path = field.split "." | ||
|
|
||
| curr_submessage = request | ||
| field_path.each do |curr_field| | ||
| return nil unless curr_submessage.respond_to? curr_field | ||
| curr_submessage = curr_submessage.send curr_field | ||
| end | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A bit more idiomatic would be to use final_submessage = field_path.reduce request do |curr_submessage, curr_field|
return nil unless curr_submessage.respond_to? curr_field
curr_submessage.send curr_field
endFollowed by: final_submessage&.to_s
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
|
||
| return curr_submessage.to_s if curr_submessage | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can probably replace this line with simply
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| end | ||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason we're creating this separate factory method rather than just writing the constructor with this signature?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Symmetry with the routing headers extractor/binding