mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-06-28 20:39:51 +00:00
fix Twiter APiI (#1132)
Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>
This commit is contained in:
parent
98a9edc656
commit
29bc234dab
3 changed files with 42 additions and 29 deletions
|
@ -65,16 +65,11 @@ class AddressResolver {
|
||||||
if (addressFromBio != null) {
|
if (addressFromBio != null) {
|
||||||
return ParsedAddress.fetchTwitterAddress(address: addressFromBio, name: text);
|
return ParsedAddress.fetchTwitterAddress(address: addressFromBio, name: text);
|
||||||
}
|
}
|
||||||
final tweets = twitterUser.tweets;
|
|
||||||
if (tweets != null) {
|
|
||||||
var subString = StringBuffer();
|
|
||||||
tweets.forEach((item) {
|
|
||||||
subString.writeln(item.text);
|
|
||||||
});
|
|
||||||
final userTweetsText = subString.toString();
|
|
||||||
final addressFromPinnedTweet =
|
|
||||||
extractAddressByType(raw: userTweetsText, type: CryptoCurrency.fromString(ticker));
|
|
||||||
|
|
||||||
|
final pinnedTweet = twitterUser.pinnedTweet?.text;
|
||||||
|
if (pinnedTweet != null) {
|
||||||
|
final addressFromPinnedTweet =
|
||||||
|
extractAddressByType(raw: pinnedTweet, type: CryptoCurrency.fromString(ticker));
|
||||||
if (addressFromPinnedTweet != null) {
|
if (addressFromPinnedTweet != null) {
|
||||||
return ParsedAddress.fetchTwitterAddress(address: addressFromPinnedTweet, name: text);
|
return ParsedAddress.fetchTwitterAddress(address: addressFromPinnedTweet, name: text);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:cake_wallet/.secrets.g.dart' as secrets;
|
||||||
import 'package:cake_wallet/twitter/twitter_user.dart';
|
import 'package:cake_wallet/twitter/twitter_user.dart';
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import 'package:cake_wallet/.secrets.g.dart' as secrets;
|
|
||||||
|
|
||||||
class TwitterApi {
|
class TwitterApi {
|
||||||
static const twitterBearerToken = secrets.twitterBearerToken;
|
static const twitterBearerToken = secrets.twitterBearerToken;
|
||||||
|
@ -10,28 +11,49 @@ class TwitterApi {
|
||||||
static const userPath = '/2/users/by/username/';
|
static const userPath = '/2/users/by/username/';
|
||||||
|
|
||||||
static Future<TwitterUser> lookupUserByName({required String userName}) async {
|
static Future<TwitterUser> lookupUserByName({required String userName}) async {
|
||||||
final queryParams = {'user.fields': 'description', 'expansions': 'pinned_tweet_id'};
|
final queryParams = {
|
||||||
|
'user.fields': 'description',
|
||||||
|
'expansions': 'pinned_tweet_id',
|
||||||
|
'tweet.fields': 'note_tweet'
|
||||||
|
};
|
||||||
final headers = {'authorization': 'Bearer $twitterBearerToken'};
|
final headers = {'authorization': 'Bearer $twitterBearerToken'};
|
||||||
|
|
||||||
final uri = Uri(
|
final uri = Uri(
|
||||||
scheme: httpsScheme,
|
scheme: httpsScheme,
|
||||||
host: apiHost,
|
host: apiHost,
|
||||||
path: userPath + userName,
|
path: userPath + userName,
|
||||||
queryParameters: queryParams,
|
queryParameters: queryParams);
|
||||||
);
|
|
||||||
|
|
||||||
var response = await http.get(uri, headers: headers);
|
final response = await http.get(uri, headers: headers).catchError((error) {
|
||||||
|
throw Exception('HTTP request failed: $error');
|
||||||
|
});
|
||||||
|
|
||||||
if (response.statusCode != 200) {
|
if (response.statusCode != 200) {
|
||||||
throw Exception('Unexpected http status: ${response.statusCode}');
|
throw Exception('Unexpected http status: ${response.statusCode}');
|
||||||
}
|
}
|
||||||
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
||||||
|
|
||||||
|
final Map<String, dynamic> responseJSON = jsonDecode(response.body) as Map<String, dynamic>;
|
||||||
if (responseJSON['errors'] != null) {
|
if (responseJSON['errors'] != null) {
|
||||||
throw Exception(responseJSON['errors'][0]['detail']);
|
throw Exception(responseJSON['errors'][0]['detail']);
|
||||||
}
|
}
|
||||||
|
|
||||||
return TwitterUser.fromJson(responseJSON);
|
return TwitterUser.fromJson(responseJSON, _getPinnedTweet(responseJSON));
|
||||||
|
}
|
||||||
|
|
||||||
|
static Tweet? _getPinnedTweet(Map<String, dynamic> responseJSON) {
|
||||||
|
final tweetId = responseJSON['data']['pinned_tweet_id'] as String?;
|
||||||
|
if (tweetId == null || responseJSON['includes'] == null) return null;
|
||||||
|
|
||||||
|
final tweetIncludes = List.from(responseJSON['includes']['tweets'] as List);
|
||||||
|
final pinnedTweetData = tweetIncludes.firstWhere(
|
||||||
|
(tweet) => tweet['id'] == tweetId,
|
||||||
|
orElse: () => null,
|
||||||
|
) as Map<String, dynamic>?;
|
||||||
|
|
||||||
|
if (pinnedTweetData == null) return null;
|
||||||
|
|
||||||
|
final pinnedTweetText =
|
||||||
|
(pinnedTweetData['note_tweet']?['text'] ?? pinnedTweetData['text']) as String;
|
||||||
|
|
||||||
|
return Tweet(id: tweetId, text: pinnedTweetText);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,25 +4,21 @@ class TwitterUser {
|
||||||
required this.username,
|
required this.username,
|
||||||
required this.name,
|
required this.name,
|
||||||
required this.description,
|
required this.description,
|
||||||
this.tweets});
|
this.pinnedTweet});
|
||||||
|
|
||||||
final String id;
|
final String id;
|
||||||
final String username;
|
final String username;
|
||||||
final String name;
|
final String name;
|
||||||
final String description;
|
final String description;
|
||||||
final List<Tweet>? tweets;
|
final Tweet? pinnedTweet;
|
||||||
|
|
||||||
factory TwitterUser.fromJson(Map<String, dynamic> json) {
|
factory TwitterUser.fromJson(Map<String, dynamic> json, [Tweet? pinnedTweet]) {
|
||||||
return TwitterUser(
|
return TwitterUser(
|
||||||
id: json['data']['id'] as String,
|
id: json['data']['id'] as String,
|
||||||
username: json['data']['username'] as String,
|
username: json['data']['username'] as String,
|
||||||
name: json['data']['name'] as String,
|
name: json['data']['name'] as String,
|
||||||
description: json['data']['description'] as String? ?? '',
|
description: json['data']['description'] as String? ?? '',
|
||||||
tweets: json['includes'] != null
|
pinnedTweet: pinnedTweet,
|
||||||
? List.from(json['includes']['tweets'] as List)
|
|
||||||
.map((e) => Tweet.fromJson(e as Map<String, dynamic>))
|
|
||||||
.toList()
|
|
||||||
: null,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue