Skip to content

Commit d01638e

Browse files
committed
Initial commit (WIP)
1 parent d9bc7b8 commit d01638e

27 files changed

+1069
-2
lines changed

.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
12+
# IntelliJ related
13+
*.iml
14+
*.ipr
15+
*.iws
16+
.idea/
17+
18+
# The .vscode folder contains launch configuration and tasks you configure in
19+
# VS Code which you may wish to be included in version control, so this line
20+
# is commented out by default.
21+
#.vscode/
22+
23+
# Flutter/Dart/Pub related
24+
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
25+
/pubspec.lock
26+
**/doc/api/
27+
.dart_tool/
28+
.packages
29+
build/

.metadata

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled and should not be manually edited.
5+
6+
version:
7+
revision: db747aa1331bd95bc9b3874c842261ca2d302cd5
8+
channel: stable
9+
10+
project_type: package

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 0.0.1
2+
3+
* TODO: Describe initial release.

LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TODO: Add your license here.

README.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,39 @@
1-
# discord_api_flutter
2-
A Flutter client to connect to Discord using OAuth2 authentication
1+
<!--
2+
This README describes the package. If you publish this package to pub.dev,
3+
this README's contents appear on the landing page for your package.
4+
5+
For information about how to write a good package README, see the guide for
6+
[writing package pages](https://dart.dev/guides/libraries/writing-package-pages).
7+
8+
For general information about developing packages, see the Dart guide for
9+
[creating packages](https://dart.dev/guides/libraries/create-library-packages)
10+
and the Flutter guide for
11+
[developing packages and plugins](https://flutter.dev/developing-packages).
12+
-->
13+
14+
TODO: Put a short description of the package here that helps potential users
15+
know whether this package might be useful for them.
16+
17+
## Features
18+
19+
TODO: List what your package can do. Maybe include images, gifs, or videos.
20+
21+
## Getting started
22+
23+
TODO: List prerequisites and provide or point to information on how to
24+
start using the package.
25+
26+
## Usage
27+
28+
TODO: Include short and useful examples for package users. Add longer examples
29+
to `/example` folder.
30+
31+
```dart
32+
const like = 'sample';
33+
```
34+
35+
## Additional information
36+
37+
TODO: Tell users more about the package: where to find more information, how to
38+
contribute to the package, how to file issues, what response they can expect
39+
from the package authors, and more.

analysis_options.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
include: package:flutter_lints/flutter.yaml
2+
3+
# Additional information about this file can be found at
4+
# https://dart.dev/guides/language/analysis-options

lib/discord_api.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
library discord_api;

lib/src/discord_client.dart

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class DiscordApiException implements Exception {
2+
final String message;
3+
4+
const DiscordApiException(this.message);
5+
6+
@override
7+
String toString() => 'DiscordException: $message';
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extension StringExtension on String {
2+
String capitalize() {
3+
return "${this[0].toUpperCase()}${substring(1).toLowerCase()}";
4+
}
5+
}

0 commit comments

Comments
 (0)