api_recipes 3.0.0.beta4 → 3.0.0.beta6
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 +340 -107
- data/UPGRADING.md +197 -49
- data/lib/api_recipes/cli/calls.rb +27 -0
- data/lib/api_recipes/cli/convert/migration.rb +201 -6
- data/lib/api_recipes/cli/convert.rb +130 -23
- data/lib/api_recipes/cli/description.rb +65 -0
- data/lib/api_recipes/cli/import/server.rb +20 -9
- data/lib/api_recipes/cli/import/writer.rb +16 -4
- data/lib/api_recipes/cli/import.rb +23 -9
- data/lib/api_recipes/cli/recipes/summary.rb +7 -5
- data/lib/api_recipes/cli/recipes.rb +5 -2
- data/lib/api_recipes/cli/routes.rb +62 -7
- data/lib/api_recipes/cli/tags.rb +19 -0
- data/lib/api_recipes/cli/upgrade/fix.rb +206 -0
- data/lib/api_recipes/cli/upgrade/screen.rb +165 -0
- data/lib/api_recipes/cli/upgrade/tree_rules.rb +290 -0
- data/lib/api_recipes/cli/upgrade.rb +385 -63
- data/lib/api_recipes/cli.rb +84 -17
- data/lib/api_recipes/configuration.rb +17 -108
- data/lib/api_recipes/errors.rb +20 -3
- data/lib/api_recipes/internals/compiler.rb +77 -70
- data/lib/api_recipes/internals/context.rb +7 -2
- data/lib/api_recipes/internals/declaration.rb +17 -15
- data/lib/api_recipes/internals/definition.rb +15 -2
- data/lib/api_recipes/internals/level.rb +6 -1
- data/lib/api_recipes/internals/origins.rb +70 -0
- data/lib/api_recipes/internals/path_template.rb +23 -6
- data/lib/api_recipes/internals/recipe.rb +31 -5
- data/lib/api_recipes/internals/recipe_lock.rb +12 -9
- data/lib/api_recipes/internals/registry.rb +21 -10
- data/lib/api_recipes/internals/request.rb +2 -2
- data/lib/api_recipes/internals/template.rb +32 -2
- data/lib/api_recipes/response.rb +125 -6
- data/lib/api_recipes/version.rb +1 -1
- data/lib/api_recipes.rb +1 -0
- metadata +8 -1
data/README.md
CHANGED
|
@@ -69,10 +69,15 @@ Or install it directly:
|
|
|
69
69
|
|
|
70
70
|
## Quick start
|
|
71
71
|
|
|
72
|
-
**1. Describe the API** in a YAML file, for
|
|
72
|
+
**1. Describe the API** in a recipe, a YAML file called after the API, for
|
|
73
|
+
example `config/api_recipes/github.recipe.yml`:
|
|
73
74
|
|
|
74
75
|
```yaml
|
|
75
76
|
github:
|
|
77
|
+
recipe:
|
|
78
|
+
name: github
|
|
79
|
+
version: 1.0.0
|
|
80
|
+
format: 1
|
|
76
81
|
host: api.github.com
|
|
77
82
|
endpoints:
|
|
78
83
|
users: # GET /users
|
|
@@ -89,7 +94,7 @@ github:
|
|
|
89
94
|
require 'api_recipes'
|
|
90
95
|
|
|
91
96
|
ApiRecipes.configure do |config|
|
|
92
|
-
config.
|
|
97
|
+
config.recipes_paths = Dir['config/api_recipes/*.recipe.yml']
|
|
93
98
|
end
|
|
94
99
|
```
|
|
95
100
|
|
|
@@ -215,7 +220,8 @@ stay in the path is written `\:`.
|
|
|
215
220
|
|
|
216
221
|
A `{name}` in a path is a [variable](#variables): a value given once, to a
|
|
217
222
|
chain, an object, a class or the whole application, where a `:name` is an
|
|
218
|
-
argument of each call.
|
|
223
|
+
argument of each call. A `{*name}` is the variable form of `*name`: its value
|
|
224
|
+
can be several segments.
|
|
219
225
|
|
|
220
226
|
### Endpoints that only group others
|
|
221
227
|
|
|
@@ -289,15 +295,18 @@ ApiRecipes.billing.invoices.show(42).url.to_s
|
|
|
289
295
|
|
|
290
296
|
`host` is a plain host name: no scheme, port, path or spaces. Use `protocol` and
|
|
291
297
|
`port` for those. A `base_url` cannot contain a `:name` or `*name` placeholder;
|
|
292
|
-
for a value that changes, use a [variable](#variables)
|
|
298
|
+
for a value that changes, use a [variable](#variables), and `{*name}` for a
|
|
299
|
+
value of several segments, such as a prefix.
|
|
293
300
|
|
|
294
301
|
All options are listed in the [options reference](#options-reference).
|
|
295
302
|
|
|
296
303
|
### Headers and parameters sent with every request
|
|
297
304
|
|
|
298
305
|
`default_headers` are sent with every request, and `default_params` are added
|
|
299
|
-
to every query string. Both can also be written on an endpoint
|
|
300
|
-
for
|
|
306
|
+
to every query string. Both can also be written on an endpoint. A request
|
|
307
|
+
looks for them up the tree: on its endpoint, then on each endpoint that
|
|
308
|
+
contains it, then on the API, and sends what it finds, the nearest winning
|
|
309
|
+
for a name written more than once.
|
|
301
310
|
|
|
302
311
|
```yaml
|
|
303
312
|
billing:
|
|
@@ -349,8 +358,10 @@ of an endpoint, the value of a header or of a parameter sent with every
|
|
|
349
358
|
request, or in one of the options that are read when a request is sent:
|
|
350
359
|
`protocol`, `port`, `timeout`, `on_bad_code`, `mime_type`, `verb` and
|
|
351
360
|
`ok_code`. Use it for what changes from one account or one environment to the
|
|
352
|
-
next, such as the address of a shop.
|
|
353
|
-
|
|
361
|
+
next, such as the address of a shop. Every `{name}` that the API uses is one of
|
|
362
|
+
its variables. Declare a variable under `variables` to give it a `default` or a
|
|
363
|
+
`description`; one with neither needs no declaration, and a value must be
|
|
364
|
+
given to it:
|
|
354
365
|
|
|
355
366
|
```yaml
|
|
356
367
|
shopify:
|
|
@@ -359,8 +370,6 @@ shopify:
|
|
|
359
370
|
description: the part of the shop address before .myshopify.com
|
|
360
371
|
api_version:
|
|
361
372
|
default: '2026-10'
|
|
362
|
-
location:
|
|
363
|
-
description: the id of the location this client works for
|
|
364
373
|
host: '{shop}.myshopify.com'
|
|
365
374
|
base_url: admin/api/{api_version}
|
|
366
375
|
auth:
|
|
@@ -372,6 +381,8 @@ shopify:
|
|
|
372
381
|
path: /locations/{location}/inventory_levels
|
|
373
382
|
```
|
|
374
383
|
|
|
384
|
+
Here `location` is a variable too, with no default.
|
|
385
|
+
|
|
375
386
|
A variable without a value and without a default raises
|
|
376
387
|
`ApiRecipes::MissingVariable` as soon as it is needed: when the chain reaches
|
|
377
388
|
the `base_url` or the path that uses it, and at the latest when the request is
|
|
@@ -426,8 +437,8 @@ store.shopify.products.url.to_s
|
|
|
426
437
|
```
|
|
427
438
|
|
|
428
439
|
The options that a class gives to `api` can use the variables of the API, as in
|
|
429
|
-
`api :shopify, host: '{shop}.staging.example.test'
|
|
430
|
-
a
|
|
440
|
+
`api :shopify, host: '{shop}.staging.example.test'`, and new ones: a `{name}`
|
|
441
|
+
they use is a variable of the API for that class. `variables:` gives them a
|
|
431
442
|
value there, and an object or `with` can give another:
|
|
432
443
|
|
|
433
444
|
```ruby
|
|
@@ -442,7 +453,8 @@ Sandbox.sandbox.with(variables: { tenant: 'acme' }).ping.url.to_s
|
|
|
442
453
|
```
|
|
443
454
|
|
|
444
455
|
`config.values` holds, for each API, its `variables` and its credential
|
|
445
|
-
(`authorization` or `basic_auth`, see [Authorization](#authorization))
|
|
456
|
+
(`authorization` or `basic_auth`, see [Authorization](#authorization)), and with
|
|
457
|
+
the credential the other origins it may go to (`credential_origins`). A later
|
|
446
458
|
`configure` that assigns `config.values` replaces them all; one that does not
|
|
447
459
|
keeps them.
|
|
448
460
|
|
|
@@ -513,17 +525,52 @@ an option as it is written. What decides the shape of the tree is read when
|
|
|
513
525
|
the configuration is loaded and takes no variable: `endpoints`, `resource`,
|
|
514
526
|
`route`, the names, `verify_with` and `auth`.
|
|
515
527
|
|
|
528
|
+
A variable written with a star, `{*name}`, in a `base_url` or in a `path`
|
|
529
|
+
takes a value of several segments, as a `*name` placeholder does: the value is
|
|
530
|
+
split on `/`, each segment is encoded and the slashes stay. It lets an
|
|
531
|
+
application move an API under a prefix:
|
|
532
|
+
|
|
533
|
+
```yaml
|
|
534
|
+
rabbitmq:
|
|
535
|
+
variables:
|
|
536
|
+
api_path:
|
|
537
|
+
default: api
|
|
538
|
+
protocol: http
|
|
539
|
+
host: localhost
|
|
540
|
+
port: 15672
|
|
541
|
+
base_url: '{*api_path}'
|
|
542
|
+
endpoints:
|
|
543
|
+
overview:
|
|
544
|
+
```
|
|
545
|
+
|
|
546
|
+
```ruby
|
|
547
|
+
ApiRecipes.rabbitmq.overview.url.to_s
|
|
548
|
+
# => "http://localhost:15672/api/overview"
|
|
549
|
+
|
|
550
|
+
ApiRecipes.rabbitmq.with(variables: { api_path: 'rabbitmq/api' }).overview.url.to_s
|
|
551
|
+
# => "http://localhost:15672/rabbitmq/api/overview"
|
|
552
|
+
```
|
|
553
|
+
|
|
554
|
+
No segment of the value can be empty, `.` or `..`, so the value never climbs
|
|
555
|
+
out of the path: `'a/../b'`, `'/a'` and `'a//b'` raise
|
|
556
|
+
`ApiRecipes::InvalidVariable`. In a header or a parameter `{*name}` is text,
|
|
557
|
+
a `host` refuses it, and `\{*name}` is text in a `path` and in a `base_url`.
|
|
558
|
+
A portable recipe that uses `{*name}` declares `recipe.format: 2`.
|
|
559
|
+
|
|
516
560
|
The value of a variable is a String, a Symbol or a number, and is kept as
|
|
517
561
|
text. A value is checked for the place where it ends up. In a `host` it can contain
|
|
518
562
|
only letters, digits, dots, hyphens and underscores. In a `base_url` and in a
|
|
519
563
|
`path` it is encoded like a path parameter, and it cannot be empty, `.` or
|
|
520
|
-
|
|
564
|
+
`..`; in a `{*name}` it is encoded like the value of a `*name` placeholder. In a header it cannot contain a line break or another control character.
|
|
521
565
|
In a parameter it is encoded like every value of a query string. Anything else
|
|
522
566
|
raises `ApiRecipes::InvalidVariable`.
|
|
523
567
|
A word in braces that is not a variable is written `\{word}` in a path and in
|
|
524
568
|
a `base_url`.
|
|
525
|
-
A name that the API
|
|
526
|
-
every level, so a
|
|
569
|
+
A value given for a name that the API neither declares nor uses raises
|
|
570
|
+
`ApiRecipes::ConfigurationError` at every level, so a name misspelled in your
|
|
571
|
+
code is never ignored. A `{name}` misspelled in the description is a variable
|
|
572
|
+
with no default: the first request that needs it raises
|
|
573
|
+
`ApiRecipes::MissingVariable`, which names it.
|
|
527
574
|
`variables` without an assignment returns the values of the level you call it
|
|
528
575
|
on: those of `config.values` on `ApiRecipes.shopify`, those given to `api` on a
|
|
529
576
|
class, those assigned on an object. On an object, assigning `nil` clears them,
|
|
@@ -533,39 +580,52 @@ and a default declared in the file applies again.
|
|
|
533
580
|
|
|
534
581
|
Configure ApiRecipes once, when your application starts.
|
|
535
582
|
|
|
536
|
-
### From
|
|
583
|
+
### From recipes
|
|
537
584
|
|
|
538
585
|
```ruby
|
|
539
586
|
ApiRecipes.configure do |config|
|
|
540
|
-
config.
|
|
587
|
+
config.recipes_paths = Dir['config/api_recipes/*.recipe.yml']
|
|
588
|
+
config.values = {
|
|
589
|
+
billing: { variables: { billing_host: ENV.fetch('BILLING_HOST', 'billing.example.com') } }
|
|
590
|
+
}
|
|
541
591
|
end
|
|
542
592
|
```
|
|
543
593
|
|
|
544
|
-
A
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
`ApiRecipes::RecipeFileError`, which names the file. A file called
|
|
551
|
-
`something.recipe.yml` is a [portable recipe](#portable-recipes) and goes in
|
|
552
|
-
`recipes_paths`, where it is never evaluated with ERB: here it is refused,
|
|
553
|
-
unless `recipes_paths` has it too, in which case it is left to `recipes_paths`.
|
|
554
|
-
So one pattern, such as `config/apis/*.yml*`, can match both kinds of file in
|
|
555
|
-
one folder.
|
|
556
|
-
|
|
557
|
-
Files are evaluated with ERB before being parsed, so values can come from the
|
|
558
|
-
environment:
|
|
594
|
+
A recipe is a file called `NAME.recipe.yml` that describes the API called
|
|
595
|
+
`NAME`, with the key `recipe` that gives its name, its version and the format.
|
|
596
|
+
It holds data and no code: it is never evaluated with ERB, and `<%` anywhere in
|
|
597
|
+
it is an error. What changes from one environment to another is a
|
|
598
|
+
[variable](#variables), and the application gives its value with
|
|
599
|
+
`config.values`:
|
|
559
600
|
|
|
560
601
|
```yaml
|
|
561
602
|
billing:
|
|
562
|
-
|
|
563
|
-
|
|
603
|
+
recipe:
|
|
604
|
+
name: billing
|
|
605
|
+
version: 1.0.0
|
|
606
|
+
format: 1
|
|
607
|
+
variables:
|
|
608
|
+
billing_host:
|
|
609
|
+
default: billing.example.com
|
|
610
|
+
host: '{billing_host}'
|
|
564
611
|
endpoints:
|
|
565
612
|
invoices:
|
|
566
613
|
```
|
|
567
614
|
|
|
568
|
-
|
|
615
|
+
A recipe you write in your project is loaded as it is written, and you change
|
|
616
|
+
it as any other file. One that you took from a repository with
|
|
617
|
+
`api_recipes install` is also compared with `recipes.lock`: see
|
|
618
|
+
[Portable recipes](#portable-recipes). The rules of the format are under
|
|
619
|
+
[Writing a portable recipe](#writing-a-portable-recipe), and
|
|
620
|
+
`api_recipes check FILE` says whether a file follows them.
|
|
621
|
+
|
|
622
|
+
A file that is missing, not valid YAML, not valid UTF-8 or not a recipe raises
|
|
623
|
+
`ApiRecipes::RecipeFileError`, which names the file.
|
|
624
|
+
|
|
625
|
+
2.x loaded YAML files evaluated with ERB through `apis_files_paths`. That
|
|
626
|
+
option was removed: assigning it raises `ApiRecipes::Error`, and
|
|
627
|
+
`api_recipes convert` moves those files to recipes. See
|
|
628
|
+
[From a file of your project](#from-a-file-of-your-project).
|
|
569
629
|
|
|
570
630
|
### From a Hash
|
|
571
631
|
|
|
@@ -592,11 +652,11 @@ configuration stays active.
|
|
|
592
652
|
When an API publishes an OpenAPI description, the `api_recipes` command writes
|
|
593
653
|
the recipe for you:
|
|
594
654
|
|
|
595
|
-
$ api_recipes import openapi.yml --name github --output config/
|
|
655
|
+
$ api_recipes import openapi.yml --name github --output config/api_recipes/github.recipe.yml
|
|
596
656
|
|
|
597
657
|
The description is read once, when you run the command. Your application never
|
|
598
|
-
reads it: it loads the recipe, which is a file like the ones above,
|
|
599
|
-
read it, trim it and rename things in it. These operations
|
|
658
|
+
reads it: it loads the recipe, which is a file like the ones above, complete with its
|
|
659
|
+
`recipe` key, and you can read it, trim it and rename things in it. These operations
|
|
600
660
|
|
|
601
661
|
```
|
|
602
662
|
GET /users
|
|
@@ -613,6 +673,10 @@ become this recipe:
|
|
|
613
673
|
```
|
|
614
674
|
# Generated by api_recipes import from GitHub 1.0
|
|
615
675
|
github:
|
|
676
|
+
recipe:
|
|
677
|
+
name: github
|
|
678
|
+
version: 1.0.0
|
|
679
|
+
format: 1
|
|
616
680
|
host: api.github.com
|
|
617
681
|
endpoints:
|
|
618
682
|
users: # GET /users (users/list)
|
|
@@ -652,12 +716,24 @@ github:
|
|
|
652
716
|
|
|
653
717
|
| Option | Meaning |
|
|
654
718
|
| --- | --- |
|
|
655
|
-
| `--name NAME` | the name of the API
|
|
719
|
+
| `--name NAME` | the name of the API and of the recipe: lowercase letters, digits and `_`. Default: the title of the description, in lower case with `_` |
|
|
720
|
+
| `--version VERSION` | the version of the recipe. Default: `1.0.0` |
|
|
656
721
|
| `--only GLOB` | only the paths that match; can be repeated. `*` also crosses `/`: `--only 'repos/*'` |
|
|
657
722
|
| `--except GLOB` | leaves out the paths that match; can be repeated |
|
|
658
723
|
| `--server N` | which entry of `servers` gives the address, counted from 0. Default: the first |
|
|
724
|
+
| `--calls` | writes under each endpoint how it is called in Ruby |
|
|
659
725
|
| `--output FILE` | writes the recipe in FILE, which must not exist, instead of on STDOUT |
|
|
660
726
|
|
|
727
|
+
With `--calls`, who reads the recipe sees the call beside the request it
|
|
728
|
+
sends. The call starts after the name of the API, as in `github.users.show('octocat')`:
|
|
729
|
+
|
|
730
|
+
users: # GET /users (users/list)
|
|
731
|
+
# .users
|
|
732
|
+
endpoints:
|
|
733
|
+
show: # GET /users/{username} (users/get-by-username)
|
|
734
|
+
# .users.show(username)
|
|
735
|
+
path: "/:username"
|
|
736
|
+
|
|
661
737
|
The input is one local file, YAML or JSON, OpenAPI 3.0 or 3.1. Warnings and a
|
|
662
738
|
summary go to STDERR, so the recipe can be redirected. The command exits with 1
|
|
663
739
|
when the description cannot be imported and with 2 when the arguments are wrong.
|
|
@@ -667,6 +743,7 @@ when the description cannot be imported and with 2 when the arguments are wrong.
|
|
|
667
743
|
| From the description | In the recipe |
|
|
668
744
|
| --- | --- |
|
|
669
745
|
| the address of the server | `protocol`, `host`, `port`, `base_url` |
|
|
746
|
+
| no server, or an address without a host | `host: '{host}'`, a variable that the application gives with `config.values` |
|
|
670
747
|
| the variables of the server | `variables`, with their defaults |
|
|
671
748
|
| the paths | the tree of endpoints |
|
|
672
749
|
| the path parameters | `:name` placeholders |
|
|
@@ -728,30 +805,34 @@ A portable recipe is a recipe made to be shared: one API in one file, and
|
|
|
728
805
|
nothing but data. You install it with a command that shows what it does before
|
|
729
806
|
it writes anything, and you load it with `recipes_paths`.
|
|
730
807
|
|
|
731
|
-
$ api_recipes install
|
|
732
|
-
github
|
|
733
|
-
|
|
808
|
+
$ api_recipes install github_essentials
|
|
809
|
+
reading github_essentials from https://github.com/madAle/api_recipes-cookbook
|
|
810
|
+
github_essentials 1.0.0
|
|
811
|
+
summary: GitHub REST API, the most used calls on users, repositories, issues and pull requests
|
|
734
812
|
homepage: https://docs.github.com/en/rest
|
|
735
|
-
from: https://github.com/madAle/api_recipes-cookbook, commit
|
|
813
|
+
from: https://github.com/madAle/api_recipes-cookbook, commit cd19ca58aea1
|
|
736
814
|
address: https://api.github.com
|
|
737
815
|
credential: in the Authorization header
|
|
738
816
|
headers: Accept: application/vnd.github+json
|
|
739
817
|
calls: 14, of which 2 are not GET:
|
|
740
|
-
POST
|
|
741
|
-
POST
|
|
742
|
-
Install
|
|
743
|
-
|
|
818
|
+
POST github_essentials.repos.issues(owner, repo).create /repos/:owner/:repo/issues
|
|
819
|
+
POST github_essentials.repos.issues(owner, repo).comments(issue_number).create /repos/:owner/:repo/issues/:issue_number/comments
|
|
820
|
+
Install github_essentials 1.0.0 in config/api_recipes? [y/N] y
|
|
821
|
+
github_essentials 1.0.0 is installed in config/api_recipes/github_essentials.recipe.yml
|
|
744
822
|
|
|
745
823
|
```ruby
|
|
746
824
|
ApiRecipes.configure do |config|
|
|
747
825
|
config.recipes_paths = Dir['config/api_recipes/*.recipe.yml']
|
|
748
826
|
end
|
|
749
827
|
|
|
750
|
-
ApiRecipes.
|
|
828
|
+
ApiRecipes.github_essentials.users.show('octocat').run
|
|
751
829
|
```
|
|
752
830
|
|
|
831
|
+
`github_essentials` has the most used calls of the GitHub API; the recipe
|
|
832
|
+
called `github` has every operation, 1,281 calls with the same names.
|
|
833
|
+
|
|
753
834
|
The command writes two files in `config/api_recipes`: the recipe,
|
|
754
|
-
`
|
|
835
|
+
`github_essentials.recipe.yml`, and `recipes.lock`, which records the version, the
|
|
755
836
|
repository, the commit and the checksum of each installed recipe. Commit both
|
|
756
837
|
to your repository: the application downloads nothing when it starts.
|
|
757
838
|
|
|
@@ -763,7 +844,7 @@ layout.
|
|
|
763
844
|
### Installing, updating and checking
|
|
764
845
|
|
|
765
846
|
$ api_recipes install github # the recipe at the head of the cookbook
|
|
766
|
-
$ api_recipes install github --version
|
|
847
|
+
$ api_recipes install github --version 2.0.0 # the one at the tag github-2.0.0
|
|
767
848
|
$ api_recipes install billing --from https://github.com/acme/api-recipes
|
|
768
849
|
$ api_recipes install ./billing.recipe.yml # a file of yours
|
|
769
850
|
$ api_recipes update github
|
|
@@ -848,15 +929,21 @@ this when it runs: the installed recipes are files of your project.
|
|
|
848
929
|
|
|
849
930
|
### What `configure` checks
|
|
850
931
|
|
|
851
|
-
Every file of `recipes_paths` must be called `NAME.recipe.yml` and
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
`ApiRecipes::ConfigurationError`
|
|
932
|
+
Every file of `recipes_paths` must be called `NAME.recipe.yml` and follow the
|
|
933
|
+
rules of the format. A recipe that `recipes.lock` ties to a repository, because
|
|
934
|
+
`install` took it from one, must also have the checksum the lock records. When
|
|
935
|
+
it was changed afterwards, `configure` raises `ApiRecipes::ConfigurationError`
|
|
936
|
+
and the previous configuration stays:
|
|
937
|
+
|
|
938
|
+
config/api_recipes/github.recipe.yml: was changed after it was installed from its repository: its checksum is not the one in recipes.lock. To keep the file as it is now, as a recipe of the project, run `api_recipes install config/api_recipes/github.recipe.yml`
|
|
855
939
|
|
|
856
|
-
|
|
940
|
+
A recipe that the lock does not tie to a repository is a recipe of the project:
|
|
941
|
+
one you wrote, one that `convert` or `import` wrote, or one you accepted with
|
|
942
|
+
`install FILE` after changing it. It is loaded as it is written, with nothing
|
|
943
|
+
to compare it with, and `update` leaves it alone.
|
|
857
944
|
|
|
858
|
-
A recipe never replaces an API: a name that `apis_configs
|
|
859
|
-
|
|
945
|
+
A recipe never replaces an API: a name that `apis_configs` or another recipe
|
|
946
|
+
already defines is an error. Everything else works as for any
|
|
860
947
|
API: credentials, variables, `with`, and `api :github` in a class. To add a
|
|
861
948
|
`verify_with`, which a recipe cannot carry, declare it in your code:
|
|
862
949
|
|
|
@@ -905,7 +992,10 @@ The rules, beyond those of every description:
|
|
|
905
992
|
(`billing.recipe.yml`) are the same: lowercase letters, digits, `-` and `_`.
|
|
906
993
|
- `recipe.name` and `recipe.version` are required. The version is text made of
|
|
907
994
|
digits, letters and dots: write `'1.2'` between quotes. `format` is the
|
|
908
|
-
version of the format,
|
|
995
|
+
version of the format: 1, or none, for most recipes; 2 for a recipe that
|
|
996
|
+
uses a `{*name}` variable in its `base_url` or in the `path` of an endpoint,
|
|
997
|
+
so that a version of the gem that does not know `{*name}` refuses it.
|
|
998
|
+
`summary` and `homepage` are optional.
|
|
909
999
|
- `host` is required. A variable can be the whole host, or stand before fixed
|
|
910
1000
|
text that starts with a dot: `{account}.billing.example.com` and
|
|
911
1001
|
`s3.{region}.example.com` are accepted, `api.example.{tld}` is not.
|
|
@@ -935,14 +1025,19 @@ published version is the tag `NAME-VERSION`, for example `billing-1.2.0`.
|
|
|
935
1025
|
When the API is already described in a file of your project, `convert` moves
|
|
936
1026
|
it to a portable recipe. Run it in the root of the project: a Rails
|
|
937
1027
|
application or any other Ruby project, wherever `ApiRecipes.configure` is
|
|
938
|
-
called.
|
|
1028
|
+
called. With no file it moves every API that a file of the project describes,
|
|
1029
|
+
one after the other, which is what a project that comes from 2.x needs:
|
|
1030
|
+
|
|
1031
|
+
$ bundle exec api_recipes convert
|
|
1032
|
+
|
|
1033
|
+
With a file it moves the API of that file:
|
|
939
1034
|
|
|
940
1035
|
$ bundle exec api_recipes convert config/apis/billing.yml.erb
|
|
941
1036
|
billing 1.0.0 now comes from a portable recipe: 2 calls
|
|
942
1037
|
|
|
943
1038
|
Changed in the project
|
|
944
1039
|
created config/api_recipes/billing.recipe.yml, with its line in config/api_recipes/recipes.lock
|
|
945
|
-
edited config/initializers/api_recipes.rb: recipes_paths and config.values
|
|
1040
|
+
edited config/initializers/api_recipes.rb: recipes_paths and config.values; apis_files_paths, which loads nothing now, is gone
|
|
946
1041
|
edited app/models/invoice.rb: verify_with on the declaration of the api
|
|
947
1042
|
deleted config/apis/billing.yml.erb
|
|
948
1043
|
|
|
@@ -959,7 +1054,6 @@ the code of each tag is moved, as it is written, to `config.values`.
|
|
|
959
1054
|
|
|
960
1055
|
```ruby
|
|
961
1056
|
ApiRecipes.configure do |config|
|
|
962
|
-
config.apis_files_paths = Dir[File.expand_path('../apis/*.yml.erb', __dir__)]
|
|
963
1057
|
config.values = {
|
|
964
1058
|
billing: { variables: {
|
|
965
1059
|
billing_host: (ENV['BILLING_HOST'] || 'billing.example.test'),
|
|
@@ -986,7 +1080,23 @@ What the command does:
|
|
|
986
1080
|
after its place, and a default written in the tag as plain text or as a
|
|
987
1081
|
number (`ENV.fetch('NAME', 'text')`, `ENV['NAME'] || 443`) becomes the
|
|
988
1082
|
default of the variable. A default that a tag gave to a variable the file
|
|
989
|
-
declares is given by the application from now on.
|
|
1083
|
+
declares is given by the application from now on. Only a variable with a
|
|
1084
|
+
default is declared in the recipe. In the `base_url` and in a `path` the
|
|
1085
|
+
variable is a `{*name}`, since 2.x put the value of the tag there with its
|
|
1086
|
+
slashes, and the recipe then declares `recipe.format: 2`. A value of one or
|
|
1087
|
+
more segments goes where 2.x sent it; one that starts or ends with a slash,
|
|
1088
|
+
has two in a row or is empty, which 2.x joined, raises
|
|
1089
|
+
`ApiRecipes::InvalidVariable` at the request: give it without those slashes.
|
|
1090
|
+
The command says so when the default written in the tag is such a value.
|
|
1091
|
+
- **A word in braces stays text**, as 2.x sent it, unless the file declares a
|
|
1092
|
+
variable with that name: it is written `\{word}` in a `path` and in the
|
|
1093
|
+
`base_url`, and a header or a parameter that holds one is given by a
|
|
1094
|
+
variable whose default is its text. With a star, `{*word}` is written
|
|
1095
|
+
`\{*word}` in a `path` and in the `base_url` even when the file declares
|
|
1096
|
+
`word`; in a header or a parameter it stays as it is.
|
|
1097
|
+
- **`--calls` writes beside each endpoint how it is called in Ruby**, as
|
|
1098
|
+
`show: # .invoices.show(id)`, after the name of the API. The recipe is the
|
|
1099
|
+
same with or without them.
|
|
990
1100
|
- **Nothing in the file is run**, and there is no option that runs it. The
|
|
991
1101
|
code of a tag goes to `config.values` as text, and each tag has its own
|
|
992
1102
|
variable, also two tags in one value. `.compact` leaves out a variable whose
|
|
@@ -1002,7 +1112,12 @@ What the command does:
|
|
|
1002
1112
|
before you share it.
|
|
1003
1113
|
- **The old description is removed**: the file is deleted, or only the lines
|
|
1004
1114
|
of that API when it describes others, because a recipe never takes the place
|
|
1005
|
-
of an API that another source defines.
|
|
1115
|
+
of an API that another source defines. With the last description of the
|
|
1116
|
+
project the lines of `config.apis_files_paths` are taken out, when git has
|
|
1117
|
+
the file as it is now or as this run of the command left it, unless they
|
|
1118
|
+
still name a file that describes an API, as one in `vendor` that the command
|
|
1119
|
+
does not look at; otherwise the command tells you to take them out, because
|
|
1120
|
+
3.0 refuses the option.
|
|
1006
1121
|
- **`verify_with`**, which a recipe cannot carry, is given to each `api :name`
|
|
1007
1122
|
that a class or a module declares. One that a tag gave, or that an endpoint
|
|
1008
1123
|
had, is left to you, as it is when one of the declarations gives its options
|
|
@@ -1014,7 +1129,11 @@ It changes your files only when that is safe, and all at once or not at all:
|
|
|
1014
1129
|
|
|
1015
1130
|
- Lines are added only to a plain file of the project that is in the last
|
|
1016
1131
|
commit of git. A file is deleted, or lines are taken out of it, only when
|
|
1017
|
-
git has it committed as it is now
|
|
1132
|
+
git has it committed as it is now: the command works out the id of the
|
|
1133
|
+
file and compares it with the one in the index and in the last commit, so
|
|
1134
|
+
that no filter of the repository runs. A file that differs only by CR LF
|
|
1135
|
+
at the end of its lines, as git writes it with `core.autocrlf`, counts as
|
|
1136
|
+
committed. A symbolic link, and a file that git was told to skip, are never
|
|
1018
1137
|
changed. Outside a git repository the project is not changed.
|
|
1019
1138
|
- Ruby is read as a tree, never matched as lines and never run, and the
|
|
1020
1139
|
command edits only what it can read whole:
|
|
@@ -1024,14 +1143,18 @@ It changes your files only when that is safe, and all at once or not at all:
|
|
|
1024
1143
|
- `config.values`, which is added, or which gets one more entry when it is
|
|
1025
1144
|
given a Hash between braces whose keys are all written as names, none of
|
|
1026
1145
|
them the API, and whose closing brace starts a line;
|
|
1027
|
-
- `config.apis_files_paths`,
|
|
1028
|
-
patterns of `Dir`, or a list between brackets of names
|
|
1029
|
-
from which the one element that names the file is taken
|
|
1146
|
+
- `config.apis_files_paths`, while other files still describe an API: it
|
|
1147
|
+
must be given patterns of `Dir`, or a list between brackets of names
|
|
1148
|
+
written as text, from which the one element that names the file is taken
|
|
1149
|
+
out. With the last description the whole assignment goes, unless it still
|
|
1150
|
+
names another file that describes an API.
|
|
1030
1151
|
|
|
1031
1152
|
Anything else stops it: another mention of the old file in the code, another
|
|
1032
1153
|
file that describes the API, a `recipes_paths` it cannot read.
|
|
1033
1154
|
- Every Ruby file it changes must still be Ruby with the change, and when a
|
|
1034
|
-
write fails every file is put back as it was.
|
|
1155
|
+
write fails every file is put back as it was. A file that changed after the
|
|
1156
|
+
command read it is not written over. The recipe is not written below a
|
|
1157
|
+
symbolic link.
|
|
1035
1158
|
- When a step is not possible, nothing is written, the command says why, and
|
|
1036
1159
|
it gives the steps to do by hand with the code to paste: the commands that
|
|
1037
1160
|
write and install the recipe, the lines for the block of
|
|
@@ -1085,7 +1208,7 @@ ApiRecipes[:github].users.run
|
|
|
1085
1208
|
|
|
1086
1209
|
```ruby
|
|
1087
1210
|
ApiRecipes.apis.inspect
|
|
1088
|
-
# => "[:github, :billing, :shopify]"
|
|
1211
|
+
# => "[:github, :billing, :shopify, :rabbitmq]"
|
|
1089
1212
|
```
|
|
1090
1213
|
|
|
1091
1214
|
### In your classes
|
|
@@ -1290,8 +1413,9 @@ in a variable, and for an endpoint whose name is already a method (see
|
|
|
1290
1413
|
|
|
1291
1414
|
## Reading the response
|
|
1292
1415
|
|
|
1293
|
-
`run` returns an `ApiRecipes::Response`. It adds `data
|
|
1294
|
-
|
|
1416
|
+
`run` returns an `ApiRecipes::Response`. It adds `data`, and has the status,
|
|
1417
|
+
the headers and the body of the
|
|
1418
|
+
[http.rb response](https://github.com/httprb/http/wiki/Response-Handling):
|
|
1295
1419
|
|
|
1296
1420
|
```ruby
|
|
1297
1421
|
response = ApiRecipes.github.users.show('octocat').run
|
|
@@ -1303,10 +1427,38 @@ response.headers['Content-Type'] # => "application/json; charset=utf-8"
|
|
|
1303
1427
|
response.body.to_s # => "{\"login\":\"octocat\", ...}"
|
|
1304
1428
|
```
|
|
1305
1429
|
|
|
1306
|
-
`data` parses the body according to the `Content-Type` of the response
|
|
1307
|
-
|
|
1308
|
-
`
|
|
1309
|
-
|
|
1430
|
+
`data` parses the body according to the `Content-Type` of the response, with
|
|
1431
|
+
the parser of http.rb, which reads JSON. A type that ends in `+json`, like
|
|
1432
|
+
`application/problem+json`, is read as JSON too, unless your application gave
|
|
1433
|
+
http.rb a parser for it. An empty body gives `nil`, whatever
|
|
1434
|
+
the status and the type: a `204 No Content` needs nothing special. A body of
|
|
1435
|
+
spaces and line breaks counts as empty. A JSON `null` gives `nil` too: read
|
|
1436
|
+
`response.body.to_s` to tell them apart.
|
|
1437
|
+
|
|
1438
|
+
When there is a body that `data` cannot read (no `Content-Type`, a type that is
|
|
1439
|
+
not JSON, JSON that is not valid) it raises `ApiRecipes::UnreadableResponse`.
|
|
1440
|
+
The error has `path` and `response`, and its message gives the status and the
|
|
1441
|
+
type, when there is one, never the body: read `error.response.body.to_s`.
|
|
1442
|
+
`fill`, and a `verify_with` method that reads `data`, raise it as well. If a
|
|
1443
|
+
server sends JSON with the wrong `Content-Type`, set
|
|
1444
|
+
`mime_type: application/json` on the endpoint or on the API.
|
|
1445
|
+
|
|
1446
|
+
`data` raises the same error when the body is no longer there, because it was
|
|
1447
|
+
read with `readpartial` or because reading it failed: then `body.to_s` raises
|
|
1448
|
+
`HTTP::StateError` too.
|
|
1449
|
+
|
|
1450
|
+
`response.parse` reads the body as `data` does: `nil` for an empty body, the
|
|
1451
|
+
same error for one it cannot read. It takes a type, a String or a Symbol, for
|
|
1452
|
+
one response that is to be read with another one, and it reads the body again
|
|
1453
|
+
at every call:
|
|
1454
|
+
|
|
1455
|
+
```ruby
|
|
1456
|
+
response.parse # as data, read again
|
|
1457
|
+
response.parse('application/json') # whatever the Content-Type and mime_type say
|
|
1458
|
+
```
|
|
1459
|
+
|
|
1460
|
+
It is not the `parse` of http.rb, whose error quotes the body: that one is
|
|
1461
|
+
still `response.original_response.parse`.
|
|
1310
1462
|
|
|
1311
1463
|
`response.success?` tells whether the response passed the rule of the endpoint
|
|
1312
1464
|
(see the next section). It is the way to check a response when `on_bad_code` is
|
|
@@ -1409,6 +1561,8 @@ end
|
|
|
1409
1561
|
The method can be private. It is looked up on the caller, so `verify_with` is
|
|
1410
1562
|
meant for APIs used from a class or an instance. An endpoint that has no
|
|
1411
1563
|
`verify_with` of its own looks for one up the tree, as for the other options.
|
|
1564
|
+
`response.data` is `nil` when the body is empty: a rule that reads a key from
|
|
1565
|
+
it checks that first, where an answer without a body is possible.
|
|
1412
1566
|
|
|
1413
1567
|
## Handling errors
|
|
1414
1568
|
|
|
@@ -1435,6 +1589,7 @@ body, a credential or a query string: the body is on `error.response`.
|
|
|
1435
1589
|
| Error | Raised when |
|
|
1436
1590
|
| --- | --- |
|
|
1437
1591
|
| `ApiRecipes::ResponseCodeNotAsExpected` | the response is not a success; has `response`, `path` and `expected_code` |
|
|
1592
|
+
| `ApiRecipes::UnreadableResponse` | `response.data` or `response.parse` cannot read the body; has `response` and `path` |
|
|
1438
1593
|
| `ApiRecipes::PathParamsMismatch` | an endpoint gets the wrong number of path parameters |
|
|
1439
1594
|
| `ApiRecipes::InvalidPathParameter` | a path parameter is `nil`, empty, `.` or `..` |
|
|
1440
1595
|
| `ApiRecipes::NoRouteExists` | `run` is called on an endpoint with `route: no` |
|
|
@@ -1445,7 +1600,7 @@ body, a credential or a query string: the body is on `error.response`.
|
|
|
1445
1600
|
| `ApiRecipes::CredentialOriginMismatch` | a request would send a credential to an origin other than the one it was set for |
|
|
1446
1601
|
| `ApiRecipes::InvalidUrl` | the address that would be contacted is not the one that was validated |
|
|
1447
1602
|
| `ApiRecipes::ConfigurationError` | the configuration or the options of `with` or `follow` are not valid; has `errors`, the list of problems |
|
|
1448
|
-
| `ApiRecipes::RecipeFileError` | a
|
|
1603
|
+
| `ApiRecipes::RecipeFileError` | a recipe file cannot be read; a kind of `ConfigurationError` that names the file |
|
|
1449
1604
|
| `ApiRecipes::ApiNameClashError` | an API is declared twice in a class hierarchy, or `api :name` names a method the class already has |
|
|
1450
1605
|
| `ApiRecipes::ApiConfigIsNotAnHash` | the second argument of `api` is not a Hash |
|
|
1451
1606
|
| `HTTP::TimeoutError`, `HTTP::ConnectionError` | http.rb network errors |
|
|
@@ -1565,6 +1720,37 @@ through it raises the same error instead of sending a credential of the levels
|
|
|
1565
1720
|
to the old origin: ask `ApiRecipes` (or the class, or the object) for the API
|
|
1566
1721
|
again. A `configure` that does not move the origin changes nothing for it.
|
|
1567
1722
|
|
|
1723
|
+
When one credential is meant for several origins, like the nodes of a cluster
|
|
1724
|
+
that a variable selects, say so where the application gives the credential,
|
|
1725
|
+
with `credential_origins` in `config.values`:
|
|
1726
|
+
|
|
1727
|
+
```ruby
|
|
1728
|
+
ApiRecipes.configure do |config|
|
|
1729
|
+
config.values = {
|
|
1730
|
+
broker: {
|
|
1731
|
+
basic_auth: { user: 'me', pass: 'secret' },
|
|
1732
|
+
credential_origins: ['https://node2.example.com:15672', 'https://node3.example.com:15672']
|
|
1733
|
+
}
|
|
1734
|
+
}
|
|
1735
|
+
end
|
|
1736
|
+
```
|
|
1737
|
+
|
|
1738
|
+
Each entry is a whole origin, `https://host` or `https://host:port`, and
|
|
1739
|
+
nothing else: no path, no user, no variable, no wildcard. An entry can also
|
|
1740
|
+
begin with `http://`: the credential then goes there in clear. The origin the
|
|
1741
|
+
credential was given for needs no entry. `credential_origins: :any` sends the
|
|
1742
|
+
credential wherever the request goes, plain `http` included. Use it only when
|
|
1743
|
+
the host, the port and the protocol that reach `with` and the variables never
|
|
1744
|
+
come from the data of a request: whoever chooses the destination receives the
|
|
1745
|
+
credential.
|
|
1746
|
+
|
|
1747
|
+
`credential_origins` is taken only there, next to `authorization` or
|
|
1748
|
+
`basic_auth`. `api` in a class, `with`, a recipe and `apis_configs` refuse it,
|
|
1749
|
+
and it says nothing of a credential that a class or an object sets for itself.
|
|
1750
|
+
A redirect to another origin still drops the credential. An API kept in a
|
|
1751
|
+
variable across a `configure` that moves it sends the credential to its old
|
|
1752
|
+
origin when that origin is named, or with `:any`.
|
|
1753
|
+
|
|
1568
1754
|
The readers `authorization` and `basic_auth` return the nearest credential that
|
|
1569
1755
|
was assigned. They do not apply that check: only a request does.
|
|
1570
1756
|
|
|
@@ -1602,7 +1788,7 @@ ApiRecipes does not check its origin, so prefer the levels above.
|
|
|
1602
1788
|
ApiRecipes.configure do |config|
|
|
1603
1789
|
config.logger = Rails.logger # any Logger
|
|
1604
1790
|
config.print_urls = true # log the URL of every request
|
|
1605
|
-
config.
|
|
1791
|
+
config.recipes_paths = Dir['config/api_recipes/*.recipe.yml']
|
|
1606
1792
|
end
|
|
1607
1793
|
```
|
|
1608
1794
|
|
|
@@ -1629,8 +1815,11 @@ become a method, when the configuration is loaded (see
|
|
|
1629
1815
|
|
|
1630
1816
|
It also warns, once for each endpoint, when the value of a path parameter
|
|
1631
1817
|
contains a `/` (it is sent as `%2F`) or looks already percent-encoded (it is
|
|
1632
|
-
encoded again). The warning names the endpoint and never the value.
|
|
1633
|
-
|
|
1818
|
+
encoded again). The warning names the endpoint and never the value. A value
|
|
1819
|
+
that your code encoded is given decoded; a value that the API gave already
|
|
1820
|
+
encoded, such as an id it returned in that form, is given as it is, and then
|
|
1821
|
+
the warning is expected. `config.upgrade_warnings = false` turns these two
|
|
1822
|
+
warnings off.
|
|
1634
1823
|
|
|
1635
1824
|
## Inspecting an API
|
|
1636
1825
|
|
|
@@ -1689,8 +1878,22 @@ github.users.show(username) GET https://api.github
|
|
|
1689
1878
|
github.repos.issues(owner, repo).create POST https://api.github.com/repos/:owner/:repo/issues
|
|
1690
1879
|
```
|
|
1691
1880
|
|
|
1692
|
-
|
|
1693
|
-
|
|
1881
|
+
With no file, in the root of a project, it lists the calls of every API of the
|
|
1882
|
+
project: it finds the files that describe an API, and the installed recipes, as
|
|
1883
|
+
`api_recipes upgrade` does.
|
|
1884
|
+
|
|
1885
|
+
```
|
|
1886
|
+
$ bundle exec api_recipes routes
|
|
1887
|
+
shop.orders POST https://shop.example.com/orders
|
|
1888
|
+
billing.invoices GET {erb}://{erb}/api/v1/invoices
|
|
1889
|
+
billing.invoices.show(id) GET {erb}://{erb}/api/v1/invoices/:id
|
|
1890
|
+
config/apis/billing.yml.erb: what an ERB tag gives is shown as {erb}; --erb evaluates the file, which runs the code in it
|
|
1891
|
+
```
|
|
1892
|
+
|
|
1893
|
+
A file that contains ERB is not run: what a `<%= ... %>` tag gives is shown as
|
|
1894
|
+
the variable `{erb}`. `--erb` evaluates the file instead, which runs the code
|
|
1895
|
+
in it. A file where a tag gives a name, or where a `<% ... %>` tag decides
|
|
1896
|
+
which lines exist, is listed only with `--erb`.
|
|
1694
1897
|
|
|
1695
1898
|
## Recipes
|
|
1696
1899
|
|
|
@@ -1700,7 +1903,7 @@ in it.
|
|
|
1700
1903
|
# config/initializers/api_recipes.rb
|
|
1701
1904
|
ApiRecipes.configure do |config|
|
|
1702
1905
|
config.logger = Rails.logger
|
|
1703
|
-
config.
|
|
1906
|
+
config.recipes_paths = Dir[Rails.root.join('config', 'api_recipes', '*.recipe.yml').to_s]
|
|
1704
1907
|
config.values = { github: { authorization: "Bearer #{Rails.application.credentials.github_token}" } }
|
|
1705
1908
|
end
|
|
1706
1909
|
```
|
|
@@ -1710,16 +1913,35 @@ A value that depends on the user who is signed in does not go there: see
|
|
|
1710
1913
|
|
|
1711
1914
|
### One host per environment
|
|
1712
1915
|
|
|
1713
|
-
Keep one
|
|
1916
|
+
Keep one recipe, with the address as variables, and let each environment give
|
|
1917
|
+
its values:
|
|
1714
1918
|
|
|
1715
1919
|
```yaml
|
|
1716
1920
|
reports:
|
|
1717
|
-
|
|
1718
|
-
|
|
1921
|
+
recipe:
|
|
1922
|
+
name: reports
|
|
1923
|
+
version: 1.0.0
|
|
1924
|
+
format: 1
|
|
1925
|
+
variables:
|
|
1926
|
+
protocol:
|
|
1927
|
+
default: https
|
|
1928
|
+
host:
|
|
1929
|
+
default: reports.example.com
|
|
1930
|
+
protocol: '{protocol}'
|
|
1931
|
+
host: '{host}'
|
|
1719
1932
|
endpoints:
|
|
1720
1933
|
monthly:
|
|
1721
1934
|
```
|
|
1722
1935
|
|
|
1936
|
+
```ruby
|
|
1937
|
+
ApiRecipes.configure do |config|
|
|
1938
|
+
config.recipes_paths = Dir['config/api_recipes/*.recipe.yml']
|
|
1939
|
+
config.values = {
|
|
1940
|
+
reports: { variables: { protocol: ENV['REPORTS_PROTOCOL'], host: ENV['REPORTS_HOST'] }.compact }
|
|
1941
|
+
}
|
|
1942
|
+
end
|
|
1943
|
+
```
|
|
1944
|
+
|
|
1723
1945
|
### Every page of a list
|
|
1724
1946
|
|
|
1725
1947
|
```ruby
|
|
@@ -1833,11 +2055,14 @@ end
|
|
|
1833
2055
|
|
|
1834
2056
|
What ApiRecipes does:
|
|
1835
2057
|
|
|
1836
|
-
- **Path parameters** are encoded one by one. A value cannot add a
|
|
1837
|
-
|
|
2058
|
+
- **Path parameters** are encoded one by one. A value cannot add a query
|
|
2059
|
+
string or a fragment, nor a segment unless the placeholder is a `*name`.
|
|
2060
|
+
`nil`, an empty value, `.` and `..` are refused, and so is an empty, `.` or
|
|
2061
|
+
`..` segment of a `*name`.
|
|
1838
2062
|
- **Variables** are checked for the place where they end up: a host, a path, a
|
|
1839
2063
|
header or a query parameter. In a path a value is one encoded segment, like a
|
|
1840
|
-
path parameter
|
|
2064
|
+
path parameter, or, written `{*name}`, several encoded segments, none of them
|
|
2065
|
+
empty, `.` or `..`.
|
|
1841
2066
|
- **Parameters sent with every request** cannot take the place of the
|
|
1842
2067
|
credential: with `auth.type: query`, `default_params` that name its parameter
|
|
1843
2068
|
are refused, and the credential is always the only parameter with its name.
|
|
@@ -1849,8 +2074,10 @@ What ApiRecipes does:
|
|
|
1849
2074
|
another finds it. A `configure` changes the descriptions and those values in
|
|
1850
2075
|
one step.
|
|
1851
2076
|
- **Credentials** are sent only to the origin they were set for, and a request
|
|
1852
|
-
that would send one elsewhere raises.
|
|
1853
|
-
|
|
2077
|
+
that would send one elsewhere raises. Only the application names other
|
|
2078
|
+
origins for the credential it gives, with `credential_origins` in
|
|
2079
|
+
`config.values`. A value with a control character is refused, so a credential
|
|
2080
|
+
cannot add a header.
|
|
1854
2081
|
- **Redirects.** With `follow`, a redirect to another origin drops the
|
|
1855
2082
|
credentials, the `Cookie` header and the credential query parameter. They are
|
|
1856
2083
|
not sent again if the chain comes back.
|
|
@@ -1863,8 +2090,9 @@ What ApiRecipes does:
|
|
|
1863
2090
|
evaluated with ERB and is read as plain YAML, without anchors, aliases or
|
|
1864
2091
|
tags. It cannot carry `verify_with`, cannot take the name of an API you
|
|
1865
2092
|
already have, and cannot let a variable decide where its host ends. Its
|
|
1866
|
-
text, comments included, holds only visible characters.
|
|
1867
|
-
it matches the checksum in
|
|
2093
|
+
text, comments included, holds only visible characters. One that `install`
|
|
2094
|
+
took from a repository is loaded only if it matches the checksum in
|
|
2095
|
+
`recipes.lock`.
|
|
1868
2096
|
- **`api_recipes install` and `update`** show where the requests and the
|
|
1869
2097
|
credential go, the headers and the parameters sent with every request, those
|
|
1870
2098
|
that an endpoint adds, and which calls are not GET, the ones that `resource`
|
|
@@ -1890,8 +2118,8 @@ What ApiRecipes does:
|
|
|
1890
2118
|
`Marshal.dump` and `YAML.dump`, and its JSON is the one of `to_h`.
|
|
1891
2119
|
- **`fill`** calls only public writers and skips keys that are not plain
|
|
1892
2120
|
attribute names.
|
|
1893
|
-
- **
|
|
1894
|
-
|
|
2121
|
+
- **The configuration** is validated when it is loaded, whether it comes
|
|
2122
|
+
from a recipe or from `apis_configs`.
|
|
1895
2123
|
|
|
1896
2124
|
What stays with your application:
|
|
1897
2125
|
|
|
@@ -1905,7 +2133,12 @@ What stays with your application:
|
|
|
1905
2133
|
the variables of one user on it. Use `with`, or an object made for the request.
|
|
1906
2134
|
- `response.original_response` is the raw http.rb response, and it gives access
|
|
1907
2135
|
to the request that was sent, headers included. Do not log or serialize it.
|
|
1908
|
-
-
|
|
2136
|
+
- The gem evaluates no ERB. `api_recipes upgrade --erb` and
|
|
2137
|
+
`api_recipes routes --erb` do, on the files of a 2.x project that you name:
|
|
2138
|
+
that runs the code in them.
|
|
2139
|
+
- A recipe of the project, which the lock ties to no repository, is loaded as
|
|
2140
|
+
it is written: review a recipe that someone adds to the folder as you review
|
|
2141
|
+
code, because it decides where the requests and the credential go.
|
|
1909
2142
|
- Read what `api_recipes install` shows before you answer yes. ApiRecipes checks
|
|
1910
2143
|
the form of a recipe, not whose host it names. Commit the installed recipes
|
|
1911
2144
|
and `recipes.lock`, and review their changes as you review code: the lock
|
|
@@ -1922,9 +2155,8 @@ Every protection has an example in [`spec/security`](spec/security); its
|
|
|
1922
2155
|
|
|
1923
2156
|
| Option | Meaning |
|
|
1924
2157
|
| --- | --- |
|
|
1925
|
-
| `
|
|
2158
|
+
| `recipes_paths` | Array of portable recipes (`NAME.recipe.yml`), read as data only; one that `recipes.lock` ties to a repository is compared with it |
|
|
1926
2159
|
| `apis_configs` | Hash of API descriptions |
|
|
1927
|
-
| `recipes_paths` | Array of installed portable recipes (`NAME.recipe.yml`), read as data only and checked against `recipes.lock` |
|
|
1928
2160
|
| `values` | for each API, its credential and the values of its variables for the whole application, as `{ api_name: { authorization: '...', variables: { name: value } } }`; `basic_auth` in place of `authorization` |
|
|
1929
2161
|
| `logger` | a Logger to use |
|
|
1930
2162
|
| `log_to` | path or IO for the default logger (default: STDERR) |
|
|
@@ -1939,12 +2171,12 @@ Every protection has an example in [`spec/security`](spec/security); its
|
|
|
1939
2171
|
| `protocol` | `https` | `http` or `https`; can be a `{variable}` |
|
|
1940
2172
|
| `host` | `localhost` | host name, which can contain `{variables}` |
|
|
1941
2173
|
| `port` | port of the protocol | port number; can be a `{variable}` |
|
|
1942
|
-
| `base_url` | none | prefix of every path, for example `api/v1`; can contain `{variables}` |
|
|
2174
|
+
| `base_url` | none | prefix of every path, for example `api/v1`; can contain `{variables}`, and `{*name}` for a value of several segments |
|
|
1943
2175
|
| `timeout` | `3` | seconds allowed for the whole request; can be a `{variable}`, as `on_bad_code` and `mime_type` |
|
|
1944
2176
|
| `default_headers` | none | headers sent with every request; a value can contain `{variables}` |
|
|
1945
2177
|
| `default_params` | none | query parameters sent with every request; a value is text, a number or a boolean, and can contain `{variables}` |
|
|
1946
2178
|
| `on_bad_code` | `raise` | `raise` or `ignore` when the response is not a success |
|
|
1947
|
-
| `mime_type` | from the response | content type used by `response.data` |
|
|
2179
|
+
| `mime_type` | from the response | content type used by `response.data` and `response.parse` |
|
|
1948
2180
|
| `verify_with` | none | method that decides whether a response is a success |
|
|
1949
2181
|
| `auth` | none | where the credential goes: `type` (`none`, `bearer`, `basic`, `header`, `query`) and `name` (for `header` and `query`) |
|
|
1950
2182
|
| `variables` | none | the variables of the API, each with an optional `default` and `description` |
|
|
@@ -1954,16 +2186,16 @@ Every protection has an example in [`spec/security`](spec/security); its
|
|
|
1954
2186
|
|
|
1955
2187
|
| Option | Default | Meaning |
|
|
1956
2188
|
| --- | --- | --- |
|
|
1957
|
-
| `path` | the endpoint name | segment added to the parent path; `''` adds nothing; can contain `:name` and `*name` placeholders and `{
|
|
2189
|
+
| `path` | the endpoint name | segment added to the parent path; `''` adds nothing; can contain `:name` and `*name` placeholders, `{variables}` and `{*name}` |
|
|
1958
2190
|
| `verb` | `get` | HTTP verb; can be a `{variable}`, as `ok_code` (one status), `timeout`, `on_bad_code` and `mime_type` |
|
|
1959
2191
|
| `route` | `yes` | `no` makes the endpoint a group that cannot be called |
|
|
1960
2192
|
| `ok_code` | any 2xx | the status, or the list of statuses, accepted as a success |
|
|
1961
2193
|
| `timeout` | looked for up the tree | seconds allowed for the whole request |
|
|
1962
2194
|
| `on_bad_code` | looked for up the tree | `raise` or `ignore` |
|
|
1963
|
-
| `mime_type` | looked for up the tree | content type used by `response.data` |
|
|
2195
|
+
| `mime_type` | looked for up the tree | content type used by `response.data` and `response.parse` |
|
|
1964
2196
|
| `verify_with` | looked for up the tree | method that decides whether a response is a success |
|
|
1965
|
-
| `default_headers` | none | headers
|
|
1966
|
-
| `default_params` | none | query parameters
|
|
2197
|
+
| `default_headers` | none | headers of this endpoint; a request finds them here when it looks up the tree, before those of the API |
|
|
2198
|
+
| `default_params` | none | query parameters of this endpoint; a request finds them here when it looks up the tree, before those of the API |
|
|
1967
2199
|
| `resource` | `no` | `yes` adds the endpoints `show`, `create`, `update`, `replace` and `delete`; a list adds only the ones it names |
|
|
1968
2200
|
| `endpoints` | none | nested endpoints |
|
|
1969
2201
|
|
|
@@ -2013,7 +2245,8 @@ operation, as its verb does.
|
|
|
2013
2245
|
`ApiNameClashError`, `ConfigurationError`, `CredentialOriginMismatch`,
|
|
2014
2246
|
`InvalidCredential`, `InvalidPathParameter`, `InvalidUrl`, `InvalidVariable`,
|
|
2015
2247
|
`MissingVariable`, `NoRouteExists`, `PathParamsMismatch`, `RecipeFileError`,
|
|
2016
|
-
`ResponseCodeNotAsExpected` and `
|
|
2248
|
+
`ResponseCodeNotAsExpected`, `UnknownEndpoint` and `UnreadableResponse`). If
|
|
2249
|
+
your application has a
|
|
2017
2250
|
class with one of these names, write it with a leading `::` inside the classes
|
|
2018
2251
|
that include `ApiRecipes`, for example `::Response`. The internal classes of
|
|
2019
2252
|
the gem are all under `ApiRecipes::Internals`, so they hide nothing else.
|
|
@@ -2038,8 +2271,8 @@ Run one from the root of the repository:
|
|
|
2038
2271
|
|
|
2039
2272
|
$ bundle exec ruby examples/simple_usage.rb
|
|
2040
2273
|
|
|
2041
|
-
[`examples/config/github.yml
|
|
2042
|
-
|
|
2274
|
+
[`examples/config/github.recipe.yml`](examples/config/github.recipe.yml) is a
|
|
2275
|
+
recipe with every option commented.
|
|
2043
2276
|
|
|
2044
2277
|
## Compatibility
|
|
2045
2278
|
|