Skip to content

Commit

Permalink
Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
mullermp committed Oct 4, 2024
1 parent 872942a commit 0b2a323
Show file tree
Hide file tree
Showing 26 changed files with 439 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,25 +146,39 @@
"smithy.api#httpHeader",
"smithy.api#httpLabel",
"smithy.api#httpPayload",
"smithy.api#httpQuery",
"smithy.api#idempotent",
"smithy.api#idRef",
"smithy.api#length",
"smithy.api#notProperty",
"smithy.api#paginated",
"smithy.api#pattern",
"smithy.api#private",
"smithy.api#protocolDefinition",
"smithy.api#range",
"smithy.api#readonly",
"smithy.api#required",
"smithy.api#retryable",
"smithy.api#tags",
"smithy.api#title",
"smithy.api#trait",
"smithy.api#traitValidators",
"smithy.api#uniqueItems",
"smithy.api#unitType",
"smithy.api#unstable",
"smithy.ruby.protocols#railsJson"
"smithy.ruby.protocols#railsJson",
"smithy.waiters#waitable"
],
"validationEvents": [
{
"id": "WaitableTraitInvalidErrorType.HighScoreExists.0",
"severity": "WARNING",
"shapeId": "myapp#GetHighScore",
"message": "Waiter `HighScoreExists`, acceptor 0: errorType 'NotFoundError' not found on operation. This operation defines the following errors: []",
"filename": "/Users/mamuller/workplace/smithy-ruby/example/model/high-score-service.smithy",
"line": 54,
"column": 1
}
],
"validationEvents": [],
"version": "1.0"
}
54 changes: 51 additions & 3 deletions example/build/smithy/high-score-service/model/model.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,26 @@
"method": "GET",
"uri": "/high_scores/{id}"
},
"smithy.api#readonly": {}
"smithy.api#readonly": {},
"smithy.waiters#waitable": {
"HighScoreExists": {
"documentation": "Waits until a high score has been created",
"acceptors": [
{
"state": "retry",
"matcher": {
"errorType": "NotFoundError"
}
},
{
"state": "success",
"matcher": {
"success": true
}
}
]
}
}
}
},
"myapp#GetHighScoreInput": {
Expand Down Expand Up @@ -260,12 +279,33 @@
"method": "GET",
"uri": "/high_scores"
},
"smithy.api#paginated": {
"inputToken": "nextToken",
"outputToken": "nextToken",
"pageSize": "maxResults",
"items": "highScores"
},
"smithy.api#readonly": {}
}
},
"myapp#ListHighScoresInput": {
"type": "structure",
"members": {}
"members": {
"nextToken": {
"target": "smithy.api#String",
"traits": {
"smithy.api#documentation": "The next token to use for pagination",
"smithy.api#httpQuery": "nextToken"
}
},
"maxResults": {
"target": "smithy.api#Integer",
"traits": {
"smithy.api#documentation": "The maximum number of results to return",
"smithy.api#httpQuery": "maxResults"
}
}
}
},
"myapp#ListHighScoresOutput": {
"type": "structure",
Expand All @@ -276,6 +316,13 @@
"smithy.api#documentation": "A list of high scores",
"smithy.api#httpPayload": {}
}
},
"nextToken": {
"target": "smithy.api#String",
"traits": {
"smithy.api#documentation": "The next token to use for pagination",
"smithy.api#httpHeader": "nextToken"
}
}
},
"traits": {
Expand Down Expand Up @@ -460,7 +507,8 @@
},
"traits": {
"smithy.api#error": "client",
"smithy.api#httpError": 422
"smithy.api#httpError": 422,
"smithy.api#retryable": {}
}
},
"smithy.ruby.protocols#nestedAttributes": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# WARNING ABOUT GENERATED CODE

require 'hearth'

require_relative 'high_score_service/auth'
require_relative 'high_score_service/builders'
require_relative 'high_score_service/client'
Expand All @@ -24,6 +23,9 @@
require_relative 'high_score_service/types'
require_relative 'high_score_service/validators'
require_relative 'high_score_service/waiters'
begin
require_relative 'high_score_service/customizations'
rescue LoadError; end

module HighScoreService
VERSION = File.read(File.expand_path('../VERSION', __dir__)).strip
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ class ListHighScores
def self.build(http_req, input:)
http_req.http_method = 'GET'
http_req.append_path('/high_scores')
params = Hearth::Query::ParamList.new
params['nextToken'] = input.next_token.to_s unless input.next_token.nil?
params['maxResults'] = input.max_results.to_s unless input.max_results.nil?
http_req.append_query_param_list(params)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@ def get_high_score(params = {}, options = {})
# Some configurations cannot be overridden.
# @return [Hearth::Output]
# @example Request syntax with placeholder values
# resp = client.list_high_scores()
# resp = client.list_high_scores(
# next_token: 'nextToken',
# max_results: 1
# )
# @example Response structure
# resp.data #=> Types::ListHighScoresOutput
# resp.data.high_scores #=> Array<HighScoreAttributes>
Expand All @@ -184,6 +187,7 @@ def get_high_score(params = {}, options = {})
# resp.data.high_scores[0].score #=> Integer
# resp.data.high_scores[0].created_at #=> Time
# resp.data.high_scores[0].updated_at #=> Time
# resp.data.next_token #=> String
def list_high_scores(params = {}, options = {})
response_body = ::StringIO.new
middleware_opts = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def _defaults
auth_resolver: [Auth::Resolver.new],
auth_schemes: [Auth::SCHEMES],
disable_host_prefix: [false],
endpoint: [proc { |cfg| cfg[:stub_responses] ? 'http://localhost' : nil }],
endpoint: [proc { |cfg| ('http://localhost' if cfg[:stub_responses]) }],
endpoint_resolver: [Endpoint::Resolver.new],
http_client: [proc { |cfg| Hearth::HTTP::Client.new(logger: cfg[:logger]) }],
interceptors: [Hearth::InterceptorList.new],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,16 @@ def self.error_code(resp)
end

# Base class for all errors returned by this service
class ApiError < Hearth::HTTP::ApiError; end
class ApiError < Hearth::ApiError; end

# Base class for all errors returned where the client is at fault.
# These are generally errors with 4XX HTTP status codes.
class ApiClientError < ApiError; end

# Base class for all errors returned where the server is at fault.
# These are generally errors with 5XX HTTP status codes.
class ApiServerError < ApiError; end

# Base class for all errors returned where the service returned
# a 3XX redirection.
# a redirection.
class ApiRedirectError < ApiError
def initialize(location:, **kwargs)
@location = location
Expand All @@ -85,15 +83,19 @@ def initialize(location:, **kwargs)
end

class UnprocessableEntityError < ApiClientError
def initialize(http_resp:, **kwargs)
@data = Parsers::UnprocessableEntityError.parse(http_resp)
def initialize(data:, **kwargs)
@data = data
kwargs[:message] = @data.message if @data.respond_to?(:message)

super(http_resp: http_resp, **kwargs)
super(**kwargs)
end

# @return [Types::UnprocessableEntityError]
attr_reader :data

def retryable?
true
end
end

end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ def self.build(config, options = {})
error_parser: Hearth::HTTP::ErrorParser.new(
error_module: Errors,
success_status: 201,
errors: [Errors::UnprocessableEntityError]
error_parsers: [Parsers::UnprocessableEntityError]
)
)
stack.use(Middleware::RequestId)
stack.use(Hearth::Middleware::Send,
client: config.http_client,
event_handler: nil,
stub_data_class: Stubs::CreateHighScore,
stub_error_classes: [Stubs::UnprocessableEntityError],
stub_message_encoder: Hearth::EventStream::Binary.const_get(:MessageEncoder).new,
stub_responses: config.stub_responses,
stubs: config.stubs
)
Expand Down Expand Up @@ -96,14 +98,16 @@ def self.build(config, options = {})
error_parser: Hearth::HTTP::ErrorParser.new(
error_module: Errors,
success_status: 200,
errors: []
error_parsers: []
)
)
stack.use(Middleware::RequestId)
stack.use(Hearth::Middleware::Send,
client: config.http_client,
event_handler: nil,
stub_data_class: Stubs::DeleteHighScore,
stub_error_classes: [],
stub_message_encoder: Hearth::EventStream::Binary.const_get(:MessageEncoder).new,
stub_responses: config.stub_responses,
stubs: config.stubs
)
Expand Down Expand Up @@ -145,14 +149,16 @@ def self.build(config, options = {})
error_parser: Hearth::HTTP::ErrorParser.new(
error_module: Errors,
success_status: 200,
errors: []
error_parsers: []
)
)
stack.use(Middleware::RequestId)
stack.use(Hearth::Middleware::Send,
client: config.http_client,
event_handler: nil,
stub_data_class: Stubs::GetHighScore,
stub_error_classes: [],
stub_message_encoder: Hearth::EventStream::Binary.const_get(:MessageEncoder).new,
stub_responses: config.stub_responses,
stubs: config.stubs
)
Expand Down Expand Up @@ -194,14 +200,16 @@ def self.build(config, options = {})
error_parser: Hearth::HTTP::ErrorParser.new(
error_module: Errors,
success_status: 200,
errors: []
error_parsers: []
)
)
stack.use(Middleware::RequestId)
stack.use(Hearth::Middleware::Send,
client: config.http_client,
event_handler: nil,
stub_data_class: Stubs::ListHighScores,
stub_error_classes: [],
stub_message_encoder: Hearth::EventStream::Binary.const_get(:MessageEncoder).new,
stub_responses: config.stub_responses,
stubs: config.stubs
)
Expand Down Expand Up @@ -243,14 +251,16 @@ def self.build(config, options = {})
error_parser: Hearth::HTTP::ErrorParser.new(
error_module: Errors,
success_status: 200,
errors: [Errors::UnprocessableEntityError]
error_parsers: [Parsers::UnprocessableEntityError]
)
)
stack.use(Middleware::RequestId)
stack.use(Hearth::Middleware::Send,
client: config.http_client,
event_handler: nil,
stub_data_class: Stubs::UpdateHighScore,
stub_error_classes: [Stubs::UnprocessableEntityError],
stub_message_encoder: Hearth::EventStream::Binary.const_get(:MessageEncoder).new,
stub_responses: config.stub_responses,
stubs: config.stubs
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,46 @@
module HighScoreService
module Paginators

class ListHighScores
# @param [Client] client
# @param (see Client#list_high_scores)
def initialize(client, params = {}, options = {})
@client = client
@params = params
@options = options
end

# Iterate all response pages of the list_high_scores operation.
# @return [Enumerator]
def pages
params = @params
Enumerator.new do |e|
@prev_token = params[:next_token]
output = @client.list_high_scores(params, @options)
e.yield(output)
output_token = output.data.next_token

until output_token.nil? || @prev_token == output_token
params = params.merge(next_token: output_token)
output = @client.list_high_scores(params, @options)
e.yield(output)
output_token = output.data.next_token
end
end
end

# Iterate all items from pages in the list_high_scores operation.
# @return [Enumerator]
def items
Enumerator.new do |e|
pages.each do |page|
page.data.high_scores.each do |item|
e.yield(item)
end
end
end
end
end

end
end
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ def self.build(params, context:)
Hearth::Validator.validate_types!(params, ::Hash, Types::ListHighScoresInput, context: context)
type = Types::ListHighScoresInput.new
Hearth::Validator.validate_unknown!(type, params, context: context) if params.is_a?(Hash)
type.next_token = params[:next_token] unless params[:next_token].nil?
type.max_results = params[:max_results] unless params[:max_results].nil?
type
end
end
Expand All @@ -144,6 +146,7 @@ def self.build(params, context:)
type = Types::ListHighScoresOutput.new
Hearth::Validator.validate_unknown!(type, params, context: context) if params.is_a?(Hash)
type.high_scores = HighScores.build(params[:high_scores], context: "#{context}[:high_scores]") unless params[:high_scores].nil?
type.next_token = params[:next_token] unless params[:next_token].nil?
type
end
end
Expand Down
Loading

0 comments on commit 0b2a323

Please sign in to comment.