api_recipes 3.0.0.beta2 → 3.0.0.beta4
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 +704 -81
- data/UPGRADING.md +149 -46
- data/lib/api_recipes/api.rb +14 -10
- data/lib/api_recipes/cli/code.rb +153 -0
- data/lib/api_recipes/cli/convert/migration.rb +624 -0
- data/lib/api_recipes/cli/convert/snippet.rb +67 -0
- data/lib/api_recipes/cli/convert.rb +533 -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 +245 -0
- data/lib/api_recipes/cli/recipes.rb +287 -0
- data/lib/api_recipes/cli/routes.rb +8 -3
- data/lib/api_recipes/cli/upgrade/project.rb +194 -0
- data/lib/api_recipes/cli/upgrade.rb +361 -24
- data/lib/api_recipes/cli.rb +161 -8
- data/lib/api_recipes/configuration.rb +68 -18
- data/lib/api_recipes/endpoint.rb +10 -4
- data/lib/api_recipes/errors.rb +2 -1
- data/lib/api_recipes/internals/compiler.rb +174 -33
- data/lib/api_recipes/internals/context.rb +87 -20
- data/lib/api_recipes/internals/declaration.rb +18 -18
- data/lib/api_recipes/internals/definition.rb +76 -23
- data/lib/api_recipes/internals/level.rb +54 -43
- data/lib/api_recipes/internals/option_value.rb +92 -0
- 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 +42 -14
- data/lib/api_recipes/internals/template.rb +17 -5
- data/lib/api_recipes/version.rb +1 -1
- data/lib/api_recipes.rb +8 -3
- metadata +14 -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,8 +335,11 @@ 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)
|
|
342
|
+
@option_uses = nil
|
|
297
343
|
variables = config[:variables].is_a?(Hash) ? config[:variables] : {}
|
|
298
344
|
declared = known == :any ? nil : variables.keys + known
|
|
299
345
|
attrs = {}
|
|
@@ -308,12 +354,20 @@ module ApiRecipes
|
|
|
308
354
|
attrs[:verify_with] = option(:verify_with, config, location, :api, declared)
|
|
309
355
|
attrs[:default_headers] = option(:default_headers, config, location, :api, declared)
|
|
310
356
|
attrs[:auth] = option(:auth, config, location, :api, declared)
|
|
357
|
+
credential_param = credential_param(attrs[:auth]) unless config[:auth].nil?
|
|
358
|
+
names = Names.new(variables: declared, credential_param: credential_param)
|
|
359
|
+
attrs[:default_params] = option(:default_params, config, location, :api, names)
|
|
311
360
|
attrs[:variables] = option(:variables, config, location, :api, declared)
|
|
312
361
|
attrs[:recipe] = option(:recipe, config, location, :api, declared)
|
|
313
|
-
attrs[:endpoints] = endpoints_attrs(config[:endpoints], join(location, :endpoints))
|
|
362
|
+
attrs[:endpoints] = endpoints_attrs(config[:endpoints], join(location, :endpoints), names)
|
|
363
|
+
check_option_defaults(attrs[:variables], location)
|
|
314
364
|
attrs
|
|
315
365
|
end
|
|
316
366
|
|
|
367
|
+
def credential_param(auth)
|
|
368
|
+
auth && auth[:type] == :query ? auth[:name] : nil
|
|
369
|
+
end
|
|
370
|
+
|
|
317
371
|
def validate_with(options, scope)
|
|
318
372
|
allowed = SCOPE_KEYS.fetch(scope)
|
|
319
373
|
check_keys(options, allowed, '')
|
|
@@ -327,7 +381,7 @@ module ApiRecipes
|
|
|
327
381
|
|
|
328
382
|
# ---- endpoints ----
|
|
329
383
|
|
|
330
|
-
def endpoints_attrs(value, location)
|
|
384
|
+
def endpoints_attrs(value, location, names)
|
|
331
385
|
return {} if value.nil?
|
|
332
386
|
|
|
333
387
|
unless value.is_a?(Hash)
|
|
@@ -344,20 +398,46 @@ module ApiRecipes
|
|
|
344
398
|
add(node_location, 'must be a Hash or empty')
|
|
345
399
|
next
|
|
346
400
|
end
|
|
347
|
-
out[name.to_sym] = endpoint_attrs(name.to_sym, options, node_location)
|
|
401
|
+
out[name.to_sym] = endpoint_attrs(name.to_sym, options, node_location, names)
|
|
348
402
|
end
|
|
349
403
|
end
|
|
350
404
|
|
|
351
|
-
def endpoint_attrs(name, options, location)
|
|
405
|
+
def endpoint_attrs(name, options, location, names)
|
|
352
406
|
check_keys(options, ENDPOINT_KEYS, location)
|
|
407
|
+
declared = names.variables
|
|
353
408
|
own = {}
|
|
354
|
-
(ENDPOINT_KEYS - %i[endpoints]).each do |key|
|
|
355
|
-
value = option(key, options, location, :endpoint,
|
|
409
|
+
(ENDPOINT_KEYS - %i[endpoints resource]).each do |key|
|
|
410
|
+
value = option(key, options, location, :endpoint, key == :default_params ? names : declared)
|
|
356
411
|
own[key] = value unless value.nil?
|
|
357
412
|
end
|
|
413
|
+
children = with_resource(options[:resource], options[:endpoints], join(location, :resource))
|
|
358
414
|
path = own[:path] || name.to_s
|
|
359
415
|
template = parse_path(path, join(location, :path), options.key?(:path) && !options[:path].nil?)
|
|
360
|
-
|
|
416
|
+
unless declared.nil?
|
|
417
|
+
(template.variable_names - declared).each { |variable| add(join(location, :path), "unknown variable #{shown(variable)}") }
|
|
418
|
+
end
|
|
419
|
+
{ own: own, template: template,
|
|
420
|
+
children: endpoints_attrs(children, join(location, :endpoints), names) }
|
|
421
|
+
end
|
|
422
|
+
|
|
423
|
+
# The endpoints written under an endpoint, with those its `resource`
|
|
424
|
+
# adds before them. An endpoint written with the name of a generated
|
|
425
|
+
# one completes it: its options win, a nil one keeps the generated value.
|
|
426
|
+
def with_resource(value, written, where)
|
|
427
|
+
added = self.class.resource_names(value)
|
|
428
|
+
if added.nil?
|
|
429
|
+
add(where, "must be true, false or a list of: #{RESOURCE.keys.join(', ')}")
|
|
430
|
+
return written
|
|
431
|
+
end
|
|
432
|
+
return written if added.empty? || !(written.nil? || written.is_a?(Hash))
|
|
433
|
+
|
|
434
|
+
written ||= {}
|
|
435
|
+
generated = added.to_h do |name|
|
|
436
|
+
given = written[name]
|
|
437
|
+
given = RESOURCE[name].merge(given) { |_key, usual, own| own.nil? ? usual : own } if given.is_a?(Hash)
|
|
438
|
+
[name, given.nil? ? RESOURCE[name] : given]
|
|
439
|
+
end
|
|
440
|
+
generated.merge(written.reject { |name, _| generated.key?(name) })
|
|
361
441
|
end
|
|
362
442
|
|
|
363
443
|
def parse_path(path, location, written)
|
|
@@ -377,6 +457,8 @@ module ApiRecipes
|
|
|
377
457
|
return nil if value.nil?
|
|
378
458
|
|
|
379
459
|
where = join(location, key)
|
|
460
|
+
return option_with_variables(key, value, where, declared) if OptionValue.variable?(key, value)
|
|
461
|
+
|
|
380
462
|
case key
|
|
381
463
|
when :protocol then one_of(value, PROTOCOLS, where)
|
|
382
464
|
when :host then host(value, where, declared)
|
|
@@ -387,6 +469,7 @@ module ApiRecipes
|
|
|
387
469
|
when :mime_type then mime_type(value, where)
|
|
388
470
|
when :verify_with then method_name(value, where)
|
|
389
471
|
when :default_headers then headers(value, where, declared)
|
|
472
|
+
when :default_params then params(value, where, declared)
|
|
390
473
|
when :auth then auth(value, where)
|
|
391
474
|
when :variables then scope == :with_api ? variable_values(value, where) : variable_declarations(value, where)
|
|
392
475
|
when :recipe then recipe(value, where)
|
|
@@ -397,6 +480,30 @@ module ApiRecipes
|
|
|
397
480
|
end
|
|
398
481
|
end
|
|
399
482
|
|
|
483
|
+
# An option of a request written with {variables}: kept as it is
|
|
484
|
+
# written, and made when the request is built.
|
|
485
|
+
def option_with_variables(key, value, where, declared)
|
|
486
|
+
check_variables(value, where, declared)
|
|
487
|
+
(@option_uses ||= []) << [key, value]
|
|
488
|
+
value.dup.freeze
|
|
489
|
+
end
|
|
490
|
+
|
|
491
|
+
# The default of a variable that gives the whole value of an option must
|
|
492
|
+
# be a value of that option.
|
|
493
|
+
def check_option_defaults(variables, location)
|
|
494
|
+
uses = @option_uses || []
|
|
495
|
+
@option_uses = nil
|
|
496
|
+
return unless variables
|
|
497
|
+
|
|
498
|
+
uses.uniq.each do |key, value|
|
|
499
|
+
name = OptionValue.whole(value)
|
|
500
|
+
default = name && variables[name] && variables[name][:default]
|
|
501
|
+
next if default.nil? || !OptionValue.typed(key, default).nil?
|
|
502
|
+
|
|
503
|
+
add(join(join(join(location, :variables), name), :default), "is used as #{OptionValue::USED_AS.fetch(key)}")
|
|
504
|
+
end
|
|
505
|
+
end
|
|
506
|
+
|
|
400
507
|
def one_of(value, allowed, where)
|
|
401
508
|
text = value.is_a?(String) || value.is_a?(Symbol) ? value.to_s : nil
|
|
402
509
|
return text.dup.freeze if text && allowed.include?(text)
|
|
@@ -476,26 +583,21 @@ module ApiRecipes
|
|
|
476
583
|
add(where, 'must be a string')
|
|
477
584
|
return nil
|
|
478
585
|
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
|
|
586
|
+
# Read as a path: the same fixed text, {variables} and \{ of the
|
|
587
|
+
# path of an endpoint, without its placeholders.
|
|
588
|
+
template = begin
|
|
589
|
+
PathTemplate.parse(value.to_s)
|
|
590
|
+
rescue ArgumentError
|
|
591
|
+
nil
|
|
592
|
+
end
|
|
593
|
+
if template.nil? || !template.param_names.empty?
|
|
495
594
|
add(where, 'path placeholders are not allowed here; declare a variable and write {name}')
|
|
496
595
|
return nil
|
|
497
596
|
end
|
|
498
|
-
|
|
597
|
+
unless declared.nil?
|
|
598
|
+
(template.variable_names - declared).each { |name| add(where, "unknown variable #{shown(name)}") }
|
|
599
|
+
end
|
|
600
|
+
template.to_s.freeze
|
|
499
601
|
end
|
|
500
602
|
|
|
501
603
|
def headers(value, where, declared)
|
|
@@ -503,10 +605,16 @@ module ApiRecipes
|
|
|
503
605
|
add(where, 'must be a Hash')
|
|
504
606
|
return nil
|
|
505
607
|
end
|
|
608
|
+
seen = {}
|
|
506
609
|
value.each_with_object({}) do |(name, header), out|
|
|
507
610
|
name = name.to_s
|
|
508
611
|
header_where = join(where, name)
|
|
509
612
|
add(header_where, 'is not a valid header name') unless TOKEN.match?(name)
|
|
613
|
+
first = seen[Definition.header_key(name)] ||= name
|
|
614
|
+
unless first.equal?(name)
|
|
615
|
+
add(header_where, "is given twice, as #{shown(first)} and as #{shown(name)}: the name of a header has " \
|
|
616
|
+
'no case, and _ is read as -')
|
|
617
|
+
end
|
|
510
618
|
unless header.is_a?(String) || header.is_a?(Symbol) || header.is_a?(Numeric)
|
|
511
619
|
add(header_where, 'must be a string')
|
|
512
620
|
next
|
|
@@ -518,6 +626,34 @@ module ApiRecipes
|
|
|
518
626
|
end.freeze
|
|
519
627
|
end
|
|
520
628
|
|
|
629
|
+
# The query parameters sent with every request, as { name => text }.
|
|
630
|
+
# names: a Names, or nil in a with, where nothing is known.
|
|
631
|
+
def params(value, where, names)
|
|
632
|
+
unless value.is_a?(Hash)
|
|
633
|
+
add(where, 'must be a Hash')
|
|
634
|
+
return nil
|
|
635
|
+
end
|
|
636
|
+
value.each_with_object({}) do |(name, param), out|
|
|
637
|
+
name = name.to_s
|
|
638
|
+
if name.empty?
|
|
639
|
+
add(where, 'a parameter name must not be empty')
|
|
640
|
+
next
|
|
641
|
+
end
|
|
642
|
+
param_where = join(where, name)
|
|
643
|
+
add(param_where, 'the name must not contain control characters') if CONTROL.match?(name)
|
|
644
|
+
if names && name == names.credential_param
|
|
645
|
+
add(param_where, 'is the parameter of the credential (auth.name): it cannot be set here')
|
|
646
|
+
end
|
|
647
|
+
unless param.is_a?(String) || param.is_a?(Symbol) || param.is_a?(Numeric) || param == true || param == false
|
|
648
|
+
add(param_where, 'must be a string, a number or a boolean')
|
|
649
|
+
next
|
|
650
|
+
end
|
|
651
|
+
text = param.to_s
|
|
652
|
+
check_variables(text, param_where, names&.variables)
|
|
653
|
+
out[name.dup.freeze] = text.dup.freeze
|
|
654
|
+
end.freeze
|
|
655
|
+
end
|
|
656
|
+
|
|
521
657
|
def auth(value, where)
|
|
522
658
|
unless value.is_a?(Hash)
|
|
523
659
|
add(where, 'must be a Hash')
|
|
@@ -576,7 +712,9 @@ module ApiRecipes
|
|
|
576
712
|
return nil
|
|
577
713
|
end
|
|
578
714
|
value.each_key { |name| add(join(where, name), 'is not a valid variable name') unless VARIABLE_NAME.match?(name.to_s) }
|
|
579
|
-
|
|
715
|
+
# Kept as text: an object that changes later would move the host after
|
|
716
|
+
# the values were given.
|
|
717
|
+
value.to_h { |name, item| [name, variable_text(item, join(where, name))] }.freeze
|
|
580
718
|
end
|
|
581
719
|
|
|
582
720
|
def recipe(value, where)
|
|
@@ -646,6 +784,7 @@ module ApiRecipes
|
|
|
646
784
|
host = attrs[:host] || 'localhost'
|
|
647
785
|
base_url = attrs[:base_url]
|
|
648
786
|
headers = attrs[:default_headers] || {}
|
|
787
|
+
params = attrs[:default_params] || {}
|
|
649
788
|
Definition::Api.new(
|
|
650
789
|
name: name,
|
|
651
790
|
protocol: attrs[:protocol] || 'https',
|
|
@@ -657,12 +796,14 @@ module ApiRecipes
|
|
|
657
796
|
mime_type: attrs[:mime_type],
|
|
658
797
|
verify_with: attrs[:verify_with],
|
|
659
798
|
default_headers: headers.freeze,
|
|
799
|
+
default_params: params.freeze,
|
|
660
800
|
auth: attrs[:auth],
|
|
661
801
|
variables: (attrs[:variables] || {}).freeze,
|
|
662
802
|
recipe: attrs[:recipe],
|
|
663
803
|
host_template: Template.new(host, :host),
|
|
664
804
|
base_url_template: base_url && Template.new(base_url, :base_url),
|
|
665
805
|
header_templates: headers.transform_values { |text| Template.new(text, :header) }.freeze,
|
|
806
|
+
param_templates: params.transform_values { |text| Template.new(text, :param) }.freeze,
|
|
666
807
|
children: ->(api) { build_endpoints(attrs[:endpoints], api) }
|
|
667
808
|
)
|
|
668
809
|
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,8 +54,34 @@ 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
|
|
58
|
-
def
|
|
57
|
+
# The level of the owner alone, or nil.
|
|
58
|
+
def own_level = Levels.own(@owner, @definition.name, @declaration)
|
|
59
|
+
|
|
60
|
+
# The value of an option, from the nearest place that has one. An
|
|
61
|
+
# option written with variables is made from those of chain.
|
|
62
|
+
def setting(key, endpoint = nil, endpoint_overrides = nil, chain: nil)
|
|
63
|
+
resolve(key, written(key, endpoint, endpoint_overrides), chain: chain)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# value as the option key: what is written with variables is filled
|
|
67
|
+
# with those of chain and checked for the option; anything else is
|
|
68
|
+
# returned as it is.
|
|
69
|
+
def resolve(key, value, chain: nil)
|
|
70
|
+
return value unless OptionValue.variable?(key, value)
|
|
71
|
+
|
|
72
|
+
OptionValue.resolve(key, value, variables(chain: chain || levels), api: @definition.name)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# The verb as inspect shows it: made from the variables, or as it is
|
|
76
|
+
# written when one is missing.
|
|
77
|
+
def shown_verb(verb)
|
|
78
|
+
OptionValue.shown_verb(resolve(:verb, verb))
|
|
79
|
+
rescue Error
|
|
80
|
+
OptionValue.shown_verb(verb)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# The option as it is written, from the nearest place that has one.
|
|
84
|
+
def written(key, endpoint = nil, endpoint_overrides = nil)
|
|
59
85
|
if endpoint
|
|
60
86
|
value = endpoint_overrides && endpoint_overrides[key]
|
|
61
87
|
return value unless value.nil?
|
|
@@ -90,13 +116,20 @@ module ApiRecipes
|
|
|
90
116
|
"/#{template.expand(variables, api: @definition.name)}".squeeze('/')
|
|
91
117
|
end
|
|
92
118
|
|
|
93
|
-
# The default headers with their variables filled
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
119
|
+
# The default headers with their variables filled: those of the api,
|
|
120
|
+
# then the ones given to with, then the ones of endpoint and of the
|
|
121
|
+
# endpoints above it. A header replaces the one with its name, whatever
|
|
122
|
+
# the case.
|
|
123
|
+
def headers(chain: levels, endpoint: nil)
|
|
124
|
+
filled(layers(:header, @definition.header_templates, @overrides[:default_headers],
|
|
125
|
+
endpoint && endpoint.header_templates), chain)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# The default query parameters with their variables filled, as
|
|
129
|
+
# { name => text }, in the same order as the headers.
|
|
130
|
+
def params(chain: levels, endpoint: nil)
|
|
131
|
+
filled(layers(:param, @definition.param_templates, @overrides[:default_params],
|
|
132
|
+
endpoint && endpoint.param_templates), chain)
|
|
100
133
|
end
|
|
101
134
|
|
|
102
135
|
# The nearest credential: the one given to with, then the levels from the
|
|
@@ -138,7 +171,7 @@ module ApiRecipes
|
|
|
138
171
|
def shown_origin
|
|
139
172
|
origin
|
|
140
173
|
rescue Error
|
|
141
|
-
address(@definition, @overrides[:host] || @definition.host, with: true)
|
|
174
|
+
address(@definition, @overrides[:host] || @definition.host, nil, with: true)
|
|
142
175
|
end
|
|
143
176
|
|
|
144
177
|
def inspect = "#<#{self.class.name} #{@definition.name}>"
|
|
@@ -154,6 +187,31 @@ module ApiRecipes
|
|
|
154
187
|
|
|
155
188
|
private
|
|
156
189
|
|
|
190
|
+
NONE = {}.freeze
|
|
191
|
+
|
|
192
|
+
# The templates of the api, of with and of the endpoint, the later over
|
|
193
|
+
# the earlier.
|
|
194
|
+
def layers(position, api, given, endpoint)
|
|
195
|
+
templates = api
|
|
196
|
+
unless given.nil? || given.empty?
|
|
197
|
+
templates = over(position, templates, given.transform_values { |text| Template.new(text, position) })
|
|
198
|
+
end
|
|
199
|
+
templates = over(position, templates, endpoint) unless endpoint.nil? || endpoint.empty?
|
|
200
|
+
templates
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def over(position, base, added)
|
|
204
|
+
position == :header ? Definition.over_headers(base, added) : base.merge(added)
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
# The variables are read only when there is something to fill.
|
|
208
|
+
def filled(templates, chain)
|
|
209
|
+
return NONE if templates.empty?
|
|
210
|
+
|
|
211
|
+
values = variables(chain: chain)
|
|
212
|
+
templates.transform_values { |template| template.expand(values, api: @definition.name) }
|
|
213
|
+
end
|
|
214
|
+
|
|
157
215
|
# The tree a level reads: the global one for the global level, else the owner's.
|
|
158
216
|
def definition_for(kind) = kind == :global ? @global_definition : @definition
|
|
159
217
|
|
|
@@ -180,17 +238,23 @@ module ApiRecipes
|
|
|
180
238
|
|
|
181
239
|
values = sum_variables(api, chain, start, with)
|
|
182
240
|
host = with && @overrides[:host] ? Template.new(@overrides[:host], :host) : api.host_template
|
|
183
|
-
address(api, host.expand(values, api: api.name), with: with)
|
|
241
|
+
address(api, host.expand(values, api: api.name), values, with: with)
|
|
184
242
|
rescue MissingVariable
|
|
185
243
|
raise if with
|
|
186
244
|
|
|
187
245
|
nil
|
|
188
246
|
end
|
|
189
247
|
|
|
190
|
-
# "scheme://host" or "scheme://host:port", without the default port.
|
|
191
|
-
|
|
248
|
+
# "scheme://host" or "scheme://host:port", without the default port. A
|
|
249
|
+
# protocol or a port written with variables is made from values, and
|
|
250
|
+
# shown as it is written when values is nil.
|
|
251
|
+
def address(api, host, values, with:)
|
|
192
252
|
protocol = (with && @overrides[:protocol]) || api.protocol
|
|
193
253
|
port = (with && @overrides[:port]) || api.port
|
|
254
|
+
if values
|
|
255
|
+
protocol = OptionValue.resolve(:protocol, protocol, values, api: api.name)
|
|
256
|
+
port = OptionValue.resolve(:port, port, values, api: api.name)
|
|
257
|
+
end
|
|
194
258
|
port = nil if port == DEFAULT_PORTS[protocol]
|
|
195
259
|
"#{protocol}://#{host}#{":#{port}" if port}"
|
|
196
260
|
end
|
|
@@ -201,7 +265,8 @@ module ApiRecipes
|
|
|
201
265
|
# every other level of the chain was written through the declaration in
|
|
202
266
|
# use, whose tree that is.
|
|
203
267
|
def check_origin(chain, index, destination)
|
|
204
|
-
|
|
268
|
+
registry = chain.respond_to?(:registry) ? chain.registry : Registry.current
|
|
269
|
+
bound = origin_from(definition_in_force(chain[index].first, registry), chain, index, with: false)
|
|
205
270
|
return if bound && destination && bound.casecmp?(destination)
|
|
206
271
|
|
|
207
272
|
raise CredentialOriginMismatch.new(@definition.name, bound, destination)
|
|
@@ -211,9 +276,9 @@ module ApiRecipes
|
|
|
211
276
|
# configure still holds the tree it was created with, and so its
|
|
212
277
|
# destination, while the levels are read when the request is sent: their
|
|
213
278
|
# 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
|
-
|
|
279
|
+
# gives them. nil when that configuration no longer has the api. registry
|
|
280
|
+
# is the one the levels of the request were read from.
|
|
281
|
+
def definition_in_force(kind, registry)
|
|
217
282
|
return definition_for(kind) if @generation.nil? || @generation == registry.generation
|
|
218
283
|
return registry.definitions[@definition.name] if kind == :global || @declaration.nil?
|
|
219
284
|
|
|
@@ -226,7 +291,9 @@ module ApiRecipes
|
|
|
226
291
|
merged[:credential] = Credential.from(key, value)
|
|
227
292
|
elsif value.nil?
|
|
228
293
|
merged.delete(key)
|
|
229
|
-
elsif
|
|
294
|
+
elsif key == :default_headers
|
|
295
|
+
merged[key] = Definition.over_headers(merged[key] || {}, value)
|
|
296
|
+
elsif %i[default_params variables].include?(key)
|
|
230
297
|
merged[key] = (merged[key] || {}).merge(value).freeze
|
|
231
298
|
else
|
|
232
299
|
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
|