tathaastu 0.1.0 copy "tathaastu: ^0.1.0" to clipboard
tathaastu: ^0.1.0 copied to clipboard

Hindu calendar intelligence for Dart and Flutter: panchang, festivals and muhurat, with the rule and confidence behind every answer.

example/tathaastu_example.dart

// A complete tour of the client: create it, call the calendar, call festivals,
// and handle the errors you will actually hit.
//
//   dart run example/tathaastu_example.dart
//
// Runs with no API key: the /v1/demo/ endpoints are public. Set
// TATHAASTU_API_KEY to exercise the keyed endpoints too.

import 'dart:io';

import 'package:tathaastu/tathaastu.dart';

Future<void> main() async {
  // The key is read from TATHAASTU_API_KEY when you do not pass one. On Flutter
  // web there is no environment to read: proxy through your own backend rather
  // than shipping a key in a browser bundle.
  final client = TathaAstu();
  final hasKey = (Platform.environment['TATHAASTU_API_KEY'] ?? '').isNotEmpty;

  try {
    // ── Public, no signup required ──────────────────────────────────────────
    final day = await client.demo.panchangGet(date: '2026-11-08');
    final tithi = (day as Map)['tithi'] as Map;
    stdout
        .writeln('Tithi on 2026-11-08: ${tithi['name']} (${tithi['period']})');

    final festivals = await client.demo.festivalsGet(date: '2026-11-08');
    for (final festival in (festivals as Map)['festivals'] as List) {
      final f = festival as Map;
      stdout.writeln(
        '  ${f['festival_key']}  confidence ${f['confidence']} '
        '(${f['confidence_label']})',
      );
    }

    // ── Keyed endpoints ─────────────────────────────────────────────────────
    if (hasKey) {
      final panchang =
          await client.panchang.get(date: '2026-11-08', locationId: 1);
      stdout
          .writeln('Keyed panchang location: ${(panchang as Map)['location']}');

      // The reason this SDK exists: every festival carries the rule behind it.
      final why = await client.festivals.explain(
        date: '2026-11-08',
        festival: 'FESTIVAL_DIWALI',
      );
      stdout.writeln('Why: ${(why as Map)['human_readable']}');

      stdout.writeln('Rate limit: ${client.rateLimit}');
    } else {
      stdout.writeln('\nSet TATHAASTU_API_KEY to run the keyed examples.');
    }

    // ── Error handling ──────────────────────────────────────────────────────
    // Deliberately call a keyed endpoint the way a misconfigured app would.
    final anonymous = TathaAstu(apiKey: '');
    try {
      await anonymous.panchang.get(date: '2026-11-08', locationId: 1);
    } on AuthenticationException catch (e) {
      // 401: the key is missing or wrong. Check TATHAASTU_API_KEY.
      stdout.writeln('\nAuthenticationException, as expected: ${e.message}');
    } on PlanException catch (e) {
      // 402: the request was fine, your plan does not include the endpoint.
      // Deliberately not an AuthenticationException — the fix is an upgrade.
      stdout.writeln('PlanException: ${e.message}');
    } on ValidationException catch (e) {
      // 400/422: `fields` names what the server rejected.
      stdout.writeln('ValidationException on ${e.fields}: ${e.message}');
    } on RateLimitException catch (e) {
      stdout.writeln('RateLimitException, retry after ${e.retryAfterSeconds}s');
    } on TathaAstuException catch (e) {
      stdout.writeln('TathaAstuException: ${e.message}');
    } finally {
      anonymous.close();
    }
  } finally {
    client.close();
  }
}
0
likes
160
points
17
downloads

Documentation

Documentation
API reference

Publisher

unverified uploader

Weekly Downloads

Hindu calendar intelligence for Dart and Flutter: panchang, festivals and muhurat, with the rule and confidence behind every answer.

Homepage
View/report issues

Topics

#api #calendar #astronomy #hindu #panchang

License

MIT (license)

Dependencies

http

More

Packages that depend on tathaastu