2023-01-24 18:24:46 +00:00
|
|
|
import 'dart:convert';
|
2023-10-17 18:55:54 +00:00
|
|
|
|
|
|
|
import 'package:cake_wallet/.secrets.g.dart' as secrets;
|
2023-01-24 18:24:46 +00:00
|
|
|
import 'package:cake_wallet/twitter/twitter_user.dart';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
|
|
|
|
class TwitterApi {
|
|
|
|
static const twitterBearerToken = secrets.twitterBearerToken;
|
|
|
|
static const httpsScheme = 'https';
|
|
|
|
static const apiHost = 'api.twitter.com';
|
|
|
|
static const userPath = '/2/users/by/username/';
|
|
|
|
|
|
|
|
static Future<TwitterUser> lookupUserByName({required String userName}) async {
|
2023-10-17 18:55:54 +00:00
|
|
|
final queryParams = {
|
2024-03-01 19:38:48 +00:00
|
|
|
'user.fields': 'description,profile_image_url',
|
2023-10-17 18:55:54 +00:00
|
|
|
'expansions': 'pinned_tweet_id',
|
|
|
|
'tweet.fields': 'note_tweet'
|
|
|
|
};
|
2023-01-26 18:05:09 +00:00
|
|
|
final headers = {'authorization': 'Bearer $twitterBearerToken'};
|
2023-01-24 18:24:46 +00:00
|
|
|
final uri = Uri(
|
2023-10-17 18:55:54 +00:00
|
|
|
scheme: httpsScheme,
|
|
|
|
host: apiHost,
|
|
|
|
path: userPath + userName,
|
|
|
|
queryParameters: queryParams);
|
2023-01-24 18:24:46 +00:00
|
|
|
|
2023-10-17 18:55:54 +00:00
|
|
|
final response = await http.get(uri, headers: headers).catchError((error) {
|
|
|
|
throw Exception('HTTP request failed: $error');
|
|
|
|
});
|
2023-01-24 18:24:46 +00:00
|
|
|
|
|
|
|
if (response.statusCode != 200) {
|
|
|
|
throw Exception('Unexpected http status: ${response.statusCode}');
|
|
|
|
}
|
|
|
|
|
2023-10-17 18:55:54 +00:00
|
|
|
final Map<String, dynamic> responseJSON = jsonDecode(response.body) as Map<String, dynamic>;
|
2024-03-02 00:13:03 +00:00
|
|
|
if (responseJSON['errors'] != null &&
|
|
|
|
!responseJSON['errors'][0]['detail']
|
|
|
|
.toString()
|
|
|
|
.contains("Could not find tweet with pinned_tweet_id")) {
|
2023-01-24 18:24:46 +00:00
|
|
|
throw Exception(responseJSON['errors'][0]['detail']);
|
|
|
|
}
|
|
|
|
|
2023-10-17 18:55:54 +00:00
|
|
|
return TwitterUser.fromJson(responseJSON, _getPinnedTweet(responseJSON));
|
|
|
|
}
|
|
|
|
|
|
|
|
static Tweet? _getPinnedTweet(Map<String, dynamic> responseJSON) {
|
2024-03-02 00:13:03 +00:00
|
|
|
try {
|
|
|
|
final tweetId = responseJSON['data']['pinned_tweet_id'] as String?;
|
|
|
|
if (tweetId == null || responseJSON['includes'] == null) return null;
|
2023-10-17 18:55:54 +00:00
|
|
|
|
2024-03-02 00:13:03 +00:00
|
|
|
final tweetIncludes = List.from(responseJSON['includes']['tweets'] as List);
|
|
|
|
final pinnedTweetData = tweetIncludes.firstWhere(
|
|
|
|
(tweet) => tweet['id'] == tweetId,
|
|
|
|
orElse: () => null,
|
|
|
|
) as Map<String, dynamic>?;
|
2023-10-17 18:55:54 +00:00
|
|
|
|
2024-03-02 00:13:03 +00:00
|
|
|
if (pinnedTweetData == null) return null;
|
2023-10-17 18:55:54 +00:00
|
|
|
|
2024-03-02 00:13:03 +00:00
|
|
|
final pinnedTweetText =
|
|
|
|
(pinnedTweetData['note_tweet']?['text'] ?? pinnedTweetData['text']) as String;
|
2023-10-17 18:55:54 +00:00
|
|
|
|
2024-03-02 00:13:03 +00:00
|
|
|
return Tweet(id: tweetId, text: pinnedTweetText);
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-01-24 18:24:46 +00:00
|
|
|
}
|
|
|
|
}
|