Skip to content

Commit

Permalink
Redid webhook client, fully removed SS, other internal updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Hackzzila committed Oct 17, 2016
1 parent 241c20d commit 5ecbd50
Show file tree
Hide file tree
Showing 24 changed files with 101 additions and 230 deletions.
6 changes: 3 additions & 3 deletions lib/browser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import 'package:http_utils/http_utils.dart' as http_utils;
import 'package:http_parser/http_parser.dart' as http_parser;
import 'package:http/http.dart' as http;
import 'package:http/browser_client.dart' as http_browser;
//import 'package:events/events.dart' as events;

part 'src/Client.dart';
part 'src/webhook.dart';

part 'src/internal/_BaseObj.dart';
part 'src/internal/_Constants.dart';
Expand Down Expand Up @@ -76,9 +76,10 @@ part 'src/errors/HttpError.dart';
part 'src/errors/InvalidTokenError.dart';
part 'src/errors/InvalidShardError.dart';

final bool _browser = true;

class _WebSocket {
html.WebSocket _socket;
bool browser = true;

_WebSocket();

Expand All @@ -105,7 +106,6 @@ class _WebSocket {

class _HttpClient {
http_browser.BrowserClient client;
bool browser = true;

_HttpClient() {
this.client = new http_browser.BrowserClient();
Expand Down
7 changes: 3 additions & 4 deletions lib/discord.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import 'dart:collection';
import 'package:http_utils/http_utils.dart' as http_utils;
import 'package:http_parser/http_parser.dart' as http_parser;
import 'package:http/http.dart' as http;
import 'package:events/events.dart' as events;

part 'src/Client.dart';
part 'src/ss.dart';
part 'src/webhook.dart';

part 'src/internal/_BaseObj.dart';
part 'src/internal/_Constants.dart';
Expand Down Expand Up @@ -76,9 +75,10 @@ part 'src/errors/HttpError.dart';
part 'src/errors/InvalidTokenError.dart';
part 'src/errors/InvalidShardError.dart';

final bool _browser = false;

class _WebSocket {
WebSocket _socket;
bool browser = false;

_WebSocket();

Expand All @@ -100,7 +100,6 @@ class _WebSocket {

class _HttpClient {
http.Client client;
bool browser = false;

_HttpClient() {
this.client = new http.Client();
Expand Down
25 changes: 3 additions & 22 deletions lib/src/Client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class Client {
_Http _http;
_WS _ws;
_EventController _events;
_Util _util;

/// The logged in user.
ClientUser user;
Expand All @@ -31,11 +30,7 @@ class Client {
bool ready = false;

/// The current version.
String version = "0.13.3";

/// The client's SS manager, null if the client is not sharded, [SSServer] if
/// the current shard is 0, [SSClient] otherwise.
dynamic ss;
String version = _Constants.version;

/// The client's internal shards.
Map<int, Shard> shards;
Expand Down Expand Up @@ -116,16 +111,7 @@ class Client {

this._http = new _Http(this);
this._events = new _EventController(this);
this._util = new _Util();
this._ws = new _WS(this);

/*if (this._options.shardCount > 1) {
if (this._options.shardIds.contains(0)) {
this.ss = new SSServer(this);
} else {
this.ss = new SSClient(this);
}
}*/
}

/// The client's uptime.
Expand All @@ -136,11 +122,6 @@ class Client {
await this._ws.close();
this._http.httpClient.close();
await this._events.destroy();
/*if (this.ss is SSServer) {
await this.ss.close();
} else if (this.ss is SSClient) {
this.ss.destroy();
}*/
return null;
}

Expand All @@ -149,7 +130,7 @@ class Client {
/// Throws an [Exception] if the HTTP request errored.
/// Client.getUser("user id");
Future<User> getUser(dynamic user) async {
final String id = this._util.resolve('user', user);
final String id = _Util.resolve('user', user);

final _HttpResponse r = await this._http.get('/users/$id');
return new User._new(this, r.json as Map<String, dynamic>);
Expand All @@ -169,7 +150,7 @@ class Client {
/// Throws an [Exception] if the HTTP request errored
/// Client.getOAuth2Info("app id");
Future<OAuth2Info> getOAuth2Info(dynamic app) async {
final String id = this._util.resolve('app', app);
final String id = _Util.resolve('app', app);

final _HttpResponse r =
await this._http.get('/oauth2/authorize?client_id=$id&scope=bot');
Expand Down
1 change: 1 addition & 0 deletions lib/src/internal/_Constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class _OPCodes {
/// The client constants.
class _Constants {
static const String host = "https://discordapp.com/api/v6";
static const String version = "0.14.0";

/// The gateway OP codes.
static const Map<String, int> opCodes = const <String, int>{
Expand Down
59 changes: 33 additions & 26 deletions lib/src/internal/_Http.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
part of discord;

class _HttpRequest {
_HttpClient httpClient;
_Http http;
String uri;
String method;
Map<String, String> headers;
Expand All @@ -10,35 +10,36 @@ class _HttpRequest {
StreamController<http.Response> streamController;
Stream<http.Response> stream;

_HttpRequest(
this.httpClient, this.uri, this.method, this.headers, this.body) {
_HttpRequest(this.http, this.uri, this.method, this.headers, this.body) {
this.streamController = new StreamController<http.Response>();
this.stream = streamController.stream;

if (this.method == "GET") {
this.execute = () async {
return await this
.http
.httpClient
.get("${_Constants.host}$uri", headers: this.headers);
.get("${this.http.host}$uri", headers: this.headers);
};
} else if (this.method == "POST") {
this.execute = () async {
return await this.httpClient.post("${_Constants.host}$uri",
return await this.http.httpClient.post("${this.http.host}$uri",
body: JSON.encode(this.body), headers: this.headers);
};
} else if (this.method == "PATCH") {
this.execute = () async {
return await this.httpClient.patch("${_Constants.host}$uri",
return await this.http.httpClient.patch("${this.http.host}$uri",
body: JSON.encode(this.body), headers: this.headers);
};
} else if (this.method == "PUT") {
this.execute = () async {
return await this.httpClient.put("${_Constants.host}$uri",
return await this.http.httpClient.put("${this.http.host}$uri",
body: JSON.encode(this.body), headers: this.headers);
};
} else if (this.method == "DELETE") {
this.execute = () async {
return await this
.http
.httpClient
.delete("${_Constants.host}$uri", headers: this.headers);
};
Expand Down Expand Up @@ -70,7 +71,7 @@ class _Bucket {
}

void execute(_HttpRequest request) {
if (request.httpClient.browser ||
if (_browser ||
(this.ratelimitRemaining == null || this.ratelimitRemaining > 0)) {
request.execute().then((http.Response r) {
this.ratelimitRemaining = r.headers['x-ratelimit-remaining'] != null
Expand Down Expand Up @@ -133,7 +134,8 @@ class _HttpResponse {

/// The HTTP manager for the client.
class _Http {
Client client;
dynamic client;
String host;
Map<String, _Bucket> buckets = <String, _Bucket>{};

/// A map of headers that get sent on each HTTP request.
Expand All @@ -143,22 +145,23 @@ class _Http {
_HttpClient httpClient;

/// Makes a new HTTP manager.
_Http(this.client) {
_Http(this.client, [this.host = _Constants.host]) {
this.httpClient = new _HttpClient();
this.headers = <String, String>{'Content-Type': 'application/json'};

if (!httpClient.browser)
if (!_browser)
this.headers['User-Agent'] =
'Discord Dart (https://github.com/Hackzzila/Discord-Dart, ${client.version})';
}

/// Sends a GET request.
Future<_HttpResponse> get(String uri, [bool beforeReady = false]) async {
if (!this.client.ready && !beforeReady) throw new ClientNotReadyError();
if (client is Client && !this.client.ready && !beforeReady)
throw new ClientNotReadyError();
if (buckets[uri] == null) buckets[uri] = new _Bucket(uri);

await for (http.Response r in buckets[uri].push(
new _HttpRequest(this.httpClient, uri, "GET", this.headers, null))) {
await for (http.Response r in buckets[uri]
.push(new _HttpRequest(this, uri, "GET", this.headers, null))) {
http_utils.ResponseStatus status =
http_utils.ResponseStatus.fromStatusCode(r.statusCode);
if (status.family == http_utils.ResponseStatusFamily.SUCCESSFUL) {
Expand All @@ -173,11 +176,12 @@ class _Http {
/// Sends a POST request.
Future<_HttpResponse> post(String uri, Object content,
[bool beforeReady = false]) async {
if (!this.client.ready && !beforeReady) throw new ClientNotReadyError();
if (client is Client && !this.client.ready && !beforeReady)
throw new ClientNotReadyError();
if (buckets[uri] == null) buckets[uri] = new _Bucket(uri);

await for (http.Response r in buckets[uri].push(new _HttpRequest(
this.httpClient, uri, "POST", this.headers, content))) {
await for (http.Response r in buckets[uri]
.push(new _HttpRequest(this, uri, "POST", this.headers, content))) {
http_utils.ResponseStatus status =
http_utils.ResponseStatus.fromStatusCode(r.statusCode);
if (status.family == http_utils.ResponseStatusFamily.SUCCESSFUL) {
Expand All @@ -192,11 +196,12 @@ class _Http {
/// Sends a PATCH request.
Future<_HttpResponse> patch(String uri, Object content,
[bool beforeReady = false]) async {
if (!this.client.ready && !beforeReady) throw new ClientNotReadyError();
if (client is Client && !this.client.ready && !beforeReady)
throw new ClientNotReadyError();
if (buckets[uri] == null) buckets[uri] = new _Bucket(uri);

await for (http.Response r in buckets[uri].push(new _HttpRequest(
this.httpClient, uri, "PATCH", this.headers, content))) {
await for (http.Response r in buckets[uri]
.push(new _HttpRequest(this, uri, "PATCH", this.headers, content))) {
http_utils.ResponseStatus status =
http_utils.ResponseStatus.fromStatusCode(r.statusCode);
if (status.family == http_utils.ResponseStatusFamily.SUCCESSFUL) {
Expand All @@ -211,11 +216,12 @@ class _Http {
/// Sends a PUT request.
Future<_HttpResponse> put(String uri, Object content,
[bool beforeReady = false]) async {
if (!this.client.ready && !beforeReady) throw new ClientNotReadyError();
if (client is Client && !this.client.ready && !beforeReady)
throw new ClientNotReadyError();
if (buckets[uri] == null) buckets[uri] = new _Bucket(uri);

await for (http.Response r in buckets[uri].push(
new _HttpRequest(this.httpClient, uri, "PUT", this.headers, content))) {
await for (http.Response r in buckets[uri]
.push(new _HttpRequest(this, uri, "PUT", this.headers, content))) {
http_utils.ResponseStatus status =
http_utils.ResponseStatus.fromStatusCode(r.statusCode);
if (status.family == http_utils.ResponseStatusFamily.SUCCESSFUL) {
Expand All @@ -229,11 +235,12 @@ class _Http {

/// Sends a DELETE request.
Future<_HttpResponse> delete(String uri, [bool beforeReady = false]) async {
if (!this.client.ready && !beforeReady) throw new ClientNotReadyError();
if (client is Client && !this.client.ready && !beforeReady)
throw new ClientNotReadyError();
if (buckets[uri] == null) buckets[uri] = new _Bucket(uri);

await for (http.Response r in buckets[uri].push(
new _HttpRequest(this.httpClient, uri, "DELETE", this.headers, null))) {
await for (http.Response r in buckets[uri]
.push(new _HttpRequest(this, uri, "DELETE", this.headers, null))) {
http_utils.ResponseStatus status =
http_utils.ResponseStatus.fromStatusCode(r.statusCode);
if (status.family == http_utils.ResponseStatusFamily.SUCCESSFUL) {
Expand Down
4 changes: 2 additions & 2 deletions lib/src/internal/_Util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ part of discord;
/// The utility functions for the client.
class _Util {
/// Gets a DateTime from a snowflake ID.
DateTime getDate(String id) {
static DateTime getDate(String id) {
return new DateTime.fromMillisecondsSinceEpoch(
((int.parse(id) / 4194304) + 1420070400000).toInt());
}

/// Resolves an object into a target object.
String resolve(String to, dynamic object) {
static String resolve(String to, dynamic object) {
if (to == "channel") {
if (object is Message) {
return object.channel.id;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/objects/Attachment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ class Attachment extends _BaseObj {
this.size = data['size'];
this.height = data['height'];
this.width = data['width'];
this.createdAt = this._client._util.getDate(this.id);
this.createdAt = _Util.getDate(this.id);
}
}
2 changes: 1 addition & 1 deletion lib/src/objects/Channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Channel extends _BaseObj {
Channel._new(Client client, Map<String, dynamic> data, this.type)
: super(client) {
this.id = data['id'];
this.createdAt = this._client._util.getDate(this.id);
this.createdAt = _Util.getDate(this.id);

client.channels[this.id] = this;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/objects/DMChannel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class DMChannel extends Channel {
/// Channel.getMessage("message id");
Future<Message> getMessage(dynamic message) async {
if (this._client.user.bot) {
final String id = this._client._util.resolve('message', message);
final String id = _Util.resolve('message', message);

final _HttpResponse r =
await this._client._http.get('/channels/${this.id}/messages/$id');
Expand Down
2 changes: 1 addition & 1 deletion lib/src/objects/GroupDMChannel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class GroupDMChannel extends Channel {
/// Channel.getMessage("message id");
Future<Message> getMessage(dynamic message) async {
if (this._client.user.bot) {
final String id = this._client._util.resolve('message', message);
final String id = _Util.resolve('message', message);

final _HttpResponse r =
await this._client._http.get('/channels/${this.id}/messages/$id');
Expand Down
6 changes: 3 additions & 3 deletions lib/src/objects/Guild.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class Guild extends _BaseObj {
this.mfaLevel = data['mfa_level'];
this.embedEnabled = data['embed_enabled'];
this.ownerID = data['owner_id'];
this.createdAt = this._client._util.getDate(this.id);
this.createdAt = _Util.getDate(this.id);

this.roles = new Map<String, Role>();
data['roles'].forEach((Map<String, dynamic> o) {
Expand Down Expand Up @@ -224,7 +224,7 @@ class Guild extends _BaseObj {
/// Throws an [Exception] if the HTTP request errored.
/// Guild.getMember("user id");
Future<Member> getMember(dynamic member) async {
final String id = this._client._util.resolve('member', member);
final String id = _Util.resolve('member', member);

if (this.members[member] != null) {
return this.members[member];
Expand All @@ -245,7 +245,7 @@ class Guild extends _BaseObj {
/// Guild.oauth2Authorize("app id");
Future<Null> oauth2Authorize(dynamic app, [int permissions = 0]) async {
if (!this._client.user.bot) {
final String id = this._client._util.resolve('app', app);
final String id = _Util.resolve('app', app);

await this._client._http.post(
'/oauth2/authorize?client_id=$id&scope=bot', <String, dynamic>{
Expand Down
2 changes: 1 addition & 1 deletion lib/src/objects/InviteChannel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class InviteChannel extends _BaseObj {
this.id = data['id'];
this.name = data['name'];
this.type = data['type'];
this.createdAt = this._client._util.getDate(this.id);
this.createdAt = _Util.getDate(this.id);
}

/// Returns a string representation of this object.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/objects/InviteGuild.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class InviteGuild extends _BaseObj {
this.id = data['id'];
this.name = data['name'];
this.spash = data['splash_hash'];
this.createdAt = this._client._util.getDate(this.id);
this.createdAt = _Util.getDate(this.id);
}

/// Returns a string representation of this object.
Expand Down
Loading

0 comments on commit 5ecbd50

Please sign in to comment.