Skip to content

Commit

Permalink
Decaf API (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
jterapin authored Jan 6, 2025
1 parent ef9937b commit e528bef
Show file tree
Hide file tree
Showing 32 changed files with 1,421 additions and 269 deletions.
6 changes: 3 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ Metrics/BlockLength:
Metrics/ClassLength:
Max: 200

Metrics/MethodLength:
Max: 20

Metrics/ModuleLength:
Exclude:
- '**/spec/**/*.rb'

Metrics/MethodLength:
Max: 20

Naming/FileName:
Exclude:
- '**/spec/**/*.rb'
Expand Down
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@ local build using smithy cli
bundle exec smithy build --debug model/weather.smithy
```

local build using smithy-ruby executable
local build using smithy-ruby executable:
```
export SMITHY_PLUGIN_DIR=build/smithy/source/smithy-ruby
bundle exec smithy-ruby smith client --gem-name weather --gem-version 1.0.0 --destination-root projections/weather <<< $(smithy ast model/weather.smithy)
```

IRB on weather gem
IRB on `weather` gem:
```
irb -I build/smithy/weather/smithy-ruby/lib -I gems/smithy-client/lib -r weather
irb -I projections/weather/lib -I gems/smithy-client/lib -r weather
```

Create a Weather client:
```
client = Weather::Client.new(endpoint: 'https://example.com')
client.get_current_time
```

Build a fixture
Expand Down
4 changes: 2 additions & 2 deletions gems/smithy-client/lib/smithy-client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@

# model

require_relative 'smithy-client/api'
require_relative 'smithy-client/base'
require_relative 'smithy-client/errors'
require_relative 'smithy-client/operation'
require_relative 'smithy-client/schema'
require_relative 'smithy-client/shapes'
require_relative 'smithy-client/structure'

# endpoints
Expand Down
26 changes: 13 additions & 13 deletions gems/smithy-client/lib/smithy-client/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def build_input(operation_name, params = {})
# names. These are valid arguments to {#build_input} and are also
# valid methods.
def operation_names
self.class.api.operation_names
self.class.schema.operation_names
end

# @api private
Expand All @@ -58,12 +58,12 @@ def inspect
# opportunity to register options with default values.
def build_config(plugins, options)
config = Configuration.new
config.add_option(:api)
config.add_option(:schema)
config.add_option(:plugins)
plugins.each do |plugin|
plugin.add_options(config) if plugin.respond_to?(:add_options)
end
config.build!(options.merge(api: self.class.api))
config.build!(options.merge(schema: self.class.schema))
end

# Gives each plugin the opportunity to register handlers for this client.
Expand All @@ -84,7 +84,7 @@ def after_initialize(plugins)
def context_for(operation_name, params)
HandlerContext.new(
operation_name: operation_name,
operation: config.api.operation(operation_name),
operation: config.schema.operation(operation_name),
client: self,
params: params,
config: config
Expand Down Expand Up @@ -162,24 +162,24 @@ def plugins
Array(@plugins).freeze
end

# @return [API]
def api
@api ||= API.new
# @return [Schema]
def schema
@schema ||= Schema.new
end

# @param [API] api
def api=(api)
@api = api
# @param [Schema] schema
def schema=(schema)
@schema = schema
define_operation_methods
end

# @option options [API] :api (API.new)
# @option options [Schema] :schema (Schema.new)
# @option options [Array<Plugin>] :plugins ([]) A list of plugins to
# add to the client class created.
# @return [Class<Client::Base>]
def define(options = {})
subclass = Class.new(self)
subclass.api = options[:api] || api
subclass.schema = options[:schema] || schema
Array(options[:plugins]).each do |plugin|
subclass.add_plugin(plugin)
end
Expand All @@ -191,7 +191,7 @@ def define(options = {})

def define_operation_methods
operations_module = Module.new
@api.operation_names.each do |method_name|
@schema.operation_names.each do |method_name|
operations_module.send(:define_method, method_name) do |*args, &block|
params = args[0] || {}
options = args[1] || {}
Expand Down
4 changes: 2 additions & 2 deletions gems/smithy-client/lib/smithy-client/handler_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Client
# Context that is passed to handlers during execution.
class HandlerContext
# @option options [Symbol] :operation_name (nil)
# @option options [Operation] :operation (nil)
# @option options [OperationShape] :operation (nil)
# @option options [Base] :client (nil)
# @option options [Hash] :params ({})
# @option options [Configuration] :config (nil)
Expand All @@ -26,7 +26,7 @@ def initialize(options = {})
# @return [Symbol] Name of the API operation called.
attr_accessor :operation_name

# @return [Operation]
# @return [OperationShape] Shape of the Operation called.
attr_accessor :operation

# @return [Base]
Expand Down
25 changes: 0 additions & 25 deletions gems/smithy-client/lib/smithy-client/operation.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,38 @@
module Smithy
module Client
# @api private
class API
class Schema
include Enumerable

def initialize
@metadata = {}
@service = nil
@operations = {}
yield self if block_given?
end

# @return [String, nil]
attr_accessor :version
# @return [ServiceShape, nil]
attr_accessor :service

# @return [Hash]
attr_accessor :metadata
# @return [Hash<Symbol, OperationShape>]
attr_accessor :operations

# @return [OperationShape]
def add_operation(name, operation)
@operations[name] = operation
end

# @return [Hash<Symbol, OperationShape>]
def each(&)
@operations.each(&)
end

# @return [Array<Symbol>]
def add_operation(name, operation)
@operations[name] = operation
# @return [String]
def inspect
"#<#{self.class.name}>"
end

# @param [String] name
# @return [Operation]
# @return [OperationShape] operation
def operation(name)
raise ArgumentError, "unknown operation #{name.inspect}" unless @operations.key?(name)

Expand All @@ -39,11 +45,6 @@ def operation(name)
def operation_names
@operations.keys
end

# @api private
def inspect
"#<#{self.class.name}>"
end
end
end
end
Loading

0 comments on commit e528bef

Please sign in to comment.