atproto 1.0.0-preview.4 copy "atproto: ^1.0.0-preview.4" to clipboard
atproto: ^1.0.0-preview.4 copied to clipboard

The most famous and powerful Dart/Flutter library for AT Protocol.

example/example.dart

// Copyright 2023 Shinya Kato. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided the conditions.

import 'package:atproto/atproto.dart';
import 'package:atproto/com_atproto_server_create_session.dart';
import 'package:atproto/core.dart';

/// https://atprotodart.com/docs/packages/atproto
Future<void> main() async {
  try {
    //! First you need to establish session with ATP server.
    final session = await createSession(
      $service: 'SERVICE_NAME', //! The default is `bsky.social`
      identifier: 'YOUR_HANDLE_OR_EMAIL', //! Like `shinyakato.bsky.social`
      password: 'YOUR_PASSWORD',
    );

    print(session);

    final atproto = ATProto.fromSession(
      session.data.toSession(),

      //! The default is `bsky.social`, or resolve dynamically based on session
      service: 'SERVICE_NAME',

      //! The default is `bsky.network`
      relayService: 'STREAM_SERVICE_NAME',

      //! Automatic retry is available when server error or network error occurs
      //! when communicating with the API.
      retryConfig: RetryConfig(
        maxAttempts: 5,
        jitter: Jitter(
          minInSeconds: 2,
          maxInSeconds: 5,
        ),
        onExecute: (event) => print(
          'Retry after ${event.intervalInSeconds} seconds...'
          '[${event.retryCount} times]',
        ),
      ),

      //! The default timeout is 30 seconds.
      timeout: Duration(seconds: 20),
    );

    //! Create a record to specific service.
    final record = await atproto.repo.createRecord(
      repo: atproto.session!.did,
      collection: NSID.create(
        'feed.bsky.app',
        'post',
      ),
      record: {
        'text': 'Hello, Bluesky!',
        "createdAt": DateTime.now().toUtc().toIso8601String(),
      },
    );

    //! And delete it.
    await atproto.repo.deleteRecord(
      repo: record.data.uri.hostname,
      collection: record.data.uri.collection,
      rkey: record.data.uri.rkey,
    );

    //! You can use Stream API easily.
    final subscription = await atproto.sync.subscribeRepos();
    subscription.data.stream.listen((event) {
      event.when(
        commit: print,
        identity: print,
        account: print,
        info: print,
        unknown: print,
      );
    });
  } on UnauthorizedException catch (e) {
    print(e);
  } on XRPCException catch (e) {
    print(e);
  }
}
12
likes
140
pub points
60%
popularity
screenshot

Publisher

verified publisheratprotodart.com

The most famous and powerful Dart/Flutter library for AT Protocol.

Homepage
Repository (GitHub)
View/report issues
Contributing

Topics

#atproto #bluesky #api

Documentation

Documentation
API reference

Funding

Consider supporting this project:

github.com

License

BSD-3-Clause (license)

Dependencies

atproto_core, freezed_annotation, json_annotation

More

Packages that depend on atproto