speakeasy_rubygems_tp_sample 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/crystalline/enum.rbi +17 -0
- data/lib/crystalline/metadata_fields.rb +265 -0
- data/lib/crystalline/module.rb +188 -0
- data/lib/crystalline/types.rb +170 -0
- data/lib/crystalline/utils.rb +69 -0
- data/lib/crystalline.rb +13 -0
- data/lib/open_api_sdk/models/callbacks.rb +12 -0
- data/lib/open_api_sdk/models/components/pet.rb +34 -0
- data/lib/open_api_sdk/models/components/pet.rbi +20 -0
- data/lib/open_api_sdk/models/components.rb +12 -0
- data/lib/open_api_sdk/models/errors/apierror.rb +39 -0
- data/lib/open_api_sdk/models/errors/apierror.rbi +27 -0
- data/lib/open_api_sdk/models/errors.rb +12 -0
- data/lib/open_api_sdk/models/operations/listpets_response.rb +51 -0
- data/lib/open_api_sdk/models/operations/listpets_response.rbi +32 -0
- data/lib/open_api_sdk/models/operations.rb +12 -0
- data/lib/open_api_sdk/petstore.rb +223 -0
- data/lib/open_api_sdk/sdk_hooks/hooks.rb +99 -0
- data/lib/open_api_sdk/sdk_hooks/registration.rb +34 -0
- data/lib/open_api_sdk/sdk_hooks/types.rb +180 -0
- data/lib/open_api_sdk/sdkconfiguration.rb +97 -0
- data/lib/open_api_sdk/sdkconfiguration.rbi +77 -0
- data/lib/open_api_sdk/utils/forms.rb +232 -0
- data/lib/open_api_sdk/utils/headers.rb +84 -0
- data/lib/open_api_sdk/utils/query_params.rb +167 -0
- data/lib/open_api_sdk/utils/request_bodies.rb +79 -0
- data/lib/open_api_sdk/utils/retries.rb +139 -0
- data/lib/open_api_sdk/utils/security.rb +158 -0
- data/lib/open_api_sdk/utils/url.rb +124 -0
- data/lib/open_api_sdk/utils/utils.rb +245 -0
- data/lib/speakeasy_rubygems_tp_sample.rb +25 -0
- metadata +290 -0
data/lib/crystalline.rb
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
|
|
2
|
+
|
|
3
|
+
# typed: false
|
|
4
|
+
# frozen_string_literal: true
|
|
5
|
+
|
|
6
|
+
module Crystalline
|
|
7
|
+
autoload :MetadataFields, 'crystalline/metadata_fields'
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
require_relative 'crystalline/utils'
|
|
12
|
+
require_relative 'crystalline/module'
|
|
13
|
+
require_relative 'crystalline/types'
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
|
|
2
|
+
|
|
3
|
+
# typed: true
|
|
4
|
+
# frozen_string_literal: true
|
|
5
|
+
|
|
6
|
+
module OpenApiSDK
|
|
7
|
+
module Models
|
|
8
|
+
module Components
|
|
9
|
+
|
|
10
|
+
class Pet
|
|
11
|
+
extend T::Sig
|
|
12
|
+
include Crystalline::MetadataFields
|
|
13
|
+
|
|
14
|
+
field :id, ::Integer, {'format_json': {'letter_case': ::OpenApiSDK::Utils.field_name("id"), required: true}}
|
|
15
|
+
|
|
16
|
+
field :name, ::String, {'format_json': {'letter_case': ::OpenApiSDK::Utils.field_name("name"), required: true}}
|
|
17
|
+
|
|
18
|
+
sig { params(id: ::Integer, name: ::String).void }
|
|
19
|
+
def initialize(id:, name:)
|
|
20
|
+
@id = id
|
|
21
|
+
@name = name
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
sig { params(other: T.untyped).returns(T::Boolean) }
|
|
25
|
+
def ==(other)
|
|
26
|
+
return false unless other.is_a?(self.class)
|
|
27
|
+
return false unless @id == other.id
|
|
28
|
+
return false unless @name == other.name
|
|
29
|
+
true
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# typed: true
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
class OpenApiSDK::Models::Components::Pet
|
|
5
|
+
extend ::Crystalline::MetadataFields::ClassMethods
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
class OpenApiSDK::Models::Components::Pet
|
|
9
|
+
def id
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def id=(str_)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def name
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def name=(str_)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
|
|
2
|
+
|
|
3
|
+
# typed: true
|
|
4
|
+
# frozen_string_literal: true
|
|
5
|
+
|
|
6
|
+
module OpenApiSDK
|
|
7
|
+
module Models
|
|
8
|
+
module Errors
|
|
9
|
+
|
|
10
|
+
class APIError < ::StandardError
|
|
11
|
+
include Crystalline::MetadataFields
|
|
12
|
+
extend T::Sig
|
|
13
|
+
|
|
14
|
+
field :body, T.nilable(::String), {'format_json': {'letter_case': ::OpenApiSDK::Utils.field_name("body")}}
|
|
15
|
+
field :raw_response, T.nilable(Faraday::Response), {}
|
|
16
|
+
field(
|
|
17
|
+
:status_code,
|
|
18
|
+
T.nilable(::Integer),
|
|
19
|
+
{'format_json': {'letter_case': ::OpenApiSDK::Utils.field_name("status_code")}}
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
sig { params(status_code: ::Integer, body: ::String, raw_response: Faraday::Response).void }
|
|
23
|
+
def initialize(status_code:, body:, raw_response:)
|
|
24
|
+
@status_code = status_code
|
|
25
|
+
@body = body
|
|
26
|
+
@raw_response = raw_response
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def ==(other)
|
|
30
|
+
return false unless other.is_a?(self.class)
|
|
31
|
+
return false unless @status_code == other.status_code
|
|
32
|
+
return false unless @body == other.body
|
|
33
|
+
return false unless @raw_response == other.raw_response
|
|
34
|
+
true
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# typed: true
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
class OpenApiSDK::Models::Errors::APIError
|
|
5
|
+
extend ::Crystalline::MetadataFields::ClassMethods
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
class OpenApiSDK::Models::Errors::APIError
|
|
9
|
+
|
|
10
|
+
def body
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def body=(body)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def raw_response
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def raw_response=(raw_response)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def status_code
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def status_code=(status_code)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
|
|
2
|
+
|
|
3
|
+
# typed: true
|
|
4
|
+
# frozen_string_literal: true
|
|
5
|
+
|
|
6
|
+
module OpenApiSDK
|
|
7
|
+
module Models
|
|
8
|
+
module Operations
|
|
9
|
+
|
|
10
|
+
class ListPetsResponse
|
|
11
|
+
extend T::Sig
|
|
12
|
+
include Crystalline::MetadataFields
|
|
13
|
+
|
|
14
|
+
# HTTP response content type for this operation
|
|
15
|
+
field :content_type, ::String
|
|
16
|
+
# HTTP response status code for this operation
|
|
17
|
+
field :status_code, ::Integer
|
|
18
|
+
# Raw HTTP response; suitable for custom response parsing
|
|
19
|
+
field :raw_response, ::Faraday::Response
|
|
20
|
+
# A list of pets
|
|
21
|
+
field :pets, Crystalline::Nilable.new(Crystalline::Array.new(Models::Components::Pet))
|
|
22
|
+
|
|
23
|
+
sig {
|
|
24
|
+
params(
|
|
25
|
+
content_type: ::String,
|
|
26
|
+
status_code: ::Integer,
|
|
27
|
+
raw_response: ::Faraday::Response,
|
|
28
|
+
pets: T.nilable(T::Array[Models::Components::Pet])
|
|
29
|
+
)
|
|
30
|
+
.void
|
|
31
|
+
}
|
|
32
|
+
def initialize(content_type:, status_code:, raw_response:, pets: nil)
|
|
33
|
+
@content_type = content_type
|
|
34
|
+
@status_code = status_code
|
|
35
|
+
@raw_response = raw_response
|
|
36
|
+
@pets = pets
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
sig { params(other: T.untyped).returns(T::Boolean) }
|
|
40
|
+
def ==(other)
|
|
41
|
+
return false unless other.is_a?(self.class)
|
|
42
|
+
return false unless @content_type == other.content_type
|
|
43
|
+
return false unless @status_code == other.status_code
|
|
44
|
+
return false unless @raw_response == other.raw_response
|
|
45
|
+
return false unless @pets == other.pets
|
|
46
|
+
true
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# typed: true
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
class OpenApiSDK::Models::Operations::ListPetsResponse
|
|
5
|
+
extend ::Crystalline::MetadataFields::ClassMethods
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
class OpenApiSDK::Models::Operations::ListPetsResponse
|
|
9
|
+
def content_type
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def content_type=(str_)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def status_code
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def status_code=(str_)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def raw_response
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def raw_response=(str_)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def pets
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def pets=(str_)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
|
|
2
|
+
|
|
3
|
+
# typed: true
|
|
4
|
+
# frozen_string_literal: true
|
|
5
|
+
|
|
6
|
+
module OpenApiSDK
|
|
7
|
+
module Models
|
|
8
|
+
module Operations
|
|
9
|
+
autoload :ListPetsResponse, "open_api_sdk/models/operations/listpets_response.rb"
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
# Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
|
|
2
|
+
|
|
3
|
+
# typed: true
|
|
4
|
+
# frozen_string_literal: true
|
|
5
|
+
|
|
6
|
+
require "faraday"
|
|
7
|
+
require "faraday/multipart"
|
|
8
|
+
require "faraday/retry"
|
|
9
|
+
require "sorbet-runtime"
|
|
10
|
+
|
|
11
|
+
require_relative "sdk_hooks/hooks"
|
|
12
|
+
require_relative "utils/retries"
|
|
13
|
+
|
|
14
|
+
module OpenApiSDK
|
|
15
|
+
extend T::Sig
|
|
16
|
+
|
|
17
|
+
class PetStore
|
|
18
|
+
extend T::Sig
|
|
19
|
+
|
|
20
|
+
# Instantiates the SDK, configuring it with the provided parameters.
|
|
21
|
+
#
|
|
22
|
+
# @param client [Faraday::Connection, nil] The faraday HTTP client to use for all operations
|
|
23
|
+
# @param retry_config [::OpenApiSDK::Utils::RetryConfig, nil] The retry configuration to use for all operations
|
|
24
|
+
# @param timeout_ms [Integer, nil] Request timeout in milliseconds for all operations
|
|
25
|
+
# @param server_idx [Integer, nil] The index of the server to use for all operations
|
|
26
|
+
# @param server_url [String, nil] The server URL to use for all operations
|
|
27
|
+
# @param url_params [Hash{Symbol => String}, nil] Parameters to optionally template the server URL with
|
|
28
|
+
sig do
|
|
29
|
+
params(
|
|
30
|
+
client: T.nilable(Faraday::Connection),
|
|
31
|
+
retry_config: T.nilable(::OpenApiSDK::Utils::RetryConfig),
|
|
32
|
+
timeout_ms: T.nilable(::Integer),
|
|
33
|
+
server_idx: T.nilable(::Integer),
|
|
34
|
+
server_url: T.nilable(::String),
|
|
35
|
+
url_params: T.nilable(T::Hash[::Symbol, ::String])
|
|
36
|
+
)
|
|
37
|
+
.void
|
|
38
|
+
end
|
|
39
|
+
def initialize(client: nil, retry_config: nil, timeout_ms: nil, server_idx: nil, server_url: nil, url_params: nil)
|
|
40
|
+
|
|
41
|
+
connection_options = {
|
|
42
|
+
request: {
|
|
43
|
+
params_encoder: Faraday::FlatParamsEncoder
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
connection_options[:request][:timeout] = (timeout_ms.to_f / 1000) unless timeout_ms.nil?
|
|
47
|
+
|
|
48
|
+
client ||= Faraday.new(**connection_options) do |f|
|
|
49
|
+
f.request(:multipart, {flat_encode: true})
|
|
50
|
+
# f.response :logger, nil, { headers: true, bodies: true, errors: true }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
if !server_url.nil?
|
|
54
|
+
if !url_params.nil?
|
|
55
|
+
server_url = Utils.template_url(server_url, url_params)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
server_idx = 0 if server_idx.nil?
|
|
60
|
+
hooks = SDKHooks::Hooks.new
|
|
61
|
+
@sdk_configuration = SDKConfiguration.new(
|
|
62
|
+
client,
|
|
63
|
+
hooks,
|
|
64
|
+
retry_config,
|
|
65
|
+
timeout_ms,
|
|
66
|
+
server_url,
|
|
67
|
+
server_idx
|
|
68
|
+
)
|
|
69
|
+
@sdk_configuration = hooks.sdk_init(config: @sdk_configuration)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
sig {
|
|
73
|
+
params(base_url: ::String, url_variables: T.nilable(T::Hash[::Symbol, T.any(::String, T::Enum)])).returns(
|
|
74
|
+
::String
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
def get_url(base_url:, url_variables: nil)
|
|
78
|
+
sd_base_url, sd_options = @sdk_configuration.get_server_details
|
|
79
|
+
|
|
80
|
+
if base_url.nil?
|
|
81
|
+
base_url = sd_base_url
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
if url_variables.nil?
|
|
85
|
+
url_variables = sd_options
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
return Utils.template_url(base_url, url_variables)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
sig {
|
|
92
|
+
params(timeout_ms: T.nilable(::Integer), http_headers: T.nilable(T::Hash[T.any(::String, ::Symbol), ::String]))
|
|
93
|
+
.returns(Models::Operations::ListPetsResponse)
|
|
94
|
+
}
|
|
95
|
+
def list_pets(timeout_ms: nil, http_headers: nil)
|
|
96
|
+
# list_pets - List pets
|
|
97
|
+
url, params = @sdk_configuration.get_server_details
|
|
98
|
+
base_url = Utils.template_url(url, params)
|
|
99
|
+
url = "#{base_url}/pets"
|
|
100
|
+
headers = {}
|
|
101
|
+
headers = T.cast(headers, T::Hash[::String, ::String])
|
|
102
|
+
headers["Accept"] = "application/json"
|
|
103
|
+
headers["user-agent"] = @sdk_configuration.user_agent
|
|
104
|
+
|
|
105
|
+
timeout = (timeout_ms.to_f / 1000) unless timeout_ms.nil?
|
|
106
|
+
timeout ||= @sdk_configuration.timeout
|
|
107
|
+
|
|
108
|
+
connection = @sdk_configuration.client
|
|
109
|
+
|
|
110
|
+
hook_ctx = SDKHooks::HookContext.new(
|
|
111
|
+
config: @sdk_configuration,
|
|
112
|
+
base_url: base_url,
|
|
113
|
+
oauth2_scopes: nil,
|
|
114
|
+
operation_id: "listPets",
|
|
115
|
+
security_source: nil
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
error = T.let(nil, T.nilable(::StandardError))
|
|
119
|
+
http_response = T.let(nil, T.nilable(Faraday::Response))
|
|
120
|
+
|
|
121
|
+
begin
|
|
122
|
+
http_response = T.must(connection).get(url) do |req|
|
|
123
|
+
req.headers.merge!(headers)
|
|
124
|
+
req.options.timeout = timeout unless timeout.nil?
|
|
125
|
+
http_headers&.each do |key, value|
|
|
126
|
+
req.headers[key.to_s] = value
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
@sdk_configuration.hooks.before_request(
|
|
130
|
+
hook_ctx: SDKHooks::BeforeRequestHookContext.new(
|
|
131
|
+
hook_ctx: hook_ctx
|
|
132
|
+
),
|
|
133
|
+
request: req
|
|
134
|
+
)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
rescue ::StandardError => e
|
|
138
|
+
error = e
|
|
139
|
+
ensure
|
|
140
|
+
if http_response.nil? || Utils.error_status?(http_response.status)
|
|
141
|
+
http_response = @sdk_configuration.hooks.after_error(
|
|
142
|
+
error: error,
|
|
143
|
+
hook_ctx: SDKHooks::AfterErrorHookContext.new(
|
|
144
|
+
hook_ctx: hook_ctx
|
|
145
|
+
),
|
|
146
|
+
response: http_response
|
|
147
|
+
)
|
|
148
|
+
else
|
|
149
|
+
http_response = @sdk_configuration.hooks.after_success(
|
|
150
|
+
hook_ctx: SDKHooks::AfterSuccessHookContext.new(
|
|
151
|
+
hook_ctx: hook_ctx
|
|
152
|
+
),
|
|
153
|
+
response: http_response
|
|
154
|
+
)
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
if http_response.nil?
|
|
158
|
+
raise error if !error.nil?
|
|
159
|
+
raise "no response"
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
content_type = http_response.headers.fetch("Content-Type", "application/octet-stream")
|
|
164
|
+
if Utils.match_status_code(http_response.status, ["200"])
|
|
165
|
+
if Utils.match_content_type(content_type, "application/json")
|
|
166
|
+
http_response = @sdk_configuration.hooks.after_success(
|
|
167
|
+
hook_ctx: SDKHooks::AfterSuccessHookContext.new(
|
|
168
|
+
hook_ctx: hook_ctx
|
|
169
|
+
),
|
|
170
|
+
response: http_response
|
|
171
|
+
)
|
|
172
|
+
response_data = http_response.env.response_body
|
|
173
|
+
obj = Crystalline.unmarshal_json(::JSON.parse(response_data), Crystalline::Array.new(Models::Components::Pet))
|
|
174
|
+
response = Models::Operations::ListPetsResponse.new(
|
|
175
|
+
status_code: http_response.status,
|
|
176
|
+
content_type: content_type,
|
|
177
|
+
raw_response: http_response,
|
|
178
|
+
pets: T.unsafe(obj)
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
return response
|
|
182
|
+
else
|
|
183
|
+
raise(
|
|
184
|
+
::OpenApiSDK::Models::Errors::APIError.new(
|
|
185
|
+
status_code: http_response.status,
|
|
186
|
+
body: http_response.env.response_body,
|
|
187
|
+
raw_response: http_response
|
|
188
|
+
),
|
|
189
|
+
"Unknown content type received"
|
|
190
|
+
)
|
|
191
|
+
end
|
|
192
|
+
elsif Utils.match_status_code(http_response.status, ["4XX"])
|
|
193
|
+
raise(
|
|
194
|
+
::OpenApiSDK::Models::Errors::APIError.new(
|
|
195
|
+
status_code: http_response.status,
|
|
196
|
+
body: http_response.env.response_body,
|
|
197
|
+
raw_response: http_response
|
|
198
|
+
),
|
|
199
|
+
"API error occurred"
|
|
200
|
+
)
|
|
201
|
+
elsif Utils.match_status_code(http_response.status, ["5XX"])
|
|
202
|
+
raise(
|
|
203
|
+
::OpenApiSDK::Models::Errors::APIError.new(
|
|
204
|
+
status_code: http_response.status,
|
|
205
|
+
body: http_response.env.response_body,
|
|
206
|
+
raw_response: http_response
|
|
207
|
+
),
|
|
208
|
+
"API error occurred"
|
|
209
|
+
)
|
|
210
|
+
else
|
|
211
|
+
raise(
|
|
212
|
+
::OpenApiSDK::Models::Errors::APIError.new(
|
|
213
|
+
status_code: http_response.status,
|
|
214
|
+
body: http_response.env.response_body,
|
|
215
|
+
raw_response: http_response
|
|
216
|
+
),
|
|
217
|
+
"Unknown status code received"
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
end
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
|
|
2
|
+
|
|
3
|
+
# typed: true
|
|
4
|
+
# frozen_string_literal: true
|
|
5
|
+
|
|
6
|
+
require_relative "./types"
|
|
7
|
+
require_relative "./registration"
|
|
8
|
+
|
|
9
|
+
module OpenApiSDK
|
|
10
|
+
module SDKHooks
|
|
11
|
+
class Hooks
|
|
12
|
+
extend T::Sig
|
|
13
|
+
|
|
14
|
+
sig { void }
|
|
15
|
+
def initialize
|
|
16
|
+
@sdk_init_hooks = T.let([], T::Array[AbstractSDKHook])
|
|
17
|
+
@before_request_hooks = T.let([], T::Array[AbstractSDKHook])
|
|
18
|
+
@after_success_hooks = T.let([], T::Array[AbstractSDKHook])
|
|
19
|
+
@after_error_hooks = T.let([], T::Array[AbstractSDKHook])
|
|
20
|
+
Registration.init_hooks(self)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
sig { params(hook: AbstractSDKHook).void }
|
|
24
|
+
def register_sdk_init_hook(hook)
|
|
25
|
+
@sdk_init_hooks << hook
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
sig { params(hook: AbstractSDKHook).void }
|
|
29
|
+
def register_before_request_hook(hook)
|
|
30
|
+
@before_request_hooks << hook
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
sig { params(hook: AbstractSDKHook).void }
|
|
34
|
+
def register_after_success_hook(hook)
|
|
35
|
+
@after_success_hooks << hook
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
sig { params(hook: AbstractSDKHook).void }
|
|
39
|
+
def register_after_error_hook(hook)
|
|
40
|
+
@after_error_hooks << hook
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
sig { params(config: SDKConfiguration).returns(SDKConfiguration) }
|
|
44
|
+
def sdk_init(config:)
|
|
45
|
+
@sdk_init_hooks.each do |hook|
|
|
46
|
+
config = hook.sdk_init(config: config)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
return config
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
sig do
|
|
53
|
+
params(
|
|
54
|
+
hook_ctx: BeforeRequestHookContext,
|
|
55
|
+
request: Faraday::Request
|
|
56
|
+
)
|
|
57
|
+
.returns(Faraday::Request)
|
|
58
|
+
end
|
|
59
|
+
def before_request(hook_ctx:, request:)
|
|
60
|
+
@before_request_hooks.each do |hook|
|
|
61
|
+
request = hook.before_request(hook_ctx: hook_ctx, request: request)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
request
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
sig do
|
|
68
|
+
params(
|
|
69
|
+
hook_ctx: AfterSuccessHookContext,
|
|
70
|
+
response: Faraday::Response
|
|
71
|
+
)
|
|
72
|
+
.returns(Faraday::Response)
|
|
73
|
+
end
|
|
74
|
+
def after_success(hook_ctx:, response:)
|
|
75
|
+
@after_success_hooks.each do |hook|
|
|
76
|
+
response = hook.after_success(hook_ctx: hook_ctx, response: response)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
response
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
sig do
|
|
83
|
+
params(
|
|
84
|
+
error: T.nilable(::StandardError),
|
|
85
|
+
hook_ctx: AfterErrorHookContext,
|
|
86
|
+
response: T.nilable(Faraday::Response)
|
|
87
|
+
)
|
|
88
|
+
.returns(T.nilable(Faraday::Response))
|
|
89
|
+
end
|
|
90
|
+
def after_error(error:, hook_ctx:, response:)
|
|
91
|
+
@after_error_hooks.each do |hook|
|
|
92
|
+
response = hook.after_error(error: error, hook_ctx: hook_ctx, response: response)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
response
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# typed: true
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
#
|
|
5
|
+
# This file is only ever generated once on the first generation and then is free to be modified.
|
|
6
|
+
# Any hooks you wish to add should be registered in the init_hooks method.
|
|
7
|
+
#
|
|
8
|
+
# Hooks are registered per SDK instance, and are valid for the lifetime of the SDK instance.
|
|
9
|
+
#
|
|
10
|
+
|
|
11
|
+
require_relative "./types"
|
|
12
|
+
|
|
13
|
+
module OpenApiSDK
|
|
14
|
+
module SDKHooks
|
|
15
|
+
class Registration
|
|
16
|
+
extend T::Sig
|
|
17
|
+
|
|
18
|
+
sig do
|
|
19
|
+
params(
|
|
20
|
+
hooks: Hooks
|
|
21
|
+
)
|
|
22
|
+
.void
|
|
23
|
+
end
|
|
24
|
+
def self.init_hooks(hooks)
|
|
25
|
+
# example_hook = ExampleHook.new
|
|
26
|
+
|
|
27
|
+
# hooks.register_sdk_init_hook example_hook
|
|
28
|
+
# hooks.register_before_request_hook example_hook
|
|
29
|
+
# hooks.register_after_error_hook example_hook
|
|
30
|
+
# hooks.register_after_success_hook example_hook
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|