api_recipes 3.0.0.beta1 → 3.0.0.beta3
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 +667 -78
- data/UPGRADING.md +67 -22
- data/lib/api_recipes/api.rb +14 -10
- data/lib/api_recipes/cli/import/document.rb +234 -0
- data/lib/api_recipes/cli/import/security.rb +138 -0
- data/lib/api_recipes/cli/import/server.rb +166 -0
- data/lib/api_recipes/cli/import/tree.rb +288 -0
- data/lib/api_recipes/cli/import/writer.rb +127 -0
- data/lib/api_recipes/cli/import.rb +129 -0
- data/lib/api_recipes/cli/recipes/git.rb +197 -0
- data/lib/api_recipes/cli/recipes/lock_file.rb +34 -0
- data/lib/api_recipes/cli/recipes/summary.rb +226 -0
- data/lib/api_recipes/cli/recipes.rb +287 -0
- data/lib/api_recipes/cli/routes.rb +55 -0
- data/lib/api_recipes/cli/upgrade.rb +10 -2
- data/lib/api_recipes/cli.rb +163 -1
- data/lib/api_recipes/configuration.rb +50 -13
- data/lib/api_recipes/endpoint.rb +9 -3
- data/lib/api_recipes/errors.rb +2 -1
- data/lib/api_recipes/internals/compiler.rb +146 -33
- data/lib/api_recipes/internals/context.rb +52 -14
- data/lib/api_recipes/internals/declaration.rb +18 -18
- data/lib/api_recipes/internals/definition.rb +75 -22
- data/lib/api_recipes/internals/level.rb +54 -43
- data/lib/api_recipes/internals/path_template.rb +29 -4
- data/lib/api_recipes/internals/plain_yaml.rb +133 -0
- data/lib/api_recipes/internals/recipe.rb +297 -0
- data/lib/api_recipes/internals/recipe_lock.rb +115 -0
- data/lib/api_recipes/internals/registry.rb +61 -15
- data/lib/api_recipes/internals/request.rb +26 -9
- data/lib/api_recipes/internals/template.rb +17 -5
- data/lib/api_recipes/version.rb +1 -1
- data/lib/api_recipes.rb +7 -3
- metadata +15 -1
|
@@ -10,35 +10,52 @@ module ApiRecipes
|
|
|
10
10
|
|
|
11
11
|
attr_accessor :upgrade_warnings
|
|
12
12
|
attr_reader :log_to, :log_level, :print_urls
|
|
13
|
-
|
|
13
|
+
# What values= was given inside this call of configure, as
|
|
14
|
+
# { api name => Internals::Level }, or nil. A level never shows or
|
|
15
|
+
# serializes its content. Once the configuration is in force the values
|
|
16
|
+
# are those of the registry, and this is nil again.
|
|
17
|
+
attr_reader :value_levels
|
|
14
18
|
|
|
15
19
|
def initialize
|
|
16
20
|
@sources = []
|
|
17
21
|
@apis_configs = {}
|
|
18
22
|
@apis_files_paths = []
|
|
19
|
-
@
|
|
23
|
+
@recipes_paths = []
|
|
24
|
+
@value_levels = nil
|
|
25
|
+
@draft = false
|
|
20
26
|
@print_urls = false
|
|
21
27
|
@upgrade_warnings = true
|
|
22
28
|
end
|
|
23
29
|
|
|
24
30
|
# configure works on a copy: what a setter or the caller can change in
|
|
25
31
|
# place (the list of sources, and the Hashes, Arrays and Strings of
|
|
26
|
-
# apis_configs
|
|
27
|
-
#
|
|
32
|
+
# apis_configs and apis_files_paths at every depth) is copied, so the
|
|
33
|
+
# original stays as it was. The logger of the application is shared. The
|
|
34
|
+
# copy is the draft that configure yields, the only one that takes values.
|
|
28
35
|
def initialize_copy(source)
|
|
29
36
|
super
|
|
30
37
|
@default_logger = nil
|
|
31
38
|
@sources = @sources.dup
|
|
32
39
|
@apis_configs = deep_copy(@apis_configs)
|
|
33
40
|
@apis_files_paths = deep_copy(@apis_files_paths)
|
|
34
|
-
@
|
|
41
|
+
@recipes_paths = deep_copy(@recipes_paths)
|
|
42
|
+
@value_levels = nil
|
|
43
|
+
@draft = true
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Called by configure when the draft becomes the configuration in force.
|
|
47
|
+
def close_draft
|
|
48
|
+
@draft = false
|
|
49
|
+
@value_levels = nil
|
|
50
|
+
self
|
|
35
51
|
end
|
|
36
52
|
|
|
37
53
|
# The descriptions of the apis and the variables can hold secrets (header
|
|
38
54
|
# values, tenant names): only the names of the apis are shown.
|
|
39
55
|
def inspect
|
|
40
56
|
"#<#{self.class.name} apis_configs: #{@apis_configs.keys.inspect} " \
|
|
41
|
-
"apis_files_paths: #{@apis_files_paths.size}
|
|
57
|
+
"apis_files_paths: #{@apis_files_paths.size} recipes_paths: #{@recipes_paths.size} " \
|
|
58
|
+
"print_urls: #{@print_urls.inspect}>"
|
|
42
59
|
end
|
|
43
60
|
|
|
44
61
|
def to_s = inspect
|
|
@@ -52,7 +69,7 @@ module ApiRecipes
|
|
|
52
69
|
record_source(:hash)
|
|
53
70
|
end
|
|
54
71
|
|
|
55
|
-
attr_reader :apis_configs, :apis_files_paths
|
|
72
|
+
attr_reader :apis_configs, :apis_files_paths, :recipes_paths
|
|
56
73
|
|
|
57
74
|
def apis_files_paths=(paths = [])
|
|
58
75
|
raise ArgumentError, 'apis_files_paths must be an Array' unless paths.is_a? Array
|
|
@@ -61,14 +78,34 @@ module ApiRecipes
|
|
|
61
78
|
record_source(:files)
|
|
62
79
|
end
|
|
63
80
|
|
|
64
|
-
|
|
65
|
-
|
|
81
|
+
# Portable recipes: files written by someone else, installed with the
|
|
82
|
+
# api_recipes command. They are read as data only, never with ERB, and each
|
|
83
|
+
# must match the recipes.lock of its folder.
|
|
84
|
+
def recipes_paths=(paths = [])
|
|
85
|
+
raise ArgumentError, 'recipes_paths must be an Array' unless paths.is_a? Array
|
|
86
|
+
|
|
87
|
+
@recipes_paths = paths
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# The credential and the variable values of each api, for the whole
|
|
91
|
+
# application: { github: { authorization: '...', variables: { ... } } }.
|
|
92
|
+
# Each api takes authorization or basic_auth, and variables. They are
|
|
93
|
+
# checked and copied here, and replace those given before. It is the only
|
|
94
|
+
# way to give them at this level, and it works only inside the block of
|
|
95
|
+
# configure: see Api#authorization=.
|
|
96
|
+
def values=(values)
|
|
97
|
+
unless @draft
|
|
98
|
+
raise Error, 'values are given inside the block of ApiRecipes.configure, as config.values = { ... }'
|
|
99
|
+
end
|
|
100
|
+
raise ArgumentError, 'values must be an Hash' unless values.is_a? Hash
|
|
66
101
|
|
|
67
|
-
@
|
|
102
|
+
@value_levels = Internals::Registry.value_levels(values)
|
|
68
103
|
end
|
|
69
104
|
|
|
70
105
|
# Applies the sources in the order they were assigned and returns the
|
|
71
|
-
# apis as { name => description }. A later source replaces whole apis.
|
|
106
|
+
# apis as { name => description }. A later source replaces whole apis. The
|
|
107
|
+
# recipes of recipes_paths come last and replace nothing: a name that
|
|
108
|
+
# another source defines is an error.
|
|
72
109
|
def load
|
|
73
110
|
apis = {}
|
|
74
111
|
@sources.each do |kind|
|
|
@@ -78,7 +115,7 @@ module ApiRecipes
|
|
|
78
115
|
@apis_files_paths.each { |path| merge_file(apis, path) }
|
|
79
116
|
end
|
|
80
117
|
end
|
|
81
|
-
apis
|
|
118
|
+
apis.merge(Internals::Recipe.load_all(@recipes_paths, taken: apis.keys))
|
|
82
119
|
end
|
|
83
120
|
|
|
84
121
|
# Reads one file with the rules of apis_files_paths and returns the apis as
|
|
@@ -139,7 +176,7 @@ module ApiRecipes
|
|
|
139
176
|
|
|
140
177
|
full_path = File.expand_path(path)
|
|
141
178
|
if shared_recipe?(full_path)
|
|
142
|
-
raise RecipeFileError.new(path, '
|
|
179
|
+
raise RecipeFileError.new(path, 'is a portable recipe (.recipe.yml): give it to recipes_paths, which reads it as data only')
|
|
143
180
|
end
|
|
144
181
|
|
|
145
182
|
text = read_text(path, full_path)
|
data/lib/api_recipes/endpoint.rb
CHANGED
|
@@ -14,7 +14,9 @@ module ApiRecipes
|
|
|
14
14
|
|
|
15
15
|
# args: the path values, then optionally a Hash of options for http.rb.
|
|
16
16
|
# overrides: the options given to with, for this request only.
|
|
17
|
-
|
|
17
|
+
# path: the path already built, for the copy that with returns: the
|
|
18
|
+
# variables of a path are read once, when the chain passes there.
|
|
19
|
+
def initialize(context, definition, parent_path, args, overrides = nil, path = nil)
|
|
18
20
|
@context = context
|
|
19
21
|
@definition = definition
|
|
20
22
|
@parent_path = parent_path
|
|
@@ -26,7 +28,11 @@ module ApiRecipes
|
|
|
26
28
|
count = @options ? args.size - 1 : args.size
|
|
27
29
|
raise PathParamsMismatch.new(definition.full_path, params, args.first(count)) unless count == params.size
|
|
28
30
|
|
|
29
|
-
|
|
31
|
+
if path
|
|
32
|
+
@path = path
|
|
33
|
+
return
|
|
34
|
+
end
|
|
35
|
+
own = definition.template.expand(args, endpoint: definition.full_name, context: context, &UPGRADE_WARNING)
|
|
30
36
|
if own.empty?
|
|
31
37
|
# A root endpoint with an empty path is the address of the api, which
|
|
32
38
|
# ends with a slash; a nested one is the address of its parent.
|
|
@@ -69,7 +75,7 @@ module ApiRecipes
|
|
|
69
75
|
options.each { |key, value| merged.delete(key.to_sym) if value.nil? }
|
|
70
76
|
overrides = merged.freeze
|
|
71
77
|
end
|
|
72
|
-
self.class.new(@context, @definition, @parent_path, @args, overrides)
|
|
78
|
+
self.class.new(@context, @definition, @parent_path, @args, overrides, @path)
|
|
73
79
|
end
|
|
74
80
|
|
|
75
81
|
attr_reader :definition
|
data/lib/api_recipes/errors.rb
CHANGED
|
@@ -17,9 +17,10 @@ module ApiRecipes
|
|
|
17
17
|
class RecipeFileError < ConfigurationError
|
|
18
18
|
attr_reader :path
|
|
19
19
|
|
|
20
|
+
# reason: one reason, or several.
|
|
20
21
|
def initialize(path, reason)
|
|
21
22
|
@path = path
|
|
22
|
-
super(
|
|
23
|
+
super(Array(reason).map { |one| "#{path}: #{one}" })
|
|
23
24
|
end
|
|
24
25
|
end
|
|
25
26
|
|
|
@@ -7,10 +7,11 @@ module ApiRecipes
|
|
|
7
7
|
# all the problems found are reported together in one ConfigurationError.
|
|
8
8
|
class Compiler
|
|
9
9
|
API_KEYS = %i[protocol host port base_url timeout on_bad_code mime_type verify_with default_headers
|
|
10
|
-
endpoints auth variables recipe].freeze
|
|
10
|
+
default_params endpoints auth variables recipe].freeze
|
|
11
11
|
WITH_API_KEYS = %i[protocol host port base_url timeout on_bad_code mime_type verify_with default_headers
|
|
12
|
-
variables authorization basic_auth].freeze
|
|
13
|
-
ENDPOINT_KEYS = %i[path verb route ok_code timeout on_bad_code mime_type verify_with
|
|
12
|
+
default_params variables authorization basic_auth].freeze
|
|
13
|
+
ENDPOINT_KEYS = %i[path verb route ok_code timeout on_bad_code mime_type verify_with default_headers
|
|
14
|
+
default_params resource endpoints].freeze
|
|
14
15
|
WITH_ENDPOINT_KEYS = %i[timeout on_bad_code ok_code mime_type verify_with].freeze
|
|
15
16
|
SCOPE_KEYS = { api: API_KEYS, with_api: WITH_API_KEYS, endpoint: ENDPOINT_KEYS,
|
|
16
17
|
with_endpoint: WITH_ENDPOINT_KEYS }.freeze
|
|
@@ -26,6 +27,22 @@ module ApiRecipes
|
|
|
26
27
|
CONTROL = /[\x00-\x1F\x7F]/
|
|
27
28
|
IGNORED_KEY = /\Ax[-_]/
|
|
28
29
|
|
|
30
|
+
# The endpoints that `resource` adds under an endpoint, which is itself
|
|
31
|
+
# the list: the names the importer gives to the same operations.
|
|
32
|
+
RESOURCE = {
|
|
33
|
+
show: { path: '/:id' }.freeze,
|
|
34
|
+
create: { path: '', verb: :post }.freeze,
|
|
35
|
+
update: { path: '/:id', verb: :patch }.freeze,
|
|
36
|
+
replace: { path: '/:id', verb: :put }.freeze,
|
|
37
|
+
delete: { path: '/:id', verb: :delete }.freeze
|
|
38
|
+
}.freeze
|
|
39
|
+
|
|
40
|
+
# What the endpoints of one api are checked against. variables: the
|
|
41
|
+
# names a path, a header or a parameter may use, nil when they are not
|
|
42
|
+
# known. credential_param: the query parameter that carries the
|
|
43
|
+
# credential, which default_params cannot set.
|
|
44
|
+
Names = Data.define(:variables, :credential_param)
|
|
45
|
+
|
|
29
46
|
class << self
|
|
30
47
|
def compile(name, config)
|
|
31
48
|
compile_all({ name => config }).values.first
|
|
@@ -58,11 +75,14 @@ module ApiRecipes
|
|
|
58
75
|
# scope: :api, :with_api or :with_endpoint. Returns the problems, never raises.
|
|
59
76
|
# variables, for :api only: the names that the options may use beside
|
|
60
77
|
# those they declare; :any when they are not known yet.
|
|
61
|
-
|
|
78
|
+
# credential_param, for :api only: the query parameter that carries
|
|
79
|
+
# the credential of the api the options are merged with, which they
|
|
80
|
+
# cannot set unless they give an auth of their own.
|
|
81
|
+
def check_options(options, scope, variables: [], credential_param: nil)
|
|
62
82
|
raise ArgumentError, "unknown scope '#{scope}'" unless %i[api with_api with_endpoint].include?(scope)
|
|
63
83
|
|
|
64
84
|
compiler = new
|
|
65
|
-
compiler.check(options, scope, variables)
|
|
85
|
+
compiler.check(options, scope, variables, credential_param)
|
|
66
86
|
compiler.errors
|
|
67
87
|
end
|
|
68
88
|
|
|
@@ -91,6 +111,20 @@ module ApiRecipes
|
|
|
91
111
|
# for a later compilation cannot be changed by the caller.
|
|
92
112
|
def frozen_copy(value) = new.frozen_copy(value)
|
|
93
113
|
|
|
114
|
+
# The names of the endpoints that a value of `resource` adds, in the
|
|
115
|
+
# order of RESOURCE; nil when the value is not valid.
|
|
116
|
+
def resource_names(value)
|
|
117
|
+
case value
|
|
118
|
+
when nil, false, 'no' then []
|
|
119
|
+
when true, 'yes' then RESOURCE.keys
|
|
120
|
+
when Array
|
|
121
|
+
names = value.map { |name| name.is_a?(String) || name.is_a?(Symbol) ? name.to_sym : nil }
|
|
122
|
+
return nil if names.empty? || !names.all? { |name| RESOURCE.key?(name) }
|
|
123
|
+
|
|
124
|
+
RESOURCE.keys & names
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
94
128
|
private
|
|
95
129
|
|
|
96
130
|
# A nil option keeps what the base has. Among endpoints a nil value
|
|
@@ -103,12 +137,21 @@ module ApiRecipes
|
|
|
103
137
|
|
|
104
138
|
merged[key] = if IGNORED_KEY.match?(key.to_s) || !(value.is_a?(Hash) && merged[key].is_a?(Hash))
|
|
105
139
|
value
|
|
140
|
+
elsif key == :default_headers && !endpoints
|
|
141
|
+
merge_headers(merged[key], value)
|
|
106
142
|
else
|
|
107
143
|
merge(merged[key], value, !endpoints && key == :endpoints)
|
|
108
144
|
end
|
|
109
145
|
end
|
|
110
146
|
merged
|
|
111
147
|
end
|
|
148
|
+
|
|
149
|
+
# A header of override takes the place of the one of base with its
|
|
150
|
+
# name, however it is written.
|
|
151
|
+
def merge_headers(base, override)
|
|
152
|
+
names = override.each_key.to_h { |name| [Definition.header_key(name), true] }
|
|
153
|
+
base.reject { |name, _| names.key?(Definition.header_key(name)) }.merge(override)
|
|
154
|
+
end
|
|
112
155
|
end
|
|
113
156
|
|
|
114
157
|
attr_reader :errors
|
|
@@ -130,13 +173,13 @@ module ApiRecipes
|
|
|
130
173
|
end.freeze
|
|
131
174
|
end
|
|
132
175
|
|
|
133
|
-
def check(options, scope, variables = [])
|
|
176
|
+
def check(options, scope, variables = [], credential_param = nil)
|
|
134
177
|
unless options.is_a?(Hash)
|
|
135
178
|
add('', 'options must be a Hash')
|
|
136
179
|
return
|
|
137
180
|
end
|
|
138
181
|
options = normalize(options, '')
|
|
139
|
-
scope == :api ? api_attrs(nil, options, '', variables) : validate_with(options, scope)
|
|
182
|
+
scope == :api ? api_attrs(nil, options, '', variables, credential_param) : validate_with(options, scope)
|
|
140
183
|
end
|
|
141
184
|
|
|
142
185
|
def resolve(options, scope)
|
|
@@ -292,7 +335,9 @@ module ApiRecipes
|
|
|
292
335
|
# ---- api ----
|
|
293
336
|
|
|
294
337
|
# known: names of variables declared elsewhere, or :any to accept every name.
|
|
295
|
-
|
|
338
|
+
# credential_param: the one of the api these options are merged with,
|
|
339
|
+
# used when they give no auth.
|
|
340
|
+
def api_attrs(_name, config, location, known = [], credential_param = nil)
|
|
296
341
|
check_keys(config, API_KEYS, location)
|
|
297
342
|
variables = config[:variables].is_a?(Hash) ? config[:variables] : {}
|
|
298
343
|
declared = known == :any ? nil : variables.keys + known
|
|
@@ -308,12 +353,19 @@ module ApiRecipes
|
|
|
308
353
|
attrs[:verify_with] = option(:verify_with, config, location, :api, declared)
|
|
309
354
|
attrs[:default_headers] = option(:default_headers, config, location, :api, declared)
|
|
310
355
|
attrs[:auth] = option(:auth, config, location, :api, declared)
|
|
356
|
+
credential_param = credential_param(attrs[:auth]) unless config[:auth].nil?
|
|
357
|
+
names = Names.new(variables: declared, credential_param: credential_param)
|
|
358
|
+
attrs[:default_params] = option(:default_params, config, location, :api, names)
|
|
311
359
|
attrs[:variables] = option(:variables, config, location, :api, declared)
|
|
312
360
|
attrs[:recipe] = option(:recipe, config, location, :api, declared)
|
|
313
|
-
attrs[:endpoints] = endpoints_attrs(config[:endpoints], join(location, :endpoints))
|
|
361
|
+
attrs[:endpoints] = endpoints_attrs(config[:endpoints], join(location, :endpoints), names)
|
|
314
362
|
attrs
|
|
315
363
|
end
|
|
316
364
|
|
|
365
|
+
def credential_param(auth)
|
|
366
|
+
auth && auth[:type] == :query ? auth[:name] : nil
|
|
367
|
+
end
|
|
368
|
+
|
|
317
369
|
def validate_with(options, scope)
|
|
318
370
|
allowed = SCOPE_KEYS.fetch(scope)
|
|
319
371
|
check_keys(options, allowed, '')
|
|
@@ -327,7 +379,7 @@ module ApiRecipes
|
|
|
327
379
|
|
|
328
380
|
# ---- endpoints ----
|
|
329
381
|
|
|
330
|
-
def endpoints_attrs(value, location)
|
|
382
|
+
def endpoints_attrs(value, location, names)
|
|
331
383
|
return {} if value.nil?
|
|
332
384
|
|
|
333
385
|
unless value.is_a?(Hash)
|
|
@@ -344,20 +396,46 @@ module ApiRecipes
|
|
|
344
396
|
add(node_location, 'must be a Hash or empty')
|
|
345
397
|
next
|
|
346
398
|
end
|
|
347
|
-
out[name.to_sym] = endpoint_attrs(name.to_sym, options, node_location)
|
|
399
|
+
out[name.to_sym] = endpoint_attrs(name.to_sym, options, node_location, names)
|
|
348
400
|
end
|
|
349
401
|
end
|
|
350
402
|
|
|
351
|
-
def endpoint_attrs(name, options, location)
|
|
403
|
+
def endpoint_attrs(name, options, location, names)
|
|
352
404
|
check_keys(options, ENDPOINT_KEYS, location)
|
|
405
|
+
declared = names.variables
|
|
353
406
|
own = {}
|
|
354
|
-
(ENDPOINT_KEYS - %i[endpoints]).each do |key|
|
|
355
|
-
value = option(key, options, location, :endpoint,
|
|
407
|
+
(ENDPOINT_KEYS - %i[endpoints resource]).each do |key|
|
|
408
|
+
value = option(key, options, location, :endpoint, key == :default_params ? names : declared)
|
|
356
409
|
own[key] = value unless value.nil?
|
|
357
410
|
end
|
|
411
|
+
children = with_resource(options[:resource], options[:endpoints], join(location, :resource))
|
|
358
412
|
path = own[:path] || name.to_s
|
|
359
413
|
template = parse_path(path, join(location, :path), options.key?(:path) && !options[:path].nil?)
|
|
360
|
-
|
|
414
|
+
unless declared.nil?
|
|
415
|
+
(template.variable_names - declared).each { |variable| add(join(location, :path), "unknown variable #{shown(variable)}") }
|
|
416
|
+
end
|
|
417
|
+
{ own: own, template: template,
|
|
418
|
+
children: endpoints_attrs(children, join(location, :endpoints), names) }
|
|
419
|
+
end
|
|
420
|
+
|
|
421
|
+
# The endpoints written under an endpoint, with those its `resource`
|
|
422
|
+
# adds before them. An endpoint written with the name of a generated
|
|
423
|
+
# one completes it: its options win, a nil one keeps the generated value.
|
|
424
|
+
def with_resource(value, written, where)
|
|
425
|
+
added = self.class.resource_names(value)
|
|
426
|
+
if added.nil?
|
|
427
|
+
add(where, "must be true, false or a list of: #{RESOURCE.keys.join(', ')}")
|
|
428
|
+
return written
|
|
429
|
+
end
|
|
430
|
+
return written if added.empty? || !(written.nil? || written.is_a?(Hash))
|
|
431
|
+
|
|
432
|
+
written ||= {}
|
|
433
|
+
generated = added.to_h do |name|
|
|
434
|
+
given = written[name]
|
|
435
|
+
given = RESOURCE[name].merge(given) { |_key, usual, own| own.nil? ? usual : own } if given.is_a?(Hash)
|
|
436
|
+
[name, given.nil? ? RESOURCE[name] : given]
|
|
437
|
+
end
|
|
438
|
+
generated.merge(written.reject { |name, _| generated.key?(name) })
|
|
361
439
|
end
|
|
362
440
|
|
|
363
441
|
def parse_path(path, location, written)
|
|
@@ -387,6 +465,7 @@ module ApiRecipes
|
|
|
387
465
|
when :mime_type then mime_type(value, where)
|
|
388
466
|
when :verify_with then method_name(value, where)
|
|
389
467
|
when :default_headers then headers(value, where, declared)
|
|
468
|
+
when :default_params then params(value, where, declared)
|
|
390
469
|
when :auth then auth(value, where)
|
|
391
470
|
when :variables then scope == :with_api ? variable_values(value, where) : variable_declarations(value, where)
|
|
392
471
|
when :recipe then recipe(value, where)
|
|
@@ -476,26 +555,21 @@ module ApiRecipes
|
|
|
476
555
|
add(where, 'must be a string')
|
|
477
556
|
return nil
|
|
478
557
|
end
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
placeholder ||= !template.param_names.empty?
|
|
488
|
-
template.to_s
|
|
489
|
-
rescue ArgumentError
|
|
490
|
-
placeholder = true
|
|
491
|
-
piece
|
|
492
|
-
end
|
|
493
|
-
end.join
|
|
494
|
-
if placeholder
|
|
558
|
+
# Read as a path: the same fixed text, {variables} and \{ of the
|
|
559
|
+
# path of an endpoint, without its placeholders.
|
|
560
|
+
template = begin
|
|
561
|
+
PathTemplate.parse(value.to_s)
|
|
562
|
+
rescue ArgumentError
|
|
563
|
+
nil
|
|
564
|
+
end
|
|
565
|
+
if template.nil? || !template.param_names.empty?
|
|
495
566
|
add(where, 'path placeholders are not allowed here; declare a variable and write {name}')
|
|
496
567
|
return nil
|
|
497
568
|
end
|
|
498
|
-
|
|
569
|
+
unless declared.nil?
|
|
570
|
+
(template.variable_names - declared).each { |name| add(where, "unknown variable #{shown(name)}") }
|
|
571
|
+
end
|
|
572
|
+
template.to_s.freeze
|
|
499
573
|
end
|
|
500
574
|
|
|
501
575
|
def headers(value, where, declared)
|
|
@@ -503,10 +577,16 @@ module ApiRecipes
|
|
|
503
577
|
add(where, 'must be a Hash')
|
|
504
578
|
return nil
|
|
505
579
|
end
|
|
580
|
+
seen = {}
|
|
506
581
|
value.each_with_object({}) do |(name, header), out|
|
|
507
582
|
name = name.to_s
|
|
508
583
|
header_where = join(where, name)
|
|
509
584
|
add(header_where, 'is not a valid header name') unless TOKEN.match?(name)
|
|
585
|
+
first = seen[Definition.header_key(name)] ||= name
|
|
586
|
+
unless first.equal?(name)
|
|
587
|
+
add(header_where, "is given twice, as #{shown(first)} and as #{shown(name)}: the name of a header has " \
|
|
588
|
+
'no case, and _ is read as -')
|
|
589
|
+
end
|
|
510
590
|
unless header.is_a?(String) || header.is_a?(Symbol) || header.is_a?(Numeric)
|
|
511
591
|
add(header_where, 'must be a string')
|
|
512
592
|
next
|
|
@@ -518,6 +598,34 @@ module ApiRecipes
|
|
|
518
598
|
end.freeze
|
|
519
599
|
end
|
|
520
600
|
|
|
601
|
+
# The query parameters sent with every request, as { name => text }.
|
|
602
|
+
# names: a Names, or nil in a with, where nothing is known.
|
|
603
|
+
def params(value, where, names)
|
|
604
|
+
unless value.is_a?(Hash)
|
|
605
|
+
add(where, 'must be a Hash')
|
|
606
|
+
return nil
|
|
607
|
+
end
|
|
608
|
+
value.each_with_object({}) do |(name, param), out|
|
|
609
|
+
name = name.to_s
|
|
610
|
+
if name.empty?
|
|
611
|
+
add(where, 'a parameter name must not be empty')
|
|
612
|
+
next
|
|
613
|
+
end
|
|
614
|
+
param_where = join(where, name)
|
|
615
|
+
add(param_where, 'the name must not contain control characters') if CONTROL.match?(name)
|
|
616
|
+
if names && name == names.credential_param
|
|
617
|
+
add(param_where, 'is the parameter of the credential (auth.name): it cannot be set here')
|
|
618
|
+
end
|
|
619
|
+
unless param.is_a?(String) || param.is_a?(Symbol) || param.is_a?(Numeric) || param == true || param == false
|
|
620
|
+
add(param_where, 'must be a string, a number or a boolean')
|
|
621
|
+
next
|
|
622
|
+
end
|
|
623
|
+
text = param.to_s
|
|
624
|
+
check_variables(text, param_where, names&.variables)
|
|
625
|
+
out[name.dup.freeze] = text.dup.freeze
|
|
626
|
+
end.freeze
|
|
627
|
+
end
|
|
628
|
+
|
|
521
629
|
def auth(value, where)
|
|
522
630
|
unless value.is_a?(Hash)
|
|
523
631
|
add(where, 'must be a Hash')
|
|
@@ -576,7 +684,9 @@ module ApiRecipes
|
|
|
576
684
|
return nil
|
|
577
685
|
end
|
|
578
686
|
value.each_key { |name| add(join(where, name), 'is not a valid variable name') unless VARIABLE_NAME.match?(name.to_s) }
|
|
579
|
-
|
|
687
|
+
# Kept as text: an object that changes later would move the host after
|
|
688
|
+
# the values were given.
|
|
689
|
+
value.to_h { |name, item| [name, variable_text(item, join(where, name))] }.freeze
|
|
580
690
|
end
|
|
581
691
|
|
|
582
692
|
def recipe(value, where)
|
|
@@ -646,6 +756,7 @@ module ApiRecipes
|
|
|
646
756
|
host = attrs[:host] || 'localhost'
|
|
647
757
|
base_url = attrs[:base_url]
|
|
648
758
|
headers = attrs[:default_headers] || {}
|
|
759
|
+
params = attrs[:default_params] || {}
|
|
649
760
|
Definition::Api.new(
|
|
650
761
|
name: name,
|
|
651
762
|
protocol: attrs[:protocol] || 'https',
|
|
@@ -657,12 +768,14 @@ module ApiRecipes
|
|
|
657
768
|
mime_type: attrs[:mime_type],
|
|
658
769
|
verify_with: attrs[:verify_with],
|
|
659
770
|
default_headers: headers.freeze,
|
|
771
|
+
default_params: params.freeze,
|
|
660
772
|
auth: attrs[:auth],
|
|
661
773
|
variables: (attrs[:variables] || {}).freeze,
|
|
662
774
|
recipe: attrs[:recipe],
|
|
663
775
|
host_template: Template.new(host, :host),
|
|
664
776
|
base_url_template: base_url && Template.new(base_url, :base_url),
|
|
665
777
|
header_templates: headers.transform_values { |text| Template.new(text, :header) }.freeze,
|
|
778
|
+
param_templates: params.transform_values { |text| Template.new(text, :param) }.freeze,
|
|
666
779
|
children: ->(api) { build_endpoints(attrs[:endpoints], api) }
|
|
667
780
|
)
|
|
668
781
|
end
|
|
@@ -28,8 +28,8 @@ module ApiRecipes
|
|
|
28
28
|
end
|
|
29
29
|
|
|
30
30
|
# A new context with these options on top of the current ones, as the
|
|
31
|
-
# compiler resolved them. default_headers and variables
|
|
32
|
-
# options replace. A key given with nil: no credential for authorization
|
|
31
|
+
# compiler resolved them. default_headers, default_params and variables
|
|
32
|
+
# add up, the other options replace. A key given with nil: no credential for authorization
|
|
33
33
|
# and basic_auth, no override for the others.
|
|
34
34
|
def with(options)
|
|
35
35
|
resolved = Compiler.resolve_options(options, :with_api)
|
|
@@ -54,6 +54,9 @@ module ApiRecipes
|
|
|
54
54
|
# reads them once and passes them as chain to the methods below.
|
|
55
55
|
def levels = Levels.chain(@owner, @definition.name, @declaration)
|
|
56
56
|
|
|
57
|
+
# The level of the owner alone, or nil.
|
|
58
|
+
def own_level = Levels.own(@owner, @definition.name, @declaration)
|
|
59
|
+
|
|
57
60
|
# The value of an option, from the nearest place that has one.
|
|
58
61
|
def setting(key, endpoint = nil, endpoint_overrides = nil)
|
|
59
62
|
if endpoint
|
|
@@ -90,13 +93,20 @@ module ApiRecipes
|
|
|
90
93
|
"/#{template.expand(variables, api: @definition.name)}".squeeze('/')
|
|
91
94
|
end
|
|
92
95
|
|
|
93
|
-
# The default headers with their variables filled
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
96
|
+
# The default headers with their variables filled: those of the api,
|
|
97
|
+
# then the ones given to with, then the ones of endpoint and of the
|
|
98
|
+
# endpoints above it. A header replaces the one with its name, whatever
|
|
99
|
+
# the case.
|
|
100
|
+
def headers(chain: levels, endpoint: nil)
|
|
101
|
+
filled(layers(:header, @definition.header_templates, @overrides[:default_headers],
|
|
102
|
+
endpoint && endpoint.header_templates), chain)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# The default query parameters with their variables filled, as
|
|
106
|
+
# { name => text }, in the same order as the headers.
|
|
107
|
+
def params(chain: levels, endpoint: nil)
|
|
108
|
+
filled(layers(:param, @definition.param_templates, @overrides[:default_params],
|
|
109
|
+
endpoint && endpoint.param_templates), chain)
|
|
100
110
|
end
|
|
101
111
|
|
|
102
112
|
# The nearest credential: the one given to with, then the levels from the
|
|
@@ -154,6 +164,31 @@ module ApiRecipes
|
|
|
154
164
|
|
|
155
165
|
private
|
|
156
166
|
|
|
167
|
+
NONE = {}.freeze
|
|
168
|
+
|
|
169
|
+
# The templates of the api, of with and of the endpoint, the later over
|
|
170
|
+
# the earlier.
|
|
171
|
+
def layers(position, api, given, endpoint)
|
|
172
|
+
templates = api
|
|
173
|
+
unless given.nil? || given.empty?
|
|
174
|
+
templates = over(position, templates, given.transform_values { |text| Template.new(text, position) })
|
|
175
|
+
end
|
|
176
|
+
templates = over(position, templates, endpoint) unless endpoint.nil? || endpoint.empty?
|
|
177
|
+
templates
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def over(position, base, added)
|
|
181
|
+
position == :header ? Definition.over_headers(base, added) : base.merge(added)
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
# The variables are read only when there is something to fill.
|
|
185
|
+
def filled(templates, chain)
|
|
186
|
+
return NONE if templates.empty?
|
|
187
|
+
|
|
188
|
+
values = variables(chain: chain)
|
|
189
|
+
templates.transform_values { |template| template.expand(values, api: @definition.name) }
|
|
190
|
+
end
|
|
191
|
+
|
|
157
192
|
# The tree a level reads: the global one for the global level, else the owner's.
|
|
158
193
|
def definition_for(kind) = kind == :global ? @global_definition : @definition
|
|
159
194
|
|
|
@@ -201,7 +236,8 @@ module ApiRecipes
|
|
|
201
236
|
# every other level of the chain was written through the declaration in
|
|
202
237
|
# use, whose tree that is.
|
|
203
238
|
def check_origin(chain, index, destination)
|
|
204
|
-
|
|
239
|
+
registry = chain.respond_to?(:registry) ? chain.registry : Registry.current
|
|
240
|
+
bound = origin_from(definition_in_force(chain[index].first, registry), chain, index, with: false)
|
|
205
241
|
return if bound && destination && bound.casecmp?(destination)
|
|
206
242
|
|
|
207
243
|
raise CredentialOriginMismatch.new(@definition.name, bound, destination)
|
|
@@ -211,9 +247,9 @@ module ApiRecipes
|
|
|
211
247
|
# configure still holds the tree it was created with, and so its
|
|
212
248
|
# destination, while the levels are read when the request is sent: their
|
|
213
249
|
# credentials are bound to the origin that the configuration in force
|
|
214
|
-
# gives them. nil when that configuration no longer has the api.
|
|
215
|
-
|
|
216
|
-
|
|
250
|
+
# gives them. nil when that configuration no longer has the api. registry
|
|
251
|
+
# is the one the levels of the request were read from.
|
|
252
|
+
def definition_in_force(kind, registry)
|
|
217
253
|
return definition_for(kind) if @generation.nil? || @generation == registry.generation
|
|
218
254
|
return registry.definitions[@definition.name] if kind == :global || @declaration.nil?
|
|
219
255
|
|
|
@@ -226,7 +262,9 @@ module ApiRecipes
|
|
|
226
262
|
merged[:credential] = Credential.from(key, value)
|
|
227
263
|
elsif value.nil?
|
|
228
264
|
merged.delete(key)
|
|
229
|
-
elsif
|
|
265
|
+
elsif key == :default_headers
|
|
266
|
+
merged[key] = Definition.over_headers(merged[key] || {}, value)
|
|
267
|
+
elsif %i[default_params variables].include?(key)
|
|
230
268
|
merged[key] = (merged[key] || {}).merge(value).freeze
|
|
231
269
|
else
|
|
232
270
|
merged[key] = value
|