api_recipes 2.10.4 → 3.0.0.beta1
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/README.md +631 -81
- data/UPGRADING.md +473 -0
- data/exe/api_recipes +6 -0
- data/lib/api_recipes/api.rb +71 -49
- data/lib/api_recipes/cli/upgrade.rb +179 -0
- data/lib/api_recipes/cli.rb +70 -0
- data/lib/api_recipes/configuration.rb +185 -36
- data/lib/api_recipes/endpoint.rb +68 -118
- data/lib/api_recipes/errors.rb +137 -0
- data/lib/api_recipes/internals/compiler.rb +680 -0
- data/lib/api_recipes/internals/context.rb +237 -0
- data/lib/api_recipes/internals/credential.rb +86 -0
- data/lib/api_recipes/internals/declaration.rb +225 -0
- data/lib/api_recipes/internals/definition.rb +179 -0
- data/lib/api_recipes/internals/handles.rb +81 -0
- data/lib/api_recipes/internals/level.rb +192 -0
- data/lib/api_recipes/internals/path_template.rb +131 -0
- data/lib/api_recipes/internals/registry.rb +146 -0
- data/lib/api_recipes/internals/request.rb +285 -0
- data/lib/api_recipes/internals/template.rb +73 -0
- data/lib/api_recipes/response.rb +90 -9
- data/lib/api_recipes/version.rb +3 -1
- data/lib/api_recipes.rb +42 -152
- metadata +26 -12
- data/lib/api_recipes/exceptions.rb +0 -91
- data/lib/api_recipes/route.rb +0 -165
- data/lib/api_recipes/settings.rb +0 -30
- data/lib/api_recipes/utils.rb +0 -195
data/lib/api_recipes.rb
CHANGED
|
@@ -1,179 +1,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'http'
|
|
2
4
|
|
|
3
|
-
require 'api_recipes/
|
|
4
|
-
require 'api_recipes/
|
|
5
|
+
require 'api_recipes/version'
|
|
6
|
+
require 'api_recipes/errors'
|
|
7
|
+
require 'api_recipes/internals/credential'
|
|
8
|
+
require 'api_recipes/internals/level'
|
|
9
|
+
require 'api_recipes/internals/path_template'
|
|
10
|
+
require 'api_recipes/internals/template'
|
|
11
|
+
require 'api_recipes/internals/definition'
|
|
12
|
+
require 'api_recipes/internals/compiler'
|
|
13
|
+
require 'api_recipes/internals/context'
|
|
14
|
+
require 'api_recipes/internals/request'
|
|
15
|
+
require 'api_recipes/response'
|
|
5
16
|
require 'api_recipes/configuration'
|
|
6
|
-
require 'api_recipes/route'
|
|
7
|
-
require 'api_recipes/endpoint'
|
|
8
17
|
require 'api_recipes/api'
|
|
9
|
-
require 'api_recipes/
|
|
10
|
-
require 'api_recipes/
|
|
11
|
-
|
|
12
|
-
|
|
18
|
+
require 'api_recipes/endpoint'
|
|
19
|
+
require 'api_recipes/internals/handles'
|
|
20
|
+
require 'api_recipes/internals/registry'
|
|
21
|
+
require 'api_recipes/internals/declaration'
|
|
13
22
|
|
|
14
23
|
module ApiRecipes
|
|
15
|
-
|
|
24
|
+
# `include ApiRecipes` adds the class macro `api`. options is a positional
|
|
25
|
+
# Hash, so that `api :github, host: 'x'` works; besides the api options it
|
|
26
|
+
# takes `as` (the name of the method) and `variables` (values for the level
|
|
27
|
+
# of the class).
|
|
16
28
|
def self.included(receiver)
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
unless api_name.is_a?(String) || api_name.is_a?(Symbol)
|
|
20
|
-
raise ArgumentError, "api name must be a Symbol or String"
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
if configs && !configs.is_a?(Hash)
|
|
24
|
-
raise ApiRecipes::ApiConfigIsNotAnHash.new(api_name)
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
api_name = api_name.to_sym
|
|
28
|
-
# configs = ApiRecipes._aprcps_merge_apis_configs(api_name, configs.deep_symbolize_keys)
|
|
29
|
-
if self.respond_to? api_name
|
|
30
|
-
raise ApiNameClashError.new(self, api_name)
|
|
31
|
-
else
|
|
32
|
-
|
|
33
|
-
define_method api_name do
|
|
34
|
-
configs = ApiRecipes._aprcps_merge_apis_configs(api_name, configs.deep_symbolize_keys)
|
|
35
|
-
api = Api.new(api_name, configs, self)
|
|
36
|
-
ApiRecipes.copy_global_authorizations_to_api api
|
|
37
|
-
api
|
|
38
|
-
end
|
|
39
|
-
define_singleton_method api_name do
|
|
40
|
-
configs = ApiRecipes._aprcps_merge_apis_configs(api_name, configs.deep_symbolize_keys)
|
|
41
|
-
api = Api.new(api_name, configs, self)
|
|
42
|
-
ApiRecipes.copy_global_authorizations_to_api api
|
|
43
|
-
api
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
end
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
def self.print_urls=(value)
|
|
50
|
-
@print_urls = value
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
def self.print_urls
|
|
54
|
-
@print_urls || false
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
def self.configuration
|
|
58
|
-
unless @configuration
|
|
59
|
-
@configuration = Configuration.new
|
|
29
|
+
def receiver.api(name, options = {})
|
|
30
|
+
Internals::Declaration.declare(self, name, options)
|
|
60
31
|
end
|
|
61
|
-
|
|
62
|
-
@configuration
|
|
63
32
|
end
|
|
64
33
|
|
|
34
|
+
# Yields a copy of the configuration, then loads and compiles it. The copy
|
|
35
|
+
# becomes the configuration only if that succeeds, together with the apis
|
|
36
|
+
# built from it: otherwise the previous configuration and apis stay as they
|
|
37
|
+
# were. Without a block, returns the configuration.
|
|
65
38
|
def self.configure
|
|
66
|
-
|
|
67
|
-
yield(configuration)
|
|
68
|
-
configuration.setup
|
|
69
|
-
else
|
|
70
|
-
configuration
|
|
71
|
-
end
|
|
72
|
-
end
|
|
39
|
+
return configuration unless block_given?
|
|
73
40
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
end
|
|
79
|
-
if auth = _aprcps_global_storage[api.name].authorization
|
|
80
|
-
api.authorization = auth
|
|
81
|
-
end
|
|
82
|
-
end
|
|
41
|
+
draft = configuration.dup
|
|
42
|
+
yield draft
|
|
43
|
+
Internals::Registry.install(draft) { @configuration = draft }
|
|
44
|
+
nil
|
|
83
45
|
end
|
|
84
46
|
|
|
85
|
-
def self.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
# Set authorization on thread storage
|
|
89
|
-
if _aprcps_thread_storage[api_name]
|
|
90
|
-
_aprcps_thread_storage[api_name].each do |_, api|
|
|
91
|
-
api.authorization = authorization
|
|
92
|
-
end
|
|
93
|
-
end
|
|
47
|
+
def self.configuration
|
|
48
|
+
@configuration ||= Configuration.new
|
|
94
49
|
end
|
|
95
50
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
if _aprcps_thread_storage[api_name]
|
|
101
|
-
_aprcps_thread_storage[api_name].each do |_, api|
|
|
102
|
-
api.authorization = basic_auth
|
|
103
|
-
end
|
|
104
|
-
end
|
|
51
|
+
# The global api called name, for any name.
|
|
52
|
+
def self.[](name)
|
|
53
|
+
key = name.is_a?(String) && name.valid_encoding? ? name.to_sym : name
|
|
54
|
+
Internals::Registry.current.global(key) || raise(UnknownEndpoint.new('ApiRecipes', name, apis))
|
|
105
55
|
end
|
|
106
56
|
|
|
107
|
-
def self.
|
|
108
|
-
configuration.apis_configs.each do |api_name, api_configs|
|
|
109
|
-
api_name = api_name.to_sym
|
|
110
|
-
_aprcps_global_storage[api_name] = Api.new api_name, api_configs, self
|
|
111
|
-
define_singleton_method api_name do
|
|
112
|
-
_aprcps_global_storage[api_name]
|
|
113
|
-
end
|
|
114
|
-
end
|
|
115
|
-
end
|
|
57
|
+
def self.apis = Internals::Registry.current.apis
|
|
116
58
|
|
|
117
|
-
# Removes the global
|
|
59
|
+
# Removes the global apis and their methods, the configuration and every
|
|
60
|
+
# credential and variable value.
|
|
118
61
|
def self.reset!
|
|
119
|
-
|
|
120
|
-
singleton_class.send(:remove_method, api_name) if singleton_class.method_defined?(api_name, false)
|
|
121
|
-
end
|
|
122
|
-
@storage = nil
|
|
123
|
-
@configuration = nil
|
|
124
|
-
@print_urls = nil
|
|
62
|
+
Internals::Registry.reset! { @configuration = nil }
|
|
125
63
|
nil
|
|
126
64
|
end
|
|
127
65
|
|
|
128
|
-
def self._aprcps_global_storage
|
|
129
|
-
unless @storage
|
|
130
|
-
@storage = {}
|
|
131
|
-
end
|
|
132
|
-
|
|
133
|
-
@storage
|
|
134
|
-
end
|
|
135
|
-
|
|
136
|
-
def self._aprcps_thread_storage
|
|
137
|
-
unless Thread.current[:api_recipes]
|
|
138
|
-
Thread.current[:api_recipes] = {}
|
|
139
|
-
end
|
|
140
|
-
Thread.current[:api_recipes]
|
|
141
|
-
end
|
|
142
|
-
|
|
143
|
-
def self._aprcps_merge_apis_configs(api_name, configs = nil)
|
|
144
|
-
unless api_name.is_a?(String) || api_name.is_a?(Symbol)
|
|
145
|
-
raise ArgumentError, "no api_name provided. Given: #{api_name.inspect}"
|
|
146
|
-
end
|
|
147
|
-
global_api_configs = _aprcps_global_storage[api_name]&.configs || {}
|
|
148
|
-
if configs
|
|
149
|
-
global_api_configs.deep_merge(configs) do |_, old_val, new_val|
|
|
150
|
-
if new_val.nil?
|
|
151
|
-
old_val
|
|
152
|
-
else
|
|
153
|
-
new_val
|
|
154
|
-
end
|
|
155
|
-
end
|
|
156
|
-
else
|
|
157
|
-
global_api_configs
|
|
158
|
-
end
|
|
159
|
-
end
|
|
160
|
-
|
|
161
66
|
def self.logger
|
|
162
67
|
configuration.logger
|
|
163
68
|
end
|
|
164
69
|
end
|
|
165
|
-
|
|
166
|
-
# Monkey-patch URI so it can accept hostnames with underscores like "web_service_1"
|
|
167
|
-
# (e.g. docker-compose service names), which Route#build_uri validates through
|
|
168
|
-
# URI::DEFAULT_PARSER. Only the uri gem's RFC 2396 parser (uri < 1.0) rejects them.
|
|
169
|
-
# From uri 1.0 the default parser follows RFC 3986, which accepts them, and
|
|
170
|
-
# URI::Parser is that parser, whose constructor takes no arguments.
|
|
171
|
-
if defined?(URI::RFC2396_Parser) && URI::DEFAULT_PARSER.instance_of?(URI::RFC2396_Parser)
|
|
172
|
-
module URI
|
|
173
|
-
# Undef DEFAULT_PARSER
|
|
174
|
-
URI.send(:remove_const, :DEFAULT_PARSER)
|
|
175
|
-
|
|
176
|
-
# Redefine DEFAULT_PARSER
|
|
177
|
-
DEFAULT_PARSER = RFC2396_Parser.new(HOSTNAME: "(?:(?:[a-zA-Z\\d](?:[-\\_a-zA-Z\\d]*[a-zA-Z\\d])?)\\.)*(?:[a-zA-Z](?:[-\\_a-zA-Z\\d]*[a-zA-Z\\d])?)\\.?")
|
|
178
|
-
end
|
|
179
|
-
end
|
metadata
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: api_recipes
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 3.0.0.beta1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Alessandro Verlato
|
|
8
|
-
bindir:
|
|
8
|
+
bindir: exe
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
@@ -15,20 +15,20 @@ dependencies:
|
|
|
15
15
|
requirements:
|
|
16
16
|
- - ">="
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version:
|
|
18
|
+
version: 6.0.4
|
|
19
19
|
- - "<"
|
|
20
20
|
- !ruby/object:Gem::Version
|
|
21
|
-
version: '
|
|
21
|
+
version: '7'
|
|
22
22
|
type: :runtime
|
|
23
23
|
prerelease: false
|
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
|
25
25
|
requirements:
|
|
26
26
|
- - ">="
|
|
27
27
|
- !ruby/object:Gem::Version
|
|
28
|
-
version:
|
|
28
|
+
version: 6.0.4
|
|
29
29
|
- - "<"
|
|
30
30
|
- !ruby/object:Gem::Version
|
|
31
|
-
version: '
|
|
31
|
+
version: '7'
|
|
32
32
|
- !ruby/object:Gem::Dependency
|
|
33
33
|
name: logger
|
|
34
34
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -45,21 +45,34 @@ dependencies:
|
|
|
45
45
|
version: '0'
|
|
46
46
|
email:
|
|
47
47
|
- averlato@gmail.com
|
|
48
|
-
executables:
|
|
48
|
+
executables:
|
|
49
|
+
- api_recipes
|
|
49
50
|
extensions: []
|
|
50
51
|
extra_rdoc_files: []
|
|
51
52
|
files:
|
|
52
53
|
- MIT-LICENSE
|
|
53
54
|
- README.md
|
|
55
|
+
- UPGRADING.md
|
|
56
|
+
- exe/api_recipes
|
|
54
57
|
- lib/api_recipes.rb
|
|
55
58
|
- lib/api_recipes/api.rb
|
|
59
|
+
- lib/api_recipes/cli.rb
|
|
60
|
+
- lib/api_recipes/cli/upgrade.rb
|
|
56
61
|
- lib/api_recipes/configuration.rb
|
|
57
62
|
- lib/api_recipes/endpoint.rb
|
|
58
|
-
- lib/api_recipes/
|
|
63
|
+
- lib/api_recipes/errors.rb
|
|
64
|
+
- lib/api_recipes/internals/compiler.rb
|
|
65
|
+
- lib/api_recipes/internals/context.rb
|
|
66
|
+
- lib/api_recipes/internals/credential.rb
|
|
67
|
+
- lib/api_recipes/internals/declaration.rb
|
|
68
|
+
- lib/api_recipes/internals/definition.rb
|
|
69
|
+
- lib/api_recipes/internals/handles.rb
|
|
70
|
+
- lib/api_recipes/internals/level.rb
|
|
71
|
+
- lib/api_recipes/internals/path_template.rb
|
|
72
|
+
- lib/api_recipes/internals/registry.rb
|
|
73
|
+
- lib/api_recipes/internals/request.rb
|
|
74
|
+
- lib/api_recipes/internals/template.rb
|
|
59
75
|
- lib/api_recipes/response.rb
|
|
60
|
-
- lib/api_recipes/route.rb
|
|
61
|
-
- lib/api_recipes/settings.rb
|
|
62
|
-
- lib/api_recipes/utils.rb
|
|
63
76
|
- lib/api_recipes/version.rb
|
|
64
77
|
homepage: https://github.com/madAle/api_recipes
|
|
65
78
|
licenses:
|
|
@@ -67,6 +80,7 @@ licenses:
|
|
|
67
80
|
metadata:
|
|
68
81
|
changelog_uri: https://github.com/madAle/api_recipes/blob/master/CHANGELOG.md
|
|
69
82
|
source_code_uri: https://github.com/madAle/api_recipes
|
|
83
|
+
rubygems_mfa_required: 'true'
|
|
70
84
|
rdoc_options: []
|
|
71
85
|
require_paths:
|
|
72
86
|
- lib
|
|
@@ -74,7 +88,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
74
88
|
requirements:
|
|
75
89
|
- - ">="
|
|
76
90
|
- !ruby/object:Gem::Version
|
|
77
|
-
version:
|
|
91
|
+
version: 3.3.0
|
|
78
92
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
93
|
requirements:
|
|
80
94
|
- - ">="
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
module ApiRecipes
|
|
2
|
-
|
|
3
|
-
class ApiNameClashError < Exception
|
|
4
|
-
def initialize(object, endpoint_name)
|
|
5
|
-
message = "#{object.class} already defines a method called '#{endpoint_name}'. Tip: change api name"
|
|
6
|
-
super(message)
|
|
7
|
-
end
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
class RouteAndResourceNamesClashError < Exception
|
|
11
|
-
def initialize(route_name, resource_name)
|
|
12
|
-
message = "route name (#{route_name}) can't be equal to resource name (#{resource_name}). Please change route or resource name."
|
|
13
|
-
super(message)
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
class MissingRouteAttribute < Exception
|
|
18
|
-
def initialize(endpoint = nil, route = nil, attribute = nil)
|
|
19
|
-
message = "route '#{endpoint}.#{route}' requires '#{attribute}' attribute but this was not provided"
|
|
20
|
-
super(message)
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
class PathParamsMismatch < Exception
|
|
25
|
-
def initialize(path, expected_params, provided_params)
|
|
26
|
-
if expected_params.size == 0
|
|
27
|
-
message = "route '#{path}' requires NO PARAMS but #{provided_params} were provided"
|
|
28
|
-
else
|
|
29
|
-
message = "route '#{path}' requires params #{expected_params} but #{provided_params} were provided"
|
|
30
|
-
end
|
|
31
|
-
super(message)
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
class ProvidedObjectNotAsResponseData < Exception
|
|
36
|
-
def initialize(object_class, data_class)
|
|
37
|
-
message = "provided object #{object_class} is not compatible with response data that is of type #{data_class}"
|
|
38
|
-
super(message)
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
class ResponseCodeNotAsExpected < Exception
|
|
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"
|
|
52
|
-
end
|
|
53
|
-
super(message)
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
class ApiConfigIsNotAnHash < Exception
|
|
58
|
-
def initialize(endpoint)
|
|
59
|
-
message = "provided config for endpoint '#{endpoint}' must be an Hash"
|
|
60
|
-
super(message)
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
class NoConfigurationGivenForEndpoint < Exception
|
|
65
|
-
attr_reader :endpoint
|
|
66
|
-
|
|
67
|
-
def initialize(message = nil, endpoint = nil)
|
|
68
|
-
@endpoint = endpoint
|
|
69
|
-
if message
|
|
70
|
-
# Nothing to do
|
|
71
|
-
else
|
|
72
|
-
message = "no configuration provided for endpoint '#{@endpoint}'"
|
|
73
|
-
end
|
|
74
|
-
super(message)
|
|
75
|
-
end
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
class RouteNameClashWithExistentMethod < Exception
|
|
79
|
-
def initialize(endpoint_name, route_name)
|
|
80
|
-
message = "can't define route '#{route_name}' method in endpoint '#{endpoint_name}' because method '#{route_name}' has already been defined"
|
|
81
|
-
super(message)
|
|
82
|
-
end
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
class NoRouteExists < Exception
|
|
86
|
-
def initialize(endpoint_name)
|
|
87
|
-
message = "no route defined on #{endpoint_name}"
|
|
88
|
-
super(message)
|
|
89
|
-
end
|
|
90
|
-
end
|
|
91
|
-
end
|
data/lib/api_recipes/route.rb
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
module ApiRecipes
|
|
2
|
-
class Route
|
|
3
|
-
|
|
4
|
-
attr_reader :request, :response, :url
|
|
5
|
-
attr_accessor :request_params, :attributes, :path
|
|
6
|
-
|
|
7
|
-
def initialize(api: nil, endpoint: nil, path: nil, attributes: {}, req_pars: {})
|
|
8
|
-
@api = api
|
|
9
|
-
@endpoint = endpoint
|
|
10
|
-
@path = path.to_s
|
|
11
|
-
@attributes = attributes
|
|
12
|
-
self.request_params = req_pars
|
|
13
|
-
@url = nil
|
|
14
|
-
|
|
15
|
-
prepare_request
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def fill(object)
|
|
19
|
-
data = @response.parse @attributes[:mime_type]
|
|
20
|
-
if block_given?
|
|
21
|
-
tap do
|
|
22
|
-
try_to_fill object, data
|
|
23
|
-
yield object, data, @response.status
|
|
24
|
-
end
|
|
25
|
-
else
|
|
26
|
-
try_to_fill object, data
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
def start_request(&block)
|
|
31
|
-
original_response = @request.send http_verb, @url, request_params
|
|
32
|
-
@response = Response.new original_response, attributes
|
|
33
|
-
check_response_code
|
|
34
|
-
|
|
35
|
-
if block_given?
|
|
36
|
-
tap { block.call @response }
|
|
37
|
-
else
|
|
38
|
-
response
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
private
|
|
43
|
-
|
|
44
|
-
def build_url_from_path
|
|
45
|
-
attrs = {
|
|
46
|
-
scheme: settings[:protocol].to_s,
|
|
47
|
-
host: settings[:host],
|
|
48
|
-
port: port,
|
|
49
|
-
path: path
|
|
50
|
-
}
|
|
51
|
-
URI::Generic.build2 attrs
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
def check_response_code
|
|
55
|
-
ok_code = false
|
|
56
|
-
code = @response.code
|
|
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}"
|
|
63
|
-
else
|
|
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
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
unless ok_code
|
|
76
|
-
case attributes[:on_bad_code].to_s
|
|
77
|
-
when 'ignore'
|
|
78
|
-
when 'raise'
|
|
79
|
-
raise ResponseCodeNotAsExpected.new(path, expected_code, @response, message: message)
|
|
80
|
-
end
|
|
81
|
-
end
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
def extract_headers
|
|
85
|
-
settings[:default_headers] || {}
|
|
86
|
-
end
|
|
87
|
-
|
|
88
|
-
def timeout
|
|
89
|
-
attributes.fetch(:timeout, ApiRecipes::Settings::GLOBAL_TIMEOUT)
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
def port
|
|
93
|
-
settings[:port].to_s || case settings[:protocol].to_s
|
|
94
|
-
when 'http'
|
|
95
|
-
'80'
|
|
96
|
-
when 'https'
|
|
97
|
-
'443'
|
|
98
|
-
end
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
def prepare_request
|
|
102
|
-
@url = build_url_from_path
|
|
103
|
-
puts @url if ApiRecipes.configuration.print_urls
|
|
104
|
-
|
|
105
|
-
@request = request_with_auth
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
def request_params=(params)
|
|
109
|
-
unless params.is_a? Hash
|
|
110
|
-
raise ArgumentError, 'provided params must be an Hash'
|
|
111
|
-
end
|
|
112
|
-
# Merge route attributes with defaults and deep clone route attributes
|
|
113
|
-
@request_params = params
|
|
114
|
-
end
|
|
115
|
-
|
|
116
|
-
def request_with_auth
|
|
117
|
-
req = HTTP
|
|
118
|
-
req = req.headers(extract_headers)
|
|
119
|
-
.timeout(timeout)
|
|
120
|
-
|
|
121
|
-
basic_auth = @api.basic_auth
|
|
122
|
-
if basic_auth
|
|
123
|
-
req = req.basic_auth basic_auth
|
|
124
|
-
end
|
|
125
|
-
authorization = @api.authorization
|
|
126
|
-
if authorization
|
|
127
|
-
req = req.auth authorization
|
|
128
|
-
end
|
|
129
|
-
req
|
|
130
|
-
end
|
|
131
|
-
|
|
132
|
-
def settings
|
|
133
|
-
@api.configs
|
|
134
|
-
end
|
|
135
|
-
|
|
136
|
-
def try_to_fill(object, data)
|
|
137
|
-
case data
|
|
138
|
-
when Hash
|
|
139
|
-
res = fill_object_with object, data
|
|
140
|
-
when Array
|
|
141
|
-
res = []
|
|
142
|
-
data.each do |element|
|
|
143
|
-
res << fill_object_with(object.new, element)
|
|
144
|
-
end
|
|
145
|
-
end
|
|
146
|
-
|
|
147
|
-
res
|
|
148
|
-
end
|
|
149
|
-
|
|
150
|
-
def fill_object_with(object, data)
|
|
151
|
-
data.each do |key, value|
|
|
152
|
-
begin
|
|
153
|
-
object.send "#{key}=", value
|
|
154
|
-
rescue
|
|
155
|
-
end
|
|
156
|
-
end
|
|
157
|
-
|
|
158
|
-
object
|
|
159
|
-
end
|
|
160
|
-
|
|
161
|
-
def http_verb
|
|
162
|
-
attributes[:verb]
|
|
163
|
-
end
|
|
164
|
-
end
|
|
165
|
-
end
|
data/lib/api_recipes/settings.rb
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
module ApiRecipes
|
|
2
|
-
module Settings
|
|
3
|
-
GLOBAL_TIMEOUT = 1
|
|
4
|
-
|
|
5
|
-
DEFAULT = {
|
|
6
|
-
protocol: 'https',
|
|
7
|
-
host: 'localhost',
|
|
8
|
-
port: nil,
|
|
9
|
-
base_url: nil,
|
|
10
|
-
timeout: 3,
|
|
11
|
-
on_bad_code: 'raise',
|
|
12
|
-
endpoints: {},
|
|
13
|
-
verify_with: nil,
|
|
14
|
-
mime_type: nil
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
DEFAULT_ROUTE_ATTRIBUTES = {
|
|
18
|
-
verb: :get,
|
|
19
|
-
route: 'yes',
|
|
20
|
-
path: nil,
|
|
21
|
-
ok_code: nil,
|
|
22
|
-
timeout: DEFAULT[:timeout],
|
|
23
|
-
on_bad_code: DEFAULT[:on_bad_code],
|
|
24
|
-
mime_type: DEFAULT[:mime_type],
|
|
25
|
-
verify_with: DEFAULT[:verify_with]
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
AVAILABLE_PARAMS_ENCODINGS = %w(form params json body)
|
|
29
|
-
end
|
|
30
|
-
end
|