Get started
Dart and Flutter SDK
Fetch data

Pagination and safe writes

Pagination and safe writes

A listing is read a page at a time. A write carries a key that makes repeating it safe, and a tag that refuses it once the resource has changed.

Read a whole listing

Beside each listing stands its walk, walk and the operation's name. It answers a Stream that asks for each page as you read the rows before it:

import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_client/rest_schema.dart' as rest_schema;

const publishableKey = String.fromEnvironment('MOUNTAYA_PUBLISHABLE_KEY');
const sessionToken = String.fromEnvironment('MOUNTAYA_SESSION_TOKEN');

final mountaya = Mountaya(
  credential: .publishable(key: publishableKey, sessionToken: const .value(sessionToken)),
);
await for (final itinerary in mountaya.studio.itineraries.walkGetItineraries(
  query: rest_schema.GetItinerariesQuery(
    sort: rest_schema.GetItinerariesSort.updatedAtDescending,
  ),
  pageSize: 50,
  maxPages: 10,
)) {
  print(itinerary.name);
}
await mountaya.close();

PageWalk is the same walk over any listing whose page method you call yourself.

Retry a write safely

A write sends one Idempotency-Key on every attempt the client makes. Keep your own key with the write, so a retry of yours lands once too:

import 'dart:math';

import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_client/rest_schema.dart' as rest_schema;

const publishableKey = String.fromEnvironment('MOUNTAYA_PUBLISHABLE_KEY');
const sessionToken = String.fromEnvironment('MOUNTAYA_SESSION_TOKEN');

final mountaya = Mountaya(
  credential: .publishable(key: publishableKey, sessionToken: const .value(sessionToken)),
);
// Drawn once, and kept with the write until it lands.
final random = Random.secure();
final key = [
  for (var byte = 0; byte < 16; byte += 1)
    random.nextInt(256).toRadixString(16).padLeft(2, '0'),
].join();
Future<Response<rest_schema.ItineraryWithDetails, Object?>> create() =>
    mountaya.studio.itineraries.postItinerary(
      body: rest_schema.ItineraryCreateParams(
        activity_id: 'a1c4e7b2-3d59-4f08-8b6a-2e5c9d0f7134',
        is_private: true,
        name: 'Tour du Mont-Blanc',
      ),
      idempotencyKey: key,
    );
try {
  print((await create()).data.id);
} on ConnectionException {
  // No answer came: send it again under the same key, and it is made once.
  print((await create()).data.id);
}
await mountaya.close();

Write only if nothing changed

Pass the tag you read the resource under. A write over a newer version is refused:

import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_client/rest_schema.dart' as rest_schema;

const publishableKey = String.fromEnvironment('MOUNTAYA_PUBLISHABLE_KEY');
const sessionToken = String.fromEnvironment('MOUNTAYA_SESSION_TOKEN');

final mountaya = Mountaya(
  credential: .publishable(key: publishableKey, sessionToken: const .value(sessionToken)),
);
const itineraryId = '0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21';
var read = await mountaya.studio.itineraries.getItinerary(itinerary_id: itineraryId);
try {
  await mountaya.studio.itineraries.patchItinerary(
    itinerary_id: itineraryId,
    body: rest_schema.ItineraryUpdateParams(name: 'Tour du Mont-Blanc, summer'),
    ifMatch: read.data.etag,
  );
} on PreconditionFailedException {
  // Someone changed it first: read it again, then decide.
  read = await mountaya.studio.itineraries.getItinerary(itinerary_id: itineraryId);
}
await mountaya.close();

Next steps

Last updated