api_recipes 3.0.0.beta2 → 3.0.0.beta3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +515 -78
- data/UPGRADING.md +67 -22
- data/lib/api_recipes/api.rb +14 -10
- data/lib/api_recipes/cli/recipes/git.rb +197 -0
- data/lib/api_recipes/cli/recipes/lock_file.rb +34 -0
- data/lib/api_recipes/cli/recipes/summary.rb +226 -0
- data/lib/api_recipes/cli/recipes.rb +287 -0
- data/lib/api_recipes/cli/routes.rb +7 -2
- data/lib/api_recipes/cli/upgrade.rb +10 -2
- data/lib/api_recipes/cli.rb +67 -1
- data/lib/api_recipes/configuration.rb +50 -13
- data/lib/api_recipes/endpoint.rb +9 -3
- data/lib/api_recipes/errors.rb +2 -1
- data/lib/api_recipes/internals/compiler.rb +146 -33
- data/lib/api_recipes/internals/context.rb +52 -14
- data/lib/api_recipes/internals/declaration.rb +18 -18
- data/lib/api_recipes/internals/definition.rb +75 -22
- data/lib/api_recipes/internals/level.rb +54 -43
- data/lib/api_recipes/internals/path_template.rb +29 -4
- data/lib/api_recipes/internals/plain_yaml.rb +133 -0
- data/lib/api_recipes/internals/recipe.rb +297 -0
- data/lib/api_recipes/internals/recipe_lock.rb +115 -0
- data/lib/api_recipes/internals/registry.rb +61 -15
- data/lib/api_recipes/internals/request.rb +26 -9
- data/lib/api_recipes/internals/template.rb +17 -5
- data/lib/api_recipes/version.rb +1 -1
- data/lib/api_recipes.rb +7 -3
- metadata +8 -1
data/README.md
CHANGED
|
@@ -34,6 +34,7 @@ Coming from version 2? Read [UPGRADING.md](UPGRADING.md) first.
|
|
|
34
34
|
- [Variables](#variables)
|
|
35
35
|
- [Loading the configuration](#loading-the-configuration)
|
|
36
36
|
- [Importing an OpenAPI description](#importing-an-openapi-description)
|
|
37
|
+
- [Portable recipes](#portable-recipes)
|
|
37
38
|
- [Where an API is available](#where-an-api-is-available)
|
|
38
39
|
- [Making requests](#making-requests)
|
|
39
40
|
- [Options for one chain: `with`](#options-for-one-chain-with)
|
|
@@ -143,6 +144,16 @@ github:
|
|
|
143
144
|
repositories: # GET /search/repositories
|
|
144
145
|
```
|
|
145
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
|
+
|
|
146
157
|
The configuration is checked when it is loaded. A mistake, such as an unknown
|
|
147
158
|
option, a verb that does not exist or a negative timeout, raises
|
|
148
159
|
`ApiRecipes::ConfigurationError`. Its message lists every problem at once, so you
|
|
@@ -202,12 +213,57 @@ ApiRecipes.github.repos('rails', 'rails').contents('lib/rails.rb').url.to_s
|
|
|
202
213
|
A `*name` placeholder works only at the start of a segment, and a `:` that must
|
|
203
214
|
stay in the path is written `\:`.
|
|
204
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
|
+
|
|
205
220
|
### Endpoints that only group others
|
|
206
221
|
|
|
207
222
|
With `route: no` an endpoint exists only to hold nested endpoints. `search` above
|
|
208
223
|
is not callable, `search.repositories` is. Calling `run` on it raises
|
|
209
224
|
`ApiRecipes::NoRouteExists`.
|
|
210
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
|
+
|
|
211
267
|
### Settings for the whole API
|
|
212
268
|
|
|
213
269
|
```yaml
|
|
@@ -237,10 +293,60 @@ for a value that changes, use a [variable](#variables).
|
|
|
237
293
|
|
|
238
294
|
All options are listed in the [options reference](#options-reference).
|
|
239
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
|
+
|
|
240
345
|
## Variables
|
|
241
346
|
|
|
242
|
-
A variable is a `{name}` placeholder in the `host`, the `base_url
|
|
243
|
-
of
|
|
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
|
|
244
350
|
next, such as the address of a shop. Declare the variables of the API, with an
|
|
245
351
|
optional `default` and `description`:
|
|
246
352
|
|
|
@@ -251,6 +357,8 @@ shopify:
|
|
|
251
357
|
description: the part of the shop address before .myshopify.com
|
|
252
358
|
api_version:
|
|
253
359
|
default: '2026-10'
|
|
360
|
+
location:
|
|
361
|
+
description: the id of the location this client works for
|
|
254
362
|
host: '{shop}.myshopify.com'
|
|
255
363
|
base_url: admin/api/{api_version}
|
|
256
364
|
auth:
|
|
@@ -258,18 +366,31 @@ shopify:
|
|
|
258
366
|
name: X-Shopify-Access-Token
|
|
259
367
|
endpoints:
|
|
260
368
|
products:
|
|
369
|
+
inventory:
|
|
370
|
+
path: /locations/{location}/inventory_levels
|
|
261
371
|
```
|
|
262
372
|
|
|
263
373
|
A variable without a value and without a default raises
|
|
264
|
-
`ApiRecipes::MissingVariable`
|
|
265
|
-
|
|
266
|
-
|
|
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:
|
|
267
381
|
|
|
268
382
|
```ruby
|
|
269
383
|
ApiRecipes.shopify.with(variables: { shop: 'acme' }).products.url.to_s
|
|
270
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
|
|
271
393
|
|
|
272
|
-
ApiRecipes.shopify.variables = { shop: 'acme' }
|
|
273
394
|
ApiRecipes.shopify.products.url.to_s
|
|
274
395
|
# => "https://acme.myshopify.com/admin/api/2026-10/products"
|
|
275
396
|
|
|
@@ -277,6 +398,14 @@ ApiRecipes.shopify.with(variables: { api_version: '2027-01' }).products.url.to_s
|
|
|
277
398
|
# => "https://acme.myshopify.com/admin/api/2027-01/products"
|
|
278
399
|
```
|
|
279
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
|
+
|
|
280
409
|
For one class, or for the objects of a class:
|
|
281
410
|
|
|
282
411
|
```ruby
|
|
@@ -310,30 +439,51 @@ Sandbox.sandbox.with(variables: { tenant: 'acme' }).ping.url.to_s
|
|
|
310
439
|
# => "https://acme.sandbox.example.test/ping"
|
|
311
440
|
```
|
|
312
441
|
|
|
313
|
-
|
|
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:
|
|
314
454
|
|
|
315
455
|
```ruby
|
|
316
|
-
|
|
317
|
-
|
|
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
|
|
318
462
|
end
|
|
319
463
|
```
|
|
320
464
|
|
|
321
|
-
ApiRecipes
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
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`.
|
|
328
481
|
A name that the API does not declare raises `ApiRecipes::ConfigurationError` at
|
|
329
482
|
every level, so a misspelled name is never ignored.
|
|
330
|
-
`variables` without an assignment returns the values
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
a default declared in the file applies again.
|
|
334
|
-
The values given with `config.variables` are applied again at every `configure`,
|
|
335
|
-
so a value assigned directly with `ApiRecipes.shopify.variables = ...` under the
|
|
336
|
-
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.
|
|
337
487
|
|
|
338
488
|
## Loading the configuration
|
|
339
489
|
|
|
@@ -354,7 +504,8 @@ together: they are applied in the order they are assigned, so assigning
|
|
|
354
504
|
|
|
355
505
|
A file that is missing, empty, not valid YAML or not valid UTF-8 raises
|
|
356
506
|
`ApiRecipes::RecipeFileError`, which names the file. A file called
|
|
357
|
-
`something.recipe.yml` is refused:
|
|
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.
|
|
358
509
|
|
|
359
510
|
Files are evaluated with ERB before being parsed, so values can come from the
|
|
360
511
|
environment:
|
|
@@ -524,6 +675,214 @@ segment (`/a//b`), because the gem joins two slashes into one.
|
|
|
524
675
|
The command reads that one file, never uses the network and runs nothing of the
|
|
525
676
|
description.
|
|
526
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
|
+
|
|
527
886
|
## Where an API is available
|
|
528
887
|
|
|
529
888
|
### On the `ApiRecipes` module
|
|
@@ -649,6 +1008,10 @@ They are the options of
|
|
|
649
1008
|
| `headers:` | extra headers for this request only |
|
|
650
1009
|
| `follow:` | follow redirects: `true`, or a Hash with `strict`, `max_hops` and `on_redirect` |
|
|
651
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
|
+
|
|
652
1015
|
```ruby
|
|
653
1016
|
# GET /search/repositories?q=api%20language:ruby&per_page=5
|
|
654
1017
|
ApiRecipes.github.search.repositories(params: { q: 'api language:ruby', per_page: 5 }).run
|
|
@@ -699,8 +1062,8 @@ ApiRecipes.github.with(host: 'github.example.com').users.show('octocat').url.to_
|
|
|
699
1062
|
```
|
|
700
1063
|
|
|
701
1064
|
On an API, `with` takes `protocol`, `host`, `port`, `base_url`, `timeout`,
|
|
702
|
-
`on_bad_code`, `mime_type`, `verify_with`, `default_headers`, `
|
|
703
|
-
`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,
|
|
704
1067
|
and an option that is not in this list raises `ApiRecipes::ConfigurationError`.
|
|
705
1068
|
|
|
706
1069
|
On an endpoint, `with` takes `timeout`, `on_bad_code`, `ok_code`, `mime_type` and
|
|
@@ -711,7 +1074,8 @@ ApiRecipes.github.users.show('octocat').with(timeout: 10, ok_code: [200, 304]).r
|
|
|
711
1074
|
```
|
|
712
1075
|
|
|
713
1076
|
Giving an option as `nil` drops the value given before it in the same chain.
|
|
714
|
-
`default_headers` given to `with` are added to the ones
|
|
1077
|
+
`default_headers` and `default_params` given to `with` are added to the ones
|
|
1078
|
+
already configured:
|
|
715
1079
|
|
|
716
1080
|
```ruby
|
|
717
1081
|
ApiRecipes.github.with(default_headers: { 'X-Request-Id' => 'abc123' }).users.run
|
|
@@ -857,8 +1221,8 @@ end
|
|
|
857
1221
|
```
|
|
858
1222
|
|
|
859
1223
|
The method can be private. It is looked up on the caller, so `verify_with` is
|
|
860
|
-
meant for APIs used from a class or an instance.
|
|
861
|
-
`verify_with` of its own
|
|
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.
|
|
862
1226
|
|
|
863
1227
|
## Handling errors
|
|
864
1228
|
|
|
@@ -907,30 +1271,40 @@ nearest to the farthest:
|
|
|
907
1271
|
|
|
908
1272
|
1. the chain, with `with(authorization: ...)` or `with(basic_auth: ...)`
|
|
909
1273
|
2. the object that makes the call
|
|
910
|
-
3.
|
|
911
|
-
4. the
|
|
1274
|
+
3. the class or the module that declares the API
|
|
1275
|
+
4. the whole application
|
|
912
1276
|
|
|
913
|
-
|
|
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.
|
|
1282
|
+
|
|
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:
|
|
914
1285
|
|
|
915
1286
|
```ruby
|
|
916
|
-
ApiRecipes.
|
|
1287
|
+
ApiRecipes.configure do |config|
|
|
1288
|
+
config.values = { github: { authorization: "Bearer #{ENV.fetch('GITHUB_TOKEN', 'abc')}" } }
|
|
1289
|
+
end
|
|
917
1290
|
```
|
|
918
1291
|
|
|
919
1292
|
**Basic auth:**
|
|
920
1293
|
|
|
921
1294
|
```ruby
|
|
922
|
-
ApiRecipes.
|
|
1295
|
+
ApiRecipes.configure do |config|
|
|
1296
|
+
config.values = { github: { basic_auth: { user: 'me', pass: 'secret' } } }
|
|
1297
|
+
end
|
|
923
1298
|
```
|
|
924
1299
|
|
|
925
|
-
**A different token for one class.** It applies to the class
|
|
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:
|
|
926
1302
|
|
|
927
1303
|
```ruby
|
|
928
1304
|
class AdminClient
|
|
929
1305
|
include ApiRecipes
|
|
930
|
-
api :github
|
|
1306
|
+
api :github, authorization: "Bearer #{ENV.fetch('ADMIN_TOKEN', 'admin')}"
|
|
931
1307
|
end
|
|
932
|
-
|
|
933
|
-
AdminClient.github.authorization = "Bearer #{ENV.fetch('ADMIN_TOKEN')}"
|
|
934
1308
|
```
|
|
935
1309
|
|
|
936
1310
|
**A different token for one object**, for example one object per user:
|
|
@@ -952,26 +1326,38 @@ end
|
|
|
952
1326
|
ApiRecipes.github.with(authorization: "Bearer #{user.token}").users.repos('octocat').run
|
|
953
1327
|
```
|
|
954
1328
|
|
|
955
|
-
The reader returns what the nearest level
|
|
1329
|
+
The reader returns what the nearest level gives:
|
|
956
1330
|
|
|
957
1331
|
```ruby
|
|
958
|
-
ApiRecipes.
|
|
1332
|
+
ApiRecipes.configure do |config|
|
|
1333
|
+
config.values = { github: { authorization: 'Bearer abc' } }
|
|
1334
|
+
end
|
|
959
1335
|
ApiRecipes.github.authorization
|
|
960
1336
|
# => "Bearer abc"
|
|
1337
|
+
ApiRecipes.github.with(authorization: 'Bearer of this chain').authorization
|
|
1338
|
+
# => "Bearer of this chain"
|
|
961
1339
|
```
|
|
962
1340
|
|
|
963
|
-
**No credential.**
|
|
964
|
-
does not fall back to a level farther away.
|
|
965
|
-
`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:
|
|
966
1343
|
|
|
967
1344
|
```ruby
|
|
968
|
-
|
|
969
|
-
|
|
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
|
|
970
1351
|
```
|
|
971
1352
|
|
|
972
|
-
`authorization=`, `basic_auth=` and `variables=`
|
|
973
|
-
they are called on.
|
|
974
|
-
|
|
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.
|
|
975
1361
|
|
|
976
1362
|
A credential is valid only for the origin it was set for: the scheme, the host
|
|
977
1363
|
and the port. If a nearer level changes the destination (its own `host`,
|
|
@@ -983,10 +1369,8 @@ destination:
|
|
|
983
1369
|
```ruby
|
|
984
1370
|
class EnterpriseClient
|
|
985
1371
|
include ApiRecipes
|
|
986
|
-
api :github, host: 'github.example.com'
|
|
1372
|
+
api :github, host: 'github.example.com', authorization: "Bearer #{ENV.fetch('ENTERPRISE_TOKEN', 'enterprise')}"
|
|
987
1373
|
end
|
|
988
|
-
|
|
989
|
-
EnterpriseClient.github.authorization = "Bearer #{ENV.fetch('ENTERPRISE_TOKEN')}"
|
|
990
1374
|
```
|
|
991
1375
|
|
|
992
1376
|
An API or an endpoint kept in a variable keeps the destination it was created
|
|
@@ -1004,7 +1388,7 @@ restored from a serialized form has no credential.
|
|
|
1004
1388
|
|
|
1005
1389
|
An API can say where its credential goes with `auth`:
|
|
1006
1390
|
|
|
1007
|
-
| `auth.type` | The credential
|
|
1391
|
+
| `auth.type` | The credential given as `authorization` goes |
|
|
1008
1392
|
| --- | --- |
|
|
1009
1393
|
| none of these, or `none`, `bearer`, `basic` | in the `Authorization` header, as it is written |
|
|
1010
1394
|
| `header`, with a `name` | in the header called `name` |
|
|
@@ -1021,7 +1405,7 @@ weather:
|
|
|
1021
1405
|
```
|
|
1022
1406
|
|
|
1023
1407
|
Basic auth cannot be sent to an API of type `header` or `query`: the request
|
|
1024
|
-
raises `ApiRecipes::InvalidCredential`.
|
|
1408
|
+
raises `ApiRecipes::InvalidCredential`. Give the value as `authorization`.
|
|
1025
1409
|
|
|
1026
1410
|
A credential written in `headers:` or in `default_headers` is sent as it is.
|
|
1027
1411
|
ApiRecipes does not check its origin, so prefer the levels above.
|
|
@@ -1086,14 +1470,20 @@ ApiRecipes.github.users.show('octocat').definition.full_path
|
|
|
1086
1470
|
```
|
|
1087
1471
|
|
|
1088
1472
|
For an API: `name`, `protocol`, `host`, `port`, `base_url`, `timeout`,
|
|
1089
|
-
`on_bad_code`, `mime_type`, `verify_with`, `default_headers`, `
|
|
1090
|
-
`variables`, `endpoints`, `routes` and `to_h`. For an endpoint: `name`,
|
|
1091
|
-
`full_name`, `path`, `full_path`, `path_params`, `
|
|
1092
|
-
`timeout`, `on_bad_code`, `mime_type`, `verify_with`,
|
|
1093
|
-
`
|
|
1094
|
-
`
|
|
1095
|
-
|
|
1096
|
-
|
|
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.
|
|
1097
1487
|
|
|
1098
1488
|
`routes` lists every endpoint that can be called, with the call that reaches it:
|
|
1099
1489
|
|
|
@@ -1125,11 +1515,13 @@ in it.
|
|
|
1125
1515
|
ApiRecipes.configure do |config|
|
|
1126
1516
|
config.logger = Rails.logger
|
|
1127
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}" } }
|
|
1128
1519
|
end
|
|
1129
|
-
|
|
1130
|
-
ApiRecipes.github.authorization = "Bearer #{Rails.application.credentials.github_token}"
|
|
1131
1520
|
```
|
|
1132
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
|
+
|
|
1133
1525
|
### One host per environment
|
|
1134
1526
|
|
|
1135
1527
|
Keep one file and let ERB pick the values:
|
|
@@ -1241,8 +1633,9 @@ end
|
|
|
1241
1633
|
```
|
|
1242
1634
|
|
|
1243
1635
|
If your tests configure ApiRecipes themselves, `ApiRecipes.reset!` removes the
|
|
1244
|
-
global configuration, the API methods defined on the module
|
|
1245
|
-
and
|
|
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:
|
|
1246
1639
|
|
|
1247
1640
|
```ruby
|
|
1248
1641
|
RSpec.configure do |config|
|
|
@@ -1256,8 +1649,19 @@ What ApiRecipes does:
|
|
|
1256
1649
|
|
|
1257
1650
|
- **Path parameters** are encoded one by one. A value cannot add a segment, a
|
|
1258
1651
|
query string or a fragment. `nil`, an empty value, `.` and `..` are refused.
|
|
1259
|
-
- **Variables** are checked for the place where they end up: a host, a path
|
|
1260
|
-
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.
|
|
1261
1665
|
- **Credentials** are sent only to the origin they were set for, and a request
|
|
1262
1666
|
that would send one elsewhere raises. A value with a control character is
|
|
1263
1667
|
refused, so a credential cannot add a header.
|
|
@@ -1269,14 +1673,31 @@ What ApiRecipes does:
|
|
|
1269
1673
|
- **`api_recipes import`** reads one local file, follows only the references
|
|
1270
1674
|
inside it, never uses the network and runs nothing of the description. Text of
|
|
1271
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.
|
|
1272
1692
|
- **Logs, errors and `inspect`.** A log line and an error message of the gem
|
|
1273
1693
|
never contain a credential, a header value, a body or a query string. Neither
|
|
1274
1694
|
does the `inspect` or the `pp` of an API, an endpoint, a response, a
|
|
1275
1695
|
definition or the configuration, nor their `to_s`, except that the `to_s` of
|
|
1276
1696
|
a response is its body, as for the http.rb response. `definition` never holds
|
|
1277
|
-
a credential
|
|
1278
|
-
It does hold the values of `default_headers`, which its
|
|
1279
|
-
`inspect`, its `to_h` and its JSON show them as
|
|
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]`.
|
|
1280
1701
|
- **Serialization.** An API, an endpoint and a response refuse `Marshal.dump`,
|
|
1281
1702
|
`YAML.dump`, `as_json` and `to_json`, and raise `TypeError`: serialize
|
|
1282
1703
|
`response.data` or `response.body.to_s` instead. A definition refuses
|
|
@@ -1293,9 +1714,16 @@ What stays with your application:
|
|
|
1293
1714
|
variable that is the entire host. Check it against a list of hosts you know. A
|
|
1294
1715
|
variable that is only a part of a host, like the shop name in
|
|
1295
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.
|
|
1296
1720
|
- `response.original_response` is the raw http.rb response, and it gives access
|
|
1297
1721
|
to the request that was sent, headers included. Do not log or serialize it.
|
|
1298
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.
|
|
1299
1727
|
- Do not put a credential in the source of an application or in a file of the
|
|
1300
1728
|
repository: read it from the place where your application keeps secrets.
|
|
1301
1729
|
|
|
@@ -1310,7 +1738,8 @@ Every protection has an example in [`spec/security`](spec/security); its
|
|
|
1310
1738
|
| --- | --- |
|
|
1311
1739
|
| `apis_files_paths` | Array of YAML files, evaluated with ERB |
|
|
1312
1740
|
| `apis_configs` | Hash of API descriptions |
|
|
1313
|
-
| `
|
|
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` |
|
|
1314
1743
|
| `logger` | a Logger to use |
|
|
1315
1744
|
| `log_to` | path or IO for the default logger (default: STDERR) |
|
|
1316
1745
|
| `log_level` | level of the default logger (default: the `LOG_LEVEL` variable, or warn) |
|
|
@@ -1327,6 +1756,7 @@ Every protection has an example in [`spec/security`](spec/security); its
|
|
|
1327
1756
|
| `base_url` | none | prefix of every path, for example `api/v1`; can contain `{variables}` |
|
|
1328
1757
|
| `timeout` | `3` | seconds allowed for the whole request |
|
|
1329
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}` |
|
|
1330
1760
|
| `on_bad_code` | `raise` | `raise` or `ignore` when the response is not a success |
|
|
1331
1761
|
| `mime_type` | from the response | content type used by `response.data` |
|
|
1332
1762
|
| `verify_with` | none | method that decides whether a response is a success |
|
|
@@ -1338,18 +1768,25 @@ Every protection has an example in [`spec/security`](spec/security); its
|
|
|
1338
1768
|
|
|
1339
1769
|
| Option | Default | Meaning |
|
|
1340
1770
|
| --- | --- | --- |
|
|
1341
|
-
| `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}` |
|
|
1342
1772
|
| `verb` | `get` | HTTP verb |
|
|
1343
1773
|
| `route` | `yes` | `no` makes the endpoint a group that cannot be called |
|
|
1344
1774
|
| `ok_code` | any 2xx | the status, or the list of statuses, accepted as a success |
|
|
1345
|
-
| `timeout` | the
|
|
1346
|
-
| `on_bad_code` | the
|
|
1347
|
-
| `mime_type` | the
|
|
1348
|
-
| `verify_with` |
|
|
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 |
|
|
1349
1782
|
| `endpoints` | none | nested endpoints |
|
|
1350
1783
|
|
|
1351
|
-
|
|
1352
|
-
the
|
|
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.
|
|
1353
1790
|
|
|
1354
1791
|
## Good to know
|
|
1355
1792
|
|