api_recipes 3.0.0.beta2 → 3.0.0.beta4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +704 -81
- data/UPGRADING.md +149 -46
- data/lib/api_recipes/api.rb +14 -10
- data/lib/api_recipes/cli/code.rb +153 -0
- data/lib/api_recipes/cli/convert/migration.rb +624 -0
- data/lib/api_recipes/cli/convert/snippet.rb +67 -0
- data/lib/api_recipes/cli/convert.rb +533 -0
- data/lib/api_recipes/cli/recipes/git.rb +197 -0
- data/lib/api_recipes/cli/recipes/lock_file.rb +34 -0
- data/lib/api_recipes/cli/recipes/summary.rb +245 -0
- data/lib/api_recipes/cli/recipes.rb +287 -0
- data/lib/api_recipes/cli/routes.rb +8 -3
- data/lib/api_recipes/cli/upgrade/project.rb +194 -0
- data/lib/api_recipes/cli/upgrade.rb +361 -24
- data/lib/api_recipes/cli.rb +161 -8
- data/lib/api_recipes/configuration.rb +68 -18
- data/lib/api_recipes/endpoint.rb +10 -4
- data/lib/api_recipes/errors.rb +2 -1
- data/lib/api_recipes/internals/compiler.rb +174 -33
- data/lib/api_recipes/internals/context.rb +87 -20
- data/lib/api_recipes/internals/declaration.rb +18 -18
- data/lib/api_recipes/internals/definition.rb +76 -23
- data/lib/api_recipes/internals/level.rb +54 -43
- data/lib/api_recipes/internals/option_value.rb +92 -0
- data/lib/api_recipes/internals/path_template.rb +29 -4
- data/lib/api_recipes/internals/plain_yaml.rb +133 -0
- data/lib/api_recipes/internals/recipe.rb +297 -0
- data/lib/api_recipes/internals/recipe_lock.rb +115 -0
- data/lib/api_recipes/internals/registry.rb +61 -15
- data/lib/api_recipes/internals/request.rb +42 -14
- data/lib/api_recipes/internals/template.rb +17 -5
- data/lib/api_recipes/version.rb +1 -1
- data/lib/api_recipes.rb +8 -3
- metadata +14 -1
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,62 @@ 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, the value of a header or of a parameter sent with every
|
|
349
|
+
request, or in one of the options that are read when a request is sent:
|
|
350
|
+
`protocol`, `port`, `timeout`, `on_bad_code`, `mime_type`, `verb` and
|
|
351
|
+
`ok_code`. Use it for what changes from one account or one environment to the
|
|
244
352
|
next, such as the address of a shop. Declare the variables of the API, with an
|
|
245
353
|
optional `default` and `description`:
|
|
246
354
|
|
|
@@ -251,6 +359,8 @@ shopify:
|
|
|
251
359
|
description: the part of the shop address before .myshopify.com
|
|
252
360
|
api_version:
|
|
253
361
|
default: '2026-10'
|
|
362
|
+
location:
|
|
363
|
+
description: the id of the location this client works for
|
|
254
364
|
host: '{shop}.myshopify.com'
|
|
255
365
|
base_url: admin/api/{api_version}
|
|
256
366
|
auth:
|
|
@@ -258,18 +368,31 @@ shopify:
|
|
|
258
368
|
name: X-Shopify-Access-Token
|
|
259
369
|
endpoints:
|
|
260
370
|
products:
|
|
371
|
+
inventory:
|
|
372
|
+
path: /locations/{location}/inventory_levels
|
|
261
373
|
```
|
|
262
374
|
|
|
263
375
|
A variable without a value and without a default raises
|
|
264
|
-
`ApiRecipes::MissingVariable`
|
|
265
|
-
|
|
266
|
-
|
|
376
|
+
`ApiRecipes::MissingVariable` as soon as it is needed: when the chain reaches
|
|
377
|
+
the `base_url` or the path that uses it, and at the latest when the request is
|
|
378
|
+
built. Give it a value in the way that fits your code. The nearest one wins: the chain (`with`), then the
|
|
379
|
+
object, then the class that declares the API, then the whole application, then
|
|
380
|
+
the default of the file.
|
|
381
|
+
|
|
382
|
+
For one chain:
|
|
267
383
|
|
|
268
384
|
```ruby
|
|
269
385
|
ApiRecipes.shopify.with(variables: { shop: 'acme' }).products.url.to_s
|
|
270
386
|
# => "https://acme.myshopify.com/admin/api/2026-10/products"
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
For the whole application, when it starts, with `config.values`:
|
|
390
|
+
|
|
391
|
+
```ruby
|
|
392
|
+
ApiRecipes.configure do |config|
|
|
393
|
+
config.values = { shopify: { variables: { shop: 'acme' } } }
|
|
394
|
+
end
|
|
271
395
|
|
|
272
|
-
ApiRecipes.shopify.variables = { shop: 'acme' }
|
|
273
396
|
ApiRecipes.shopify.products.url.to_s
|
|
274
397
|
# => "https://acme.myshopify.com/admin/api/2026-10/products"
|
|
275
398
|
|
|
@@ -277,6 +400,14 @@ ApiRecipes.shopify.with(variables: { api_version: '2027-01' }).products.url.to_s
|
|
|
277
400
|
# => "https://acme.myshopify.com/admin/api/2027-01/products"
|
|
278
401
|
```
|
|
279
402
|
|
|
403
|
+
In a path, a `:name` is an argument of the method and a `{name}` is a variable.
|
|
404
|
+
The location is set once, and no call has to repeat it:
|
|
405
|
+
|
|
406
|
+
```ruby
|
|
407
|
+
ApiRecipes.shopify.with(variables: { shop: 'acme', location: 7 }).inventory.url.to_s
|
|
408
|
+
# => "https://acme.myshopify.com/admin/api/2026-10/locations/7/inventory_levels"
|
|
409
|
+
```
|
|
410
|
+
|
|
280
411
|
For one class, or for the objects of a class:
|
|
281
412
|
|
|
282
413
|
```ruby
|
|
@@ -310,30 +441,93 @@ Sandbox.sandbox.with(variables: { tenant: 'acme' }).ping.url.to_s
|
|
|
310
441
|
# => "https://acme.sandbox.example.test/ping"
|
|
311
442
|
```
|
|
312
443
|
|
|
313
|
-
|
|
444
|
+
`config.values` holds, for each API, its `variables` and its credential
|
|
445
|
+
(`authorization` or `basic_auth`, see [Authorization](#authorization)). A later
|
|
446
|
+
`configure` that assigns `config.values` replaces them all; one that does not
|
|
447
|
+
keeps them.
|
|
448
|
+
|
|
449
|
+
ApiRecipes never reads the environment for you: if the shop comes from an
|
|
450
|
+
environment variable, read it in your code and pass it in.
|
|
451
|
+
|
|
452
|
+
When the value changes with each request, as in a server where it depends on
|
|
453
|
+
the user who is signed in, give it to `with` or to an object that lives as long
|
|
454
|
+
as the request. Both belong to the code that created them, so two requests
|
|
455
|
+
served at the same time, in two threads, never see the value of each other:
|
|
314
456
|
|
|
315
457
|
```ruby
|
|
316
|
-
|
|
317
|
-
|
|
458
|
+
class OrdersController < ApplicationController
|
|
459
|
+
def index
|
|
460
|
+
shop = ApiRecipes.shopify.with(variables: { shop: current_user.shop, location: current_user.location_id },
|
|
461
|
+
authorization: current_user.shopify_token)
|
|
462
|
+
render json: shop.inventory.run.data
|
|
463
|
+
end
|
|
318
464
|
end
|
|
319
465
|
```
|
|
320
466
|
|
|
321
|
-
ApiRecipes
|
|
322
|
-
|
|
467
|
+
What `ApiRecipes.shopify` and a class use is shared by the whole process, in
|
|
468
|
+
every thread. That is why it is given only where the application starts, to
|
|
469
|
+
`config.values` and to `api`, and why `ApiRecipes.shopify.variables = ...` and
|
|
470
|
+
`Store.shopify.variables = ...` raise `ApiRecipes::Error`: a value that changes
|
|
471
|
+
while requests are served goes to `with` or to an object. The same holds for
|
|
472
|
+
credentials.
|
|
473
|
+
|
|
474
|
+
A service whose address changes with the environment is described once, and
|
|
475
|
+
each environment gives its values:
|
|
323
476
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
477
|
+
```yaml
|
|
478
|
+
billing:
|
|
479
|
+
variables:
|
|
480
|
+
protocol:
|
|
481
|
+
default: https
|
|
482
|
+
host:
|
|
483
|
+
default: billing.example.com
|
|
484
|
+
port:
|
|
485
|
+
default: 443
|
|
486
|
+
timeout:
|
|
487
|
+
default: 5
|
|
488
|
+
protocol: '{protocol}'
|
|
489
|
+
host: '{host}'
|
|
490
|
+
port: '{port}'
|
|
491
|
+
timeout: '{timeout}'
|
|
492
|
+
endpoints:
|
|
493
|
+
invoices:
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
```ruby
|
|
497
|
+
ApiRecipes.configure do |config|
|
|
498
|
+
config.values = { billing: { variables: { protocol: 'http', host: 'localhost', port: 3000 } } }
|
|
499
|
+
end
|
|
500
|
+
|
|
501
|
+
ApiRecipes.billing.invoices.url.to_s
|
|
502
|
+
# => "http://localhost:3000/invoices"
|
|
503
|
+
```
|
|
504
|
+
|
|
505
|
+
In these options the variable gives a value of the option: `http` or `https`
|
|
506
|
+
for the protocol, an integer from 1 to 65535 for the port, a positive number
|
|
507
|
+
for the timeout, `raise` or `ignore`, a verb, a status. It is made when the
|
|
508
|
+
request is built, and a value that is not one raises
|
|
509
|
+
`ApiRecipes::InvalidVariable` before anything is sent, as the default of a
|
|
510
|
+
variable that gives the whole option raises `ApiRecipes::ConfigurationError`
|
|
511
|
+
when the configuration is loaded. `definition`, `routes` and the `api_recipes routes` command show such
|
|
512
|
+
an option as it is written. What decides the shape of the tree is read when
|
|
513
|
+
the configuration is loaded and takes no variable: `endpoints`, `resource`,
|
|
514
|
+
`route`, the names, `verify_with` and `auth`.
|
|
515
|
+
|
|
516
|
+
The value of a variable is a String, a Symbol or a number, and is kept as
|
|
517
|
+
text. A value is checked for the place where it ends up. In a `host` it can contain
|
|
518
|
+
only letters, digits, dots, hyphens and underscores. In a `base_url` and in a
|
|
519
|
+
`path` it is encoded like a path parameter, and it cannot be empty, `.` or
|
|
520
|
+
`..`. In a header it cannot contain a line break or another control character.
|
|
521
|
+
In a parameter it is encoded like every value of a query string. Anything else
|
|
522
|
+
raises `ApiRecipes::InvalidVariable`.
|
|
523
|
+
A word in braces that is not a variable is written `\{word}` in a path and in
|
|
524
|
+
a `base_url`.
|
|
328
525
|
A name that the API does not declare raises `ApiRecipes::ConfigurationError` at
|
|
329
526
|
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`.
|
|
527
|
+
`variables` without an assignment returns the values of the level you call it
|
|
528
|
+
on: those of `config.values` on `ApiRecipes.shopify`, those given to `api` on a
|
|
529
|
+
class, those assigned on an object. On an object, assigning `nil` clears them,
|
|
530
|
+
and a default declared in the file applies again.
|
|
337
531
|
|
|
338
532
|
## Loading the configuration
|
|
339
533
|
|
|
@@ -354,7 +548,11 @@ together: they are applied in the order they are assigned, so assigning
|
|
|
354
548
|
|
|
355
549
|
A file that is missing, empty, not valid YAML or not valid UTF-8 raises
|
|
356
550
|
`ApiRecipes::RecipeFileError`, which names the file. A file called
|
|
357
|
-
`something.recipe.yml` is
|
|
551
|
+
`something.recipe.yml` is a [portable recipe](#portable-recipes) and goes in
|
|
552
|
+
`recipes_paths`, where it is never evaluated with ERB: here it is refused,
|
|
553
|
+
unless `recipes_paths` has it too, in which case it is left to `recipes_paths`.
|
|
554
|
+
So one pattern, such as `config/apis/*.yml*`, can match both kinds of file in
|
|
555
|
+
one folder.
|
|
358
556
|
|
|
359
557
|
Files are evaluated with ERB before being parsed, so values can come from the
|
|
360
558
|
environment:
|
|
@@ -524,6 +722,353 @@ segment (`/a//b`), because the gem joins two slashes into one.
|
|
|
524
722
|
The command reads that one file, never uses the network and runs nothing of the
|
|
525
723
|
description.
|
|
526
724
|
|
|
725
|
+
## Portable recipes
|
|
726
|
+
|
|
727
|
+
A portable recipe is a recipe made to be shared: one API in one file, and
|
|
728
|
+
nothing but data. You install it with a command that shows what it does before
|
|
729
|
+
it writes anything, and you load it with `recipes_paths`.
|
|
730
|
+
|
|
731
|
+
$ api_recipes install github
|
|
732
|
+
github 1.0.0
|
|
733
|
+
summary: GitHub REST API, users, repositories, issues and pull requests
|
|
734
|
+
homepage: https://docs.github.com/en/rest
|
|
735
|
+
from: https://github.com/madAle/api_recipes-cookbook, commit a4e8dd017d05
|
|
736
|
+
address: https://api.github.com
|
|
737
|
+
credential: in the Authorization header
|
|
738
|
+
headers: Accept: application/vnd.github+json
|
|
739
|
+
calls: 14, of which 2 are not GET:
|
|
740
|
+
POST github.repos.issues(owner, repo).create /repos/:owner/:repo/issues
|
|
741
|
+
POST github.repos.issues(owner, repo).comments(issue_number).create /repos/:owner/:repo/issues/:issue_number/comments
|
|
742
|
+
Install github 1.0.0 in config/api_recipes? [y/N] y
|
|
743
|
+
github 1.0.0 is installed in config/api_recipes/github.recipe.yml
|
|
744
|
+
|
|
745
|
+
```ruby
|
|
746
|
+
ApiRecipes.configure do |config|
|
|
747
|
+
config.recipes_paths = Dir['config/api_recipes/*.recipe.yml']
|
|
748
|
+
end
|
|
749
|
+
|
|
750
|
+
ApiRecipes.github.users.show('octocat').run
|
|
751
|
+
```
|
|
752
|
+
|
|
753
|
+
The command writes two files in `config/api_recipes`: the recipe,
|
|
754
|
+
`github.recipe.yml`, and `recipes.lock`, which records the version, the
|
|
755
|
+
repository, the commit and the checksum of each installed recipe. Commit both
|
|
756
|
+
to your repository: the application downloads nothing when it starts.
|
|
757
|
+
|
|
758
|
+
Recipes come from the
|
|
759
|
+
[cookbook](https://github.com/madAle/api_recipes-cookbook), a Git repository
|
|
760
|
+
that takes contributions by pull request, or from any repository with the same
|
|
761
|
+
layout.
|
|
762
|
+
|
|
763
|
+
### Installing, updating and checking
|
|
764
|
+
|
|
765
|
+
$ api_recipes install github # the recipe at the head of the cookbook
|
|
766
|
+
$ api_recipes install github --version 1.0.0 # the one at the tag github-1.0.0
|
|
767
|
+
$ api_recipes install billing --from https://github.com/acme/api-recipes
|
|
768
|
+
$ api_recipes install ./billing.recipe.yml # a file of yours
|
|
769
|
+
$ api_recipes update github
|
|
770
|
+
$ api_recipes check
|
|
771
|
+
$ api_recipes check recipes/billing.recipe.yml
|
|
772
|
+
|
|
773
|
+
- `install NAME` reads `recipes/NAME.recipe.yml` from the repository. `--from`
|
|
774
|
+
names another repository: an `https://` address without a user name or a
|
|
775
|
+
password, or an `ssh` address. The `git` command of your machine does the
|
|
776
|
+
download, and the command says which address it reads before `git` connects.
|
|
777
|
+
For a repository of your company, see [private repositories](#private-repositories).
|
|
778
|
+
- Before writing, `install` shows the address the requests go to, as the recipe
|
|
779
|
+
writes it and with the defaults of its variables, where the credential goes,
|
|
780
|
+
the headers and the parameters sent with every request, with the endpoint
|
|
781
|
+
for those that only an endpoint adds, the variables to set and every call
|
|
782
|
+
that is not a GET. The address is shown whole. A name or a value longer than
|
|
783
|
+
100 characters is cut and followed by its length, so you know to read it in
|
|
784
|
+
the file. It warns when the credential would travel in the query string,
|
|
785
|
+
and when the protocol is `http`. Only `y` or `yes` installs; `--yes` installs
|
|
786
|
+
without asking.
|
|
787
|
+
- `--to DIR` installs in another folder. `update` and `check` take it as
|
|
788
|
+
`--dir DIR`.
|
|
789
|
+
- `update NAME` takes the recipe that its repository has now, or the one of
|
|
790
|
+
`--version`, and shows what changes in the address, the credential, the
|
|
791
|
+
headers, the parameters and the verbs before it replaces the file. A header
|
|
792
|
+
or a parameter that changes is shown whole, before and after. It reads the
|
|
793
|
+
repository from `recipes.lock`, so it needs no `--from`.
|
|
794
|
+
- `check` compares the installed recipes with `recipes.lock`, as `configure`
|
|
795
|
+
does. `check FILE` says whether a file is a valid portable recipe: use it
|
|
796
|
+
while you write one.
|
|
797
|
+
- `install FILE` installs a file of yours. It is also how you accept a change
|
|
798
|
+
you made by hand to an installed recipe: `api_recipes install
|
|
799
|
+
config/api_recipes/github.recipe.yml` writes its new checksum in the lock.
|
|
800
|
+
From then on the lock no longer names a repository for it, and `update`
|
|
801
|
+
refuses it.
|
|
802
|
+
|
|
803
|
+
Exit codes: 0 when the work is done or there was nothing to do; 1 when a recipe
|
|
804
|
+
is not valid, is not found, does not match the lock or the answer is no; 2 when
|
|
805
|
+
the arguments are wrong, `git` fails or a file cannot be read or written.
|
|
806
|
+
|
|
807
|
+
### Private repositories
|
|
808
|
+
|
|
809
|
+
A company can keep its recipes in a repository of its own, private, on any Git
|
|
810
|
+
server that speaks `https` or `ssh`: GitHub, GitLab, Bitbucket, Gitea or one
|
|
811
|
+
you host. The repository has the layout of the cookbook, a folder `recipes`
|
|
812
|
+
with one `NAME.recipe.yml` per recipe, and `--from` names it:
|
|
813
|
+
|
|
814
|
+
$ api_recipes install billing --from https://git.example.com/platform/api-recipes
|
|
815
|
+
$ api_recipes install billing --from git@git.example.com:platform/api-recipes.git
|
|
816
|
+
$ api_recipes update billing
|
|
817
|
+
|
|
818
|
+
**Over `ssh`** the address is written as `user@host:path` or as
|
|
819
|
+
`ssh://user@host:port/path`, and `ssh` works as it does for `git clone`: with
|
|
820
|
+
your keys, your agent and your `~/.ssh/config`. A deploy key that can read the
|
|
821
|
+
repository is enough, also in CI. When `ssh` has to ask something, such as the
|
|
822
|
+
passphrase of a key or whether to trust a host it meets for the first time, it
|
|
823
|
+
asks on the terminal; without a terminal it fails, and the message says what
|
|
824
|
+
is missing. When `ssh` warns that the key of a host has changed, the command
|
|
825
|
+
says so and does not suggest to trust it. An address with a password is
|
|
826
|
+
refused. The user of the address is written in `recipes.lock` with it: use the
|
|
827
|
+
one everybody shares, such as `git`, or leave it out of an `ssh://` address and
|
|
828
|
+
let the `ssh` configuration of each person choose.
|
|
829
|
+
|
|
830
|
+
**Over `https`** `git` uses the user name and the password it already has for
|
|
831
|
+
that host. When it has none and the command runs on a terminal, `git` asks for
|
|
832
|
+
them, as it does for a clone: they go from the terminal to `git`, and the
|
|
833
|
+
command never sees them. Without a terminal, as in CI, nothing is asked and the
|
|
834
|
+
command fails with a message that says so. They are never taken in the
|
|
835
|
+
address. Two ways to give them to `git` once, on a laptop and in CI:
|
|
836
|
+
|
|
837
|
+
# a credential helper: the one of your system, or a file
|
|
838
|
+
$ git config --global credential.helper store
|
|
839
|
+
$ git ls-remote https://git.example.com/platform/api-recipes # asks once, then remembers
|
|
840
|
+
|
|
841
|
+
# or a token that git adds by itself to the addresses of that host
|
|
842
|
+
$ git config --global url."https://ci:${TOKEN}@git.example.com/".insteadOf "https://git.example.com/"
|
|
843
|
+
|
|
844
|
+
`recipes.lock` records the address as you wrote it, so no credential reaches
|
|
845
|
+
your repository, and `update` reads it from there: whoever runs `update` needs
|
|
846
|
+
the same access. No other protocol is accepted. The application needs none of
|
|
847
|
+
this when it runs: the installed recipes are files of your project.
|
|
848
|
+
|
|
849
|
+
### What `configure` checks
|
|
850
|
+
|
|
851
|
+
Every file of `recipes_paths` must be called `NAME.recipe.yml` and must be in
|
|
852
|
+
the `recipes.lock` of its folder with the checksum it has now. A file that was
|
|
853
|
+
changed after it was installed, or that was never installed, raises
|
|
854
|
+
`ApiRecipes::ConfigurationError` and the previous configuration stays:
|
|
855
|
+
|
|
856
|
+
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`
|
|
857
|
+
|
|
858
|
+
A recipe never replaces an API: a name that `apis_configs`, `apis_files_paths`
|
|
859
|
+
or another recipe already defines is an error. Everything else works as for any
|
|
860
|
+
API: credentials, variables, `with`, and `api :github` in a class. To add a
|
|
861
|
+
`verify_with`, which a recipe cannot carry, declare it in your code:
|
|
862
|
+
|
|
863
|
+
```ruby
|
|
864
|
+
class GithubClient
|
|
865
|
+
include ApiRecipes
|
|
866
|
+
api :github, verify_with: :expected_response?
|
|
867
|
+
end
|
|
868
|
+
```
|
|
869
|
+
|
|
870
|
+
`ApiRecipes.github.definition.recipe` returns the `recipe` key of the file:
|
|
871
|
+
its name, its version and the rest.
|
|
872
|
+
|
|
873
|
+
### Writing a portable recipe
|
|
874
|
+
|
|
875
|
+
A portable recipe is an ordinary description of one API, with the key `recipe`:
|
|
876
|
+
|
|
877
|
+
```yaml
|
|
878
|
+
billing:
|
|
879
|
+
recipe:
|
|
880
|
+
name: billing
|
|
881
|
+
version: 1.2.0 # of the recipe, not of the API
|
|
882
|
+
format: 1
|
|
883
|
+
summary: Billing API, invoices and customers
|
|
884
|
+
homepage: https://billing.example.com/docs
|
|
885
|
+
auth:
|
|
886
|
+
type: header
|
|
887
|
+
name: X-Api-Key
|
|
888
|
+
variables:
|
|
889
|
+
account:
|
|
890
|
+
description: the name of your account, before .billing.example.com
|
|
891
|
+
api_version:
|
|
892
|
+
default: '2026-10'
|
|
893
|
+
host: '{account}.billing.example.com'
|
|
894
|
+
base_url: 'api/{api_version}'
|
|
895
|
+
endpoints:
|
|
896
|
+
invoices:
|
|
897
|
+
endpoints:
|
|
898
|
+
show:
|
|
899
|
+
path: /:id
|
|
900
|
+
```
|
|
901
|
+
|
|
902
|
+
The rules, beyond those of every description:
|
|
903
|
+
|
|
904
|
+
- One API per file. The name of the API, `recipe.name` and the name of the file
|
|
905
|
+
(`billing.recipe.yml`) are the same: lowercase letters, digits, `-` and `_`.
|
|
906
|
+
- `recipe.name` and `recipe.version` are required. The version is text made of
|
|
907
|
+
digits, letters and dots: write `'1.2'` between quotes. `format` is the
|
|
908
|
+
version of the format, today 1; `summary` and `homepage` are optional.
|
|
909
|
+
- `host` is required. A variable can be the whole host, or stand before fixed
|
|
910
|
+
text that starts with a dot: `{account}.billing.example.com` and
|
|
911
|
+
`s3.{region}.example.com` are accepted, `api.example.{tld}` is not.
|
|
912
|
+
- No `verify_with`, no ERB, and no `<%` anywhere in the file.
|
|
913
|
+
- Plain YAML: no anchors, aliases, tags or merge keys, no key given twice, keys
|
|
914
|
+
that are text (quote `'on'`, `'no'` and numbers).
|
|
915
|
+
- Every name and every value is visible text: no control characters, no line
|
|
916
|
+
breaks inside a value, no invisible or direction-changing characters. The
|
|
917
|
+
same holds for the rest of the file, comments included: what a person reads
|
|
918
|
+
in it is what is loaded.
|
|
919
|
+
- At most 1 MiB, 10,000 endpoints, those that `resource` adds among them, 2,000
|
|
920
|
+
characters for a name or a value, and 4,000 characters for the path of an
|
|
921
|
+
endpoint with the paths above it. The headers and parameters written on
|
|
922
|
+
endpoints are at most 100,000, counting for each endpoint that adds some
|
|
923
|
+
also those it takes from the endpoints above it.
|
|
924
|
+
- With `auth.type: query`, `default_params` cannot name the parameter of the
|
|
925
|
+
credential.
|
|
926
|
+
- A credential never goes in a recipe: `auth` says where it travels, and the
|
|
927
|
+
application assigns it.
|
|
928
|
+
|
|
929
|
+
A repository of recipes keeps each one in `recipes/NAME.recipe.yml`. A
|
|
930
|
+
published version is the tag `NAME-VERSION`, for example `billing-1.2.0`.
|
|
931
|
+
`api_recipes routes billing.recipe.yml` lists the calls of a recipe.
|
|
932
|
+
|
|
933
|
+
### From a file of your project
|
|
934
|
+
|
|
935
|
+
When the API is already described in a file of your project, `convert` moves
|
|
936
|
+
it to a portable recipe. Run it in the root of the project: a Rails
|
|
937
|
+
application or any other Ruby project, wherever `ApiRecipes.configure` is
|
|
938
|
+
called.
|
|
939
|
+
|
|
940
|
+
$ bundle exec api_recipes convert config/apis/billing.yml.erb
|
|
941
|
+
billing 1.0.0 now comes from a portable recipe: 2 calls
|
|
942
|
+
|
|
943
|
+
Changed in the project
|
|
944
|
+
created config/api_recipes/billing.recipe.yml, with its line in config/api_recipes/recipes.lock
|
|
945
|
+
edited config/initializers/api_recipes.rb: recipes_paths and config.values
|
|
946
|
+
edited app/models/invoice.rb: verify_with on the declaration of the api
|
|
947
|
+
deleted config/apis/billing.yml.erb
|
|
948
|
+
|
|
949
|
+
What changed in the description
|
|
950
|
+
host: the ERB tag is now the variable {billing_host}, with the default written in the tag
|
|
951
|
+
port: the ERB tag is now the variable {billing_port}, with the default written in the tag
|
|
952
|
+
verify_with: left out, a recipe cannot carry it: give it where a class declares the api, as `api :billing, verify_with: ...`
|
|
953
|
+
default_headers.Api-Key-Client: the ERB tag is now the variable {billing_client}; the value is not in the recipe
|
|
954
|
+
|
|
955
|
+
Every change is in `git status` and `git diff`. Check the recipes with `api_recipes check`, then start the application.
|
|
956
|
+
|
|
957
|
+
The project works as before, and the values still come from where they came:
|
|
958
|
+
the code of each tag is moved, as it is written, to `config.values`.
|
|
959
|
+
|
|
960
|
+
```ruby
|
|
961
|
+
ApiRecipes.configure do |config|
|
|
962
|
+
config.apis_files_paths = Dir[File.expand_path('../apis/*.yml.erb', __dir__)]
|
|
963
|
+
config.values = {
|
|
964
|
+
billing: { variables: {
|
|
965
|
+
billing_host: (ENV['BILLING_HOST'] || 'billing.example.test'),
|
|
966
|
+
billing_port: (ENV['BILLING_PORT'] || 443),
|
|
967
|
+
billing_client: ENV['BILLING_CLIENT']
|
|
968
|
+
}.compact }
|
|
969
|
+
}
|
|
970
|
+
config.recipes_paths = Dir[File.expand_path('../api_recipes/*.recipe.yml', __dir__)]
|
|
971
|
+
end
|
|
972
|
+
```
|
|
973
|
+
|
|
974
|
+
What the command does:
|
|
975
|
+
|
|
976
|
+
- **It writes the recipe**, with the key `recipe` (the name of the API,
|
|
977
|
+
`--version`, `1.0.0` by default, and the format), and installs it in
|
|
978
|
+
`config/api_recipes` with its line in `recipes.lock`. When the file describes
|
|
979
|
+
several APIs, `--name` chooses one. Anchors, aliases and merge keys are
|
|
980
|
+
written out.
|
|
981
|
+
- **An `<%= ... %>` tag becomes a variable**, in every place where a recipe
|
|
982
|
+
can have one: the `host`, the `base_url`, a `path`, the value of a header or
|
|
983
|
+
of a parameter, and the `protocol`, `port`, `timeout`, `on_bad_code`,
|
|
984
|
+
`mime_type`, `verb` and `ok_code` when the tag gives the whole value. The
|
|
985
|
+
variable is named after the environment variable that the tag reads, or
|
|
986
|
+
after its place, and a default written in the tag as plain text or as a
|
|
987
|
+
number (`ENV.fetch('NAME', 'text')`, `ENV['NAME'] || 443`) becomes the
|
|
988
|
+
default of the variable. A default that a tag gave to a variable the file
|
|
989
|
+
declares is given by the application from now on.
|
|
990
|
+
- **Nothing in the file is run**, and there is no option that runs it. The
|
|
991
|
+
code of a tag goes to `config.values` as text, and each tag has its own
|
|
992
|
+
variable, also two tags in one value. `.compact` leaves out a variable whose
|
|
993
|
+
code gives `nil`: the default of the recipe applies when the tag had one
|
|
994
|
+
written, and otherwise the request says which variable is missing, where
|
|
995
|
+
2.x went on with the default of the gem.
|
|
996
|
+
- **A credential is never copied into the recipe.** A header or a parameter
|
|
997
|
+
whose name contains `auth`, `key`, `token`, `secret`, `passw`, `credential`,
|
|
998
|
+
`signature`, `cookie` or `session` becomes a variable without a default,
|
|
999
|
+
unless the file already gives it with a variable it declares, and
|
|
1000
|
+
its value, written in the file or given by a tag, goes to `config.values`.
|
|
1001
|
+
The command cannot know a secret written anywhere else: read the recipe
|
|
1002
|
+
before you share it.
|
|
1003
|
+
- **The old description is removed**: the file is deleted, or only the lines
|
|
1004
|
+
of that API when it describes others, because a recipe never takes the place
|
|
1005
|
+
of an API that another source defines.
|
|
1006
|
+
- **`verify_with`**, which a recipe cannot carry, is given to each `api :name`
|
|
1007
|
+
that a class or a module declares. One that a tag gave, or that an endpoint
|
|
1008
|
+
had, is left to you, as it is when one of the declarations gives its options
|
|
1009
|
+
in a way the command cannot read, such as `**options`.
|
|
1010
|
+
- **When it changes the project it prints names and paths**, nothing of what
|
|
1011
|
+
the file holds.
|
|
1012
|
+
|
|
1013
|
+
It changes your files only when that is safe, and all at once or not at all:
|
|
1014
|
+
|
|
1015
|
+
- Lines are added only to a plain file of the project that is in the last
|
|
1016
|
+
commit of git. A file is deleted, or lines are taken out of it, only when
|
|
1017
|
+
git has it committed as it is now. A symbolic link, and a file that git was told to skip, are never
|
|
1018
|
+
changed. Outside a git repository the project is not changed.
|
|
1019
|
+
- Ruby is read as a tree, never matched as lines and never run, and the
|
|
1020
|
+
command edits only what it can read whole:
|
|
1021
|
+
- the call to `ApiRecipes.configure`, when the project has exactly one, with
|
|
1022
|
+
a block `do |config|` on several lines that has no `rescue` and cannot end
|
|
1023
|
+
early with `next`, `break` or `return`;
|
|
1024
|
+
- `config.values`, which is added, or which gets one more entry when it is
|
|
1025
|
+
given a Hash between braces whose keys are all written as names, none of
|
|
1026
|
+
them the API, and whose closing brace starts a line;
|
|
1027
|
+
- `config.apis_files_paths`, when the old file is deleted: it must be given
|
|
1028
|
+
patterns of `Dir`, or a list between brackets of names written as text,
|
|
1029
|
+
from which the one element that names the file is taken out.
|
|
1030
|
+
|
|
1031
|
+
Anything else stops it: another mention of the old file in the code, another
|
|
1032
|
+
file that describes the API, a `recipes_paths` it cannot read.
|
|
1033
|
+
- Every Ruby file it changes must still be Ruby with the change, and when a
|
|
1034
|
+
write fails every file is put back as it was.
|
|
1035
|
+
- When a step is not possible, nothing is written, the command says why, and
|
|
1036
|
+
it gives the steps to do by hand with the code to paste: the commands that
|
|
1037
|
+
write and install the recipe, the lines for the block of
|
|
1038
|
+
`ApiRecipes.configure` with the file and the line, the line that gives
|
|
1039
|
+
`verify_with`, and what to remove.
|
|
1040
|
+
|
|
1041
|
+
2. In config/initializers/api_recipes.rb, inside the block of ApiRecipes.configure (line 1), add:
|
|
1042
|
+
|
|
1043
|
+
config.recipes_paths = Dir[File.expand_path('../api_recipes/*.recipe.yml', __dir__)]
|
|
1044
|
+
config.values = {
|
|
1045
|
+
billing: { variables: {
|
|
1046
|
+
billing_host: (ENV['BILLING_HOST'] || 'billing.example.test'),
|
|
1047
|
+
billing_port: (ENV['BILLING_PORT'] || 443),
|
|
1048
|
+
billing_client: ENV['BILLING_CLIENT']
|
|
1049
|
+
}.compact }
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
The code is what the file holds, the code of each tag and each value that
|
|
1053
|
+
was written, so that what you paste works; on a terminal it is shown in
|
|
1054
|
+
colours. Only what a terminal would obey is left out, with a comment that
|
|
1055
|
+
says the line to copy it from.
|
|
1056
|
+
- What it could not do by itself after changing the project is listed under
|
|
1057
|
+
`Left to you`: a `verify_with` when no class declares the API, or on an
|
|
1058
|
+
endpoint.
|
|
1059
|
+
- A tag that is not `<%= ... %>`, that gives the name of an option, or that
|
|
1060
|
+
gives a part of the protocol, of the port or of another such option, stops
|
|
1061
|
+
the command, which says where.
|
|
1062
|
+
|
|
1063
|
+
To write the recipe alone, as for the repository of the recipes, name a
|
|
1064
|
+
folder: the project is not touched, and the command gives the code for the
|
|
1065
|
+
block of `ApiRecipes.configure`.
|
|
1066
|
+
|
|
1067
|
+
$ api_recipes convert config/apis/billing.yml.erb --to ../api-recipes/recipes
|
|
1068
|
+
|
|
1069
|
+
The command does not publish: the recipe reaches a repository with `git`,
|
|
1070
|
+
through the review that every recipe gets.
|
|
1071
|
+
|
|
527
1072
|
## Where an API is available
|
|
528
1073
|
|
|
529
1074
|
### On the `ApiRecipes` module
|
|
@@ -649,6 +1194,10 @@ They are the options of
|
|
|
649
1194
|
| `headers:` | extra headers for this request only |
|
|
650
1195
|
| `follow:` | follow redirects: `true`, or a Hash with `strict`, `max_hops` and `on_redirect` |
|
|
651
1196
|
|
|
1197
|
+
The [`default_params`](#headers-and-parameters-sent-with-every-request) of the
|
|
1198
|
+
API and of the endpoint come first in the query string; a parameter given here
|
|
1199
|
+
replaces the one with the same name.
|
|
1200
|
+
|
|
652
1201
|
```ruby
|
|
653
1202
|
# GET /search/repositories?q=api%20language:ruby&per_page=5
|
|
654
1203
|
ApiRecipes.github.search.repositories(params: { q: 'api language:ruby', per_page: 5 }).run
|
|
@@ -699,8 +1248,8 @@ ApiRecipes.github.with(host: 'github.example.com').users.show('octocat').url.to_
|
|
|
699
1248
|
```
|
|
700
1249
|
|
|
701
1250
|
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,
|
|
1251
|
+
`on_bad_code`, `mime_type`, `verify_with`, `default_headers`, `default_params`,
|
|
1252
|
+
`variables`, `authorization` and `basic_auth`. The values are checked like the ones in a file,
|
|
704
1253
|
and an option that is not in this list raises `ApiRecipes::ConfigurationError`.
|
|
705
1254
|
|
|
706
1255
|
On an endpoint, `with` takes `timeout`, `on_bad_code`, `ok_code`, `mime_type` and
|
|
@@ -711,7 +1260,8 @@ ApiRecipes.github.users.show('octocat').with(timeout: 10, ok_code: [200, 304]).r
|
|
|
711
1260
|
```
|
|
712
1261
|
|
|
713
1262
|
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
|
|
1263
|
+
`default_headers` and `default_params` given to `with` are added to the ones
|
|
1264
|
+
already configured:
|
|
715
1265
|
|
|
716
1266
|
```ruby
|
|
717
1267
|
ApiRecipes.github.with(default_headers: { 'X-Request-Id' => 'abc123' }).users.run
|
|
@@ -857,8 +1407,8 @@ end
|
|
|
857
1407
|
```
|
|
858
1408
|
|
|
859
1409
|
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
|
|
1410
|
+
meant for APIs used from a class or an instance. An endpoint that has no
|
|
1411
|
+
`verify_with` of its own looks for one up the tree, as for the other options.
|
|
862
1412
|
|
|
863
1413
|
## Handling errors
|
|
864
1414
|
|
|
@@ -907,30 +1457,40 @@ nearest to the farthest:
|
|
|
907
1457
|
|
|
908
1458
|
1. the chain, with `with(authorization: ...)` or `with(basic_auth: ...)`
|
|
909
1459
|
2. the object that makes the call
|
|
910
|
-
3.
|
|
911
|
-
4. the
|
|
1460
|
+
3. the class or the module that declares the API
|
|
1461
|
+
4. the whole application
|
|
912
1462
|
|
|
913
|
-
|
|
1463
|
+
The last two are shared by every thread of the process, so they are given where
|
|
1464
|
+
the application starts and cannot be assigned afterwards. A chain and an object
|
|
1465
|
+
are seen only by the code that holds them: they are the place for a credential
|
|
1466
|
+
that changes from one request to the next, as long as the object is not itself
|
|
1467
|
+
kept where every request finds it.
|
|
1468
|
+
|
|
1469
|
+
**A token for the whole application**, sent as the `Authorization` header, as
|
|
1470
|
+
it is written. It goes in `config.values`, with the variables of the API:
|
|
914
1471
|
|
|
915
1472
|
```ruby
|
|
916
|
-
ApiRecipes.
|
|
1473
|
+
ApiRecipes.configure do |config|
|
|
1474
|
+
config.values = { github: { authorization: "Bearer #{ENV.fetch('GITHUB_TOKEN', 'abc')}" } }
|
|
1475
|
+
end
|
|
917
1476
|
```
|
|
918
1477
|
|
|
919
1478
|
**Basic auth:**
|
|
920
1479
|
|
|
921
1480
|
```ruby
|
|
922
|
-
ApiRecipes.
|
|
1481
|
+
ApiRecipes.configure do |config|
|
|
1482
|
+
config.values = { github: { basic_auth: { user: 'me', pass: 'secret' } } }
|
|
1483
|
+
end
|
|
923
1484
|
```
|
|
924
1485
|
|
|
925
|
-
**A different token for one class.** It applies to the class
|
|
1486
|
+
**A different token for one class.** It applies to the class, to its subclasses
|
|
1487
|
+
and to their objects, and it is given where the class declares the API:
|
|
926
1488
|
|
|
927
1489
|
```ruby
|
|
928
1490
|
class AdminClient
|
|
929
1491
|
include ApiRecipes
|
|
930
|
-
api :github
|
|
1492
|
+
api :github, authorization: "Bearer #{ENV.fetch('ADMIN_TOKEN', 'admin')}"
|
|
931
1493
|
end
|
|
932
|
-
|
|
933
|
-
AdminClient.github.authorization = "Bearer #{ENV.fetch('ADMIN_TOKEN')}"
|
|
934
1494
|
```
|
|
935
1495
|
|
|
936
1496
|
**A different token for one object**, for example one object per user:
|
|
@@ -952,26 +1512,38 @@ end
|
|
|
952
1512
|
ApiRecipes.github.with(authorization: "Bearer #{user.token}").users.repos('octocat').run
|
|
953
1513
|
```
|
|
954
1514
|
|
|
955
|
-
The reader returns what the nearest level
|
|
1515
|
+
The reader returns what the nearest level gives:
|
|
956
1516
|
|
|
957
1517
|
```ruby
|
|
958
|
-
ApiRecipes.
|
|
1518
|
+
ApiRecipes.configure do |config|
|
|
1519
|
+
config.values = { github: { authorization: 'Bearer abc' } }
|
|
1520
|
+
end
|
|
959
1521
|
ApiRecipes.github.authorization
|
|
960
1522
|
# => "Bearer abc"
|
|
1523
|
+
ApiRecipes.github.with(authorization: 'Bearer of this chain').authorization
|
|
1524
|
+
# => "Bearer of this chain"
|
|
961
1525
|
```
|
|
962
1526
|
|
|
963
|
-
**No credential.**
|
|
964
|
-
does not fall back to a level farther away.
|
|
965
|
-
`with(authorization: nil)`:
|
|
1527
|
+
**No credential.** A credential given as `nil` means "send none" and ends the
|
|
1528
|
+
search: it does not fall back to a level farther away. It works at every level:
|
|
966
1529
|
|
|
967
1530
|
```ruby
|
|
968
|
-
|
|
969
|
-
|
|
1531
|
+
ApiRecipes.github.with(authorization: nil).users.run # this chain sends none
|
|
1532
|
+
|
|
1533
|
+
class PublicClient
|
|
1534
|
+
include ApiRecipes
|
|
1535
|
+
api :github, authorization: nil # this class sends none
|
|
1536
|
+
end
|
|
970
1537
|
```
|
|
971
1538
|
|
|
972
|
-
`authorization=`, `basic_auth=` and `variables=`
|
|
973
|
-
they are called on.
|
|
974
|
-
|
|
1539
|
+
`authorization=`, `basic_auth=` and `variables=` exist for objects only: they
|
|
1540
|
+
change the level of the object they are called on. On `ApiRecipes.github` and
|
|
1541
|
+
on a class they raise `ApiRecipes::Error`, and the message says where the value
|
|
1542
|
+
goes: a class that served two users at once would otherwise send the token of
|
|
1543
|
+
one with the request of the other. They also raise on an API that `with`
|
|
1544
|
+
returned: use `with(authorization: ...)` there. A token that expires and is
|
|
1545
|
+
renewed while the application runs goes to `with`, read from where you keep it
|
|
1546
|
+
at every call.
|
|
975
1547
|
|
|
976
1548
|
A credential is valid only for the origin it was set for: the scheme, the host
|
|
977
1549
|
and the port. If a nearer level changes the destination (its own `host`,
|
|
@@ -983,10 +1555,8 @@ destination:
|
|
|
983
1555
|
```ruby
|
|
984
1556
|
class EnterpriseClient
|
|
985
1557
|
include ApiRecipes
|
|
986
|
-
api :github, host: 'github.example.com'
|
|
1558
|
+
api :github, host: 'github.example.com', authorization: "Bearer #{ENV.fetch('ENTERPRISE_TOKEN', 'enterprise')}"
|
|
987
1559
|
end
|
|
988
|
-
|
|
989
|
-
EnterpriseClient.github.authorization = "Bearer #{ENV.fetch('ENTERPRISE_TOKEN')}"
|
|
990
1560
|
```
|
|
991
1561
|
|
|
992
1562
|
An API or an endpoint kept in a variable keeps the destination it was created
|
|
@@ -1004,7 +1574,7 @@ restored from a serialized form has no credential.
|
|
|
1004
1574
|
|
|
1005
1575
|
An API can say where its credential goes with `auth`:
|
|
1006
1576
|
|
|
1007
|
-
| `auth.type` | The credential
|
|
1577
|
+
| `auth.type` | The credential given as `authorization` goes |
|
|
1008
1578
|
| --- | --- |
|
|
1009
1579
|
| none of these, or `none`, `bearer`, `basic` | in the `Authorization` header, as it is written |
|
|
1010
1580
|
| `header`, with a `name` | in the header called `name` |
|
|
@@ -1021,7 +1591,7 @@ weather:
|
|
|
1021
1591
|
```
|
|
1022
1592
|
|
|
1023
1593
|
Basic auth cannot be sent to an API of type `header` or `query`: the request
|
|
1024
|
-
raises `ApiRecipes::InvalidCredential`.
|
|
1594
|
+
raises `ApiRecipes::InvalidCredential`. Give the value as `authorization`.
|
|
1025
1595
|
|
|
1026
1596
|
A credential written in `headers:` or in `default_headers` is sent as it is.
|
|
1027
1597
|
ApiRecipes does not check its origin, so prefer the levels above.
|
|
@@ -1086,14 +1656,20 @@ ApiRecipes.github.users.show('octocat').definition.full_path
|
|
|
1086
1656
|
```
|
|
1087
1657
|
|
|
1088
1658
|
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
|
-
|
|
1659
|
+
`on_bad_code`, `mime_type`, `verify_with`, `default_headers`, `default_params`,
|
|
1660
|
+
`auth`, `variables`, `endpoints`, `routes` and `to_h`. For an endpoint: `name`,
|
|
1661
|
+
`full_name`, `path`, `full_path`, `path_params`, `path_variables`, `verb`,
|
|
1662
|
+
`route?`, `ok_code`, `timeout`, `on_bad_code`, `mime_type`, `verify_with`,
|
|
1663
|
+
`default_headers`, `default_params`, `parent`, `endpoints` and `to_h`. The
|
|
1664
|
+
`default_headers` and `default_params` of an endpoint are the ones written on
|
|
1665
|
+
it and on the endpoints above it, without those of the API. A path is shown as
|
|
1666
|
+
it is written, with its `:name` and `{name}`. The endpoints that `resource`
|
|
1667
|
+
adds are endpoints like the others.
|
|
1668
|
+
A definition never holds a credential, wherever it was given, nor the value of
|
|
1669
|
+
a variable. It does return the
|
|
1670
|
+
`default_headers` and the `default_params` as they are configured, so do not
|
|
1671
|
+
write a credential there if you pass definitions around; `to_h` and `inspect`
|
|
1672
|
+
hide their values.
|
|
1097
1673
|
|
|
1098
1674
|
`routes` lists every endpoint that can be called, with the call that reaches it:
|
|
1099
1675
|
|
|
@@ -1125,11 +1701,13 @@ in it.
|
|
|
1125
1701
|
ApiRecipes.configure do |config|
|
|
1126
1702
|
config.logger = Rails.logger
|
|
1127
1703
|
config.apis_files_paths = Dir[Rails.root.join('config', 'apis', '*.yml').to_s]
|
|
1704
|
+
config.values = { github: { authorization: "Bearer #{Rails.application.credentials.github_token}" } }
|
|
1128
1705
|
end
|
|
1129
|
-
|
|
1130
|
-
ApiRecipes.github.authorization = "Bearer #{Rails.application.credentials.github_token}"
|
|
1131
1706
|
```
|
|
1132
1707
|
|
|
1708
|
+
A value that depends on the user who is signed in does not go there: see
|
|
1709
|
+
[Variables](#variables) for the request of a controller.
|
|
1710
|
+
|
|
1133
1711
|
### One host per environment
|
|
1134
1712
|
|
|
1135
1713
|
Keep one file and let ERB pick the values:
|
|
@@ -1241,8 +1819,9 @@ end
|
|
|
1241
1819
|
```
|
|
1242
1820
|
|
|
1243
1821
|
If your tests configure ApiRecipes themselves, `ApiRecipes.reset!` removes the
|
|
1244
|
-
global configuration, the API methods defined on the module
|
|
1245
|
-
and
|
|
1822
|
+
global configuration, the API methods defined on the module, the values of the
|
|
1823
|
+
application and those assigned to objects, so each example can start clean.
|
|
1824
|
+
What a class gives to `api` is part of its code and stays:
|
|
1246
1825
|
|
|
1247
1826
|
```ruby
|
|
1248
1827
|
RSpec.configure do |config|
|
|
@@ -1256,8 +1835,19 @@ What ApiRecipes does:
|
|
|
1256
1835
|
|
|
1257
1836
|
- **Path parameters** are encoded one by one. A value cannot add a segment, a
|
|
1258
1837
|
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.
|
|
1838
|
+
- **Variables** are checked for the place where they end up: a host, a path, a
|
|
1839
|
+
header or a query parameter. In a path a value is one encoded segment, like a
|
|
1840
|
+
path parameter.
|
|
1841
|
+
- **Parameters sent with every request** cannot take the place of the
|
|
1842
|
+
credential: with `auth.type: query`, `default_params` that name its parameter
|
|
1843
|
+
are refused, and the credential is always the only parameter with its name.
|
|
1844
|
+
- **Shared values.** The credential and the variables that the whole
|
|
1845
|
+
application or a class uses are read by every thread. They are given only
|
|
1846
|
+
where the application starts, to `config.values` and to `api`: on
|
|
1847
|
+
`ApiRecipes.github` and on a class the writers raise, so code that serves
|
|
1848
|
+
several users cannot leave the token of one of them where the request of
|
|
1849
|
+
another finds it. A `configure` changes the descriptions and those values in
|
|
1850
|
+
one step.
|
|
1261
1851
|
- **Credentials** are sent only to the origin they were set for, and a request
|
|
1262
1852
|
that would send one elsewhere raises. A value with a control character is
|
|
1263
1853
|
refused, so a credential cannot add a header.
|
|
@@ -1269,14 +1859,31 @@ What ApiRecipes does:
|
|
|
1269
1859
|
- **`api_recipes import`** reads one local file, follows only the references
|
|
1270
1860
|
inside it, never uses the network and runs nothing of the description. Text of
|
|
1271
1861
|
the description cannot add keys to the recipe and cannot become ERB in it.
|
|
1862
|
+
- **Portable recipes** are data only. A file of `recipes_paths` is never
|
|
1863
|
+
evaluated with ERB and is read as plain YAML, without anchors, aliases or
|
|
1864
|
+
tags. It cannot carry `verify_with`, cannot take the name of an API you
|
|
1865
|
+
already have, and cannot let a variable decide where its host ends. Its
|
|
1866
|
+
text, comments included, holds only visible characters. It is loaded only if
|
|
1867
|
+
it matches the checksum in `recipes.lock`.
|
|
1868
|
+
- **`api_recipes install` and `update`** show where the requests and the
|
|
1869
|
+
credential go, the headers and the parameters sent with every request, those
|
|
1870
|
+
that an endpoint adds, and which calls are not GET, the ones that `resource`
|
|
1871
|
+
adds among them, before they write. They read one
|
|
1872
|
+
file from the repository without checking it out, only over `https` or `ssh`,
|
|
1873
|
+
and run `git` without a shell. An `https` address with a user name or a
|
|
1874
|
+
password and an `ssh` address with a password are refused, so no secret can
|
|
1875
|
+
end up in `recipes.lock`. The commands say which address they read before
|
|
1876
|
+
`git` connects to it: `update` takes it from `recipes.lock`, so read a change
|
|
1877
|
+
to that file as you read a change to the code.
|
|
1272
1878
|
- **Logs, errors and `inspect`.** A log line and an error message of the gem
|
|
1273
1879
|
never contain a credential, a header value, a body or a query string. Neither
|
|
1274
1880
|
does the `inspect` or the `pp` of an API, an endpoint, a response, a
|
|
1275
1881
|
definition or the configuration, nor their `to_s`, except that the `to_s` of
|
|
1276
1882
|
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
|
|
1883
|
+
a credential, wherever it was given.
|
|
1884
|
+
It does hold the values of `default_headers` and `default_params`, which its
|
|
1885
|
+
readers return; its `inspect`, its `to_h` and its JSON show them as
|
|
1886
|
+
`[FILTERED]`.
|
|
1280
1887
|
- **Serialization.** An API, an endpoint and a response refuse `Marshal.dump`,
|
|
1281
1888
|
`YAML.dump`, `as_json` and `to_json`, and raise `TypeError`: serialize
|
|
1282
1889
|
`response.data` or `response.body.to_s` instead. A definition refuses
|
|
@@ -1293,9 +1900,16 @@ What stays with your application:
|
|
|
1293
1900
|
variable that is the entire host. Check it against a list of hosts you know. A
|
|
1294
1901
|
variable that is only a part of a host, like the shop name in
|
|
1295
1902
|
`{shop}.myshopify.com`, is the intended use.
|
|
1903
|
+
- An object that every request reaches, such as a client kept in a constant or
|
|
1904
|
+
memoized on a class, is as shared as a class: do not assign the credential or
|
|
1905
|
+
the variables of one user on it. Use `with`, or an object made for the request.
|
|
1296
1906
|
- `response.original_response` is the raw http.rb response, and it gives access
|
|
1297
1907
|
to the request that was sent, headers included. Do not log or serialize it.
|
|
1298
1908
|
- ERB in a configuration file runs code. Load only files you trust.
|
|
1909
|
+
- Read what `api_recipes install` shows before you answer yes. ApiRecipes checks
|
|
1910
|
+
the form of a recipe, not whose host it names. Commit the installed recipes
|
|
1911
|
+
and `recipes.lock`, and review their changes as you review code: the lock
|
|
1912
|
+
makes a change visible, it does not stop someone who can change both files.
|
|
1299
1913
|
- Do not put a credential in the source of an application or in a file of the
|
|
1300
1914
|
repository: read it from the place where your application keeps secrets.
|
|
1301
1915
|
|
|
@@ -1310,7 +1924,8 @@ Every protection has an example in [`spec/security`](spec/security); its
|
|
|
1310
1924
|
| --- | --- |
|
|
1311
1925
|
| `apis_files_paths` | Array of YAML files, evaluated with ERB |
|
|
1312
1926
|
| `apis_configs` | Hash of API descriptions |
|
|
1313
|
-
| `
|
|
1927
|
+
| `recipes_paths` | Array of installed portable recipes (`NAME.recipe.yml`), read as data only and checked against `recipes.lock` |
|
|
1928
|
+
| `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
1929
|
| `logger` | a Logger to use |
|
|
1315
1930
|
| `log_to` | path or IO for the default logger (default: STDERR) |
|
|
1316
1931
|
| `log_level` | level of the default logger (default: the `LOG_LEVEL` variable, or warn) |
|
|
@@ -1321,12 +1936,13 @@ Every protection has an example in [`spec/security`](spec/security); its
|
|
|
1321
1936
|
|
|
1322
1937
|
| Option | Default | Meaning |
|
|
1323
1938
|
| --- | --- | --- |
|
|
1324
|
-
| `protocol` | `https` | `http` or `https` |
|
|
1939
|
+
| `protocol` | `https` | `http` or `https`; can be a `{variable}` |
|
|
1325
1940
|
| `host` | `localhost` | host name, which can contain `{variables}` |
|
|
1326
|
-
| `port` | port of the protocol | port number |
|
|
1941
|
+
| `port` | port of the protocol | port number; can be a `{variable}` |
|
|
1327
1942
|
| `base_url` | none | prefix of every path, for example `api/v1`; can contain `{variables}` |
|
|
1328
|
-
| `timeout` | `3` | seconds allowed for the whole request |
|
|
1943
|
+
| `timeout` | `3` | seconds allowed for the whole request; can be a `{variable}`, as `on_bad_code` and `mime_type` |
|
|
1329
1944
|
| `default_headers` | none | headers sent with every request; a value can contain `{variables}` |
|
|
1945
|
+
| `default_params` | none | query parameters sent with every request; a value is text, a number or a boolean, and can contain `{variables}` |
|
|
1330
1946
|
| `on_bad_code` | `raise` | `raise` or `ignore` when the response is not a success |
|
|
1331
1947
|
| `mime_type` | from the response | content type used by `response.data` |
|
|
1332
1948
|
| `verify_with` | none | method that decides whether a response is a success |
|
|
@@ -1338,18 +1954,25 @@ Every protection has an example in [`spec/security`](spec/security); its
|
|
|
1338
1954
|
|
|
1339
1955
|
| Option | Default | Meaning |
|
|
1340
1956
|
| --- | --- | --- |
|
|
1341
|
-
| `path` | the endpoint name | segment added to the parent path; `''` adds nothing |
|
|
1342
|
-
| `verb` | `get` | HTTP verb |
|
|
1957
|
+
| `path` | the endpoint name | segment added to the parent path; `''` adds nothing; can contain `:name` and `*name` placeholders and `{variables}` |
|
|
1958
|
+
| `verb` | `get` | HTTP verb; can be a `{variable}`, as `ok_code` (one status), `timeout`, `on_bad_code` and `mime_type` |
|
|
1343
1959
|
| `route` | `yes` | `no` makes the endpoint a group that cannot be called |
|
|
1344
1960
|
| `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` |
|
|
1961
|
+
| `timeout` | looked for up the tree | seconds allowed for the whole request |
|
|
1962
|
+
| `on_bad_code` | looked for up the tree | `raise` or `ignore` |
|
|
1963
|
+
| `mime_type` | looked for up the tree | content type used by `response.data` |
|
|
1964
|
+
| `verify_with` | looked for up the tree | method that decides whether a response is a success |
|
|
1965
|
+
| `default_headers` | none | headers added to those of the API, for this endpoint and the ones below it |
|
|
1966
|
+
| `default_params` | none | query parameters added to those of the API, for this endpoint and the ones below it |
|
|
1967
|
+
| `resource` | `no` | `yes` adds the endpoints `show`, `create`, `update`, `replace` and `delete`; a list adds only the ones it names |
|
|
1349
1968
|
| `endpoints` | none | nested endpoints |
|
|
1350
1969
|
|
|
1351
|
-
|
|
1352
|
-
the
|
|
1970
|
+
"Looked for up the tree" means: in the endpoints that contain this one, from
|
|
1971
|
+
the nearest, then in the API; the first that sets the option gives the value,
|
|
1972
|
+
and the default of the API table applies when none does. `default_headers` and
|
|
1973
|
+
`default_params` are looked for in the same way, one name at a time. `ok_code`
|
|
1974
|
+
is read only on its endpoint: the status that means success belongs to one
|
|
1975
|
+
operation, as its verb does.
|
|
1353
1976
|
|
|
1354
1977
|
## Good to know
|
|
1355
1978
|
|