add address lookup for pinned tweet

This commit is contained in:
Serhii
2023-02-09 12:23:13 +02:00
parent 709c031f28
commit 624036d00c
3 changed files with 25 additions and 7 deletions
+12 -2
View File
@@ -10,7 +10,7 @@ class TwitterApi {
static const userPath = '/2/users/by/username/';
static Future<TwitterUser> lookupUserByName({required String userName}) async {
final queryParams = {'user.fields': 'description'};
final queryParams = {'user.fields': 'description', 'expansions': 'pinned_tweet_id'};
final headers = {'authorization': 'Bearer $twitterBearerToken'};
@@ -32,6 +32,16 @@ class TwitterApi {
throw Exception(responseJSON['errors'][0]['detail']);
}
return TwitterUser.fromJson(responseJSON['data'] as Map<String, dynamic>);
final user = responseJSON['data'] as Map<String, dynamic>;
try {
if (responseJSON['includes'] != null) {
user['pinnedTweet'] = responseJSON['includes']['tweets'][0]['text'];
}
} catch (e) {
print('responseJSON[includes][tweets][0][text] $e');
}
return TwitterUser.fromJson(user);
}
}
+5 -2
View File
@@ -1,16 +1,19 @@
class TwitterUser {
TwitterUser({required this.id, required this.username, required this.name, this.description});
TwitterUser({required this.id, required this.username, required this.name, this.description,
this.pinnedTweet});
final String id;
final String username;
final String name;
final String? description;
final String? pinnedTweet;
factory TwitterUser.fromJson(Map<String, dynamic> json) {
return TwitterUser(
id: json['id'] as String,
username: json['username'] as String,
name: json['name'] as String,
description: json['description'] as String?);
description: json['description'] as String?,
pinnedTweet: json['pinnedTweet'] as String?);
}
}