listSubscriptionEvents method

Future<List<SubscriptionEvent>> listSubscriptionEvents({
  1. required int subscriptionId,
  2. String? cursor,
  3. int? limit,
  4. String? authToken,
})

Lists all events for a specific subscription.

In the current implementation, only START_SUBSCRIPTION and STOP_SUBSCRIPTION (when the subscription was canceled) events are returned.

Implementation

Future<List<SubscriptionEvent>> listSubscriptionEvents({
  required int subscriptionId,
  String? cursor,
  int? limit,
  String? authToken,
}) async {

  limit ??= 200;
  authToken ??= authenticationService.getCachedToken()?.accessToken;

  Map<String, String> headers = {
    "Authorization": "Bearer ${authToken ?? ""}",
    'Content-Type': 'application/json; charset=UTF-8',
    'Accept': 'application/json',

  };

  Map<String, dynamic> params = {
    "limit": limit
  };

  if (cursor != null) {
    params.putIfAbsent("cursor", () => cursor);
  }


  Uri endpoint = Uri.https(
      baseUrl, "/v2/subscriptions/$subscriptionId/events", params);

  //print (endpoint.toString());

  var response = await
  http.get(endpoint, headers: headers);

  if (response.statusCode == 200) {
    print (jsonDecode(response.body));
    return SubscriptionEventResponse.fromJson(jsonDecode(response.body)).subscriptionEvents!;
  }
  else {
    print (response.body);
    throw SubscriptionException(statusCode: response.statusCode, message: SubscriptionEventResponse.fromJson(jsonDecode(response.body)).errors?[0].detail?.toString());
  }
}