lichess_client 0.0.0 copy "lichess_client: ^0.0.0" to clipboard
lichess_client: ^0.0.0 copied to clipboard

Lichess client for Dart.

Lichess API Wrapper for Dart #

Pub Version

Lichess Client is a Dart library designed to simplify interaction with the Lichess API for Flutter and Dart. It exposes a collection of models and a extendable client interface and implementation.

Installation #

dependencies:
  lichess_client: ^<latest-version>

Import:

import 'package:lichess_client/lichess_client.dart';

Usage #

The usage is pretty straightforward:

final lichess = LichessClient.create();
final user = await lichess.getUserPublicData(username: 'alexrintt');

// If you wanna make authenticated requests:
final lichess = LichessClient.create(accessToken: '<your-access-token>');
final email = await lichess.getMyEmailAddress();

Custom Dio instance #

By default, this package uses fresh Dio instance for handling HTTP requests, if you want to provide a custom instance, use dio argument:

final myDioInstance = Dio();
final lichess = LichessClient.create(dio: myDioInstance);

Custom client #

LichessClient is just an abstract class, you can always extend or implement it.

class MyCustomLichessClient implements LichessClient {
  // TODO: Implement/override methods.
}

Now you can do your own implementation and re-use the data models.

This can also be used to mock the LichessClient class.

Retrieve access token #

TL;DR: This package doesn't handle authentication for Lichess.

Why?

It may not be ideal for a package to handle user authentication due to the following reasons:

  1. Security: Handling user authentication in a package can pose potential security risks. The package would require to store Lichess API tokens locally. If mishandled or not handled securely, it could result in unauthorized access to user accounts or other security issues.

  2. Platform-specific concerns: If the package handles user authentication, it can have platform-specific implications. Different platforms may have different ways of handling sensitive user data, such as storing tokens in different locations or using different encryption methods. By leaving authentication up to users, the package can remain platform-independent and avoid these platform-specific concerns.

  3. Flexibility: Lichess API tokens can be generated with different levels of access and permissions, and different users may require different levels of access depending on their use case. By allowing users to handle authentication themselves, the package gives them the flexibility to generate and use tokens with the necessary permissions for their specific use case.

  4. Complexity: Handling user authentication can add additional complexity to the package and increase the likelihood of bugs or errors. By allowing users to handle authentication themselves, the package can focus on providing a clean and easy-to-use API for interacting with the Lichess API.

  5. Best practice: Leaving authentication up to the user is generally considered best practice in API design. By following this best practice, the package can ensure that its design is in line with established industry standards.

So it is generally considered best practice to leave authentication up to the user and avoid potential security risks, platform-specific concerns, and unnecessary complexity in the package's design.

That said, to get an access token for your platform and for your use-case refer to the Lichess authentication section.

Contributing #

These steps are required to setup your local environment for development.

Base environment #

This package is based on Dart:

Dart SDK version: 2.19.2 (stable) (Tue Feb 7 18:37:17 2023 +0000) on "windows_x64"

Getting started #

Warning Before starting, remember to put your personal token in a file called .env in the project root; use .env.example as template, you may already know, but never let this token leak, and if it does, revoke it immediately.

This package uses code generation from retrofit and freezed package, so first off run:

# Get deps.
dart pub get

# Start generating code and watch for code changes.
dart pub run build_runner watch --delete-conflicting-outputs

Now, when there are no more errors due missing generated code, run the example:

dart bin/lichess_client.dart