Skip to content

Commit

Permalink
feat: fixed for the issue (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
myConsciousness committed Sep 20, 2022
1 parent 122e844 commit 75de92d
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 167 deletions.
27 changes: 16 additions & 11 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Release Note

## v3.3.0
## v4.0.0

### New Feature

Expand All @@ -12,7 +12,9 @@
- `connectVolumeStream`
- `connectFilteredStream`

And now the responses from the above 2 methods can be used as follows:
- Also, methods that previously returned the result of processing as a `bool` have been modified to return a `TwitterResponse`. This is to have a `RateLimit` object as above. So, for example, the `createLike` method can also obtain a `RateLimit` object, and the `bool` value of the processing result can be obtained from the TwitterResponse's `data` field.

And now the responses from the modified methods can be used as follows:

```dart
import 'package:twitter_api_v2/twitter_api_v2.dart' as v2;
Expand All @@ -21,12 +23,12 @@ Future<void> main() async {
final twitter = v2.TwitterApi(bearerToken: 'YOUR_TOKEN_HERE');
try {
final response = await twitter.tweetsService.connectVolumeStream();
final response = await twitter.tweetsService.connectFilteredStream();
print(response.rateLimit);
final stream = response.stream;
await for (final response in stream.handleError(print)) {
print(response);
await for (final event in stream.handleError(print)) {
print(event);
}
} on v2.TwitterException catch (e) {
print(e);
Expand All @@ -41,13 +43,16 @@ Future<void> main() async {
final twitter = v2.TwitterApi(bearerToken: 'YOUR_TOKEN_HERE');
try {
final response = await twitter.tweetsService.connectFilteredStream();
print(response.rateLimit);
final me = await twitter.usersService.lookupMe();
final tweets = await twitter.tweetsService.searchRecent(query: '#ElonMusk');
final stream = response.stream;
await for (final response in stream.handleError(print)) {
print(response);
}
final response = await twitter.tweetsService.createLike(
userId: me.data.id,
tweetId: tweets.data.first.id,
);
print(response.rateLimit);
print(response.data); // true or false
} on v2.TwitterException catch (e) {
print(e);
}
Expand Down
14 changes: 10 additions & 4 deletions lib/src/service/base_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ abstract class _Service {
M Function(Map<String, Object?> json)? metaBuilder,
});

bool evaluateResponse(final http.Response response);
TwitterResponse<bool, void> evaluateResponse(final http.Response response);
}

abstract class BaseService implements _Service {
Expand Down Expand Up @@ -195,9 +195,15 @@ abstract class BaseService implements _Service {
}

@override
bool evaluateResponse(final http.Response response) => !core
.tryJsonDecode(response, response.body)
.containsKey(ResponseField.errors.value);
TwitterResponse<bool, void> evaluateResponse(final http.Response response) =>
TwitterResponse(
rateLimit: RateLimit.fromJson(
headerConverter.convert(response.headers),
),
data: !core
.tryJsonDecode(response, response.body)
.containsKey(ResponseField.errors.value),
);

Map<String, dynamic> _checkResponseBody(final http.Response response) {
final jsonBody = core.tryJsonDecode(response, response.body);
Expand Down
43 changes: 24 additions & 19 deletions lib/src/service/lists/lists_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ abstract class ListsService {
/// ## Reference
///
/// - https://developer.twitter.com/en/docs/twitter-api/lists/pinned-lists/api-reference/post-users-id-pinned-lists
Future<bool> createPinnedList(
Future<TwitterResponse<bool, void>> createPinnedList(
{required String userId, required String listId});

/// Enables the authenticated user to unpin a List.
Expand Down Expand Up @@ -235,7 +235,7 @@ abstract class ListsService {
/// ## Reference
///
/// - https://developer.twitter.com/en/docs/twitter-api/lists/pinned-lists/api-reference/delete-users-id-pinned-lists-list_id
Future<bool> destroyPinnedList(
Future<TwitterResponse<bool, void>> destroyPinnedList(
{required String userId, required String listId});

/// Returns the Lists pinned by a specified user.
Expand Down Expand Up @@ -473,7 +473,7 @@ abstract class ListsService {
/// ## Reference
///
/// - https://developer.twitter.com/en/docs/twitter-api/lists/manage-lists/api-reference/delete-lists-id
Future<bool> destroyList({required String listId});
Future<TwitterResponse<bool, void>> destroyList({required String listId});

/// Enables the authenticated user to update the meta data of a specified List
/// that they own as a public scope.
Expand Down Expand Up @@ -509,7 +509,7 @@ abstract class ListsService {
/// ## Reference
///
/// - https://developer.twitter.com/en/docs/twitter-api/lists/manage-lists/api-reference/delete-lists-id
Future<bool> updateListAsPublic(
Future<TwitterResponse<bool, void>> updateListAsPublic(
{required String listId, String? name, String? description});

/// Enables the authenticated user to update the meta data of a specified List
Expand Down Expand Up @@ -546,7 +546,7 @@ abstract class ListsService {
/// ## Reference
///
/// - https://developer.twitter.com/en/docs/twitter-api/lists/manage-lists/api-reference/delete-lists-id
Future<bool> updateListAsPrivate(
Future<TwitterResponse<bool, void>> updateListAsPrivate(
{required String listId, String? name, String? description});

/// Enables the authenticated user to follow a List.
Expand Down Expand Up @@ -583,7 +583,8 @@ abstract class ListsService {
/// ## Reference
///
/// - https://developer.twitter.com/en/docs/twitter-api/lists/list-follows/api-reference/post-users-id-followed-lists
Future<bool> createFollow({required String userId, required String listId});
Future<TwitterResponse<bool, void>> createFollow(
{required String userId, required String listId});

/// Enables the authenticated user to unfollow a List.
///
Expand Down Expand Up @@ -620,7 +621,8 @@ abstract class ListsService {
/// ## Reference
///
/// - https://developer.twitter.com/en/docs/twitter-api/lists/list-follows/api-reference/delete-users-id-followed-lists-list_id
Future<bool> destroyFollow({required String userId, required String listId});
Future<TwitterResponse<bool, void>> destroyFollow(
{required String userId, required String listId});

/// Returns a list of users who are followers of the specified List.
///
Expand Down Expand Up @@ -795,7 +797,8 @@ abstract class ListsService {
/// ## Reference
///
/// - https://developer.twitter.com/en/docs/twitter-api/lists/list-members/api-reference/post-lists-id-members
Future<bool> createMember({required String listId, required String userId});
Future<TwitterResponse<bool, void>> createMember(
{required String listId, required String userId});

/// Enables the authenticated user to remove a member from a List they own.
///
Expand Down Expand Up @@ -829,7 +832,8 @@ abstract class ListsService {
/// ## Reference
///
/// - https://developer.twitter.com/en/docs/twitter-api/lists/list-members/api-reference/delete-lists-id-members-user_id
Future<bool> destroyMember({required String listId, required String userId});
Future<TwitterResponse<bool, void>> destroyMember(
{required String listId, required String userId});

/// Returns a list of users who are members of the specified List.
///
Expand Down Expand Up @@ -1034,7 +1038,7 @@ class _ListsService extends BaseService implements ListsService {
);

@override
Future<bool> createPinnedList({
Future<TwitterResponse<bool, void>> createPinnedList({
required String userId,
required String listId,
}) async =>
Expand All @@ -1047,7 +1051,7 @@ class _ListsService extends BaseService implements ListsService {
);

@override
Future<bool> destroyPinnedList({
Future<TwitterResponse<bool, void>> destroyPinnedList({
required String userId,
required String listId,
}) async =>
Expand Down Expand Up @@ -1127,7 +1131,8 @@ class _ListsService extends BaseService implements ListsService {
);

@override
Future<bool> destroyList({required String listId}) async =>
Future<TwitterResponse<bool, void>> destroyList(
{required String listId}) async =>
super.evaluateResponse(
await super.delete(
core.UserContext.oauth2OrOAuth1,
Expand All @@ -1136,7 +1141,7 @@ class _ListsService extends BaseService implements ListsService {
);

@override
Future<bool> updateListAsPublic({
Future<TwitterResponse<bool, void>> updateListAsPublic({
required String listId,
String? name,
String? description,
Expand All @@ -1149,7 +1154,7 @@ class _ListsService extends BaseService implements ListsService {
);

@override
Future<bool> updateListAsPrivate({
Future<TwitterResponse<bool, void>> updateListAsPrivate({
required String listId,
String? name,
String? description,
Expand All @@ -1162,7 +1167,7 @@ class _ListsService extends BaseService implements ListsService {
);

@override
Future<bool> createFollow({
Future<TwitterResponse<bool, void>> createFollow({
required String userId,
required String listId,
}) async =>
Expand All @@ -1177,7 +1182,7 @@ class _ListsService extends BaseService implements ListsService {
);

@override
Future<bool> destroyFollow({
Future<TwitterResponse<bool, void>> destroyFollow({
required String userId,
required String listId,
}) async =>
Expand Down Expand Up @@ -1237,7 +1242,7 @@ class _ListsService extends BaseService implements ListsService {
);

@override
Future<bool> createMember({
Future<TwitterResponse<bool, void>> createMember({
required String listId,
required String userId,
}) async =>
Expand All @@ -1252,7 +1257,7 @@ class _ListsService extends BaseService implements ListsService {
);

@override
Future<bool> destroyMember({
Future<TwitterResponse<bool, void>> destroyMember({
required String listId,
required String userId,
}) async =>
Expand Down Expand Up @@ -1331,7 +1336,7 @@ class _ListsService extends BaseService implements ListsService {
dataBuilder: ListData.fromJson,
);

Future<bool> _updateList({
Future<TwitterResponse<bool, void>> _updateList({
required String listId,
String? name,
String? description,
Expand Down
Loading

0 comments on commit 75de92d

Please sign in to comment.