-
Notifications
You must be signed in to change notification settings - Fork 397
Transports: DRY HTTP Transport & Client classes #5099
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
Merged
+364
−433
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative 'http/client' | ||
|
|
||
| module Datadog | ||
| module Core | ||
| module Transport | ||
| # Raised when configured with an unknown API version | ||
| class UnknownApiVersionError < StandardError | ||
| attr_reader :version | ||
|
|
||
| def initialize(version) | ||
| super | ||
|
|
||
| @version = version | ||
| end | ||
|
|
||
| def message | ||
| "No matching transport API for version #{version}!" | ||
| end | ||
| end | ||
|
|
||
| # Raised when the API verson to downgrade to does not map to a | ||
| # defined API. | ||
| class NoDowngradeAvailableError < StandardError | ||
| attr_reader :version | ||
|
|
||
| def initialize(version) | ||
| super | ||
|
|
||
| @version = version | ||
| end | ||
|
|
||
| def message | ||
| "No downgrade from transport API version #{version} is available!" | ||
| end | ||
| end | ||
p-datadog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Base class for transports. | ||
| class Transport | ||
| attr_reader :client, :apis, :default_api, :current_api_id, :logger | ||
p-datadog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| class << self | ||
| # The HTTP client class to use for requests, derived from | ||
| # Core::Transport::HTTP::Client. | ||
| # | ||
| # Important: this attribute is NOT inherited by derived classes - | ||
| # it must be set by every Transport class that wants to have a | ||
| # non-default HTTP::Client instance. | ||
| attr_accessor :http_client_class | ||
| end | ||
p-datadog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def initialize(apis, default_api, logger:) | ||
| @apis = apis | ||
| @default_api = default_api | ||
| @logger = logger | ||
|
|
||
| set_api!(default_api) | ||
| end | ||
|
|
||
| def current_api | ||
| apis[current_api_id] | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def set_api!(api_id) | ||
| raise UnknownApiVersionError, api_id unless apis.key?(api_id) | ||
|
|
||
| @current_api_id = api_id | ||
| client_class = self.class.http_client_class || Core::Transport::HTTP::Client | ||
| @client = client_class.new(current_api, logger: logger) | ||
| end | ||
|
|
||
| def downgrade?(response) | ||
| return false unless apis.fallbacks.key?(current_api_id) | ||
|
|
||
| response.not_found? || response.unsupported? | ||
| end | ||
|
|
||
| def downgrade! | ||
| downgrade_api_id = apis.fallbacks[current_api_id] | ||
| raise NoDowngradeAvailableError, current_api_id if downgrade_api_id.nil? | ||
|
|
||
| set_api!(downgrade_api_id) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.