api_recipes 2.6.0 → 2.8.0
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 +4 -4
- data/lib/api_recipes.rb +4 -3
- data/lib/api_recipes/api.rb +5 -3
- data/lib/api_recipes/endpoint.rb +8 -1
- data/lib/api_recipes/exceptions.rb +10 -2
- data/lib/api_recipes/response.rb +1 -0
- data/lib/api_recipes/route.rb +25 -16
- data/lib/api_recipes/settings.rb +4 -2
- data/lib/api_recipes/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b8b743f62651355bb59b3d641637d8df6b28a860d5000a3b684758a7d1de5c47
|
4
|
+
data.tar.gz: d2febad009985547a7a148e1b07b193d568b76bd75a2064a9c55257652ce7997
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a4b6882211bbae0c27f546267674e501df253eb9a22303fadf0bbf85dfd26a2ada943f813ae1660164c20e3dae68d3afd61caf132e6e40be04bf1f62e9cfcd4
|
7
|
+
data.tar.gz: e5429acaf4fbcaa1f5e83090d8ede9dcee36253a61b8d17b5e79fb5c4c4178115b677d91ac39e8c145af63dd1813e904834201fddbc930e45e07b3d8df3455b9
|
data/lib/api_recipes.rb
CHANGED
@@ -32,13 +32,13 @@ module ApiRecipes
|
|
32
32
|
|
33
33
|
define_method api_name do
|
34
34
|
configs = ApiRecipes._aprcps_merge_apis_configs(api_name, configs.deep_symbolize_keys)
|
35
|
-
api = Api.new(api_name, configs)
|
35
|
+
api = Api.new(api_name, configs, self)
|
36
36
|
ApiRecipes.copy_global_authorizations_to_api api
|
37
37
|
api
|
38
38
|
end
|
39
39
|
define_singleton_method api_name do
|
40
40
|
configs = ApiRecipes._aprcps_merge_apis_configs(api_name, configs.deep_symbolize_keys)
|
41
|
-
api = Api.new(api_name, configs)
|
41
|
+
api = Api.new(api_name, configs, self)
|
42
42
|
ApiRecipes.copy_global_authorizations_to_api api
|
43
43
|
api
|
44
44
|
end
|
@@ -58,6 +58,7 @@ module ApiRecipes
|
|
58
58
|
unless @configuration
|
59
59
|
@configuration = Configuration.new
|
60
60
|
end
|
61
|
+
|
61
62
|
@configuration
|
62
63
|
end
|
63
64
|
|
@@ -105,7 +106,7 @@ module ApiRecipes
|
|
105
106
|
def self._aprcps_define_global_apis
|
106
107
|
configuration.apis_configs.each do |api_name, api_configs|
|
107
108
|
api_name = api_name.to_sym
|
108
|
-
_aprcps_global_storage[api_name] = Api.new api_name, api_configs
|
109
|
+
_aprcps_global_storage[api_name] = Api.new api_name, api_configs, self
|
109
110
|
define_singleton_method api_name do
|
110
111
|
_aprcps_global_storage[api_name]
|
111
112
|
end
|
data/lib/api_recipes/api.rb
CHANGED
@@ -2,13 +2,14 @@ module ApiRecipes
|
|
2
2
|
class Api
|
3
3
|
|
4
4
|
attr_accessor :name, :configs, :authorization, :basic_auth
|
5
|
-
attr_reader :base_configs
|
5
|
+
attr_reader :base_configs, :object
|
6
6
|
|
7
|
-
BASE_CONFIGS_KEYS = [:protocol, :host, :port, :api_version, :timeout, :on_bad_code]
|
7
|
+
BASE_CONFIGS_KEYS = [:protocol, :host, :port, :api_version, :timeout, :on_bad_code, :verify_with]
|
8
8
|
|
9
|
-
def initialize(name, configs)
|
9
|
+
def initialize(name, configs, object)
|
10
10
|
@name = name
|
11
11
|
@configs = ApiRecipes::Settings::DEFAULT.merge configs
|
12
|
+
@object = object
|
12
13
|
|
13
14
|
# Generate some_api.some_endpoint methods
|
14
15
|
# e.g. github.users
|
@@ -16,6 +17,7 @@ module ApiRecipes
|
|
16
17
|
if endpoints && endpoints.is_a?(Hash)
|
17
18
|
endpoints.each do |ep_name, params|
|
18
19
|
unless respond_to? ep_name
|
20
|
+
# TODO: store endpoints, routes and routes_urls
|
19
21
|
define_singleton_method ep_name do |*request_params, &block|
|
20
22
|
# puts "API params: #{params}"
|
21
23
|
Endpoint.new api: self, name: ep_name, path: path, params: params, request_params: request_params, &block
|
data/lib/api_recipes/endpoint.rb
CHANGED
@@ -3,6 +3,8 @@ module ApiRecipes
|
|
3
3
|
|
4
4
|
attr_reader :api, :name, :params, :route, :children
|
5
5
|
|
6
|
+
FORWARDABLE_PARAMS = %i[verify_with].freeze
|
7
|
+
|
6
8
|
def initialize(api: nil, name: nil, path: nil, params: {}, request_params: [], &block)
|
7
9
|
@api = api
|
8
10
|
@name = name
|
@@ -52,6 +54,8 @@ module ApiRecipes
|
|
52
54
|
# puts "generating children of #{@name}: #{children.inspect}"
|
53
55
|
if children
|
54
56
|
children.each do |ep_name, pars|
|
57
|
+
pars = forwardable_params.merge(pars) if pars
|
58
|
+
# TODO: Merge pars with params
|
55
59
|
# puts "Creating Endpoint '#{@name}' child '#{ep_name}' passing path #{build_path}"
|
56
60
|
define_singleton_method ep_name do |*request_params, &block|
|
57
61
|
Endpoint.new api: @api, name: ep_name, path: build_path, params: pars, request_params: request_params, &block
|
@@ -89,7 +93,6 @@ module ApiRecipes
|
|
89
93
|
end
|
90
94
|
# Merge DEFAULT_ROUTE_ATTRIBUTES with Api base_configs
|
91
95
|
# Then merge the result with provided attributes
|
92
|
-
|
93
96
|
@params = Settings::DEFAULT_ROUTE_ATTRIBUTES.inject({}) do |out, key_val|
|
94
97
|
new_val = @api.base_configs[key_val.first]
|
95
98
|
out[key_val.first] = new_val.nil? ? key_val.last : new_val
|
@@ -126,5 +129,9 @@ module ApiRecipes
|
|
126
129
|
def required_params_for_path
|
127
130
|
absolute_path.scan(/:(\w+)/).flatten.map { |p| p.to_sym }
|
128
131
|
end
|
132
|
+
|
133
|
+
def forwardable_params
|
134
|
+
params.select { |k, v| FORWARDABLE_PARAMS.include? k }
|
135
|
+
end
|
129
136
|
end
|
130
137
|
end
|
@@ -40,8 +40,16 @@ module ApiRecipes
|
|
40
40
|
end
|
41
41
|
|
42
42
|
class ResponseCodeNotAsExpected < Exception
|
43
|
-
|
44
|
-
|
43
|
+
|
44
|
+
attr_reader :path, :expected_code, :response
|
45
|
+
|
46
|
+
def initialize(path, expected_code = nil, response = nil, message: nil)
|
47
|
+
@path = path
|
48
|
+
@expected_code = expected_code
|
49
|
+
@response = response
|
50
|
+
unless message
|
51
|
+
message = "response code for request on route '#{@path}' has returned '#{@response.code}', but '#{@expected_code}' was expected\n\nResponse body:\n #{@response.body}"
|
52
|
+
end
|
45
53
|
super(message)
|
46
54
|
end
|
47
55
|
end
|
data/lib/api_recipes/response.rb
CHANGED
data/lib/api_recipes/route.rb
CHANGED
@@ -43,31 +43,40 @@ module ApiRecipes
|
|
43
43
|
|
44
44
|
def build_url_from_path
|
45
45
|
attrs = {
|
46
|
-
scheme: settings[:protocol],
|
46
|
+
scheme: settings[:protocol].to_s,
|
47
47
|
host: settings[:host],
|
48
48
|
port: port,
|
49
49
|
path: path
|
50
50
|
}
|
51
|
-
URI::Generic.
|
51
|
+
URI::Generic.build2 attrs
|
52
52
|
end
|
53
53
|
|
54
54
|
def check_response_code
|
55
|
-
# If :ok_code property is present, check the response code
|
56
55
|
ok_code = false
|
57
56
|
code = @response.code
|
58
|
-
|
59
|
-
|
60
|
-
|
57
|
+
message = nil
|
58
|
+
|
59
|
+
verify_with = attributes[:verify_with]
|
60
|
+
if verify_with && @api.object.respond_to?(verify_with, true )
|
61
|
+
ok_code = @api.object.send verify_with, @response
|
62
|
+
message = "response for request on route '#{path}' was not valid. Verified with #{@api.object}##{verify_with}.\n\nResponse body:\n #{@response.body}"
|
61
63
|
else
|
62
|
-
#
|
63
|
-
|
64
|
-
|
64
|
+
# If :ok_code property is present, check the response code
|
65
|
+
if expected_code = attributes[:ok_code]
|
66
|
+
# If the code does not match, apply the requested strategy
|
67
|
+
ok_code = true if code == expected_code
|
68
|
+
else
|
69
|
+
# Default: 200 <= OK < 300
|
70
|
+
ok_code = true if @response.status.success?
|
71
|
+
expected_code = '200 <= CODE < 300'
|
72
|
+
end
|
65
73
|
end
|
74
|
+
|
66
75
|
unless ok_code
|
67
76
|
case attributes[:on_bad_code].to_s
|
68
77
|
when 'ignore'
|
69
78
|
when 'raise'
|
70
|
-
raise ResponseCodeNotAsExpected.new(path, expected_code,
|
79
|
+
raise ResponseCodeNotAsExpected.new(path, expected_code, @response, message: message)
|
71
80
|
end
|
72
81
|
end
|
73
82
|
end
|
@@ -81,12 +90,12 @@ module ApiRecipes
|
|
81
90
|
end
|
82
91
|
|
83
92
|
def port
|
84
|
-
settings[:port] || case settings[:protocol]
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
93
|
+
settings[:port].to_s || case settings[:protocol].to_s
|
94
|
+
when 'http'
|
95
|
+
'80'
|
96
|
+
when 'https'
|
97
|
+
'443'
|
98
|
+
end
|
90
99
|
end
|
91
100
|
|
92
101
|
def prepare_request
|
data/lib/api_recipes/settings.rb
CHANGED
@@ -9,7 +9,8 @@ module ApiRecipes
|
|
9
9
|
base_url: nil,
|
10
10
|
timeout: 3,
|
11
11
|
on_bad_code: 'raise',
|
12
|
-
endpoints: {}
|
12
|
+
endpoints: {},
|
13
|
+
verify_with: nil
|
13
14
|
}
|
14
15
|
|
15
16
|
DEFAULT_ROUTE_ATTRIBUTES = {
|
@@ -18,7 +19,8 @@ module ApiRecipes
|
|
18
19
|
path: nil,
|
19
20
|
ok_code: nil,
|
20
21
|
timeout: DEFAULT[:timeout],
|
21
|
-
on_bad_code: DEFAULT[:on_bad_code]
|
22
|
+
on_bad_code: DEFAULT[:on_bad_code],
|
23
|
+
verify_with: DEFAULT[:verify_with]
|
22
24
|
}
|
23
25
|
|
24
26
|
AVAILABLE_PARAMS_ENCODINGS = %w(form params json body)
|
data/lib/api_recipes/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api_recipes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alessandro Verlato
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-02-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: http
|
@@ -62,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
62
|
- !ruby/object:Gem::Version
|
63
63
|
version: '0'
|
64
64
|
requirements: []
|
65
|
-
rubygems_version: 3.
|
65
|
+
rubygems_version: 3.0.8
|
66
66
|
signing_key:
|
67
67
|
specification_version: 4
|
68
68
|
summary: Consume HTTP APIs with style
|