api_recipes 3.0.0.beta1 → 3.0.0.beta3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +667 -78
  3. data/UPGRADING.md +67 -22
  4. data/lib/api_recipes/api.rb +14 -10
  5. data/lib/api_recipes/cli/import/document.rb +234 -0
  6. data/lib/api_recipes/cli/import/security.rb +138 -0
  7. data/lib/api_recipes/cli/import/server.rb +166 -0
  8. data/lib/api_recipes/cli/import/tree.rb +288 -0
  9. data/lib/api_recipes/cli/import/writer.rb +127 -0
  10. data/lib/api_recipes/cli/import.rb +129 -0
  11. data/lib/api_recipes/cli/recipes/git.rb +197 -0
  12. data/lib/api_recipes/cli/recipes/lock_file.rb +34 -0
  13. data/lib/api_recipes/cli/recipes/summary.rb +226 -0
  14. data/lib/api_recipes/cli/recipes.rb +287 -0
  15. data/lib/api_recipes/cli/routes.rb +55 -0
  16. data/lib/api_recipes/cli/upgrade.rb +10 -2
  17. data/lib/api_recipes/cli.rb +163 -1
  18. data/lib/api_recipes/configuration.rb +50 -13
  19. data/lib/api_recipes/endpoint.rb +9 -3
  20. data/lib/api_recipes/errors.rb +2 -1
  21. data/lib/api_recipes/internals/compiler.rb +146 -33
  22. data/lib/api_recipes/internals/context.rb +52 -14
  23. data/lib/api_recipes/internals/declaration.rb +18 -18
  24. data/lib/api_recipes/internals/definition.rb +75 -22
  25. data/lib/api_recipes/internals/level.rb +54 -43
  26. data/lib/api_recipes/internals/path_template.rb +29 -4
  27. data/lib/api_recipes/internals/plain_yaml.rb +133 -0
  28. data/lib/api_recipes/internals/recipe.rb +297 -0
  29. data/lib/api_recipes/internals/recipe_lock.rb +115 -0
  30. data/lib/api_recipes/internals/registry.rb +61 -15
  31. data/lib/api_recipes/internals/request.rb +26 -9
  32. data/lib/api_recipes/internals/template.rb +17 -5
  33. data/lib/api_recipes/version.rb +1 -1
  34. data/lib/api_recipes.rb +7 -3
  35. metadata +15 -1
data/README.md CHANGED
@@ -33,6 +33,8 @@ Coming from version 2? Read [UPGRADING.md](UPGRADING.md) first.
33
33
  - [Describing an API](#describing-an-api)
34
34
  - [Variables](#variables)
35
35
  - [Loading the configuration](#loading-the-configuration)
36
+ - [Importing an OpenAPI description](#importing-an-openapi-description)
37
+ - [Portable recipes](#portable-recipes)
36
38
  - [Where an API is available](#where-an-api-is-available)
37
39
  - [Making requests](#making-requests)
38
40
  - [Options for one chain: `with`](#options-for-one-chain-with)
@@ -142,6 +144,16 @@ github:
142
144
  repositories: # GET /search/repositories
143
145
  ```
144
146
 
147
+ An option is written once, at the highest place where it holds. An endpoint
148
+ that does not set `timeout`, `on_bad_code`, `mime_type` or `verify_with` looks
149
+ for it up the tree: in the endpoint that contains it, then in the one that
150
+ contains that one, up to the API. The first that sets the option gives the
151
+ value, and when none does the default applies. An endpoint that is not on that
152
+ way up, such as one next to it, is never looked at. `default_headers` and
153
+ `default_params` are looked for in the same way, one name at a time.
154
+ `path`, `verb`, `route`, `ok_code` and `resource` describe one endpoint and are
155
+ read only there.
156
+
145
157
  The configuration is checked when it is loaded. A mistake, such as an unknown
146
158
  option, a verb that does not exist or a negative timeout, raises
147
159
  `ApiRecipes::ConfigurationError`. Its message lists every problem at once, so you
@@ -201,12 +213,57 @@ ApiRecipes.github.repos('rails', 'rails').contents('lib/rails.rb').url.to_s
201
213
  A `*name` placeholder works only at the start of a segment, and a `:` that must
202
214
  stay in the path is written `\:`.
203
215
 
216
+ A `{name}` in a path is a [variable](#variables): a value given once, to a
217
+ chain, an object, a class or the whole application, where a `:name` is an
218
+ argument of each call.
219
+
204
220
  ### Endpoints that only group others
205
221
 
206
222
  With `route: no` an endpoint exists only to hold nested endpoints. `search` above
207
223
  is not callable, `search.repositories` is. Calling `run` on it raises
208
224
  `ApiRecipes::NoRouteExists`.
209
225
 
226
+ ### REST resources
227
+
228
+ When an endpoint is a REST resource, `resource: true` adds the usual endpoints
229
+ under it, so you do not write them one by one:
230
+
231
+ ```yaml
232
+ billing:
233
+ host: billing.example.test
234
+ endpoints:
235
+ invoices:
236
+ resource: true
237
+ ```
238
+
239
+ | Call | Request |
240
+ | --- | --- |
241
+ | `invoices` | `GET /invoices`: the endpoint itself, as always |
242
+ | `invoices.show(id)` | `GET /invoices/:id` |
243
+ | `invoices.create` | `POST /invoices` |
244
+ | `invoices.update(id)` | `PATCH /invoices/:id` |
245
+ | `invoices.replace(id)` | `PUT /invoices/:id` |
246
+ | `invoices.delete(id)` | `DELETE /invoices/:id` |
247
+
248
+ ```ruby
249
+ ApiRecipes.billing.invoices.show(42).url.to_s
250
+ # => "https://billing.example.test/invoices/42"
251
+
252
+ ApiRecipes.billing.invoices.delete(42).inspect
253
+ # => "#<ApiRecipes::Endpoint billing.invoices.delete DELETE https://billing.example.test/invoices/42>"
254
+ ```
255
+
256
+ - A list adds only some of them: `resource: [show, create]`.
257
+ - An endpoint written under `endpoints` with one of those names completes the
258
+ generated one: its options win and the others stay. `show: { path: /:number }`
259
+ changes the placeholder, `delete: { ok_code: 204 }` adds the expected status.
260
+ - The other endpoints written by hand come after the generated ones.
261
+ - `resource` only adds endpoints. The options of the endpoint itself stay its
262
+ own: `route: no` when the list cannot be called, `path` to change the path of
263
+ all of them.
264
+ - A verb is never read from a name: an endpoint called `delete` that you write
265
+ by hand is a GET until you give it `verb: delete`.
266
+
210
267
  ### Settings for the whole API
211
268
 
212
269
  ```yaml
@@ -236,10 +293,60 @@ for a value that changes, use a [variable](#variables).
236
293
 
237
294
  All options are listed in the [options reference](#options-reference).
238
295
 
296
+ ### Headers and parameters sent with every request
297
+
298
+ `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: there they hold
300
+ for that endpoint and for the ones below it, on top of those of the API.
301
+
302
+ ```yaml
303
+ billing:
304
+ host: billing.example.test
305
+ default_headers:
306
+ Accept: application/json
307
+ default_params:
308
+ locale: en
309
+ endpoints:
310
+ invoices:
311
+ endpoints:
312
+ show:
313
+ path: /:id
314
+ overdue: # GET /invoices?locale=en&status=overdue
315
+ path: ''
316
+ default_params:
317
+ status: overdue
318
+ pdf: # GET /invoices/:id/pdf with Accept: application/pdf
319
+ path: /:id/pdf
320
+ default_headers:
321
+ Accept: application/pdf
322
+ ```
323
+
324
+ ```ruby
325
+ ApiRecipes.billing.invoices.overdue.run.uri.to_s
326
+ # => "https://billing.example.test/invoices?locale=en&status=overdue"
327
+
328
+ ApiRecipes.billing.invoices.overdue(params: { status: 'paid', page: 2 }).run.uri.to_s
329
+ # => "https://billing.example.test/invoices?locale=en&status=paid&page=2"
330
+ ```
331
+
332
+ On the same name the nearest to the request wins: the API, then
333
+ [`with`](#options-for-one-chain-with), then the endpoints from the farthest to
334
+ the nearest. After them come the credential and what the request gives. For a
335
+ header: the credential, then the `headers:` of the request. For a parameter:
336
+ the `params:` of the request, then the credential when it travels in the query,
337
+ which nothing replaces.
338
+
339
+ The names of headers are compared as HTTP does, without their case and with
340
+ `_` read as `-`: `accept` on an endpoint replaces the `Accept` of the API. The
341
+ same name written twice in one place is an error. A value can contain
342
+ [variables](#variables). When an API has `default_params`, the `params:` of a
343
+ request must be a Hash.
344
+
239
345
  ## Variables
240
346
 
241
- A variable is a `{name}` placeholder in the `host`, the `base_url` or the value
242
- of a header. Use it for what changes from one account or one environment to the
347
+ A variable is a `{name}` placeholder in the `host`, the `base_url`, the `path`
348
+ of an endpoint, or the value of a header or of a parameter sent with every
349
+ request. Use it for what changes from one account or one environment to the
243
350
  next, such as the address of a shop. Declare the variables of the API, with an
244
351
  optional `default` and `description`:
245
352
 
@@ -250,6 +357,8 @@ shopify:
250
357
  description: the part of the shop address before .myshopify.com
251
358
  api_version:
252
359
  default: '2026-10'
360
+ location:
361
+ description: the id of the location this client works for
253
362
  host: '{shop}.myshopify.com'
254
363
  base_url: admin/api/{api_version}
255
364
  auth:
@@ -257,18 +366,31 @@ shopify:
257
366
  name: X-Shopify-Access-Token
258
367
  endpoints:
259
368
  products:
369
+ inventory:
370
+ path: /locations/{location}/inventory_levels
260
371
  ```
261
372
 
262
373
  A variable without a value and without a default raises
263
- `ApiRecipes::MissingVariable` when the request is built. Give it a value in the
264
- way that fits your code. The nearest one wins: the chain (`with`), then the
265
- object, then its class, then `ApiRecipes.shopify`, then the default of the file.
374
+ `ApiRecipes::MissingVariable` as soon as it is needed: when the chain reaches
375
+ the `base_url` or the path that uses it, and at the latest when the request is
376
+ built. Give it a value in the way that fits your code. The nearest one wins: the chain (`with`), then the
377
+ object, then the class that declares the API, then the whole application, then
378
+ the default of the file.
379
+
380
+ For one chain:
266
381
 
267
382
  ```ruby
268
383
  ApiRecipes.shopify.with(variables: { shop: 'acme' }).products.url.to_s
269
384
  # => "https://acme.myshopify.com/admin/api/2026-10/products"
385
+ ```
386
+
387
+ For the whole application, when it starts, with `config.values`:
388
+
389
+ ```ruby
390
+ ApiRecipes.configure do |config|
391
+ config.values = { shopify: { variables: { shop: 'acme' } } }
392
+ end
270
393
 
271
- ApiRecipes.shopify.variables = { shop: 'acme' }
272
394
  ApiRecipes.shopify.products.url.to_s
273
395
  # => "https://acme.myshopify.com/admin/api/2026-10/products"
274
396
 
@@ -276,6 +398,14 @@ ApiRecipes.shopify.with(variables: { api_version: '2027-01' }).products.url.to_s
276
398
  # => "https://acme.myshopify.com/admin/api/2027-01/products"
277
399
  ```
278
400
 
401
+ In a path, a `:name` is an argument of the method and a `{name}` is a variable.
402
+ The location is set once, and no call has to repeat it:
403
+
404
+ ```ruby
405
+ ApiRecipes.shopify.with(variables: { shop: 'acme', location: 7 }).inventory.url.to_s
406
+ # => "https://acme.myshopify.com/admin/api/2026-10/locations/7/inventory_levels"
407
+ ```
408
+
279
409
  For one class, or for the objects of a class:
280
410
 
281
411
  ```ruby
@@ -309,30 +439,51 @@ Sandbox.sandbox.with(variables: { tenant: 'acme' }).ping.url.to_s
309
439
  # => "https://acme.sandbox.example.test/ping"
310
440
  ```
311
441
 
312
- Or for everybody, when the application starts:
442
+ `config.values` holds, for each API, its `variables` and its credential
443
+ (`authorization` or `basic_auth`, see [Authorization](#authorization)). A later
444
+ `configure` that assigns `config.values` replaces them all; one that does not
445
+ keeps them.
446
+
447
+ ApiRecipes never reads the environment for you: if the shop comes from an
448
+ environment variable, read it in your code and pass it in.
449
+
450
+ When the value changes with each request, as in a server where it depends on
451
+ the user who is signed in, give it to `with` or to an object that lives as long
452
+ as the request. Both belong to the code that created them, so two requests
453
+ served at the same time, in two threads, never see the value of each other:
313
454
 
314
455
  ```ruby
315
- ApiRecipes.configure do |config|
316
- config.variables = { shopify: { shop: 'acme' } }
456
+ class OrdersController < ApplicationController
457
+ def index
458
+ shop = ApiRecipes.shopify.with(variables: { shop: current_user.shop, location: current_user.location_id },
459
+ authorization: current_user.shopify_token)
460
+ render json: shop.inventory.run.data
461
+ end
317
462
  end
318
463
  ```
319
464
 
320
- ApiRecipes never reads the environment for you: if the shop comes from an
321
- environment variable, read it in your code and pass it in.
322
-
323
- A value is checked for the place where it ends up. In a `host` it can contain
324
- only letters, digits, dots, hyphens and underscores. In a `base_url` it is
325
- encoded like a path parameter. In a header it cannot contain a line break or
326
- another control character. Anything else raises `ApiRecipes::InvalidVariable`.
465
+ What `ApiRecipes.shopify` and a class use is shared by the whole process, in
466
+ every thread. That is why it is given only where the application starts, to
467
+ `config.values` and to `api`, and why `ApiRecipes.shopify.variables = ...` and
468
+ `Store.shopify.variables = ...` raise `ApiRecipes::Error`: a value that changes
469
+ while requests are served goes to `with` or to an object. The same holds for
470
+ credentials.
471
+
472
+ The value of a variable is a String, a Symbol or a number, and is kept as
473
+ text. A value is checked for the place where it ends up. In a `host` it can contain
474
+ only letters, digits, dots, hyphens and underscores. In a `base_url` and in a
475
+ `path` it is encoded like a path parameter, and it cannot be empty, `.` or
476
+ `..`. In a header it cannot contain a line break or another control character.
477
+ In a parameter it is encoded like every value of a query string. Anything else
478
+ raises `ApiRecipes::InvalidVariable`.
479
+ A word in braces that is not a variable is written `\{word}` in a path and in
480
+ a `base_url`.
327
481
  A name that the API does not declare raises `ApiRecipes::ConfigurationError` at
328
482
  every level, so a misspelled name is never ignored.
329
- `variables` without an assignment returns the values set at the level you call
330
- it on. Assigning `nil` clears them. At the global level (`ApiRecipes.shopify`)
331
- that includes the values given with `config.variables`, which are the same level;
332
- a default declared in the file applies again.
333
- The values given with `config.variables` are applied again at every `configure`,
334
- so a value assigned directly with `ApiRecipes.shopify.variables = ...` under the
335
- same name lasts only until the next `configure`.
483
+ `variables` without an assignment returns the values of the level you call it
484
+ on: those of `config.values` on `ApiRecipes.shopify`, those given to `api` on a
485
+ class, those assigned on an object. On an object, assigning `nil` clears them,
486
+ and a default declared in the file applies again.
336
487
 
337
488
  ## Loading the configuration
338
489
 
@@ -353,7 +504,8 @@ together: they are applied in the order they are assigned, so assigning
353
504
 
354
505
  A file that is missing, empty, not valid YAML or not valid UTF-8 raises
355
506
  `ApiRecipes::RecipeFileError`, which names the file. A file called
356
- `something.recipe.yml` is refused: that name is kept for another purpose.
507
+ `something.recipe.yml` is refused here: it is a [portable recipe](#portable-recipes)
508
+ and goes in `recipes_paths`, where it is never evaluated with ERB.
357
509
 
358
510
  Files are evaluated with ERB before being parsed, so values can come from the
359
511
  environment:
@@ -388,6 +540,349 @@ Keys can be strings or symbols, but not both for the same option.
388
540
  If the configuration has a problem, `configure` raises and the previous
389
541
  configuration stays active.
390
542
 
543
+ ## Importing an OpenAPI description
544
+
545
+ When an API publishes an OpenAPI description, the `api_recipes` command writes
546
+ the recipe for you:
547
+
548
+ $ api_recipes import openapi.yml --name github --output config/apis/github.yml
549
+
550
+ The description is read once, when you run the command. Your application never
551
+ reads it: it loads the recipe, which is a file like the ones above, and you can
552
+ read it, trim it and rename things in it. These operations
553
+
554
+ ```
555
+ GET /users
556
+ GET /users/{username}
557
+ GET /users/{username}/repos
558
+ GET /repos/{owner}/{repo}/issues
559
+ POST /repos/{owner}/{repo}/issues
560
+ GET /repos/{owner}/{repo}/issues/{issue_number}
561
+ PATCH /repos/{owner}/{repo}/issues/{issue_number}
562
+ ```
563
+
564
+ become this recipe:
565
+
566
+ ```
567
+ # Generated by api_recipes import from GitHub 1.0
568
+ github:
569
+ host: api.github.com
570
+ endpoints:
571
+ users: # GET /users (users/list)
572
+ endpoints:
573
+ show: # GET /users/{username} (users/get-by-username)
574
+ path: "/:username"
575
+ repos: # GET /users/{username}/repos (repos/list-for-user)
576
+ path: "/:username/repos"
577
+ repos:
578
+ route: false
579
+ endpoints:
580
+ issues: # GET /repos/{owner}/{repo}/issues (issues/list-for-repo)
581
+ path: "/:owner/:repo/issues"
582
+ endpoints:
583
+ create: # POST /repos/{owner}/{repo}/issues (issues/create)
584
+ path: ''
585
+ verb: post
586
+ ok_code: 201
587
+ show: # GET /repos/{owner}/{repo}/issues/{issue_number} (issues/get)
588
+ path: "/:issue_number"
589
+ update: # PATCH /repos/{owner}/{repo}/issues/{issue_number} (issues/update)
590
+ path: "/:issue_number"
591
+ verb: patch
592
+ ```
593
+
594
+ | Request | Call |
595
+ | --- | --- |
596
+ | `GET /users` | `github.users` |
597
+ | `GET /users/{username}` | `github.users.show('octocat')` |
598
+ | `GET /users/{username}/repos` | `github.users.repos('octocat')` |
599
+ | `GET /repos/{owner}/{repo}/issues` | `github.repos.issues('rails', 'rails')` |
600
+ | `POST /repos/{owner}/{repo}/issues` | `github.repos.issues('rails', 'rails').create(json: { title: 'Bug' })` |
601
+ | `GET /repos/{owner}/{repo}/issues/{issue_number}` | `github.repos.issues('rails', 'rails').show(1347)` |
602
+ | `PATCH /repos/{owner}/{repo}/issues/{issue_number}` | `github.repos.issues('rails', 'rails').update(1347, json: { state: 'closed' })` |
603
+
604
+ ### Options
605
+
606
+ | Option | Meaning |
607
+ | --- | --- |
608
+ | `--name NAME` | the name of the API in the recipe. Default: the title of the description, in lower case with `_` |
609
+ | `--only GLOB` | only the paths that match; can be repeated. `*` also crosses `/`: `--only 'repos/*'` |
610
+ | `--except GLOB` | leaves out the paths that match; can be repeated |
611
+ | `--server N` | which entry of `servers` gives the address, counted from 0. Default: the first |
612
+ | `--output FILE` | writes the recipe in FILE, which must not exist, instead of on STDOUT |
613
+
614
+ The input is one local file, YAML or JSON, OpenAPI 3.0 or 3.1. Warnings and a
615
+ summary go to STDERR, so the recipe can be redirected. The command exits with 1
616
+ when the description cannot be imported and with 2 when the arguments are wrong.
617
+
618
+ ### What is read
619
+
620
+ | From the description | In the recipe |
621
+ | --- | --- |
622
+ | the address of the server | `protocol`, `host`, `port`, `base_url` |
623
+ | the variables of the server | `variables`, with their defaults |
624
+ | the paths | the tree of endpoints |
625
+ | the path parameters | `:name` placeholders |
626
+ | the query parameters | a comment beside the endpoint, with a `*` on the required ones |
627
+ | the documented 2xx statuses | `ok_code`: one status, or a list. Left out when `200` is the only one or none is documented, since every 2xx is accepted by default |
628
+ | `security` and `securitySchemes` | `auth`: `bearer` for http bearer, OAuth 2 and OpenID Connect; `basic`; `header` or `query` for an API key |
629
+ | `operationId`, `deprecated` | a comment beside the endpoint |
630
+
631
+ Schemas, bodies, callbacks, webhooks, links and cookie parameters are not read:
632
+ the gem does not validate requests and responses against them.
633
+
634
+ ### How the names are made
635
+
636
+ Each segment of a path that is not a parameter is a node, named after the
637
+ segment in lower case, with `camelCase` turned into `snake_case` and every other
638
+ character into `_`. The parameters that come before a segment travel with it;
639
+ those at the end of the path travel with the operation. An operation is named
640
+ after its verb and after how its path ends:
641
+
642
+ | Verb | The path ends with a name (`/issues`) | The path ends with a parameter (`/issues/{n}`) |
643
+ | --- | --- | --- |
644
+ | GET | the node itself | `show` |
645
+ | POST | `create` | `post` |
646
+ | PUT | `replace_all` | `replace` |
647
+ | PATCH | `update_all` | `update` |
648
+ | DELETE | `delete_all` | `delete` |
649
+ | others | the name of the verb | the name of the verb and `_one` |
650
+
651
+ - A node whose path has no GET only groups others: `route: false`.
652
+ - Two endpoints with the same name under the same node: one gets a number
653
+ (`show_2`), and a warning says so.
654
+ - A name that is an existing method, such as `with`, is written all the same,
655
+ with a warning: reach it with `[]`.
656
+ - A single resource comes out badly: `PATCH /user` becomes `user.update_all`.
657
+ Rename it in the file.
658
+
659
+ What the gem cannot write as a path is left out, with a warning for each path:
660
+ a parameter directly followed by a letter, a digit or `_` (`/files/{id}_meta`),
661
+ because the gem reads `:id_meta` as one name; a segment that starts with `*` and
662
+ a name, which the gem reads as a parameter that spans segments; and an empty
663
+ segment (`/a//b`), because the gem joins two slashes into one.
664
+
665
+ ### What the command refuses
666
+
667
+ - Swagger 2.0: convert the description to OpenAPI 3 first.
668
+ - A `$ref` to another file or to an address: bundle the description into one
669
+ file first. References inside the file are followed.
670
+ - A description whose text would become code in the recipe: `<%` in a value is
671
+ an error that says where, and it is removed from the comments.
672
+ - A YAML description with binary values, with keys that are not plain text, or
673
+ with aliases that stand for more than ten million values.
674
+
675
+ The command reads that one file, never uses the network and runs nothing of the
676
+ description.
677
+
678
+ ## Portable recipes
679
+
680
+ A portable recipe is a recipe made to be shared: one API in one file, and
681
+ nothing but data. You install it with a command that shows what it does before
682
+ it writes anything, and you load it with `recipes_paths`.
683
+
684
+ $ api_recipes install github
685
+ github 1.0.0
686
+ summary: GitHub REST API, users, repositories, issues and pull requests
687
+ homepage: https://docs.github.com/en/rest
688
+ from: https://github.com/madAle/api_recipes-cookbook, commit a4e8dd017d05
689
+ address: https://api.github.com
690
+ credential: in the Authorization header
691
+ headers: Accept: application/vnd.github+json
692
+ calls: 14, of which 2 are not GET:
693
+ POST github.repos.issues(owner, repo).create /repos/:owner/:repo/issues
694
+ POST github.repos.issues(owner, repo).comments(issue_number).create /repos/:owner/:repo/issues/:issue_number/comments
695
+ Install github 1.0.0 in config/api_recipes? [y/N] y
696
+ github 1.0.0 is installed in config/api_recipes/github.recipe.yml
697
+
698
+ ```ruby
699
+ ApiRecipes.configure do |config|
700
+ config.recipes_paths = Dir['config/api_recipes/*.recipe.yml']
701
+ end
702
+
703
+ ApiRecipes.github.users.show('octocat').run
704
+ ```
705
+
706
+ The command writes two files in `config/api_recipes`: the recipe,
707
+ `github.recipe.yml`, and `recipes.lock`, which records the version, the
708
+ repository, the commit and the checksum of each installed recipe. Commit both
709
+ to your repository: the application downloads nothing when it starts.
710
+
711
+ Recipes come from the
712
+ [cookbook](https://github.com/madAle/api_recipes-cookbook), a Git repository
713
+ that takes contributions by pull request, or from any repository with the same
714
+ layout.
715
+
716
+ ### Installing, updating and checking
717
+
718
+ $ api_recipes install github # the recipe at the head of the cookbook
719
+ $ api_recipes install github --version 1.0.0 # the one at the tag github-1.0.0
720
+ $ api_recipes install billing --from https://github.com/acme/api-recipes
721
+ $ api_recipes install ./billing.recipe.yml # a file of yours
722
+ $ api_recipes update github
723
+ $ api_recipes check
724
+ $ api_recipes check recipes/billing.recipe.yml
725
+
726
+ - `install NAME` reads `recipes/NAME.recipe.yml` from the repository. `--from`
727
+ names another repository: an `https://` address without a user name or a
728
+ password, or an `ssh` address. The `git` command of your machine does the
729
+ download, and the command says which address it reads before `git` connects.
730
+ For a repository of your company, see [private repositories](#private-repositories).
731
+ - Before writing, `install` shows the address the requests go to, as the recipe
732
+ writes it and with the defaults of its variables, where the credential goes,
733
+ the headers and the parameters sent with every request, with the endpoint
734
+ for those that only an endpoint adds, the variables to set and every call
735
+ that is not a GET. The address is shown whole. A name or a value longer than
736
+ 100 characters is cut and followed by its length, so you know to read it in
737
+ the file. It warns when the credential would travel in the query string,
738
+ and when the protocol is `http`. Only `y` or `yes` installs; `--yes` installs
739
+ without asking.
740
+ - `--to DIR` installs in another folder. `update` and `check` take it as
741
+ `--dir DIR`.
742
+ - `update NAME` takes the recipe that its repository has now, or the one of
743
+ `--version`, and shows what changes in the address, the credential, the
744
+ headers, the parameters and the verbs before it replaces the file. A header
745
+ or a parameter that changes is shown whole, before and after. It reads the
746
+ repository from `recipes.lock`, so it needs no `--from`.
747
+ - `check` compares the installed recipes with `recipes.lock`, as `configure`
748
+ does. `check FILE` says whether a file is a valid portable recipe: use it
749
+ while you write one.
750
+ - `install FILE` installs a file of yours. It is also how you accept a change
751
+ you made by hand to an installed recipe: `api_recipes install
752
+ config/api_recipes/github.recipe.yml` writes its new checksum in the lock.
753
+ From then on the lock no longer names a repository for it, and `update`
754
+ refuses it.
755
+
756
+ Exit codes: 0 when the work is done or there was nothing to do; 1 when a recipe
757
+ is not valid, is not found, does not match the lock or the answer is no; 2 when
758
+ the arguments are wrong, `git` fails or a file cannot be read or written.
759
+
760
+ ### Private repositories
761
+
762
+ A company can keep its recipes in a repository of its own, private, on any Git
763
+ server that speaks `https` or `ssh`: GitHub, GitLab, Bitbucket, Gitea or one
764
+ you host. The repository has the layout of the cookbook, a folder `recipes`
765
+ with one `NAME.recipe.yml` per recipe, and `--from` names it:
766
+
767
+ $ api_recipes install billing --from https://git.example.com/platform/api-recipes
768
+ $ api_recipes install billing --from git@git.example.com:platform/api-recipes.git
769
+ $ api_recipes update billing
770
+
771
+ **Over `ssh`** the address is written as `user@host:path` or as
772
+ `ssh://user@host:port/path`, and `ssh` works as it does for `git clone`: with
773
+ your keys, your agent and your `~/.ssh/config`. A deploy key that can read the
774
+ repository is enough, also in CI. When `ssh` has to ask something, such as the
775
+ passphrase of a key or whether to trust a host it meets for the first time, it
776
+ asks on the terminal; without a terminal it fails, and the message says what
777
+ is missing. When `ssh` warns that the key of a host has changed, the command
778
+ says so and does not suggest to trust it. An address with a password is
779
+ refused. The user of the address is written in `recipes.lock` with it: use the
780
+ one everybody shares, such as `git`, or leave it out of an `ssh://` address and
781
+ let the `ssh` configuration of each person choose.
782
+
783
+ **Over `https`** `git` uses the user name and the password it already has for
784
+ that host. When it has none and the command runs on a terminal, `git` asks for
785
+ them, as it does for a clone: they go from the terminal to `git`, and the
786
+ command never sees them. Without a terminal, as in CI, nothing is asked and the
787
+ command fails with a message that says so. They are never taken in the
788
+ address. Two ways to give them to `git` once, on a laptop and in CI:
789
+
790
+ # a credential helper: the one of your system, or a file
791
+ $ git config --global credential.helper store
792
+ $ git ls-remote https://git.example.com/platform/api-recipes # asks once, then remembers
793
+
794
+ # or a token that git adds by itself to the addresses of that host
795
+ $ git config --global url."https://ci:${TOKEN}@git.example.com/".insteadOf "https://git.example.com/"
796
+
797
+ `recipes.lock` records the address as you wrote it, so no credential reaches
798
+ your repository, and `update` reads it from there: whoever runs `update` needs
799
+ the same access. No other protocol is accepted. The application needs none of
800
+ this when it runs: the installed recipes are files of your project.
801
+
802
+ ### What `configure` checks
803
+
804
+ Every file of `recipes_paths` must be called `NAME.recipe.yml` and must be in
805
+ the `recipes.lock` of its folder with the checksum it has now. A file that was
806
+ changed after it was installed, or that was never installed, raises
807
+ `ApiRecipes::ConfigurationError` and the previous configuration stays:
808
+
809
+ config/api_recipes/github.recipe.yml: was changed after it was installed: its checksum is not the one in recipes.lock. To accept the file as it is now, run `api_recipes install config/api_recipes/github.recipe.yml`
810
+
811
+ A recipe never replaces an API: a name that `apis_configs`, `apis_files_paths`
812
+ or another recipe already defines is an error. Everything else works as for any
813
+ API: credentials, variables, `with`, and `api :github` in a class. To add a
814
+ `verify_with`, which a recipe cannot carry, declare it in your code:
815
+
816
+ ```ruby
817
+ class GithubClient
818
+ include ApiRecipes
819
+ api :github, verify_with: :expected_response?
820
+ end
821
+ ```
822
+
823
+ `ApiRecipes.github.definition.recipe` returns the `recipe` key of the file:
824
+ its name, its version and the rest.
825
+
826
+ ### Writing a portable recipe
827
+
828
+ A portable recipe is an ordinary description of one API, with the key `recipe`:
829
+
830
+ ```yaml
831
+ billing:
832
+ recipe:
833
+ name: billing
834
+ version: 1.2.0 # of the recipe, not of the API
835
+ format: 1
836
+ summary: Billing API, invoices and customers
837
+ homepage: https://billing.example.com/docs
838
+ auth:
839
+ type: header
840
+ name: X-Api-Key
841
+ variables:
842
+ account:
843
+ description: the name of your account, before .billing.example.com
844
+ api_version:
845
+ default: '2026-10'
846
+ host: '{account}.billing.example.com'
847
+ base_url: 'api/{api_version}'
848
+ endpoints:
849
+ invoices:
850
+ endpoints:
851
+ show:
852
+ path: /:id
853
+ ```
854
+
855
+ The rules, beyond those of every description:
856
+
857
+ - One API per file. The name of the API, `recipe.name` and the name of the file
858
+ (`billing.recipe.yml`) are the same: lowercase letters, digits, `-` and `_`.
859
+ - `recipe.name` and `recipe.version` are required. The version is text made of
860
+ digits, letters and dots: write `'1.2'` between quotes. `format` is the
861
+ version of the format, today 1; `summary` and `homepage` are optional.
862
+ - `host` is required. A variable can be the whole host, or stand before fixed
863
+ text that starts with a dot: `{account}.billing.example.com` and
864
+ `s3.{region}.example.com` are accepted, `api.example.{tld}` is not.
865
+ - No `verify_with`, no ERB, and no `<%` anywhere in the file.
866
+ - Plain YAML: no anchors, aliases, tags or merge keys, no key given twice, keys
867
+ that are text (quote `'on'`, `'no'` and numbers).
868
+ - Every name and every value is visible text: no control characters, no line
869
+ breaks inside a value, no invisible or direction-changing characters. The
870
+ same holds for the rest of the file, comments included: what a person reads
871
+ in it is what is loaded.
872
+ - At most 1 MiB, 10,000 endpoints, those that `resource` adds among them, 2,000
873
+ characters for a name or a value, and 4,000 characters for the path of an
874
+ endpoint with the paths above it. The headers and parameters written on
875
+ endpoints are at most 100,000, counting for each endpoint that adds some
876
+ also those it takes from the endpoints above it.
877
+ - With `auth.type: query`, `default_params` cannot name the parameter of the
878
+ credential.
879
+ - A credential never goes in a recipe: `auth` says where it travels, and the
880
+ application assigns it.
881
+
882
+ A repository of recipes keeps each one in `recipes/NAME.recipe.yml`. A
883
+ published version is the tag `NAME-VERSION`, for example `billing-1.2.0`.
884
+ `api_recipes routes billing.recipe.yml` lists the calls of a recipe.
885
+
391
886
  ## Where an API is available
392
887
 
393
888
  ### On the `ApiRecipes` module
@@ -513,6 +1008,10 @@ They are the options of
513
1008
  | `headers:` | extra headers for this request only |
514
1009
  | `follow:` | follow redirects: `true`, or a Hash with `strict`, `max_hops` and `on_redirect` |
515
1010
 
1011
+ The [`default_params`](#headers-and-parameters-sent-with-every-request) of the
1012
+ API and of the endpoint come first in the query string; a parameter given here
1013
+ replaces the one with the same name.
1014
+
516
1015
  ```ruby
517
1016
  # GET /search/repositories?q=api%20language:ruby&per_page=5
518
1017
  ApiRecipes.github.search.repositories(params: { q: 'api language:ruby', per_page: 5 }).run
@@ -563,8 +1062,8 @@ ApiRecipes.github.with(host: 'github.example.com').users.show('octocat').url.to_
563
1062
  ```
564
1063
 
565
1064
  On an API, `with` takes `protocol`, `host`, `port`, `base_url`, `timeout`,
566
- `on_bad_code`, `mime_type`, `verify_with`, `default_headers`, `variables`,
567
- `authorization` and `basic_auth`. The values are checked like the ones in a file,
1065
+ `on_bad_code`, `mime_type`, `verify_with`, `default_headers`, `default_params`,
1066
+ `variables`, `authorization` and `basic_auth`. The values are checked like the ones in a file,
568
1067
  and an option that is not in this list raises `ApiRecipes::ConfigurationError`.
569
1068
 
570
1069
  On an endpoint, `with` takes `timeout`, `on_bad_code`, `ok_code`, `mime_type` and
@@ -575,7 +1074,8 @@ ApiRecipes.github.users.show('octocat').with(timeout: 10, ok_code: [200, 304]).r
575
1074
  ```
576
1075
 
577
1076
  Giving an option as `nil` drops the value given before it in the same chain.
578
- `default_headers` given to `with` are added to the ones already configured:
1077
+ `default_headers` and `default_params` given to `with` are added to the ones
1078
+ already configured:
579
1079
 
580
1080
  ```ruby
581
1081
  ApiRecipes.github.with(default_headers: { 'X-Request-Id' => 'abc123' }).users.run
@@ -721,8 +1221,8 @@ end
721
1221
  ```
722
1222
 
723
1223
  The method can be private. It is looked up on the caller, so `verify_with` is
724
- meant for APIs used from a class or an instance. A nested endpoint that has no
725
- `verify_with` of its own uses the one of its parent.
1224
+ meant for APIs used from a class or an instance. An endpoint that has no
1225
+ `verify_with` of its own looks for one up the tree, as for the other options.
726
1226
 
727
1227
  ## Handling errors
728
1228
 
@@ -771,30 +1271,40 @@ nearest to the farthest:
771
1271
 
772
1272
  1. the chain, with `with(authorization: ...)` or `with(basic_auth: ...)`
773
1273
  2. the object that makes the call
774
- 3. its class, then the classes and modules it inherits from
775
- 4. the API of the `ApiRecipes` module
1274
+ 3. the class or the module that declares the API
1275
+ 4. the whole application
1276
+
1277
+ The last two are shared by every thread of the process, so they are given where
1278
+ the application starts and cannot be assigned afterwards. A chain and an object
1279
+ are seen only by the code that holds them: they are the place for a credential
1280
+ that changes from one request to the next, as long as the object is not itself
1281
+ kept where every request finds it.
776
1282
 
777
- **A token**, sent as the `Authorization` header, as it is written:
1283
+ **A token for the whole application**, sent as the `Authorization` header, as
1284
+ it is written. It goes in `config.values`, with the variables of the API:
778
1285
 
779
1286
  ```ruby
780
- ApiRecipes.github.authorization = "Bearer #{ENV.fetch('GITHUB_TOKEN')}"
1287
+ ApiRecipes.configure do |config|
1288
+ config.values = { github: { authorization: "Bearer #{ENV.fetch('GITHUB_TOKEN', 'abc')}" } }
1289
+ end
781
1290
  ```
782
1291
 
783
1292
  **Basic auth:**
784
1293
 
785
1294
  ```ruby
786
- ApiRecipes.github.basic_auth = { user: 'me', pass: 'secret' }
1295
+ ApiRecipes.configure do |config|
1296
+ config.values = { github: { basic_auth: { user: 'me', pass: 'secret' } } }
1297
+ end
787
1298
  ```
788
1299
 
789
- **A different token for one class.** It applies to the class and its instances:
1300
+ **A different token for one class.** It applies to the class, to its subclasses
1301
+ and to their objects, and it is given where the class declares the API:
790
1302
 
791
1303
  ```ruby
792
1304
  class AdminClient
793
1305
  include ApiRecipes
794
- api :github
1306
+ api :github, authorization: "Bearer #{ENV.fetch('ADMIN_TOKEN', 'admin')}"
795
1307
  end
796
-
797
- AdminClient.github.authorization = "Bearer #{ENV.fetch('ADMIN_TOKEN')}"
798
1308
  ```
799
1309
 
800
1310
  **A different token for one object**, for example one object per user:
@@ -816,26 +1326,38 @@ end
816
1326
  ApiRecipes.github.with(authorization: "Bearer #{user.token}").users.repos('octocat').run
817
1327
  ```
818
1328
 
819
- The reader returns what the nearest level assigned:
1329
+ The reader returns what the nearest level gives:
820
1330
 
821
1331
  ```ruby
822
- ApiRecipes.github.authorization = 'Bearer abc'
1332
+ ApiRecipes.configure do |config|
1333
+ config.values = { github: { authorization: 'Bearer abc' } }
1334
+ end
823
1335
  ApiRecipes.github.authorization
824
1336
  # => "Bearer abc"
1337
+ ApiRecipes.github.with(authorization: 'Bearer of this chain').authorization
1338
+ # => "Bearer of this chain"
825
1339
  ```
826
1340
 
827
- **No credential.** Assigning `nil` means "send none" and ends the search: it
828
- does not fall back to a level farther away. The same goes for
829
- `with(authorization: nil)`:
1341
+ **No credential.** A credential given as `nil` means "send none" and ends the
1342
+ search: it does not fall back to a level farther away. It works at every level:
830
1343
 
831
1344
  ```ruby
832
- AdminClient.github.authorization = nil # this class sends none
833
- ApiRecipes.github.with(authorization: nil).users.run # this chain sends none
1345
+ ApiRecipes.github.with(authorization: nil).users.run # this chain sends none
1346
+
1347
+ class PublicClient
1348
+ include ApiRecipes
1349
+ api :github, authorization: nil # this class sends none
1350
+ end
834
1351
  ```
835
1352
 
836
- `authorization=`, `basic_auth=` and `variables=` change the level of the object
837
- they are called on. They raise on an API that `with` returned: use
838
- `with(authorization: ...)` there.
1353
+ `authorization=`, `basic_auth=` and `variables=` exist for objects only: they
1354
+ change the level of the object they are called on. On `ApiRecipes.github` and
1355
+ on a class they raise `ApiRecipes::Error`, and the message says where the value
1356
+ goes: a class that served two users at once would otherwise send the token of
1357
+ one with the request of the other. They also raise on an API that `with`
1358
+ returned: use `with(authorization: ...)` there. A token that expires and is
1359
+ renewed while the application runs goes to `with`, read from where you keep it
1360
+ at every call.
839
1361
 
840
1362
  A credential is valid only for the origin it was set for: the scheme, the host
841
1363
  and the port. If a nearer level changes the destination (its own `host`,
@@ -847,10 +1369,8 @@ destination:
847
1369
  ```ruby
848
1370
  class EnterpriseClient
849
1371
  include ApiRecipes
850
- api :github, host: 'github.example.com'
1372
+ api :github, host: 'github.example.com', authorization: "Bearer #{ENV.fetch('ENTERPRISE_TOKEN', 'enterprise')}"
851
1373
  end
852
-
853
- EnterpriseClient.github.authorization = "Bearer #{ENV.fetch('ENTERPRISE_TOKEN')}"
854
1374
  ```
855
1375
 
856
1376
  An API or an endpoint kept in a variable keeps the destination it was created
@@ -868,7 +1388,7 @@ restored from a serialized form has no credential.
868
1388
 
869
1389
  An API can say where its credential goes with `auth`:
870
1390
 
871
- | `auth.type` | The credential assigned with `authorization=` goes |
1391
+ | `auth.type` | The credential given as `authorization` goes |
872
1392
  | --- | --- |
873
1393
  | none of these, or `none`, `bearer`, `basic` | in the `Authorization` header, as it is written |
874
1394
  | `header`, with a `name` | in the header called `name` |
@@ -885,7 +1405,7 @@ weather:
885
1405
  ```
886
1406
 
887
1407
  Basic auth cannot be sent to an API of type `header` or `query`: the request
888
- raises `ApiRecipes::InvalidCredential`. Assign the value with `authorization=`.
1408
+ raises `ApiRecipes::InvalidCredential`. Give the value as `authorization`.
889
1409
 
890
1410
  A credential written in `headers:` or in `default_headers` is sent as it is.
891
1411
  ApiRecipes does not check its origin, so prefer the levels above.
@@ -950,14 +1470,20 @@ ApiRecipes.github.users.show('octocat').definition.full_path
950
1470
  ```
951
1471
 
952
1472
  For an API: `name`, `protocol`, `host`, `port`, `base_url`, `timeout`,
953
- `on_bad_code`, `mime_type`, `verify_with`, `default_headers`, `auth`,
954
- `variables`, `endpoints`, `routes` and `to_h`. For an endpoint: `name`,
955
- `full_name`, `path`, `full_path`, `path_params`, `verb`, `route?`, `ok_code`,
956
- `timeout`, `on_bad_code`, `mime_type`, `verify_with`, `parent`, `endpoints` and
957
- `to_h`. A definition never holds a credential assigned with `authorization=`,
958
- `basic_auth=` or `with`, nor the value of a variable. It does return the
959
- `default_headers` as they are configured, so do not write a credential there if
960
- you pass definitions around; `to_h` and `inspect` hide the values of the headers.
1473
+ `on_bad_code`, `mime_type`, `verify_with`, `default_headers`, `default_params`,
1474
+ `auth`, `variables`, `endpoints`, `routes` and `to_h`. For an endpoint: `name`,
1475
+ `full_name`, `path`, `full_path`, `path_params`, `path_variables`, `verb`,
1476
+ `route?`, `ok_code`, `timeout`, `on_bad_code`, `mime_type`, `verify_with`,
1477
+ `default_headers`, `default_params`, `parent`, `endpoints` and `to_h`. The
1478
+ `default_headers` and `default_params` of an endpoint are the ones written on
1479
+ it and on the endpoints above it, without those of the API. A path is shown as
1480
+ it is written, with its `:name` and `{name}`. The endpoints that `resource`
1481
+ adds are endpoints like the others.
1482
+ A definition never holds a credential, wherever it was given, nor the value of
1483
+ a variable. It does return the
1484
+ `default_headers` and the `default_params` as they are configured, so do not
1485
+ write a credential there if you pass definitions around; `to_h` and `inspect`
1486
+ hide their values.
961
1487
 
962
1488
  `routes` lists every endpoint that can be called, with the call that reaches it:
963
1489
 
@@ -967,6 +1493,19 @@ route = ApiRecipes.github.definition.routes.find { |r| r.call.end_with?('.contri
967
1493
  # => "github.repos(owner, repo).contributors get https://api.github.com/repos/:owner/:repo/contributors"
968
1494
  ```
969
1495
 
1496
+ The command line gives the same list for the recipes in one or more files,
1497
+ which is handy after an import:
1498
+
1499
+ ```
1500
+ $ api_recipes routes config/apis/github.yml
1501
+ github.users GET https://api.github.com/users
1502
+ github.users.show(username) GET https://api.github.com/users/:username
1503
+ github.repos.issues(owner, repo).create POST https://api.github.com/repos/:owner/:repo/issues
1504
+ ```
1505
+
1506
+ A file that contains ERB is read only with `--erb`, because that runs the code
1507
+ in it.
1508
+
970
1509
  ## Recipes
971
1510
 
972
1511
  ### Rails setup
@@ -976,11 +1515,13 @@ route = ApiRecipes.github.definition.routes.find { |r| r.call.end_with?('.contri
976
1515
  ApiRecipes.configure do |config|
977
1516
  config.logger = Rails.logger
978
1517
  config.apis_files_paths = Dir[Rails.root.join('config', 'apis', '*.yml').to_s]
1518
+ config.values = { github: { authorization: "Bearer #{Rails.application.credentials.github_token}" } }
979
1519
  end
980
-
981
- ApiRecipes.github.authorization = "Bearer #{Rails.application.credentials.github_token}"
982
1520
  ```
983
1521
 
1522
+ A value that depends on the user who is signed in does not go there: see
1523
+ [Variables](#variables) for the request of a controller.
1524
+
984
1525
  ### One host per environment
985
1526
 
986
1527
  Keep one file and let ERB pick the values:
@@ -1092,8 +1633,9 @@ end
1092
1633
  ```
1093
1634
 
1094
1635
  If your tests configure ApiRecipes themselves, `ApiRecipes.reset!` removes the
1095
- global configuration, the API methods defined on the module and every credential
1096
- and variable value that was assigned, so each example can start clean:
1636
+ global configuration, the API methods defined on the module, the values of the
1637
+ application and those assigned to objects, so each example can start clean.
1638
+ What a class gives to `api` is part of its code and stays:
1097
1639
 
1098
1640
  ```ruby
1099
1641
  RSpec.configure do |config|
@@ -1107,8 +1649,19 @@ What ApiRecipes does:
1107
1649
 
1108
1650
  - **Path parameters** are encoded one by one. A value cannot add a segment, a
1109
1651
  query string or a fragment. `nil`, an empty value, `.` and `..` are refused.
1110
- - **Variables** are checked for the place where they end up: a host, a path or a
1111
- header.
1652
+ - **Variables** are checked for the place where they end up: a host, a path, a
1653
+ header or a query parameter. In a path a value is one encoded segment, like a
1654
+ path parameter.
1655
+ - **Parameters sent with every request** cannot take the place of the
1656
+ credential: with `auth.type: query`, `default_params` that name its parameter
1657
+ are refused, and the credential is always the only parameter with its name.
1658
+ - **Shared values.** The credential and the variables that the whole
1659
+ application or a class uses are read by every thread. They are given only
1660
+ where the application starts, to `config.values` and to `api`: on
1661
+ `ApiRecipes.github` and on a class the writers raise, so code that serves
1662
+ several users cannot leave the token of one of them where the request of
1663
+ another finds it. A `configure` changes the descriptions and those values in
1664
+ one step.
1112
1665
  - **Credentials** are sent only to the origin they were set for, and a request
1113
1666
  that would send one elsewhere raises. A value with a control character is
1114
1667
  refused, so a credential cannot add a header.
@@ -1117,14 +1670,34 @@ What ApiRecipes does:
1117
1670
  not sent again if the chain comes back.
1118
1671
  - **Before sending**, the address that http.rb will contact is checked against
1119
1672
  the origin that was validated.
1673
+ - **`api_recipes import`** reads one local file, follows only the references
1674
+ inside it, never uses the network and runs nothing of the description. Text of
1675
+ the description cannot add keys to the recipe and cannot become ERB in it.
1676
+ - **Portable recipes** are data only. A file of `recipes_paths` is never
1677
+ evaluated with ERB and is read as plain YAML, without anchors, aliases or
1678
+ tags. It cannot carry `verify_with`, cannot take the name of an API you
1679
+ already have, and cannot let a variable decide where its host ends. Its
1680
+ text, comments included, holds only visible characters. It is loaded only if
1681
+ it matches the checksum in `recipes.lock`.
1682
+ - **`api_recipes install` and `update`** show where the requests and the
1683
+ credential go, the headers and the parameters sent with every request, those
1684
+ that an endpoint adds, and which calls are not GET, the ones that `resource`
1685
+ adds among them, before they write. They read one
1686
+ file from the repository without checking it out, only over `https` or `ssh`,
1687
+ and run `git` without a shell. An `https` address with a user name or a
1688
+ password and an `ssh` address with a password are refused, so no secret can
1689
+ end up in `recipes.lock`. The commands say which address they read before
1690
+ `git` connects to it: `update` takes it from `recipes.lock`, so read a change
1691
+ to that file as you read a change to the code.
1120
1692
  - **Logs, errors and `inspect`.** A log line and an error message of the gem
1121
1693
  never contain a credential, a header value, a body or a query string. Neither
1122
1694
  does the `inspect` or the `pp` of an API, an endpoint, a response, a
1123
1695
  definition or the configuration, nor their `to_s`, except that the `to_s` of
1124
1696
  a response is its body, as for the http.rb response. `definition` never holds
1125
- a credential that was assigned with `authorization=`, `basic_auth=` or `with`.
1126
- It does hold the values of `default_headers`, which its readers return; its
1127
- `inspect`, its `to_h` and its JSON show them as `[FILTERED]`.
1697
+ a credential, wherever it was given.
1698
+ It does hold the values of `default_headers` and `default_params`, which its
1699
+ readers return; its `inspect`, its `to_h` and its JSON show them as
1700
+ `[FILTERED]`.
1128
1701
  - **Serialization.** An API, an endpoint and a response refuse `Marshal.dump`,
1129
1702
  `YAML.dump`, `as_json` and `to_json`, and raise `TypeError`: serialize
1130
1703
  `response.data` or `response.body.to_s` instead. A definition refuses
@@ -1141,9 +1714,16 @@ What stays with your application:
1141
1714
  variable that is the entire host. Check it against a list of hosts you know. A
1142
1715
  variable that is only a part of a host, like the shop name in
1143
1716
  `{shop}.myshopify.com`, is the intended use.
1717
+ - An object that every request reaches, such as a client kept in a constant or
1718
+ memoized on a class, is as shared as a class: do not assign the credential or
1719
+ the variables of one user on it. Use `with`, or an object made for the request.
1144
1720
  - `response.original_response` is the raw http.rb response, and it gives access
1145
1721
  to the request that was sent, headers included. Do not log or serialize it.
1146
1722
  - ERB in a configuration file runs code. Load only files you trust.
1723
+ - Read what `api_recipes install` shows before you answer yes. ApiRecipes checks
1724
+ the form of a recipe, not whose host it names. Commit the installed recipes
1725
+ and `recipes.lock`, and review their changes as you review code: the lock
1726
+ makes a change visible, it does not stop someone who can change both files.
1147
1727
  - Do not put a credential in the source of an application or in a file of the
1148
1728
  repository: read it from the place where your application keeps secrets.
1149
1729
 
@@ -1158,7 +1738,8 @@ Every protection has an example in [`spec/security`](spec/security); its
1158
1738
  | --- | --- |
1159
1739
  | `apis_files_paths` | Array of YAML files, evaluated with ERB |
1160
1740
  | `apis_configs` | Hash of API descriptions |
1161
- | `variables` | Hash of default variable values, as `{ api_name: { variable: value } }` |
1741
+ | `recipes_paths` | Array of installed portable recipes (`NAME.recipe.yml`), read as data only and checked against `recipes.lock` |
1742
+ | `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` |
1162
1743
  | `logger` | a Logger to use |
1163
1744
  | `log_to` | path or IO for the default logger (default: STDERR) |
1164
1745
  | `log_level` | level of the default logger (default: the `LOG_LEVEL` variable, or warn) |
@@ -1175,6 +1756,7 @@ Every protection has an example in [`spec/security`](spec/security); its
1175
1756
  | `base_url` | none | prefix of every path, for example `api/v1`; can contain `{variables}` |
1176
1757
  | `timeout` | `3` | seconds allowed for the whole request |
1177
1758
  | `default_headers` | none | headers sent with every request; a value can contain `{variables}` |
1759
+ | `default_params` | none | query parameters sent with every request; a value is text, a number or a boolean, and can contain `{variables}` |
1178
1760
  | `on_bad_code` | `raise` | `raise` or `ignore` when the response is not a success |
1179
1761
  | `mime_type` | from the response | content type used by `response.data` |
1180
1762
  | `verify_with` | none | method that decides whether a response is a success |
@@ -1186,18 +1768,25 @@ Every protection has an example in [`spec/security`](spec/security); its
1186
1768
 
1187
1769
  | Option | Default | Meaning |
1188
1770
  | --- | --- | --- |
1189
- | `path` | the endpoint name | segment added to the parent path; `''` adds nothing |
1771
+ | `path` | the endpoint name | segment added to the parent path; `''` adds nothing; can contain `:name` and `*name` placeholders and `{variables}` |
1190
1772
  | `verb` | `get` | HTTP verb |
1191
1773
  | `route` | `yes` | `no` makes the endpoint a group that cannot be called |
1192
1774
  | `ok_code` | any 2xx | the status, or the list of statuses, accepted as a success |
1193
- | `timeout` | the API value | seconds allowed for the whole request |
1194
- | `on_bad_code` | the API value | `raise` or `ignore` |
1195
- | `mime_type` | the API value | content type used by `response.data` |
1196
- | `verify_with` | the API or parent value | method that decides whether a response is a success |
1775
+ | `timeout` | looked for up the tree | seconds allowed for the whole request |
1776
+ | `on_bad_code` | looked for up the tree | `raise` or `ignore` |
1777
+ | `mime_type` | looked for up the tree | content type used by `response.data` |
1778
+ | `verify_with` | looked for up the tree | method that decides whether a response is a success |
1779
+ | `default_headers` | none | headers added to those of the API, for this endpoint and the ones below it |
1780
+ | `default_params` | none | query parameters added to those of the API, for this endpoint and the ones below it |
1781
+ | `resource` | `no` | `yes` adds the endpoints `show`, `create`, `update`, `replace` and `delete`; a list adds only the ones it names |
1197
1782
  | `endpoints` | none | nested endpoints |
1198
1783
 
1199
- An endpoint takes `timeout`, `on_bad_code` and `mime_type` from the API, not from
1200
- the endpoint that contains it.
1784
+ "Looked for up the tree" means: in the endpoints that contain this one, from
1785
+ the nearest, then in the API; the first that sets the option gives the value,
1786
+ and the default of the API table applies when none does. `default_headers` and
1787
+ `default_params` are looked for in the same way, one name at a time. `ok_code`
1788
+ is read only on its endpoint: the status that means success belongs to one
1789
+ operation, as its verb does.
1201
1790
 
1202
1791
  ## Good to know
1203
1792