@@ -40,7 +40,11 @@ namespace Discord | |||
config.VoiceClientId = unchecked(++_nextVoiceClientId); | |||
return new DiscordWSClient(config, server.Id); | |||
}); | |||
client.LogMessage += (s, e) => RaiseOnLog(e.Severity, e.Source, $"(#{client.Config.VoiceClientId}) {e.Message}", e.Exception); | |||
client.LogMessage += (s, e) => | |||
{ | |||
if (e.Source != LogMessageSource.DataWebSocket) | |||
RaiseOnLog(e.Severity, e.Source, $"(#{client.Config.VoiceClientId}) {e.Message}", e.Exception); | |||
}; | |||
await client.Connect(_gateway, _token).ConfigureAwait(false); | |||
return client; | |||
} | |||
@@ -132,9 +132,9 @@ namespace Discord | |||
_api.RestClient.OnRequest += (s, e) => | |||
{ | |||
if (e.Payload != null) | |||
RaiseOnLog(LogMessageSeverity.Verbose, LogMessageSource.Rest, $"{e.Method.Method} {e.Path}: {Math.Round(e.ElapsedMilliseconds, 2)} ms ({e.Payload})"); | |||
RaiseOnLog(LogMessageSeverity.Verbose, LogMessageSource.Rest, $"{e.Method} {e.Path}: {Math.Round(e.ElapsedMilliseconds, 2)} ms ({e.Payload})"); | |||
else | |||
RaiseOnLog(LogMessageSeverity.Verbose, LogMessageSource.Rest, $"{e.Method.Method} {e.Path}: {Math.Round(e.ElapsedMilliseconds, 2)} ms"); | |||
RaiseOnLog(LogMessageSeverity.Verbose, LogMessageSource.Rest, $"{e.Method} {e.Path}: {Math.Round(e.ElapsedMilliseconds, 2)} ms"); | |||
}; | |||
} | |||
if (_config.LogLevel >= LogMessageSeverity.Debug) | |||
@@ -79,15 +79,12 @@ namespace Discord | |||
if (e.WasUnexpected) | |||
await socket.Reconnect(_token).ConfigureAwait(false); | |||
}; | |||
if (!_config.VoiceOnly) | |||
socket.LogMessage += (s, e) => RaiseOnLog(e.Severity, LogMessageSource.DataWebSocket, e.Message, e.Exception); | |||
if (_config.LogLevel >= LogMessageSeverity.Info) | |||
{ | |||
socket.LogMessage += (s, e) => RaiseOnLog(e.Severity, LogMessageSource.DataWebSocket, e.Message, e.Exception); | |||
if (_config.LogLevel >= LogMessageSeverity.Info) | |||
{ | |||
socket.Connected += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.DataWebSocket, "Connected"); | |||
socket.Disconnected += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.DataWebSocket, "Disconnected"); | |||
} | |||
socket.Connected += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.DataWebSocket, "Connected"); | |||
socket.Disconnected += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.DataWebSocket, "Disconnected"); | |||
} | |||
socket.ReceivedEvent += async (s, e) => await OnReceivedEvent(e).ConfigureAwait(false); | |||
@@ -96,7 +93,6 @@ namespace Discord | |||
internal virtual VoiceWebSocket CreateVoiceSocket() | |||
{ | |||
var socket = new VoiceWebSocket(this); | |||
socket.LogMessage += (s, e) => RaiseOnLog(e.Severity, LogMessageSource.VoiceWebSocket, e.Message, e.Exception); | |||
socket.Connected += (s, e) => RaiseVoiceConnected(); | |||
socket.Disconnected += async (s, e) => | |||
{ | |||
@@ -104,11 +100,14 @@ namespace Discord | |||
if (e.WasUnexpected) | |||
await socket.Reconnect().ConfigureAwait(false); | |||
}; | |||
socket.LogMessage += (s, e) => RaiseOnLog(e.Severity, LogMessageSource.VoiceWebSocket, e.Message, e.Exception); | |||
if (_config.LogLevel >= LogMessageSeverity.Info) | |||
{ | |||
socket.Connected += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.VoiceWebSocket, "Connected"); | |||
socket.Disconnected += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.VoiceWebSocket, "Disconnected"); | |||
} | |||
return socket; | |||
} | |||
@@ -1,5 +1,4 @@ | |||
using System.Net.Http; | |||
using System.Threading; | |||
using System.Threading; | |||
using System.Threading.Tasks; | |||
namespace Discord.Net.Rest | |||
@@ -7,7 +6,7 @@ namespace Discord.Net.Rest | |||
internal interface IRestEngine | |||
{ | |||
void SetToken(string token); | |||
Task<string> Send(HttpMethod method, string path, string json, CancellationToken cancelToken); | |||
Task<string> SendFile(HttpMethod method, string path, string filePath, CancellationToken cancelToken); | |||
Task<string> Send(string method, string path, string json, CancellationToken cancelToken); | |||
Task<string> SendFile(string method, string path, string filePath, CancellationToken cancelToken); | |||
} | |||
} |
@@ -1,5 +1,4 @@ | |||
using System; | |||
using System.Net.Http; | |||
namespace Discord.Net.Rest | |||
{ | |||
@@ -7,11 +6,11 @@ namespace Discord.Net.Rest | |||
{ | |||
public class RequestEventArgs : EventArgs | |||
{ | |||
public HttpMethod Method { get; } | |||
public string Method { get; } | |||
public string Path { get; } | |||
public string Payload { get; } | |||
public double ElapsedMilliseconds { get; } | |||
public RequestEventArgs(HttpMethod method, string path, string payload, double milliseconds) | |||
public RequestEventArgs(string method, string path, string payload, double milliseconds) | |||
{ | |||
Method = method; | |||
Path = path; | |||
@@ -21,7 +20,7 @@ namespace Discord.Net.Rest | |||
} | |||
public event EventHandler<RequestEventArgs> OnRequest; | |||
private void RaiseOnRequest(HttpMethod method, string path, string payload, double milliseconds) | |||
private void RaiseOnRequest(string method, string path, string payload, double milliseconds) | |||
{ | |||
if (OnRequest != null) | |||
OnRequest(this, new RequestEventArgs(method, path, payload, milliseconds)); | |||
@@ -2,7 +2,6 @@ | |||
using Newtonsoft.Json; | |||
using System; | |||
using System.Diagnostics; | |||
using System.Net.Http; | |||
using System.Threading; | |||
using System.Threading.Tasks; | |||
@@ -27,69 +26,64 @@ namespace Discord.Net.Rest | |||
public void SetToken(string token) => _engine.SetToken(token); | |||
//DELETE | |||
private static readonly HttpMethod _delete = HttpMethod.Delete; | |||
internal Task<ResponseT> Delete<ResponseT>(string path, object data) where ResponseT : class | |||
=> Send<ResponseT>(_delete, path, data); | |||
=> Send<ResponseT>("DELETE", path, data); | |||
internal Task<ResponseT> Delete<ResponseT>(string path) where ResponseT : class | |||
=> Send<ResponseT>(_delete, path); | |||
=> Send<ResponseT>("DELETE", path); | |||
internal Task Delete(string path, object data) | |||
=> Send(_delete, path, data); | |||
=> Send("DELETE", path, data); | |||
internal Task Delete(string path) | |||
=> Send(_delete, path); | |||
=> Send("DELETE", path); | |||
//GET | |||
private static readonly HttpMethod _get = HttpMethod.Get; | |||
internal Task<ResponseT> Get<ResponseT>(string path) where ResponseT : class | |||
=> Send<ResponseT>(_get, path); | |||
=> Send<ResponseT>("GET", path); | |||
internal Task Get(string path) | |||
=> Send(_get, path); | |||
=> Send("GET", path); | |||
//PATCH | |||
private static readonly HttpMethod _patch = new HttpMethod("PATCH"); | |||
internal Task<ResponseT> Patch<ResponseT>(string path, object data) where ResponseT : class | |||
=> Send<ResponseT>(_patch, path, data); | |||
=> Send<ResponseT>("PATCH", path, data); | |||
internal Task Patch(string path, object data) | |||
=> Send(_patch, path, data); | |||
private static readonly HttpMethod _post = HttpMethod.Post; | |||
=> Send("PATCH", path, data); | |||
internal Task<ResponseT> Post<ResponseT>(string path, object data) where ResponseT : class | |||
=> Send<ResponseT>(_post, path, data); | |||
=> Send<ResponseT>("POST", path, data); | |||
internal Task<ResponseT> Post<ResponseT>(string path) where ResponseT : class | |||
=> Send<ResponseT>(_post, path); | |||
=> Send<ResponseT>("POST", path); | |||
internal Task Post(string path, object data) | |||
=> Send(_post, path, data); | |||
=> Send("POST", path, data); | |||
internal Task Post(string path) | |||
=> Send(_post, path); | |||
private static readonly HttpMethod _put = HttpMethod.Put; | |||
=> Send("POST", path); | |||
internal Task<ResponseT> Put<ResponseT>(string path, object data) where ResponseT : class | |||
=> Send<ResponseT>(_put, path, data); | |||
=> Send<ResponseT>("PUT", path, data); | |||
internal Task<ResponseT> Put<ResponseT>(string path) where ResponseT : class | |||
=> Send<ResponseT>(_put, path); | |||
=> Send<ResponseT>("PUT", path); | |||
internal Task Put(string path, object data) | |||
=> Send(_put, path, data); | |||
=> Send("PUT", path, data); | |||
internal Task Put(string path) | |||
=> Send(_put, path); | |||
=> Send("PUT", path); | |||
internal Task<ResponseT> PostFile<ResponseT>(string path, string filePath) where ResponseT : class | |||
=> SendFile<ResponseT>(_post, path, filePath); | |||
=> SendFile<ResponseT>("POST", path, filePath); | |||
internal Task PostFile(string path, string filePath) | |||
=> SendFile(_post, path, filePath); | |||
=> SendFile("POST", path, filePath); | |||
internal Task<ResponseT> PutFile<ResponseT>(string path, string filePath) where ResponseT : class | |||
=> SendFile<ResponseT>(_put, path, filePath); | |||
=> SendFile<ResponseT>("PUT", path, filePath); | |||
internal Task PutFile(string path, string filePath) | |||
=> SendFile(_put, path, filePath); | |||
=> SendFile("PUT", path, filePath); | |||
private async Task<ResponseT> Send<ResponseT>(HttpMethod method, string path, object content = null) | |||
private async Task<ResponseT> Send<ResponseT>(string method, string path, object content = null) | |||
where ResponseT : class | |||
{ | |||
string responseJson = await Send(method, path, content, true).ConfigureAwait(false); | |||
return DeserializeResponse<ResponseT>(responseJson); | |||
} | |||
private Task Send(HttpMethod method, string path, object content = null) | |||
private Task Send(string method, string path, object content = null) | |||
=> Send(method, path, content, false); | |||
private async Task<string> Send(HttpMethod method, string path, object content, bool hasResponse) | |||
private async Task<string> Send(string method, string path, object content, bool hasResponse) | |||
{ | |||
Stopwatch stopwatch = null; | |||
string requestJson = null; | |||
@@ -123,15 +117,15 @@ namespace Discord.Net.Rest | |||
return responseJson; | |||
} | |||
private async Task<ResponseT> SendFile<ResponseT>(HttpMethod method, string path, string filePath) | |||
private async Task<ResponseT> SendFile<ResponseT>(string method, string path, string filePath) | |||
where ResponseT : class | |||
{ | |||
string responseJson = await SendFile(method, path, filePath, true).ConfigureAwait(false); | |||
return DeserializeResponse<ResponseT>(responseJson); | |||
} | |||
private Task SendFile(HttpMethod method, string path, string filePath) | |||
private Task SendFile(string method, string path, string filePath) | |||
=> SendFile(method, path, filePath, false); | |||
private async Task<string> SendFile(HttpMethod method, string path, string filePath, bool hasResponse) | |||
private async Task<string> SendFile(string method, string path, string filePath, bool hasResponse) | |||
{ | |||
Stopwatch stopwatch = null; | |||
@@ -3,7 +3,6 @@ using Discord.API; | |||
using RestSharp; | |||
using System; | |||
using System.Net; | |||
using System.Net.Http; | |||
using System.Threading; | |||
using System.Threading.Tasks; | |||
@@ -39,13 +38,13 @@ namespace Discord.Net.Rest | |||
_client.AddDefaultHeader("authorization", token); | |||
} | |||
public Task<string> Send(HttpMethod method, string path, string json, CancellationToken cancelToken) | |||
public Task<string> Send(string method, string path, string json, CancellationToken cancelToken) | |||
{ | |||
var request = new RestRequest(path, GetMethod(method)); | |||
request.AddParameter("application/json", json, ParameterType.RequestBody); | |||
return Send(request, cancelToken); | |||
} | |||
public Task<string> SendFile(HttpMethod method, string path, string filePath, CancellationToken cancelToken) | |||
public Task<string> SendFile(string method, string path, string filePath, CancellationToken cancelToken) | |||
{ | |||
var request = new RestRequest(path, Method.POST); | |||
request.AddFile("file", filePath); | |||
@@ -70,9 +69,9 @@ namespace Discord.Net.Rest | |||
} | |||
} | |||
private Method GetMethod(HttpMethod method) | |||
private Method GetMethod(string method) | |||
{ | |||
switch (method.Method) | |||
switch (method) | |||
{ | |||
case "DELETE": return Method.DELETE; | |||
case "GET": return Method.GET; | |||
@@ -35,18 +35,12 @@ | |||
"frameworks": { | |||
"net45": { | |||
"frameworkAssemblies": { | |||
"System.Net.Http": "4.0.0.0" | |||
}, | |||
"dependencies": { | |||
"RestSharp": "105.2.3", | |||
"WebSocketSharp": "1.0.3-rc9" | |||
} | |||
}, | |||
"dnx451": { | |||
"frameworkAssemblies": { | |||
"System.Net.Http": "4.0.0.0" | |||
}, | |||
"dependencies": { | |||
"RestSharp": "105.2.3", | |||
"WebSocketSharp": "1.0.3-rc9" | |||