Git Product home page Git Product logo

scolengo-api-dart's Introduction

scolengo-api-dart

Unofficial Dart API Wrapper for the Skolengo education management system. Heavily inspired from scolengo-api

Since I don't have access to the entire API, some features or fields may be missing. Feel free to contribute.

Example:

This should work in the command line.

If you get stuck after the authorization
  • Add urlLancher: (url) => print(url) to the Authenticator

  • Paste the given URL in a browser that already has devtools open on the networking page

  • Copy the URL of the 2nd request ( sko-app://sign-in-callback?code=... )

  • Open a new browser tab and paste the url, replacing sko-app:// with localhost:3000/

  • It should work.

  • We can't provide localhost:300 as redirectUri because then we get an error from the CAS

import 'dart:convert';
import 'dart:io';

import 'package:openid_client/openid_client_io.dart';
import 'package:scolengo_api/src/models/School/school.dart';
import 'package:scolengo_api/src/skolengo.dart';

void main() async {
  final Credential credentials;
  final School school;

  final client = Skolengo.unauthenticated();
  final schools = await client.searchSchool('Lycée ...');
  school = schools.data[0];
  credentials = await createCredentials(school);

  //It's a good idea to save the credentials and school as JSON here
 
  final client = Skolengo.fromCredentials(credentials, school);

  //You should be able to use the client now
}

Future<Credential> createCredentials(School school, [Skolengo? client]) async {
  client ??= Skolengo.unauthenticated();
  final oidclient = await client.getOIDClient(school);
  final f = Flow.implicit(oidclient);
  final authenticator = Authenticator(
    oidclient,
    redirectUri: Uri.parse('skoapp-prod://sign-in-callback'),
  );
  return authenticator.authorize();
}

If you want to use this in a flutter app, change the createCredentials function according to the openid_client documentation

Caching the API responses

This library comes with a pretty flexible interface for you to be able to implement your own cache provider. As some requests take quite a long time to complete, it might be a good idea to cache the result, in memory, a JSON file, a db or anything else really For more details look at the comments in cache_provider.dart

Example

A cache provider storing the responses in JSON files

This is not a great idea, the example isn't complete and it was written in a rush but this should give a rough idea of how things should be structured.

class FSCacheProvider extends CacheProvider {
  Map<String, String> _index = {};

  @override
  Future<String> get(String key) async {
    return await File(filename(key)).readAsString();
  }

  @override
  bool raw() => false;

  @override
  void set(String key, String value) {
    _index[filename(key)] = DateTime.now().toIso8601String();
    File('./cache/index.json').createSync(recursive: true);
    File('./cache/index.json').writeAsString(jsonEncode(_index));
    File(filename(key))
      ..createSync(recursive: true)
      ..writeAsString(value);
  }

  @override
  Future<bool> shouldUseCache(String key) async {
    //TODO compare dates
    return _index.keys.contains(filename(key));
  }

  String filename(String url) {
    return './cache${Uri.parse(url).path}/${Uri.parse(url).query}.json';
  }

  init() {
    _index = File('./cache/index.json').existsSync()
        ? jsonDecode(File('./cache/index.json').readAsStringSync())
            .map<String, String>(
                (key, value) => MapEntry(key as String, value as String))
        : {};
    File('.cache/index.json').createSync(recursive: true);
  }
}

scolengo-api-dart's People

Contributors

lolocomotive avatar maelgangloff avatar vinceh121 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

vinceh121

scolengo-api-dart's Issues

Test some functions

Some functions related to evaluations and absences don't work fully when the Pronote connector is active which makes them impossible to test for me.
There are Todo comments above the definition of those functions. The parsing and data structures of those functions aren't complete either.

If someone could test this for me it'd be great

To test:

  • getEvaluation
  • getPeriodicReportsFiles
  • getAbsenceFile
  • getAbsenceReasons

Use streams for responses

Using streams instead of futures would allow to return the cached value and then the actual value using only one call, simplifying cache use.
This has the side effect of breaking backwards compatibility so there will probably be a major version change

Add authentication

  • Choose between flutter_appauth and openid_client
  • Get tokenset
  • Refresh token

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.