diff --git a/src/Discord.Net.Net45/Discord.Net.csproj b/src/Discord.Net.Net45/Discord.Net.csproj
index 64f5766b0..61efa7cbe 100644
--- a/src/Discord.Net.Net45/Discord.Net.csproj
+++ b/src/Discord.Net.Net45/Discord.Net.csproj
@@ -107,8 +107,8 @@
Helpers\AsyncCache.cs
-
- Helpers\Http.cs
+
+ Helpers\JsonHttpClient.cs
HttpException.cs
diff --git a/src/Discord.Net/API/DiscordAPI.cs b/src/Discord.Net/API/DiscordAPI.cs
index 3117abf25..9dcae95ee 100644
--- a/src/Discord.Net/API/DiscordAPI.cs
+++ b/src/Discord.Net/API/DiscordAPI.cs
@@ -6,141 +6,147 @@ using System.Threading.Tasks;
namespace Discord.API
{
- internal static class DiscordAPI
+ internal class DiscordAPI
{
public const int MaxMessageSize = 2000;
+ private readonly JsonHttpClient _http;
+
+ public DiscordAPI(JsonHttpClient http)
+ {
+ _http = http;
+ }
//Auth
- public static Task GetWebSocket()
- => Http.Get(Endpoints.Gateway);
- public static async Task LoginAnonymous(string username)
+ public Task GetWebSocket()
+ => _http.Get(Endpoints.Gateway);
+ public async Task LoginAnonymous(string username)
{
- var fingerprintResponse = await Http.Post(Endpoints.AuthFingerprint);
+ var fingerprintResponse = await _http.Post(Endpoints.AuthFingerprint);
var registerRequest = new APIRequests.AuthRegisterRequest { Fingerprint = fingerprintResponse.Fingerprint, Username = username };
- var registerResponse = await Http.Post(Endpoints.AuthRegister, registerRequest);
+ var registerResponse = await _http.Post(Endpoints.AuthRegister, registerRequest);
return registerResponse;
}
- public static async Task Login(string email, string password)
+ public async Task Login(string email, string password)
{
var request = new APIRequests.AuthLogin { Email = email, Password = password };
- var response = await Http.Post(Endpoints.AuthLogin, request);
+ var response = await _http.Post(Endpoints.AuthLogin, request);
return response;
}
- public static Task Logout()
- => Http.Post(Endpoints.AuthLogout);
+ public Task Logout()
+ => _http.Post(Endpoints.AuthLogout);
//Servers
- public static Task CreateServer(string name, string region)
+ public Task CreateServer(string name, string region)
{
var request = new APIRequests.CreateServer { Name = name, Region = region };
- return Http.Post(Endpoints.Servers, request);
+ return _http.Post(Endpoints.Servers, request);
}
- public static Task LeaveServer(string id)
- => Http.Delete(Endpoints.Server(id));
+ public Task LeaveServer(string id)
+ => _http.Delete(Endpoints.Server(id));
//Channels
- public static Task CreateChannel(string serverId, string name, string channelType)
+ public Task CreateChannel(string serverId, string name, string channelType)
{
var request = new APIRequests.CreateChannel { Name = name, Type = channelType };
- return Http.Post(Endpoints.ServerChannels(serverId), request);
+ return _http.Post(Endpoints.ServerChannels(serverId), request);
}
- public static Task CreatePMChannel(string myId, string recipientId)
+ public Task CreatePMChannel(string myId, string recipientId)
{
var request = new APIRequests.CreatePMChannel { RecipientId = recipientId };
- return Http.Post(Endpoints.UserChannels(myId), request);
+ return _http.Post(Endpoints.UserChannels(myId), request);
}
- public static Task DestroyChannel(string channelId)
- => Http.Delete(Endpoints.Channel(channelId));
- public static Task GetMessages(string channelId, int count)
- => Http.Get(Endpoints.ChannelMessages(channelId, count));
+ public Task DestroyChannel(string channelId)
+ => _http.Delete(Endpoints.Channel(channelId));
+ public Task GetMessages(string channelId, int count)
+ => _http.Get(Endpoints.ChannelMessages(channelId, count));
//Members
- public static Task Kick(string serverId, string memberId)
- => Http.Delete(Endpoints.ServerMember(serverId, memberId));
- public static Task Ban(string serverId, string memberId)
- => Http.Put(Endpoints.ServerBan(serverId, memberId));
- public static Task Unban(string serverId, string memberId)
- => Http.Delete(Endpoints.ServerBan(serverId, memberId));
+ public Task Kick(string serverId, string memberId)
+ => _http.Delete(Endpoints.ServerMember(serverId, memberId));
+ public Task Ban(string serverId, string memberId)
+ => _http.Put(Endpoints.ServerBan(serverId, memberId));
+ public Task Unban(string serverId, string memberId)
+ => _http.Delete(Endpoints.ServerBan(serverId, memberId));
//Invites
- public static Task CreateInvite(string channelId, int maxAge, int maxUses, bool isTemporary, bool hasXkcdPass)
+ public Task CreateInvite(string channelId, int maxAge, int maxUses, bool isTemporary, bool hasXkcdPass)
{
var request = new APIRequests.CreateInvite { MaxAge = maxAge, MaxUses = maxUses, IsTemporary = isTemporary, HasXkcdPass = hasXkcdPass };
- return Http.Post(Endpoints.ChannelInvites(channelId), request);
+ return _http.Post(Endpoints.ChannelInvites(channelId), request);
}
- public static Task GetInvite(string id)
- => Http.Get(Endpoints.Invite(id));
- public static Task AcceptInvite(string id)
- => Http.Post(Endpoints.Invite(id));
- public static Task DeleteInvite(string id)
- => Http.Delete(Endpoints.Invite(id));
+ public Task GetInvite(string id)
+ => _http.Get(Endpoints.Invite(id));
+ public Task AcceptInvite(string id)
+ => _http.Post(Endpoints.Invite(id));
+ public Task DeleteInvite(string id)
+ => _http.Delete(Endpoints.Invite(id));
//Chat
- public static Task SendMessage(string channelId, string message, string[] mentions, string nonce)
+ public Task SendMessage(string channelId, string message, string[] mentions, string nonce)
{
var request = new APIRequests.SendMessage { Content = message, Mentions = mentions, Nonce = nonce };
- return Http.Post(Endpoints.ChannelMessages(channelId), request);
+ return _http.Post(Endpoints.ChannelMessages(channelId), request);
}
- public static Task EditMessage(string channelId, string messageId, string message, string[] mentions)
+ public Task EditMessage(string channelId, string messageId, string message, string[] mentions)
{
var request = new APIRequests.EditMessage { Content = message, Mentions = mentions };
- return Http.Patch(Endpoints.ChannelMessage(channelId, messageId), request);
+ return _http.Patch(Endpoints.ChannelMessage(channelId, messageId), request);
}
- public static Task SendIsTyping(string channelId)
- => Http.Post(Endpoints.ChannelTyping(channelId));
- public static Task DeleteMessage(string channelId, string msgId)
- => Http.Delete(Endpoints.ChannelMessage(channelId, msgId));
- public static Task SendFile(string channelId, Stream stream, string filename = null)
- => Http.File(Endpoints.ChannelMessages(channelId), stream, filename);
+ public Task SendIsTyping(string channelId)
+ => _http.Post(Endpoints.ChannelTyping(channelId));
+ public Task DeleteMessage(string channelId, string msgId)
+ => _http.Delete(Endpoints.ChannelMessage(channelId, msgId));
+ public Task SendFile(string channelId, Stream stream, string filename = null)
+ => _http.File(Endpoints.ChannelMessages(channelId), stream, filename);
//Voice
- public static Task GetVoiceRegions()
- => Http.Get(Endpoints.VoiceRegions);
- public static Task GetVoiceIce()
- => Http.Get(Endpoints.VoiceIce);
- public static Task Mute(string serverId, string memberId)
+ public Task GetVoiceRegions()
+ => _http.Get(Endpoints.VoiceRegions);
+ public Task GetVoiceIce()
+ => _http.Get(Endpoints.VoiceIce);
+ public Task Mute(string serverId, string memberId)
{
var request = new APIRequests.SetMemberMute { Mute = true };
- return Http.Patch(Endpoints.ServerMember(serverId, memberId));
+ return _http.Patch(Endpoints.ServerMember(serverId, memberId));
}
- public static Task Unmute(string serverId, string memberId)
+ public Task Unmute(string serverId, string memberId)
{
var request = new APIRequests.SetMemberMute { Mute = false };
- return Http.Patch(Endpoints.ServerMember(serverId, memberId));
+ return _http.Patch(Endpoints.ServerMember(serverId, memberId));
}
- public static Task Deafen(string serverId, string memberId)
+ public Task Deafen(string serverId, string memberId)
{
var request = new APIRequests.SetMemberDeaf { Deaf = true };
- return Http.Patch(Endpoints.ServerMember(serverId, memberId));
+ return _http.Patch(Endpoints.ServerMember(serverId, memberId));
}
- public static Task Undeafen(string serverId, string memberId)
+ public Task Undeafen(string serverId, string memberId)
{
var request = new APIRequests.SetMemberDeaf { Deaf = false };
- return Http.Patch(Endpoints.ServerMember(serverId, memberId));
+ return _http.Patch(Endpoints.ServerMember(serverId, memberId));
}
//Profile
- public static Task ChangeUsername(string newUsername, string currentEmail, string currentPassword)
+ public Task ChangeUsername(string newUsername, string currentEmail, string currentPassword)
{
var request = new APIRequests.ChangeUsername { Username = newUsername, CurrentEmail = currentEmail, CurrentPassword = currentPassword };
- return Http.Patch(Endpoints.UserMe, request);
+ return _http.Patch(Endpoints.UserMe, request);
}
- public static Task ChangeEmail(string newEmail, string currentPassword)
+ public Task ChangeEmail(string newEmail, string currentPassword)
{
var request = new APIRequests.ChangeEmail { NewEmail = newEmail, CurrentPassword = currentPassword };
- return Http.Patch(Endpoints.UserMe, request);
+ return _http.Patch(Endpoints.UserMe, request);
}
- public static Task ChangePassword(string newPassword, string currentEmail, string currentPassword)
+ public Task ChangePassword(string newPassword, string currentEmail, string currentPassword)
{
var request = new APIRequests.ChangePassword { NewPassword = newPassword, CurrentEmail = currentEmail, CurrentPassword = currentPassword };
- return Http.Patch(Endpoints.UserMe, request);
+ return _http.Patch(Endpoints.UserMe, request);
}
- public static Task ChangeAvatar(DiscordClient.AvatarImageType imageType, byte[] bytes, string currentEmail, string currentPassword)
+ public Task ChangeAvatar(DiscordClient.AvatarImageType imageType, byte[] bytes, string currentEmail, string currentPassword)
{
string base64 = Convert.ToBase64String(bytes);
string type = imageType == DiscordClient.AvatarImageType.Jpeg ? "image/jpeg;base64" : "image/png;base64";
var request = new APIRequests.ChangeAvatar { Avatar = $"data:{type},/9j/{base64}", CurrentEmail = currentEmail, CurrentPassword = currentPassword };
- return Http.Patch(Endpoints.UserMe, request);
+ return _http.Patch(Endpoints.UserMe, request);
}
}
}
diff --git a/src/Discord.Net/DiscordClient.Events.cs b/src/Discord.Net/DiscordClient.Events.cs
index ad7eaf762..9a264b1e5 100644
--- a/src/Discord.Net/DiscordClient.Events.cs
+++ b/src/Discord.Net/DiscordClient.Events.cs
@@ -8,8 +8,7 @@ namespace Discord
Event,
Cache,
WebSocketRawInput, //TODO: Make Http instanced and add a rawoutput event
- WebSocketUnknownInput,
- WebSocketEvent,
+ WebSocketUnknownOpCode,
WebSocketUnknownEvent,
VoiceOutput
}
diff --git a/src/Discord.Net/DiscordClient.cs b/src/Discord.Net/DiscordClient.cs
index 2b954aea5..44ba6d75f 100644
--- a/src/Discord.Net/DiscordClient.cs
+++ b/src/Discord.Net/DiscordClient.cs
@@ -17,6 +17,8 @@ namespace Discord
/// Provides a connection to the DiscordApp service.
public partial class DiscordClient
{
+ private readonly JsonHttpClient _http;
+ private readonly DiscordAPI _api;
private readonly DiscordDataSocket _webSocket;
#if !DNXCORE50
private readonly DiscordVoiceSocket _voiceWebSocket;
@@ -27,7 +29,7 @@ namespace Discord
private readonly JsonSerializer _serializer;
private readonly Random _rand;
- private volatile Task _tasks;
+ private volatile Task _tasks;
/// Returns the User object for the current logged in user.
public User User { get; private set; }
@@ -86,9 +88,10 @@ namespace Discord
public DiscordClient(DiscordClientConfig config = null)
{
_blockEvent = new ManualResetEventSlim(true);
-
_config = config ?? new DiscordClientConfig();
_rand = new Random();
+ _http = new JsonHttpClient();
+ _api = new DiscordAPI(_http);
_serializer = new JsonSerializer();
#if TEST_RESPONSES
@@ -381,9 +384,12 @@ namespace Discord
await _webSocket.ReconnectAsync();
if (_config.EnableDebug)
RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket connected.");
- await _webSocket.Login();
- if (_config.EnableDebug)
- RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket logged in.");
+ if (_http.Token != null)
+ {
+ await _webSocket.Login(_http.Token);
+ if (_config.EnableDebug)
+ RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket logged in.");
+ }
break;
}
catch (Exception ex)
@@ -439,8 +445,6 @@ namespace Discord
_webSocket.GotEvent += (s, e) =>
#endif
{
- if (_config.EnableDebug)
- RaiseOnDebugMessage(DebugMessageType.WebSocketEvent, $"{e.Type}: {e.Event}");
switch (e.Type)
{
//Global
@@ -741,7 +745,7 @@ namespace Discord
APIResponses.SendMessage apiMsg = null;
try
{
- apiMsg = await DiscordAPI.SendMessage(msg.ChannelId, msg.RawText, msg.MentionIds, msg.Nonce);
+ apiMsg = await _api.SendMessage(msg.ChannelId, msg.RawText, msg.MentionIds, msg.Nonce);
}
catch (WebException) { break; }
catch (HttpException) { hasFailed = true; }
@@ -929,7 +933,7 @@ namespace Discord
{
try
{
- var msgs = await DiscordAPI.GetMessages(channel.Id, count);
+ var msgs = await _api.GetMessages(channel.Id, count);
return msgs.OrderBy(x => x.Timestamp)
.Select(x =>
{
@@ -969,7 +973,7 @@ namespace Discord
_blockEvent.Reset();
_disconnectToken = new CancellationTokenSource();
- string url = (await DiscordAPI.GetWebSocket()).Url;
+ string url = (await _api.GetWebSocket()).Url;
//Connect by Token
if (token != null)
@@ -979,9 +983,9 @@ namespace Discord
await _webSocket.ConnectAsync(url);
if (_config.EnableDebug)
RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket connected.");
- Http.Token = token;
+ _http.Token = token;
- await _webSocket.Login();
+ await _webSocket.Login(_http.Token);
if (_config.EnableDebug)
RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket got token.");
success = true;
@@ -1001,7 +1005,7 @@ namespace Discord
if (_config.EnableDebug)
RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket connected.");
- var response = await DiscordAPI.Login(emailOrUsername, password);
+ var response = await _api.Login(emailOrUsername, password);
if (_config.EnableDebug)
RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket got token.");
@@ -1009,8 +1013,8 @@ namespace Discord
//Wait for websocket to finish connecting, then send token
token = response.Token;
- Http.Token = token;
- await _webSocket.Login();
+ _http.Token = token;
+ await _webSocket.Login(_http.Token);
if (_config.EnableDebug)
RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket logged in.");
success = true;
@@ -1022,7 +1026,7 @@ namespace Discord
if (_config.EnableDebug)
RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket connected.");
- var response = await DiscordAPI.LoginAnonymous(emailOrUsername);
+ var response = await _api.LoginAnonymous(emailOrUsername);
if (_config.EnableDebug)
RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket generated anonymous token.");
@@ -1030,8 +1034,8 @@ namespace Discord
//Wait for websocket to finish connecting, then send token
token = response.Token;
- Http.Token = token;
- await _webSocket.Login();
+ _http.Token = token;
+ await _webSocket.Login(_http.Token);
if (_config.EnableDebug)
RaiseOnDebugMessage(DebugMessageType.Connection, $"DataSocket logged in.");
success = true;
@@ -1085,7 +1089,7 @@ namespace Discord
public async Task CreateServer(string name, string region)
{
CheckReady();
- var response = await DiscordAPI.CreateServer(name, region);
+ var response = await _api.CreateServer(name, region);
return _servers.Update(response.Id, response);
}
/// Leaves the provided server, destroying it if you are the owner.
@@ -1097,7 +1101,7 @@ namespace Discord
CheckReady();
try
{
- await DiscordAPI.LeaveServer(serverId);
+ await _api.LeaveServer(serverId);
}
catch (HttpException ex) when (ex.StatusCode == HttpStatusCode.NotFound) {}
return _servers.Remove(serverId);
@@ -1111,7 +1115,7 @@ namespace Discord
public async Task CreateChannel(string serverId, string name, string type)
{
CheckReady();
- var response = await DiscordAPI.CreateChannel(serverId, name, type);
+ var response = await _api.CreateChannel(serverId, name, type);
return _channels.Update(response.Id, serverId, response);
}
/// Creates a new private channel with the provided user.
@@ -1121,7 +1125,7 @@ namespace Discord
public async Task CreatePMChannel(string userId)
{
CheckReady();
- var response = await DiscordAPI.CreatePMChannel(UserId, userId);
+ var response = await _api.CreatePMChannel(UserId, userId);
return _channels.Update(response.Id, response);
}
/// Destroys the provided channel.
@@ -1133,7 +1137,7 @@ namespace Discord
CheckReady();
try
{
- var response = await DiscordAPI.DestroyChannel(channelId);
+ var response = await _api.DestroyChannel(channelId);
}
catch (HttpException ex) when (ex.StatusCode == HttpStatusCode.NotFound) { }
return _channels.Remove(channelId);
@@ -1153,7 +1157,7 @@ namespace Discord
public Task Ban(string serverId, string userId)
{
CheckReady();
- return DiscordAPI.Ban(serverId, userId);
+ return _api.Ban(serverId, userId);
}
/// Unbans a user from the provided server.
public Task Unban(Server server, User user)
@@ -1170,7 +1174,7 @@ namespace Discord
CheckReady();
try
{
- await DiscordAPI.Unban(serverId, userId);
+ await _api.Unban(serverId, userId);
}
catch (HttpException ex) when (ex.StatusCode == HttpStatusCode.NotFound) { }
}
@@ -1202,7 +1206,7 @@ namespace Discord
public async Task CreateInvite(string channelId, int maxAge, int maxUses, bool isTemporary, bool hasXkcdPass)
{
CheckReady();
- var response = await DiscordAPI.CreateInvite(channelId, maxAge, maxUses, isTemporary, hasXkcdPass);
+ var response = await _api.CreateInvite(channelId, maxAge, maxUses, isTemporary, hasXkcdPass);
_channels.Update(response.Channel.Id, response.Server.Id, response.Channel);
_servers.Update(response.Server.Id, response.Server);
_users.Update(response.Inviter.Id, response.Inviter);
@@ -1223,7 +1227,7 @@ namespace Discord
public async Task GetInvite(string id)
{
CheckReady();
- var response = await DiscordAPI.GetInvite(id);
+ var response = await _api.GetInvite(id);
return new Invite(response.Code, response.XkcdPass, this)
{
ChannelId = response.Channel.Id,
@@ -1235,7 +1239,7 @@ namespace Discord
public Task AcceptInvite(Invite invite)
{
CheckReady();
- return DiscordAPI.AcceptInvite(invite.Id);
+ return _api.AcceptInvite(invite.Id);
}
/// Accepts the provided invite.
public async Task AcceptInvite(string id)
@@ -1251,8 +1255,8 @@ namespace Discord
id = id.Substring(0, id.Length - 1);
//Check if this is a human-readable link and get its ID
- var response = await DiscordAPI.GetInvite(id);
- await DiscordAPI.AcceptInvite(response.Code);
+ var response = await _api.GetInvite(id);
+ await _api.AcceptInvite(response.Code);
}
/// Deletes the provided invite.
public async Task DeleteInvite(string id)
@@ -1261,8 +1265,8 @@ namespace Discord
try
{
//Check if this is a human-readable link and get its ID
- var response = await DiscordAPI.GetInvite(id);
- await DiscordAPI.DeleteInvite(response.Code);
+ var response = await _api.GetInvite(id);
+ await _api.DeleteInvite(response.Code);
}
catch (HttpException ex) when (ex.StatusCode == HttpStatusCode.NotFound) { }
}
@@ -1307,7 +1311,7 @@ namespace Discord
}
else
{
- var msg = await DiscordAPI.SendMessage(channelId, blockText, mentions, nonce);
+ var msg = await _api.SendMessage(channelId, blockText, mentions, nonce);
result[i] = _messages.Update(msg.Id, channelId, msg);
result[i].Nonce = nonce;
try { RaiseMessageSent(result[i]); } catch { }
@@ -1342,7 +1346,7 @@ namespace Discord
if (text.Length > DiscordAPI.MaxMessageSize)
text = text.Substring(0, DiscordAPI.MaxMessageSize);
- var msg = await DiscordAPI.EditMessage(channelId, messageId, text, mentions);
+ var msg = await _api.EditMessage(channelId, messageId, text, mentions);
_messages.Update(msg.Id, channelId, msg);
}
@@ -1354,7 +1358,7 @@ namespace Discord
{
try
{
- await DiscordAPI.DeleteMessage(channelId, msgId);
+ await _api.DeleteMessage(channelId, msgId);
return _messages.Remove(msgId);
}
catch (HttpException ex) when (ex.StatusCode == HttpStatusCode.NotFound) { }
@@ -1376,7 +1380,7 @@ namespace Discord
/// It is highly recommended that this stream be cached in memory or on disk, or the request may time out.
public Task SendFile(string channelId, Stream stream, string filename = null)
{
- return DiscordAPI.SendFile(channelId, stream, filename);
+ return _api.SendFile(channelId, stream, filename);
}
@@ -1394,7 +1398,7 @@ namespace Discord
public Task Mute(string serverId, string userId)
{
CheckReady();
- return DiscordAPI.Mute(serverId, userId);
+ return _api.Mute(serverId, userId);
}
/// Unmutes a user on the provided server.
@@ -1410,7 +1414,7 @@ namespace Discord
public Task Unmute(string serverId, string userId)
{
CheckReady();
- return DiscordAPI.Unmute(serverId, userId);
+ return _api.Unmute(serverId, userId);
}
/// Deafens a user on the provided server.
@@ -1426,7 +1430,7 @@ namespace Discord
public Task Deafen(string serverId, string userId)
{
CheckReady();
- return DiscordAPI.Deafen(serverId, userId);
+ return _api.Deafen(serverId, userId);
}
/// Undeafens a user on the provided server.
@@ -1442,7 +1446,7 @@ namespace Discord
public Task Undeafen(string serverId, string userId)
{
CheckReady();
- return DiscordAPI.Undeafen(serverId, userId);
+ return _api.Undeafen(serverId, userId);
}
#if !DNXCORE50
@@ -1506,21 +1510,21 @@ namespace Discord
public async Task ChangeUsername(string newName, string currentEmail, string currentPassword)
{
CheckReady();
- var response = await DiscordAPI.ChangeUsername(newName, currentEmail, currentPassword);
+ var response = await _api.ChangeUsername(newName, currentEmail, currentPassword);
_users.Update(response.Id, response);
}
/// Changes your email to newEmail.
public async Task ChangeEmail(string newEmail, string currentPassword)
{
CheckReady();
- var response = await DiscordAPI.ChangeEmail(newEmail, currentPassword);
+ var response = await _api.ChangeEmail(newEmail, currentPassword);
_users.Update(response.Id, response);
}
/// Changes your password to newPassword.
public async Task ChangePassword(string newPassword, string currentEmail, string currentPassword)
{
CheckReady();
- var response = await DiscordAPI.ChangePassword(newPassword, currentEmail, currentPassword);
+ var response = await _api.ChangePassword(newPassword, currentEmail, currentPassword);
_users.Update(response.Id, response);
}
@@ -1534,7 +1538,7 @@ namespace Discord
public async Task ChangeAvatar(AvatarImageType imageType, byte[] bytes, string currentEmail, string currentPassword)
{
CheckReady();
- var response = await DiscordAPI.ChangeAvatar(imageType, bytes, currentEmail, currentPassword);
+ var response = await _api.ChangeAvatar(imageType, bytes, currentEmail, currentPassword);
_users.Update(response.Id, response);
}
diff --git a/src/Discord.Net/DiscordDataSocket.cs b/src/Discord.Net/DiscordDataSocket.cs
index 35c067c40..7de5eefb6 100644
--- a/src/Discord.Net/DiscordDataSocket.cs
+++ b/src/Discord.Net/DiscordDataSocket.cs
@@ -11,16 +11,16 @@ namespace Discord
{
internal sealed partial class DiscordDataSocket : DiscordWebSocket
{
- private ManualResetEventSlim _connectWaitOnLogin, _connectWaitOnLogin2;
+ private readonly ManualResetEventSlim _connectWaitOnLogin, _connectWaitOnLogin2;
public DiscordDataSocket(DiscordClient client, int timeout, int interval)
: base(client, timeout, interval)
{
_connectWaitOnLogin = new ManualResetEventSlim(false);
_connectWaitOnLogin2 = new ManualResetEventSlim(false);
- }
+ }
- public async Task Login()
+ public async Task Login(string token)
{
var cancelToken = _disconnectToken.Token;
@@ -28,7 +28,7 @@ namespace Discord
_connectWaitOnLogin2.Reset();
TextWebSocketCommands.Login msg = new TextWebSocketCommands.Login();
- msg.Payload.Token = Http.Token;
+ msg.Payload.Token = token;
msg.Payload.Properties["$os"] = "";
msg.Payload.Properties["$browser"] = "";
msg.Payload.Properties["$device"] = "Discord.Net";
@@ -72,7 +72,7 @@ namespace Discord
}
break;
default:
- RaiseOnDebugMessage(DebugMessageType.WebSocketUnknownInput, "Unknown DataSocket op: " + msg.Operation);
+ RaiseOnDebugMessage(DebugMessageType.WebSocketUnknownOpCode, "Unknown DataSocket op: " + msg.Operation);
break;
}
#if DNXCORE
diff --git a/src/Discord.Net/DiscordVoiceSocket.cs b/src/Discord.Net/DiscordVoiceSocket.cs
index 38bb7621b..d66849ac9 100644
--- a/src/Discord.Net/DiscordVoiceSocket.cs
+++ b/src/Discord.Net/DiscordVoiceSocket.cs
@@ -287,7 +287,7 @@ namespace Discord
break;
#endif
default:
- RaiseOnDebugMessage(DebugMessageType.WebSocketUnknownInput, "Unknown VoiceSocket op: " + msg.Operation);
+ RaiseOnDebugMessage(DebugMessageType.WebSocketUnknownOpCode, "Unknown VoiceSocket op: " + msg.Operation);
break;
}
#if DNXCORE50
diff --git a/src/Discord.Net/Helpers/Http.cs b/src/Discord.Net/Helpers/JsonHttpClient.cs
similarity index 67%
rename from src/Discord.Net/Helpers/Http.cs
rename to src/Discord.Net/Helpers/JsonHttpClient.cs
index e7b94254b..688fc7a32 100644
--- a/src/Discord.Net/Helpers/Http.cs
+++ b/src/Discord.Net/Helpers/JsonHttpClient.cs
@@ -12,20 +12,20 @@ using System.Threading.Tasks;
namespace Discord.Helpers
{
- internal static class Http
+ internal class JsonHttpClient
{
#if TEST_RESPONSES
private const bool _isDebug = true;
#else
private const bool _isDebug = false;
#endif
- private static readonly HttpClient _client;
- private static readonly HttpMethod _patch;
+ private readonly HttpClient _client;
+ private readonly HttpMethod _patch;
#if TEST_RESPONSES
- private static readonly JsonSerializerSettings _settings;
+ private readonly JsonSerializerSettings _settings;
#endif
- static Http()
+ public JsonHttpClient()
{
_patch = new HttpMethod("PATCH"); //Not sure why this isn't a default...
@@ -38,7 +38,7 @@ namespace Discord.Helpers
_client.DefaultRequestHeaders.Add("accept", "*/*");
_client.DefaultRequestHeaders.Add("accept-encoding", "gzip, deflate");
- string version = typeof(Http).GetTypeInfo().Assembly.GetName().Version.ToString(2);
+ string version = typeof(JsonHttpClient).GetTypeInfo().Assembly.GetName().Version.ToString(2);
_client.DefaultRequestHeaders.Add("user-agent", $"Discord.Net/{version} (https://github.com/RogueException/Discord.Net)");
#if TEST_RESPONSES
@@ -48,8 +48,8 @@ namespace Discord.Helpers
#endif
}
- private static string _token;
- public static string Token
+ private string _token;
+ public string Token
{
get { return _token; }
set
@@ -61,63 +61,63 @@ namespace Discord.Helpers
}
}
- internal static Task Get(string path)
+ internal Task Get(string path)
where ResponseT : class
=> Send(HttpMethod.Get, path, null);
- internal static Task Get(string path)
+ internal Task Get(string path)
=> Send(HttpMethod.Get, path, null);
- internal static Task Post(string path, object data)
+ internal Task Post(string path, object data)
where ResponseT : class
=> Send(HttpMethod.Post, path, AsJson(data));
- internal static Task Post(string path, object data)
+ internal Task Post(string path, object data)
=> Send(HttpMethod.Post, path, AsJson(data));
- internal static Task Post(string path)
+ internal Task Post(string path)
where ResponseT : class
=> Send(HttpMethod.Post, path, null);
- internal static Task Post(string path)
+ internal Task Post(string path)
=> Send(HttpMethod.Post, path, null);
- internal static Task Put(string path, object data)
+ internal Task Put(string path, object data)
where ResponseT : class
=> Send(HttpMethod.Put, path, AsJson(data));
- internal static Task Put(string path, object data)
+ internal Task Put(string path, object data)
=> Send(HttpMethod.Put, path, AsJson(data));
- internal static Task Put(string path)
+ internal Task Put(string path)
where ResponseT : class
=> Send(HttpMethod.Put, path, null);
- internal static Task Put(string path)
+ internal Task Put(string path)
=> Send(HttpMethod.Put, path, null);
- internal static Task Patch(string path, object data)
+ internal Task Patch(string path, object data)
where ResponseT : class
=> Send(_patch, path, AsJson(data));
- internal static Task Patch(string path, object data)
+ internal Task Patch(string path, object data)
=> Send(_patch, path, AsJson(data));
- internal static Task Patch(string path)
+ internal Task Patch(string path)
where ResponseT : class
=> Send(_patch, path, null);
- internal static Task Patch(string path)
+ internal Task Patch(string path)
=> Send(_patch, path, null);
- internal static Task Delete(string path, object data)
+ internal Task Delete(string path, object data)
where ResponseT : class
=> Send(HttpMethod.Delete, path, AsJson(data));
- internal static Task Delete(string path, object data)
+ internal Task Delete(string path, object data)
=> Send(HttpMethod.Delete, path, AsJson(data));
- internal static Task Delete(string path)
+ internal Task Delete(string path)
where ResponseT : class
=> Send(HttpMethod.Delete, path, null);
- internal static Task Delete(string path)
+ internal Task Delete(string path)
=> Send(HttpMethod.Delete, path, null);
- internal static Task File(string path, Stream stream, string filename = null)
+ internal Task File(string path, Stream stream, string filename = null)
where ResponseT : class
=> Send(HttpMethod.Post, path, AsFormData(stream, filename));
- internal static Task File(string path, Stream stream, string filename = null)
+ internal Task File(string path, Stream stream, string filename = null)
=> Send(HttpMethod.Post, path, AsFormData(stream, filename));
- private static async Task Send(HttpMethod method, string path, HttpContent content)
+ private async Task Send(HttpMethod method, string path, HttpContent content)
where ResponseT : class
{
string responseJson = await SendRequest(method, path, content, true);
@@ -127,17 +127,20 @@ namespace Discord.Helpers
#endif
return JsonConvert.DeserializeObject(responseJson);
}
- private static async Task Send(HttpMethod method, string path, HttpContent content)
- {
- string responseJson = await SendRequest(method, path, content, _isDebug);
#if TEST_RESPONSES
+ private async Task Send(HttpMethod method, string path, HttpContent content)
+ {
+ string responseJson = await SendRequest(method, path, content, true);
if (path.StartsWith(Endpoints.BaseApi))
CheckEmptyResponse(responseJson);
-#endif
return responseJson;
}
+#else
+ private Task Send(HttpMethod method, string path, HttpContent content)
+ => SendRequest(method, path, content, false);
+#endif
- private static async Task SendRequest(HttpMethod method, string path, HttpContent content, bool hasResponse)
+ private async Task SendRequest(HttpMethod method, string path, HttpContent content, bool hasResponse)
{
#if TEST_RESPONSES
Stopwatch stopwatch = Stopwatch.StartNew();
@@ -173,18 +176,18 @@ namespace Discord.Helpers
}
#if TEST_RESPONSES
- private static void CheckEmptyResponse(string json)
+ private void CheckEmptyResponse(string json)
{
if (!string.IsNullOrEmpty(json))
throw new Exception("API check failed: Response is not empty.");
}
#endif
- private static StringContent AsJson(object obj)
+ private StringContent AsJson(object obj)
{
return new StringContent(JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/json");
}
- private static MultipartFormDataContent AsFormData(Stream stream, string filename)
+ private MultipartFormDataContent AsFormData(Stream stream, string filename)
{
var content = new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture));
content.Add(new StreamContent(stream), "file", filename);