-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor API classes and errors (#29)
* Update base api class name * Cleanup API errors
- Loading branch information
Showing
12 changed files
with
107 additions
and
120 deletions.
There are no files selected for viewing
This file contains 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 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,75 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'concerns/requestable' | ||
require_relative 'concerns/validatable' | ||
|
||
module Anthropic | ||
module Api | ||
# Error for when a beta feature is configured incorrectly. | ||
class InvalidBetaConfigurationError < StandardError; end | ||
|
||
# Error for when API version is missing a schema. | ||
class MissingSchemaError < StandardError; end | ||
|
||
# Error for when the provided params do not match the API schema | ||
class SchemaValidationError < StandardError; end | ||
|
||
# Error for when the API version is not supported. | ||
class UnsupportedApiVersionError < StandardError; end | ||
|
||
# Error for when a beta feature is not used correctly. | ||
class UnsupportedBetaUseError < StandardError; end | ||
|
||
# Error for when the provided beta is not supported. | ||
class UnsupportedBetaError < StandardError; end | ||
|
||
## | ||
# Provides a base class for APIs | ||
class Base | ||
include Anthropic::Api::Concerns::Requestable | ||
include Anthropic::Api::Concerns::Validatable | ||
|
||
def initialize(beta: nil) | ||
@beta = beta | ||
end | ||
|
||
private | ||
|
||
attr_reader :beta | ||
|
||
def api | ||
self.class.name.split('::').last.downcase | ||
end | ||
|
||
def version_config | ||
return @version_config if defined?(@version_config) | ||
|
||
@version_config ||= catch(:version_found) do | ||
found_config = Anthropic.versions[api.to_sym].find { |config| config['version'] == Anthropic.api_version } | ||
unless found_config | ||
raise Anthropic::Api::UnsupportedApiVersionError, "Unsupported API version: #{Anthropic.api_version}" | ||
end | ||
|
||
throw :version_found, found_config | ||
end | ||
end | ||
|
||
def beta_config | ||
return @beta_config if defined?(@beta_config) | ||
|
||
@beta_config = catch(:beta_found) do | ||
found_config = Anthropic.betas.find { |config| config['id'] == beta } | ||
raise Anthropic::Api::UnsupportedBetaError, "#{beta} not supported" unless found_config | ||
|
||
throw :beta_found, found_config | ||
end | ||
end | ||
|
||
def beta_loaded?(name) | ||
return false unless beta | ||
|
||
beta_config['id'] == name | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains 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 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 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 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 was deleted.
Oops, something went wrong.
This file contains 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 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 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,18 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.shared_examples 'validates the params against the specified API version' do | ||
context 'with invalid params' do | ||
let(:params) { { model: 'foo' } } | ||
|
||
it 'raises an Anthropic::Api:SchemaValidationError' do | ||
expect { call_method }.to raise_error(Anthropic::Api::SchemaValidationError) | ||
end | ||
end | ||
|
||
context 'with invalid API version configured' do | ||
it 'raises an Anthropic::Api::UnsupportedApiVersionError' do | ||
allow(Anthropic).to receive(:api_version).and_return('2023-06-02') | ||
expect { call_method }.to raise_error(Anthropic::Api::UnsupportedApiVersionError) | ||
end | ||
end | ||
end |