Browse Source

Some Http fixes, added SendMessage response

tags/docs-0.9
Brandon Smith 10 years ago
parent
commit
cf63733ee0
4 changed files with 11 additions and 29 deletions
  1. +2
    -2
      Discord.Net/API/DiscordAPI.cs
  2. +1
    -0
      Discord.Net/API/Models/APIResponses.cs
  3. +6
    -2
      Discord.Net/DiscordClient.cs
  4. +2
    -25
      Discord.Net/Helpers/Http.cs

+ 2
- 2
Discord.Net/API/DiscordAPI.cs View File

@@ -77,10 +77,10 @@ namespace Discord.API
} }
//Chat //Chat
public static Task SendMessage(string channelId, string message, string[] mentions, HttpOptions options)
public static Task<APIResponses.SendMessage> SendMessage(string channelId, string message, string[] mentions, HttpOptions options)
{ {
var request = new APIRequests.SendMessage { Content = message, Mentions = mentions }; var request = new APIRequests.SendMessage { Content = message, Mentions = mentions };
return Http.Post(Endpoints.ChannelMessages(channelId), request, options);
return Http.Post<APIResponses.SendMessage>(Endpoints.ChannelMessages(channelId), request, options);
} }
public static Task SendIsTyping(string channelId, HttpOptions options) public static Task SendIsTyping(string channelId, HttpOptions options)
{ {


+ 1
- 0
Discord.Net/API/Models/APIResponses.cs View File

@@ -56,6 +56,7 @@ namespace Discord.API.Models
} }
public class AcceptInvite : GetInvite { } public class AcceptInvite : GetInvite { }


public class SendMessage : Message { }
public class GetMessages : Message { } public class GetMessages : Message { }


public class GetRegions public class GetRegions


+ 6
- 2
Discord.Net/DiscordClient.cs View File

@@ -521,14 +521,18 @@ namespace Discord
{ {
CheckReady(); CheckReady();
if (text.Length <= 2000) if (text.Length <= 2000)
await DiscordAPI.SendMessage(channelId, text, mentions, _httpOptions);
{
var msg = await DiscordAPI.SendMessage(channelId, text, mentions, _httpOptions);
_messages.Update(msg.Id, msg);
}
else else
{ {
int blockCount = (int)Math.Ceiling(text.Length / (double)MaxMessageSize); int blockCount = (int)Math.Ceiling(text.Length / (double)MaxMessageSize);
for (int i = 0; i < blockCount; i++) for (int i = 0; i < blockCount; i++)
{ {
int index = i * MaxMessageSize; int index = i * MaxMessageSize;
await DiscordAPI.SendMessage(channelId, text.Substring(index, Math.Min(2000, text.Length - index)), mentions, _httpOptions);
var msg = await DiscordAPI.SendMessage(channelId, text.Substring(index, Math.Min(2000, text.Length - index)), mentions, _httpOptions);
_messages.Update(msg.Id, msg);
await Task.Delay(1000); await Task.Delay(1000);
} }
} }


+ 2
- 25
Discord.Net/Helpers/Http.cs View File

@@ -31,11 +31,6 @@ namespace Discord.Helpers
private const bool _isDebug = false; private const bool _isDebug = false;
#endif #endif
internal static Task<ResponseT> Get<ResponseT>(string path, object data, HttpOptions options)
where ResponseT : class
=> Send<ResponseT>("GET", path, data, options);
internal static Task<string> Get(string path, object data, HttpOptions options)
=> Send("GET", path, data, options);
internal static Task<ResponseT> Get<ResponseT>(string path, HttpOptions options) internal static Task<ResponseT> Get<ResponseT>(string path, HttpOptions options)
where ResponseT : class where ResponseT : class
=> Send<ResponseT>("GET", path, null, options); => Send<ResponseT>("GET", path, null, options);
@@ -89,7 +84,7 @@ namespace Discord.Helpers
internal static async Task<ResponseT> Send<ResponseT>(string method, string path, object data, HttpOptions options) internal static async Task<ResponseT> Send<ResponseT>(string method, string path, object data, HttpOptions options)
where ResponseT : class where ResponseT : class
{ {
string requestJson = JsonConvert.SerializeObject(data);
string requestJson = data != null ? JsonConvert.SerializeObject(data) : null;
string responseJson = await SendRequest(method, path, requestJson, options, true); string responseJson = await SendRequest(method, path, requestJson, options, true);
var response = JsonConvert.DeserializeObject<ResponseT>(responseJson); var response = JsonConvert.DeserializeObject<ResponseT>(responseJson);
#if DEBUG #if DEBUG
@@ -99,31 +94,13 @@ namespace Discord.Helpers
} }
internal static async Task<string> Send(string method, string path, object data, HttpOptions options) internal static async Task<string> Send(string method, string path, object data, HttpOptions options)
{ {
string requestJson = JsonConvert.SerializeObject(data);
string requestJson = data != null ? JsonConvert.SerializeObject(data) : null;
string responseJson = await SendRequest(method, path, requestJson, options, _isDebug); string responseJson = await SendRequest(method, path, requestJson, options, _isDebug);
#if DEBUG #if DEBUG
CheckEmptyResponse(responseJson); CheckEmptyResponse(responseJson);
#endif #endif
return responseJson; return responseJson;
} }
internal static async Task<ResponseT> Send<ResponseT>(string method, string path, HttpOptions options)
where ResponseT : class
{
string responseJson = await SendRequest(method, path, null, options, true);
var response = JsonConvert.DeserializeObject<ResponseT>(responseJson);
#if DEBUG
CheckResponse(responseJson, response);
#endif
return response;
}
internal static async Task<string> Send(string method, string path, HttpOptions options)
{
string responseJson = await SendRequest(method, path, null, options, _isDebug);
#if DEBUG
CheckEmptyResponse(responseJson);
#endif
return responseJson;
}


private static async Task<string> SendRequest(string method, string path, string data, HttpOptions options, bool hasResponse) private static async Task<string> SendRequest(string method, string path, string data, HttpOptions options, bool hasResponse)
{ {


Loading…
Cancel
Save