|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'models/discord_api_scope.dart'; |
| 4 | +import 'models/discord_token.dart'; |
| 5 | +import 'providers/discord_http_client.dart'; |
| 6 | + |
| 7 | +class DiscordClient { |
| 8 | + static const apiVersion = "9"; |
| 9 | + |
| 10 | + static const oauth2Path = "oauth2"; |
| 11 | + static const authPath = "authorize"; |
| 12 | + static const tokenPath = "token"; |
| 13 | + static const revokePath = "revoke"; |
| 14 | + static const apiPath = "api"; |
| 15 | + static const versionPath = "v$apiVersion"; |
| 16 | + |
| 17 | + static final baseUrl = Uri(scheme: 'https', host: 'discord.com'); |
| 18 | + |
| 19 | + final String redirectUri; |
| 20 | + final String clientId; |
| 21 | + final String clientSecret; |
| 22 | + final DiscordHttpClient discordHttpClient; |
| 23 | + |
| 24 | + /// If you need it, you can get the [DiscordDioProvider] from the discord_api_dio_provider package. |
| 25 | + /// Otherwise, you'll have to implement `discordHttpClient` yourself. |
| 26 | + DiscordClient({ |
| 27 | + required this.clientId, |
| 28 | + required this.clientSecret, |
| 29 | + required this.redirectUri, |
| 30 | + required this.discordHttpClient, |
| 31 | + DiscordToken? token, |
| 32 | + }) { |
| 33 | + if (token != null) { |
| 34 | + initializeToken(token); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /// Return the authorization Uri for the Discord API. |
| 39 | + Uri authorizeUri(List<DiscordApiScope> scopes) { |
| 40 | + final scopesSet = <String>{}..addAll(scopes.map((e) => e.string).toSet()); |
| 41 | + return baseUrl.replace(pathSegments: <String>[ |
| 42 | + apiPath, |
| 43 | + versionPath, |
| 44 | + oauth2Path, |
| 45 | + authPath |
| 46 | + ], queryParameters: { |
| 47 | + 'response_type': 'code', |
| 48 | + 'client_id': clientId, |
| 49 | + 'scope': scopesSet.join(' '), |
| 50 | + 'redirect_uri': redirectUri, |
| 51 | + //'state': generateState() ? |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + static Uri get tokenUri => baseUrl.replace( |
| 56 | + pathSegments: <String>[apiPath, versionPath, oauth2Path, tokenPath]); |
| 57 | + static Uri get revokeUri => baseUrl.replace( |
| 58 | + pathSegments: <String>[apiPath, versionPath, oauth2Path, revokePath]); |
| 59 | + |
| 60 | + void initializeToken(DiscordToken discordToken) => |
| 61 | + discordHttpClient.initializeToken(discordToken); |
| 62 | + |
| 63 | + // There is little reason to call either of those methods, as they're related to the authorization flow, and not any API usage. |
| 64 | + |
| 65 | + Future<void> getAccessToken(String code) async { |
| 66 | + await discordHttpClient.getAccessToken(code, redirectUri); |
| 67 | + } |
| 68 | + |
| 69 | + Future<void> refreshAccessToken() async { |
| 70 | + await discordHttpClient.refreshAccessToken(); |
| 71 | + } |
| 72 | + |
| 73 | + Future<void> revokeRefreshToken() async { |
| 74 | + await discordHttpClient.revokeRefreshToken(); |
| 75 | + } |
| 76 | + |
| 77 | + Future<DiscordAuthorizationInformation> |
| 78 | + getCurrentAuthorizationInformation() async { |
| 79 | + final data = await discordHttpClient |
| 80 | + .getCall([apiPath, versionPath, oauth2Path, '@me']); |
| 81 | + } |
| 82 | +} |
0 commit comments