# Giving the rescue step a service of its own

## What you want to do

The bank now answers which card replaced an expired one from its card registry, a system of its own in each region, and its library hands back the registry's client as an object it makes itself. You want the rescue step to ask the registry instead of the payment client, and no other step to have it.

## The code

The shop is [chapter 34](https://itinera-dev.org/book/kotlin/34-saying-which-attempt-took-the-money.md)'s. Its payment client no longer answers which card replaced one, so `Payments` keeps only `charge`:

Kotlin, from the program [`ch35_giving_the_rescue_step_a_service_of_its_own`](https://github.com/itinera-dev/book/blob/main/kotlin/examples/ch35_giving_the_rescue_step_a_service_of_its_own.kt)

```kotlin
/**
 * A payment client: it charges an order's amount to a card, and gives back a receipt, or `null`
 * when the card has expired. It throws [NetworkDown] when its network fails.
 */
interface Payments {
    fun charge(order: String, card: String, amount: Long): String?
}

```

Kotlin, the same in its asynchronous form, from the program [`ch35_giving_the_rescue_step_a_service_of_its_own_async`](https://github.com/itinera-dev/book/blob/main/kotlin/examples/ch35_giving_the_rescue_step_a_service_of_its_own_async.kt)

```kotlin
/**
 * A payment client: it charges an order's amount to a card, and gives back a receipt, or `null`
 * when the card has expired. It throws [NetworkDown] when its network fails. The answer comes over
 * the network.
 */
interface Payments {
    suspend fun charge(order: String, card: String, amount: Long): String?
}

```

The registry is a service with an interface of its own, `Cards`, and the bank's library makes its client for a region:

Kotlin, from the program [`ch35_giving_the_rescue_step_a_service_of_its_own`](https://github.com/itinera-dev/book/blob/main/kotlin/examples/ch35_giving_the_rescue_step_a_service_of_its_own.kt)

```kotlin
/** A card registry: it says which card replaced one, or `null` when none did. */
interface Cards {
    fun replacement(card: String): String?
}

/**
 * The bank's own library: it connects to the bank's card registry for a region, and hands back its
 * client.
 */
fun cardRegistry(region: String): Cards = Registry(region, thisYear = 2026)

```

Kotlin, the same in its asynchronous form, from the program [`ch35_giving_the_rescue_step_a_service_of_its_own_async`](https://github.com/itinera-dev/book/blob/main/kotlin/examples/ch35_giving_the_rescue_step_a_service_of_its_own_async.kt)

```kotlin
/**
 * A card registry: it says which card replaced one, or `null` when none did. The answer comes over
 * the network.
 */
interface Cards {
    suspend fun replacement(card: String): String?
}

/**
 * The bank's own library: it connects to the bank's card registry for a region, and hands back its
 * client.
 */
fun cardRegistry(region: String): Cards = Registry(region, thisYear = 2026)

```

`Registry` is the library's own class, faked here like the bank. In the asynchronous form, `charge`, `replacement` and the rescue step's `run` are `suspend` functions, as in [chapter 26](https://itinera-dev.org/book/kotlin/26-running-async-journeys-while-others-wait.md).

The rescue step asks for the registry under the key `cards`, though it calls it `registry`, and reads the order's card, under the key `card`, as `expired`:

Kotlin, from the program [`ch35_giving_the_rescue_step_a_service_of_its_own`](https://github.com/itinera-dev/book/blob/main/kotlin/examples/ch35_giving_the_rescue_step_a_service_of_its_own.kt)

```kotlin
/**
 * Repairs a charge that failed because its card had expired: it asks the card registry for the card
 * that replaced it, and gives it back as the order's card.
 */
@RescueStep
class RenewCard(@Input("card") private val expired: String, private val reason: Reason) :
    ItineraRescueStep {
    fun run(@Service("cards") registry: Cards, contributor: Contributor): RescueOutcome {
        if (reason.code != "card-expired") return fail("not-repairable")
        val replacement = registry.replacement(expired) ?: return fail("not-replaced")
        contributor.contribute("card", replacement)
        return succeed()
    }
}

```

Kotlin, the same in its asynchronous form, from the program [`ch35_giving_the_rescue_step_a_service_of_its_own_async`](https://github.com/itinera-dev/book/blob/main/kotlin/examples/ch35_giving_the_rescue_step_a_service_of_its_own_async.kt)

```kotlin
/**
 * Repairs a charge that failed because its card had expired: it asks the card registry for the card
 * that replaced it, and gives it back as the order's card.
 */
@RescueStep
class RenewCard(@Input("card") private val expired: String, private val reason: Reason) :
    ItineraRescueStep {
    suspend fun run(@Service("cards") registry: Cards, contributor: Contributor): RescueOutcome {
        if (reason.code != "card-expired") return fail("not-repairable")
        val replacement = registry.replacement(expired) ?: return fail("not-replaced")
        contributor.contribute("card", replacement)
        return succeed()
    }
}

```

The workflow class has one more provider, for `cards`, attached to the rescue step alone, by its name. It hands over the library's client with the region it is for:

Kotlin, from the program [`ch35_giving_the_rescue_step_a_service_of_its_own`](https://github.com/itinera-dev/book/blob/main/kotlin/examples/ch35_giving_the_rescue_step_a_service_of_its_own.kt)

```kotlin
/** The bank's card registry in Europe, for the charge's rescue step only. */
@Provider(steps = ["charge_rescue"])
fun cards(): Provided<Cards> = provide(cardRegistry("eu"), "eu")

```

Kotlin, the same in its asynchronous form, from the program [`ch35_giving_the_rescue_step_a_service_of_its_own_async`](https://github.com/itinera-dev/book/blob/main/kotlin/examples/ch35_giving_the_rescue_step_a_service_of_its_own_async.kt)

```kotlin
/** The bank's card registry in Europe, for the charge's rescue step only. */
@Provider(steps = ["charge_rescue"])
suspend fun cards(): Provided<Cards> = provide(cardRegistry("eu"), "eu")

```

The rest of the workflow class is [chapter 23](https://itinera-dev.org/book/kotlin/23-undoing-a-charge-when-the-shipping-fails.md)'s, and its asynchronous form [chapter 26](https://itinera-dev.org/book/kotlin/26-running-async-journeys-while-others-wait.md)'s, where the provider is a `suspend` function, as every provider of an asynchronous workflow is.

The log's `detail`, [chapter 33](https://itinera-dev.org/book/kotlin/33-giving-every-reporter-what-it-needs.md)'s with the parts that page points to, now hands the data a provider supplied to `supplied`, in its branch for `ServiceProvided`:

Kotlin, from the program [`ch35_giving_the_rescue_step_a_service_of_its_own`](https://github.com/itinera-dev/book/blob/main/kotlin/examples/ch35_giving_the_rescue_step_a_service_of_its_own.kt)

```kotlin
is ServiceProvided ->
    "${event.step} gets ${event.key} from ${event.provider}, a ${event.implementation}" +
        supplied(event.data)
```

`supplied` writes the data when it is text, and nothing otherwise:

Kotlin, from the program [`ch35_giving_the_rescue_step_a_service_of_its_own`](https://github.com/itinera-dev/book/blob/main/kotlin/examples/ch35_giving_the_rescue_step_a_service_of_its_own.kt)

```kotlin
/** The data a provider supplied with a service, when it is text. */
fun supplied(data: Any?): String = if (data is String) ", with $data" else ""

```

`main` runs Jo's order, whose expired card the rescue step renews, as [chapter 34](https://itinera-dev.org/book/kotlin/34-saying-which-attempt-took-the-money.md) runs it, and prints:

What `ch35_giving_the_rescue_step_a_service_of_its_own` prints, recorded in [`ch35_giving_the_rescue_step_a_service_of_its_own.txt`](https://github.com/itinera-dev/book/blob/main/kotlin/expected/ch35_giving_the_rescue_step_a_service_of_its_own.txt)

```text
Jo's order:
log orders A-8 for Jo #1 journey_started order, customer, price, parcel, card, giftCard
log orders A-8 for Jo #2 attempt_started lookup, attempt 1
log orders A-8 for Jo #3 service_provided lookup gets orderBook from ledger, a ch35_giving_the_rescue_step_a_service_of_its_own.Ledger
the order book has no order A-8 yet
log orders A-8 for Jo #4 step_succeeded lookup
log orders A-8 for Jo #5 contribution_committed completed
log orders A-8 for Jo #6 hook_called alreadyCompleted: on step success of lookup
log orders A-8 for Jo #7 attempt_started price, attempt 1
log orders A-8 for Jo #8 optional_input_absent discount
log orders A-8 for Jo #9 step_succeeded price
log orders A-8 for Jo #10 contribution_committed amount
log orders A-8 for Jo #11 attempt_started charge, attempt 1
log orders A-8 for Jo #12 service_provided charge gets payments from bank, a ch35_giving_the_rescue_step_a_service_of_its_own.Bank
the bank declines card jo-2024: it has expired
log orders A-8 for Jo #13 step_failed charge: card-expired
log orders A-8 for Jo #14 step_rescuing charge: failure, rescued by charge_rescue
log orders A-8 for Jo #15 attempt_started charge_rescue, attempt 1
log orders A-8 for Jo #16 service_provided charge_rescue gets cards from cards, a ch35_giving_the_rescue_step_a_service_of_its_own.Registry, with eu
the card registry in eu says card jo-2024 was replaced by jo-2029
log orders A-8 for Jo #17 step_succeeded charge_rescue
log orders A-8 for Jo #18 contribution_committed card
log orders A-8 for Jo #19 data_overwritten card
log orders A-8 for Jo #20 step_rescued charge by charge_rescue
log orders A-8 for Jo #21 attempt_started charge, attempt 2
the bank charges 30 for order A-8
log orders A-8 for Jo, receipt R-A-8 #22 global_metadata_set receipt
log orders A-8 for Jo, receipt R-A-8 #23 step_succeeded charge
log orders A-8 for Jo, receipt R-A-8 #24 contribution_committed receipt
log orders A-8 for Jo, receipt R-A-8 #25 hook_called audit: on step success of charge
log orders A-8 for Jo, receipt R-A-8 #26 contribution_committed audit
log orders A-8 for Jo, receipt R-A-8 #27 attempt_started ship, attempt 1
log orders A-8 for Jo, receipt R-A-8 #28 input_adapter_supplied ship gets weight from parcels
log orders A-8 for Jo, receipt R-A-8 #29 service_provided ship gets carrier from carriers, a ch35_giving_the_rescue_step_a_service_of_its_own.MainCarrier
the main carrier books order A-8
log orders A-8 for Jo, receipt R-A-8 #30 step_info ship: the parcel leaves with receipt R-A-8
log orders A-8 for Jo, receipt R-A-8 #31 step_succeeded ship
log orders A-8 for Jo, receipt R-A-8 #32 contribution_committed tracking
log orders A-8 for Jo, receipt R-A-8 #33 optional_input_absent code
email to Jo: Order A-8 is on its way, tracking T-A-8.
log orders A-8 for Jo, receipt R-A-8 #34 hook_called notify: on workflow success
log orders A-8 for Jo, receipt R-A-8 #35 journey_succeeded
the journey succeeded, with receipt R-A-8 and tracking T-A-8
audit: A-8: charge took 30 on attempt 2, receipt R-A-8
```

What the asynchronous form `ch35_giving_the_rescue_step_a_service_of_its_own_async` prints, recorded in [`ch35_giving_the_rescue_step_a_service_of_its_own_async.txt`](https://github.com/itinera-dev/book/blob/main/kotlin/expected/ch35_giving_the_rescue_step_a_service_of_its_own_async.txt)

```text
log orders A-8 for Jo #1 journey_started order, customer, price, parcel, card, giftCard
log orders A-8 for Jo #2 attempt_started lookup, attempt 1
log orders A-8 for Jo #3 service_provided lookup gets orderBook from ledger, a ch35_giving_the_rescue_step_a_service_of_its_own_async.Ledger
the order book has no order A-8 yet
log orders A-8 for Jo #4 step_succeeded lookup
log orders A-8 for Jo #5 contribution_committed completed
log orders A-8 for Jo #6 hook_called alreadyCompleted: on step success of lookup
log orders A-8 for Jo #7 attempt_started price, attempt 1
log orders A-8 for Jo #8 optional_input_absent discount
log orders A-8 for Jo #9 step_succeeded price
log orders A-8 for Jo #10 contribution_committed amount
log orders A-8 for Jo #11 attempt_started charge, attempt 1
log orders A-8 for Jo #12 service_provided charge gets payments from bank, a ch35_giving_the_rescue_step_a_service_of_its_own_async.Bank
the bank declines card jo-2024: it has expired
log orders A-8 for Jo #13 step_failed charge: card-expired
log orders A-8 for Jo #14 step_rescuing charge: failure, rescued by charge_rescue
log orders A-8 for Jo #15 attempt_started charge_rescue, attempt 1
log orders A-8 for Jo #16 service_provided charge_rescue gets cards from cards, a ch35_giving_the_rescue_step_a_service_of_its_own_async.Registry, with eu
the card registry in eu says card jo-2024 was replaced by jo-2029
log orders A-8 for Jo #17 step_succeeded charge_rescue
log orders A-8 for Jo #18 contribution_committed card
log orders A-8 for Jo #19 data_overwritten card
log orders A-8 for Jo #20 step_rescued charge by charge_rescue
log orders A-8 for Jo #21 attempt_started charge, attempt 2
the bank charges 30 for order A-8
log orders A-8 for Jo, receipt R-A-8 #22 global_metadata_set receipt
log orders A-8 for Jo, receipt R-A-8 #23 step_succeeded charge
log orders A-8 for Jo, receipt R-A-8 #24 contribution_committed receipt
log orders A-8 for Jo, receipt R-A-8 #25 hook_called audit: on step success of charge
log orders A-8 for Jo, receipt R-A-8 #26 contribution_committed audit
log orders A-8 for Jo, receipt R-A-8 #27 attempt_started ship, attempt 1
log orders A-8 for Jo, receipt R-A-8 #28 input_adapter_supplied ship gets weight from parcels
log orders A-8 for Jo, receipt R-A-8 #29 service_provided ship gets carrier from carriers, a ch35_giving_the_rescue_step_a_service_of_its_own_async.MainCarrier
the main carrier books order A-8
log orders A-8 for Jo, receipt R-A-8 #30 step_info ship: the parcel leaves with receipt R-A-8
log orders A-8 for Jo, receipt R-A-8 #31 step_succeeded ship
log orders A-8 for Jo, receipt R-A-8 #32 contribution_committed tracking
log orders A-8 for Jo, receipt R-A-8 #33 optional_input_absent code
email to Jo: Order A-8 is on its way, tracking T-A-8.
log orders A-8 for Jo, receipt R-A-8 #34 hook_called notify: on workflow success
log orders A-8 for Jo, receipt R-A-8 #35 journey_succeeded
order A-8:
the journey succeeded, with receipt R-A-8 and tracking T-A-8
audit: A-8: charge took 30 on attempt 2, receipt R-A-8
```

## How it works

1. **A request can name its key.** Until now, a step's input and service were found under the name of the parameter, as [chapter 4](https://itinera-dev.org/book/kotlin/04-a-first-step.md) and [chapter 7](https://itinera-dev.org/book/kotlin/07-giving-a-step-the-tools-it-needs.md) showed. `@Service("cards")` asks for the service under the key `cards`, and `@Input("card")` reads the data bag's `card` into the parameter `expired`. The step names what it holds for its own code, and the key says what the journey calls it.
2. **A provider can serve some steps only.** `steps = ["charge_rescue"]` attaches the registry's provider to the rescue step, named by its own name in the workflow, as [chapter 14](https://itinera-dev.org/book/kotlin/14-repairing-then-trying-once-more.md) showed. The bank's provider, [chapter 12](https://itinera-dev.org/book/kotlin/12-trying-again.md)'s, attached to no step, still serves every step that asks for `payments`, which is now the charge alone.
3. **A provider can hand over a client it didn't make.** `cardRegistry` makes the client, and the provider gives it on as it is. `service_provided`, event 16, names the implementation by the object's class, as [chapter 8](https://itinera-dev.org/book/kotlin/08-seeing-what-happened.md) showed: here the library's own class, `Registry`, which no code of the shop names.
4. **A provider can supply data with a service.** `provide(...)` takes the data as a second argument, here the region, and `service_provided` carries it as its `data`. The log now writes the data a provider supplied after the implementation: `with eu`. It says which registry served the rescue step, which the client's class can't. Like everything an event carries, it is for the reporters: the rescue step never sees it.

Jo's card is now renewed by the registry, and only the rescue step can reach it. Jo's order came complete, with its number and its customer's name. But the website has started sending orders before they are finished: one arrives without an order number, another without the customer's name.

## Outcomes

We have shown how a step names the keys of what it asks for, and how a provider serves some steps only, with a client it didn't make and data that says which one it is.

| The usual way                                                                                                              | With Itinera                                                                                                    |
| -------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- |
| A function's parameters often have to be named as its callers name the values, or a layer between them renames everything. | A step names its inputs and services for its own code, and says under which key the journey has each.           |
| A client that a vendor's library builds often hides where it came from, so nothing records which one a request used.       | The provider hands the library's client over as it is, with data saying which one it is, on `service_provided`. |
