cake_wallet/lib/twitter/twitter_api.dart

38 lines
1.2 KiB
Dart
Raw Normal View History

2023-01-24 18:24:46 +00:00
import 'dart:convert';
import 'package:cake_wallet/twitter/twitter_user.dart';
import 'package:http/http.dart' as http;
import 'package:cake_wallet/.secrets.g.dart' as secrets;
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-02-09 10:23:13 +00:00
final queryParams = {'user.fields': 'description', 'expansions': 'pinned_tweet_id'};
2023-01-24 18:24:46 +00:00
2023-01-26 18:05:09 +00:00
final headers = {'authorization': 'Bearer $twitterBearerToken'};
2023-01-24 18:24:46 +00:00
final uri = Uri(
scheme: httpsScheme,
host: apiHost,
path: userPath + userName,
queryParameters: queryParams,
);
var response = await http.get(uri, headers: headers);
if (response.statusCode != 200) {
throw Exception('Unexpected http status: ${response.statusCode}');
}
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
if (responseJSON['errors'] != null) {
throw Exception(responseJSON['errors'][0]['detail']);
}
2023-02-12 22:38:12 +00:00
return TwitterUser.fromJson(responseJSON);
2023-01-24 18:24:46 +00:00
}
}