Get started

Client basics

Mountaya is the one type that calls the network. Build one per credential, hand it to everything that needs it, and close it when you are done.

Construct a client

import 'package:mountaya_client/mountaya_client.dart';

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),
  ),
  // Answers come in French where the API has French.
  language: 'fr',
);
await mountaya.close();

Whoever made the client closes it. In a Flutter app, the State that makes it closes it in dispose.

Call the three APIs

Each method is named by the API's operation id, and hangs where the API files it:

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

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)),
);

// Map Studio API, under studio and agent.
final itinerary = await mountaya.studio.itineraries.getItinerary(
  itinerary_id: '0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21',
);
print(itinerary.data.name);

// Data API, under data: the operation's own result.
final places = await mountaya.data.geocode(
  input: data_schema.GeocodeInput(query: 'Chamonix'),
);
print(places.places?.firstOrNull?.name);

// Tile API, under tiles.
final layers = await mountaya.tiles.getTileLayers();
print(layers.data.terrains.length);
await mountaya.close();

Path parameters are named as the API names them, itinerary_id:. A query is one query: value, and a body one body: value.

Write

A body takes its fields as named parameters:

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)),
);

final renamed = await mountaya.studio.itineraries.patchItinerary(
  itinerary_id: '0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21',
  body: rest_schema.ItineraryUpdateParams(name: 'Tour du Mont-Blanc, summer'),
);
print('${renamed.status} ${renamed.data.name}');
await mountaya.close();

A write acts as the member its token was minted for, as Authentication in the SDK shows.

Ask for fewer fields

A Data API call narrows its answer to the fields select: names, true selecting a field whole:

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

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)),
);
final places = await mountaya.data.geocode(
  input: data_schema.GeocodeInput(query: 'Chamonix'),
  select: {
    'places': {'name': true, 'point': true},
  },
);
print(places.places?.firstOrNull?.point);
await mountaya.close();

The result type comes back whole: a field you did not select is null.

Stream events

A stream method answers an EventStream, a Stream of frames you read with await for:

import 'package:mountaya_client/mountaya_client.dart';

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)),
);

// The itinerary's collaboration events: presence, locks and changes.
await for (final frame in mountaya.studio.collab.getItineraryEvents(
  itinerary_id: '0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21',
)) {
  print(frame.event.toJson()['type']);
}
await mountaya.close();

The stream reopens on its own. A break out of the loop closes the stream.

Read the response

A REST or Tile call answers a Response: its data, the status, the headers and the request id.

import 'package:mountaya_client/mountaya_client.dart';

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

final mountaya = Mountaya(credential: .publishable(key: publishableKey));
final itinerary = await mountaya.studio.itineraries.getItinerary(
  itinerary_id: '0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21',
);
print('${itinerary.status} ${itinerary.requestId} ${itinerary.data.etag}');
print(itinerary.header('Content-Language'));
await mountaya.close();

Quote requestId to support: it is the id the API recorded the request under.

Next steps

Last updated