mastodon_dart icon indicating copy to clipboard operation
mastodon_dart copied to clipboard

Endpoint response type with partial parsing

Open abraham opened this issue 2 years ago • 4 comments

Initial take on #55 with the timeline, status, and search endpoints updated.

New Response class that contains instances of Result as a single result or multiple results which each have a model or exception.

One upside of this is the new response objects can start surfacing rate limit details. One downside is the searchend point is considered having single model response and if any one of them failed to parse none of the other results would be available.

  final client = Mastodon(website);
  client.token = bearerToken;

  final statusResponse = await client.status(statusId);
  final status = statusResponse.result.model;
  if (status is Status) {
    print('status: ${status.uri}');
  } else {
    print(statusResponse.result.error!.exception);
  }

  final timelineResponse = await client.timeline(limit: 40);
  for (final result in timelineResponse.results) {
    if (result.model is Status) {
      print('timeline: ${result.model!.uri}');
    } else {
      print(result.error!.exception);
    }
  }

  final searchResponse = await client.search('apple');
  final result = searchResponse.result.model;
  if (result is Results) {
    for (final account in result.accounts) {
      print('search: ${account.url}');
    }
  } else {
    print(searchResponse.result.error!.exception);
  }

Fixes #55

abraham avatar Jan 14 '23 19:01 abraham

Is there a reason to have ModelsResponse<T> instead of using ModelResponse<List<T>>?

lukepighetti avatar Jan 17 '23 14:01 lukepighetti

I don't think there was any particular reason to have two Response classes other than to have .singular and .plural getters but I don't think there isn't a reason not to have a single method where one of those is an alias of the other.

abraham avatar Jan 19 '23 03:01 abraham

This looks good to me

lukepighetti avatar Jan 19 '23 12:01 lukepighetti

I've got the implementation where it's now easy to upgrade endpoints to Responses. I'm going to integrate it into my app to validate the changes work well before finishing up the work.

abraham avatar Jan 21 '23 21:01 abraham