}
[Fact]
+ public async Task DirectMessagesNew_Test()
+ {
+ using (var twitterApi = new TwitterApi())
+ {
+ var mock = new Mock<IApiConnection>();
+ mock.Setup(x =>
+ x.PostLazyAsync<TwitterDirectMessage>(
+ new Uri("direct_messages/new.json", UriKind.Relative),
+ new Dictionary<string, string> {
+ { "text", "hogehoge" },
+ { "screen_name", "opentween" },
+ })
+ )
+ .ReturnsAsync(LazyJson.Create(new TwitterDirectMessage()));
+
+ twitterApi.apiConnection = mock.Object;
+
+ await twitterApi.DirectMessagesNew("hogehoge", "opentween")
+ .IgnoreResponse()
+ .ConfigureAwait(false);
+
+ mock.VerifyAll();
+ }
+ }
+
+ [Fact]
public async Task DirectMessagesDestroy_Test()
{
using (var twitterApi = new TwitterApi())
return this.apiConnection.PostLazyAsync<TwitterStatus>(endpoint, param);
}
+ public Task<LazyJson<TwitterDirectMessage>> DirectMessagesNew(string status, string sendTo)
+ {
+ var endpoint = new Uri("direct_messages/new.json", UriKind.Relative);
+ var param = new Dictionary<string, string>
+ {
+ ["text"] = status,
+ ["screen_name"] = sendTo,
+ };
+
+ return this.apiConnection.PostLazyAsync<TwitterDirectMessage>(endpoint, param);
+ }
+
public Task<LazyJson<TwitterDirectMessage>> DirectMessagesDestroy(long statusId)
{
var endpoint = new Uri("direct_messages/destroy.json", UriKind.Relative);
null);
}
- public HttpStatusCode SendDirectMessage(string status, string sendto, ref string content)
- {
- Dictionary<string, string> param = new Dictionary<string, string>();
- param.Add("text", status);
- param.Add("screen_name", sendto);
- //if (AppendSettingDialog.Instance.ShortenTco && AppendSettingDialog.Instance.UrlConvertAuto) param.Add("wrap_links", "true")
-
- return httpCon.GetContent(PostMethod,
- this.CreateTwitterUri("/1.1/direct_messages/new.json"),
- param,
- ref content,
- null,
- null);
- }
-
public HttpStatusCode RetweetStatus(long id, ref string content)
{
Dictionary<string, string> param = new Dictionary<string, string>();
public async Task SendByDmAsync()
{
- await Task.Run(() => this.tw.SendDirectMessage(this.EncodedReportForDM));
+ await this.tw.SendDirectMessage(this.EncodedReportForDM);
}
private void UpdateEncodedReport()
if (mediaIds == null &&
Twitter.DMSendTextRegex.IsMatch(postStr))
{
- SendDirectMessage(postStr);
+ await this.SendDirectMessage(postStr)
+ .ConfigureAwait(false);
return;
}
if (Twitter.DMSendTextRegex.IsMatch(postStr))
{
- SendDirectMessage(postStr);
+ await this.SendDirectMessage(postStr)
+ .ConfigureAwait(false);
return;
}
return status.MediaId;
}
- public void SendDirectMessage(string postStr)
+ public async Task SendDirectMessage(string postStr)
{
this.CheckAccountState();
this.CheckAccessLevel(TwitterApiAccessLevel.ReadWriteAndDirectMessage);
var mc = Twitter.DMSendTextRegex.Match(postStr);
- HttpStatusCode res;
- var content = "";
- try
- {
- res = twCon.SendDirectMessage(mc.Groups["body"].Value, mc.Groups["id"].Value, ref content);
- }
- catch(Exception ex)
- {
- throw new WebApiException("Err:" + ex.Message, ex);
- }
-
- this.CheckStatusCode(res, content);
+ var response = await this.Api.DirectMessagesNew(mc.Groups["body"].Value, mc.Groups["id"].Value)
+ .ConfigureAwait(false);
- TwitterDirectMessage status;
- try
- {
- status = TwitterDirectMessage.ParseJson(content);
- }
- catch(SerializationException ex)
- {
- MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
- throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
- }
- catch(Exception ex)
- {
- MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
- throw new WebApiException("Err:Invalid Json!", content, ex);
- }
+ var dm = await response.LoadJsonAsync()
+ .ConfigureAwait(false);
- this.UpdateUserStats(status.Sender);
+ this.UpdateUserStats(dm.Sender);
}
public void PostRetweet(long id, bool read)