import 'dart:convert';
import 'dart:developer';

import 'package:http/http.dart' as http;
import '../apiError.dart';
import '../apiRequest.dart';

abstract class MhslApi<T> extends ApiRequest {
  String subpath;
  MhslApi(this.subpath);

  http.Response? response;

  Future<http.Response>? request(Uri uri);
  T assemble(String raw);

  Future<T> run() async {
    Uri endpoint = Uri.parse("https://mhsl.eu/marianum/marianummobile/$subpath");

    http.Response? data = await request(endpoint);
    if(data == null) {
      throw ApiError("Request could not be dispatched!");
    }

    if(data.statusCode > 299) {
      log("Non 200 Status code from mhsl services: $subpath: ${data.statusCode}");
    }

    return assemble(utf8.decode(data.bodyBytes));
  }
}