Get started
Kotlin SDK
Fetch data

Pagination and safe writes

Pagination and safe writes

A listing reads as a Flow of rows, a page at a time. A write lands once, and can refuse to overwrite a change it did not see.

Read a whole listing

Every listing that pages has a walk beside it, walk before the operation's name. It takes the method's own filters and sort:

import com.mountaya.client.Mountaya
import com.mountaya.client.generated.RestSchema

suspend fun everyItinerary(mountaya: Mountaya) {
    mountaya.studio.itineraries
        .walkGetItineraries(pageSize = 50, maxPages = 10) { sort = RestSchema.GetItinerariesSort.UpdatedAtDescending }
        .collect { itinerary -> println(itinerary.name) }
}

The walk is cold: a page is asked for only as the rows before it are read, and a collector that stops early asks for no more. PageWalk walks any listing the same way, through its method.

Retry a write safely

Every write sends an Idempotency-Key on every attempt, so a repeated write lands once. Generate your own and keep it, so a retry of your own lands once too:

import com.mountaya.client.ConnectionException
import com.mountaya.client.Mountaya
import com.mountaya.client.generated.RestSchema
import kotlin.uuid.Uuid

suspend fun createTour(mountaya: Mountaya) {
    // Generated once for this intent, and kept: store it beside the pending write.
    val key = Uuid.random().toString()
    val body =
        RestSchema.ItineraryCreateParams(
            // The hiking-and-trail activity.
            activity_id = "a1c4e7b2-3d59-4f08-8b6a-2e5c9d0f7134",
            is_private = true,
            name = "Tour du Mont-Blanc",
        )
    val created =
        try {
            mountaya.studio.itineraries.postItinerary(body) { idempotencyKey = key }
        } catch (lost: ConnectionException) {
            // The same key again: the API answers the first write, never a second itinerary.
            mountaya.studio.itineraries.postItinerary(body) { idempotencyKey = key }
        }
    println("${created.status} ${created.location}")
}

Write only if nothing changed

Send the tag you read the itinerary under. The write is refused if someone changed it since:

import com.mountaya.client.Mountaya
import com.mountaya.client.PreconditionFailedException
import com.mountaya.client.generated.RestSchema

suspend fun renameIfUnchanged(mountaya: Mountaya) {
    val id = "0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21"
    val read = mountaya.studio.itineraries.getItinerary(id)
    try {
        mountaya.studio.itineraries.patchItinerary(id, body = RestSchema.ItineraryUpdateParams { name = "Tour du Mont-Blanc, summer" }) {
            ifMatch = read.data.etag
        }
    } catch (stale: PreconditionFailedException) {
        // Changed since it was read: read it again, and let the person decide.
        val now = mountaya.studio.itineraries.getItinerary(id)
        println("Now named ${now.data.name}")
    }
}

Next steps

Last updated