api_recipes 3.0.0.beta2 → 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 +515 -78
- data/UPGRADING.md +67 -22
- data/lib/api_recipes/api.rb +14 -10
- 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 +7 -2
- data/lib/api_recipes/cli/upgrade.rb +10 -2
- data/lib/api_recipes/cli.rb +67 -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 +8 -1
|
@@ -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
|
|
@@ -33,21 +33,25 @@ module ApiRecipes
|
|
|
33
33
|
|
|
34
34
|
options = Compiler.normalize(options || {})
|
|
35
35
|
method_name = method_for(options.delete(:as), name)
|
|
36
|
-
variables = options.delete(:variables)
|
|
37
36
|
api_name = name.to_sym
|
|
38
37
|
check_clash(klass, api_name, method_name)
|
|
39
|
-
|
|
38
|
+
# What the class gives to its level: values, not options of the tree.
|
|
39
|
+
given = options.slice(*Registry::VALUE_KEYS)
|
|
40
|
+
options = options.except(*Registry::VALUE_KEYS)
|
|
41
|
+
level = Registry.level_from(given, api_name.to_s)
|
|
42
|
+
variables = level.variables
|
|
40
43
|
global = Registry.current.definitions[api_name]
|
|
41
44
|
check_variables(api_name, variables, global)
|
|
42
45
|
# The variables that the options use are those of the global api.
|
|
43
46
|
# Without one yet, the tree compiled at the first use checks them.
|
|
44
|
-
|
|
47
|
+
auth = global && global.auth
|
|
48
|
+
errors = Compiler.check_options(options, :api, variables: global ? global.variables.keys : :any,
|
|
49
|
+
credential_param: auth && auth[:type] == :query ? auth[:name] : nil)
|
|
45
50
|
raise ConfigurationError.new(errors) unless errors.empty?
|
|
46
51
|
|
|
47
|
-
declaration = new(klass, api_name, Compiler.frozen_copy(options), variables)
|
|
52
|
+
declaration = new(klass, api_name, Compiler.frozen_copy(options), variables, level.credential)
|
|
48
53
|
declared = klass.instance_variable_get(IVAR)
|
|
49
54
|
klass.instance_variable_set(IVAR, (declared.is_a?(Hash) ? declared : {}).merge(api_name => declaration).freeze)
|
|
50
|
-
declaration.write_variables
|
|
51
55
|
klass.define_method(method_name) { declaration.api(self) }
|
|
52
56
|
klass.define_singleton_method(method_name) { declaration.api(self) }
|
|
53
57
|
method_name
|
|
@@ -105,12 +109,14 @@ module ApiRecipes
|
|
|
105
109
|
end
|
|
106
110
|
end
|
|
107
111
|
|
|
108
|
-
def initialize(klass, name, options, variables)
|
|
112
|
+
def initialize(klass, name, options, variables, credential = nil)
|
|
109
113
|
@klass = klass
|
|
110
114
|
@name = name
|
|
111
115
|
@options = options
|
|
112
116
|
@variables = variables
|
|
113
|
-
|
|
117
|
+
# What the class or module gives to its level, fixed here: nothing
|
|
118
|
+
# writes it afterwards, and no reset! removes it. See Api#authorization=.
|
|
119
|
+
@level = variables.nil? && credential.nil? ? nil : Level.new(credential: credential, variables: variables)
|
|
114
120
|
@tree = nil
|
|
115
121
|
@lock = Mutex.new
|
|
116
122
|
@checked = ObjectSpace::WeakMap.new
|
|
@@ -119,6 +125,9 @@ module ApiRecipes
|
|
|
119
125
|
# The class or module where `api` was called.
|
|
120
126
|
def declared_in = @klass
|
|
121
127
|
|
|
128
|
+
# The level of that class or module, or nil.
|
|
129
|
+
attr_reader :level
|
|
130
|
+
|
|
122
131
|
# The api for owner: the class or module, a subclass, an includer, or an
|
|
123
132
|
# instance of one of them.
|
|
124
133
|
def api(owner)
|
|
@@ -131,15 +140,7 @@ module ApiRecipes
|
|
|
131
140
|
# The tree of this declaration under the configuration in force.
|
|
132
141
|
def definition = current_tree.definition
|
|
133
142
|
|
|
134
|
-
#
|
|
135
|
-
# a reset! of the levels.
|
|
136
|
-
def write_variables
|
|
137
|
-
return if @variables.nil?
|
|
138
|
-
|
|
139
|
-
LevelSet.write(@klass, self) { |level| level.with(variables: @variables) }
|
|
140
|
-
@variables_generation = LevelSet.generation
|
|
141
|
-
end
|
|
142
|
-
|
|
143
|
+
# The credential is never shown.
|
|
143
144
|
def inspect = "#<#{self.class.name} #{@name}>"
|
|
144
145
|
|
|
145
146
|
def to_s = inspect
|
|
@@ -156,7 +157,7 @@ module ApiRecipes
|
|
|
156
157
|
# one, so asking for it never creates one (an object can also reach the
|
|
157
158
|
# declaration through a method copied with define_method).
|
|
158
159
|
def check_owner(owner, generation)
|
|
159
|
-
klass =
|
|
160
|
+
klass = Module === owner ? owner : owner.class
|
|
160
161
|
unless @checked[klass] == generation
|
|
161
162
|
holder = other_declarer(klass.ancestors)
|
|
162
163
|
raise ApiNameClashError.new(klass, @name, holder) if holder
|
|
@@ -206,7 +207,6 @@ module ApiRecipes
|
|
|
206
207
|
problems = Compiler.unknown_variables(@variables, definition, @name.to_s)
|
|
207
208
|
raise ConfigurationError.new(problems) unless problems.empty?
|
|
208
209
|
|
|
209
|
-
write_variables if @variables_generation != LevelSet.generation
|
|
210
210
|
@tree = Tree.new(generation: registry.generation, definition: definition, handles: handles, global: global,
|
|
211
211
|
api_class: handles.for(definition))
|
|
212
212
|
end
|