Skip to content

Commit

Permalink
Use a single Response class
Browse files Browse the repository at this point in the history
  • Loading branch information
abraham committed Jan 19, 2023
1 parent abd3d18 commit 85085fd
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 30 deletions.
11 changes: 8 additions & 3 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,21 @@ main() async {
client.token = bearerToken;

try {
var currentAccount = await client.verifyCredentials();
final currentAccount = await client.verifyCredentials();
print('Hello ${currentAccount.username}!');
print('\n');
} on MastodonException catch (e) {
print(e.message);
exit(1);
}

var timeline = await client.timeline(limit: 5);
for (var status in timeline) {
final response = await client.timeline(limit: 5);
for (final model in response.data) {
final status = model.data;
if (status == null) {
print('Error parsing status: ${model.error!.exception}');
continue;
}
print('@${status.account.acct}: ${_stripHtml(status.content)}');
}
}
Expand Down
10 changes: 6 additions & 4 deletions lib/src/endpoints/search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ import 'dart:convert';
import '../authentication.dart';
import '../exception.dart';
import '../model.dart';
import '../model_response.dart';
import '../response.dart';
import '../models/results.dart';
import '../utilities.dart';

typedef SearchResponse = Response<Model<Results>>;

mixin Search on Authentication, Utilities {
/// GET /api/v2/search
///
/// - authenticated
/// - read read:search
Future<ModelResponse<Results>> search(
Future<SearchResponse> search(
String q, {
bool resolve = false,
}) async {
Expand All @@ -27,13 +29,13 @@ mixin Search on Authentication, Utilities {
);

try {
return ModelResponse(
return Response(
Model.success(
Results.fromJson(json.decode(response.body)),
),
);
} on Exception catch (e) {
return ModelResponse(
return Response(
Model.failure(
ModelException(
exception: e,
Expand Down
14 changes: 8 additions & 6 deletions lib/src/endpoints/statuses.dart
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
import 'dart:convert';

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

import '../authentication.dart';
import '../exception.dart';
import '../model.dart';
import '../model_response.dart';
import '../response.dart';
import '../models/account.dart';
import '../models/card.dart';
import '../models/context.dart';
import '../models/status.dart';
import '../models/visibility.dart';
import '../utilities.dart';

typedef StatusResponse = Response<Model<Status>>;

mixin Statuses on Authentication, Utilities {
/// GET /api/v1/statuses/:id
///
/// - public
/// - read read:statuses
Future<ModelResponse<Status>> status(String id) async {
Future<StatusResponse> status(String id) async {
final response = await request(
Method.get,
"/api/v1/statuses/$id",
);

try {
return ModelResponse(
return Response(
Model.success(
Status.fromJson(json.decode(response.body)),
),
);
} on Exception catch (e) {
return ModelResponse(
return Response(
Model.failure(
ModelException(
exception: e,
Expand Down Expand Up @@ -174,7 +176,7 @@ mixin Statuses on Authentication, Utilities {
/// - authenticated
/// - write write:statuses
Future<Status> reblog(String id) async {
late final Response response;
late final http.Response response;

try {
response = await request(
Expand Down
8 changes: 5 additions & 3 deletions lib/src/endpoints/timelines.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import '../exception.dart';
import '../model.dart';
import '../models/conversation.dart';
import '../models/status.dart';
import '../models_response.dart';
import '../response.dart';
import '../utilities.dart';

typedef TimelineResponse = Response<List<Model<Status>>>;

mixin Timelines on Authentication, Utilities {
/// GET /api/v1/timelines/home
///
/// - authenticated
/// - read read:statuses
Future<ModelsResponse<Status>> timeline({
Future<TimelineResponse> timeline({
String? maxId,
String? sinceId,
String? minId,
Expand Down Expand Up @@ -47,7 +49,7 @@ mixin Timelines on Authentication, Utilities {
}),
);

return ModelsResponse(models);
return Response(models);
}

/// GET /api/v1/conversations
Expand Down
7 changes: 0 additions & 7 deletions lib/src/model_response.dart

This file was deleted.

7 changes: 0 additions & 7 deletions lib/src/models_response.dart

This file was deleted.

5 changes: 5 additions & 0 deletions lib/src/response.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Response<D> {
final D data;

Response(this.data);
}

0 comments on commit 85085fd

Please sign in to comment.