callFunction method

  1. @override
Future<HttpResponse<Either<List<JSON>, JSON>>> callFunction(
  1. {required String function,
  2. required String token,
  3. required JSON body,
  4. bool redact = false}
)
override

Calls the specified function on the Moodle-API with the specified token.

Note: The token must have the required permissions use the web service that is providing the function you are trying to call.

If redact is set to true, the response/request body will not be logged.

Implementation

@override
Future<HttpResponse<Either<List<JSON>, JSON>>> callFunction(
    {required String function,
    required String token,
    required JSON body,
    bool redact = false}) async {
  log.info(
      "Calling function $function ${redact ? "with [redacted body]" : "with body $body"}");

  body["wstoken"] = token;
  body["moodlewsrestformat"] = "json";
  body["wsfunction"] = function;

  var response = await networkService.post(
      "${config.kMoodleServerAdress}/webservice/rest/server.php",
      body: body);

  if (response.statusCode == 200) {
    log.info(
        "Function $function returned ${redact ? "[redacted body]" : "body ${response.body}"}");

    final responseTemplate = HttpResponse<Either<List<JSON>, JSON>>(
        statusCode: response.statusCode, body: null);

    if (response.body is List) {
      // convert List<dynamic> to List<JSON>
      final List<JSON> jsonList = (response.body as List<dynamic>)
          .map((dynamic e) => e as JSON)
          .toList();

      return responseTemplate.copyWith(
        body: Left(jsonList),
      );
    }

    return responseTemplate.copyWith(
      body: Right(response.body),
    );
  }

  log.warning(
      "Error calling function $function: ${response.statusCode} ${response.body}");

  return HttpResponse(
    statusCode: response.statusCode,
    body: Right(
      response.body,
    ),
  );
}