}
[Fact]
+ public async Task StatusesUpdate_Test()
+ {
+ using (var twitterApi = new TwitterApi())
+ {
+ var mock = new Mock<IApiConnection>();
+ mock.Setup(x =>
+ x.PostLazyAsync<TwitterStatus>(
+ new Uri("statuses/update.json", UriKind.Relative),
+ new Dictionary<string, string> {
+ { "status", "hogehoge" },
+ { "include_entities", "true" },
+ { "include_ext_alt_text", "true" },
+ { "in_reply_to_status_id", "100" },
+ { "media_ids", "10,20" },
+ })
+ )
+ .ReturnsAsync(LazyJson.Create(new TwitterStatus()));
+
+ twitterApi.apiConnection = mock.Object;
+
+ await twitterApi.StatusesUpdate("hogehoge", replyToId: 100L, mediaIds: new[] { 10L, 20L })
+ .IgnoreResponse()
+ .ConfigureAwait(false);
+
+ mock.VerifyAll();
+ }
+ }
+
+ [Fact]
public async Task StatusesDestroy_Test()
{
using (var twitterApi = new TwitterApi())
return this.apiConnection.GetAsync<TwitterStatus>(endpoint, param, "/statuses/show/:id");
}
+ public Task<LazyJson<TwitterStatus>> StatusesUpdate(string status, long? replyToId, IReadOnlyList<long> mediaIds)
+ {
+ var endpoint = new Uri("statuses/update.json", UriKind.Relative);
+ var param = new Dictionary<string, string>
+ {
+ ["status"] = status,
+ ["include_entities"] = "true",
+ ["include_ext_alt_text"] = "true",
+ };
+
+ if (replyToId != null)
+ param["in_reply_to_status_id"] = replyToId.ToString();
+ if (mediaIds != null && mediaIds.Count > 0)
+ param.Add("media_ids", string.Join(",", mediaIds));
+
+ return this.apiConnection.PostLazyAsync<TwitterStatus>(endpoint, param);
+ }
+
public Task<LazyJson<TwitterStatus>> StatusesDestroy(long statusId)
{
var endpoint = new Uri("statuses/destroy.json", UriKind.Relative);
this.Initialize("", "", "", 0);
}
- public HttpStatusCode UpdateStatus(string status, long? replyToId, List<long> mediaIds, ref string content)
- {
- Dictionary<string, string> param = new Dictionary<string, string>();
- param.Add("status", status);
- if (replyToId != null) param.Add("in_reply_to_status_id", replyToId.ToString());
- param.Add("include_entities", "true");
- param.Add("include_ext_alt_text", "true");
- //if (AppendSettingDialog.Instance.ShortenTco && AppendSettingDialog.Instance.UrlConvertAuto) param.Add("wrap_links", "true")
-
- if (mediaIds != null && mediaIds.Count > 0)
- param.Add("media_ids", string.Join(",", mediaIds));
-
- return httpCon.GetContent(PostMethod,
- this.CreateTwitterUri("/1.1/statuses/update.json"),
- param,
- ref content,
- null,
- null);
- }
-
public HttpStatusCode UpdateStatusWithMedia(string status, long? replyToId, IMediaItem item, ref string content)
{
//画像投稿用エンドポイント
var textWithImageUrl = text + " " + imageUrl.Trim();
- await Task.Run(() => this.twitter.PostStatus(textWithImageUrl, inReplyToStatusId))
+ await this.twitter.PostStatus(textWithImageUrl, inReplyToStatusId)
.ConfigureAwait(false);
}
var textWithImageUrl = text + " " + imageUrlElm.Value.Trim();
- await Task.Run(() => this.twitter.PostStatus(textWithImageUrl, inReplyToStatusId))
+ await this.twitter.PostStatus(textWithImageUrl, inReplyToStatusId)
.ConfigureAwait(false);
}
var textWithImageUrl = text + " " + imageUrlElm.Value.Trim();
- await Task.Run(() => this.twitter.PostStatus(textWithImageUrl, inReplyToStatusId))
+ await this.twitter.PostStatus(textWithImageUrl, inReplyToStatusId)
.ConfigureAwait(false);
}
throw new ArgumentException("Err:Media not found.");
}
- await Task.Run(() => this.tw.PostStatusWithMultipleMedia(text, inReplyToStatusId, mediaItems))
+ await this.tw.PostStatusWithMultipleMedia(text, inReplyToStatusId, mediaItems)
.ConfigureAwait(false);
}
var textWithImageUrl = text + " " + imageUrlElm.Value.Trim();
- await Task.Run(() => this.tw.PostStatus(textWithImageUrl, inReplyToStatusId))
+ await this.tw.PostStatus(textWithImageUrl, inReplyToStatusId)
.ConfigureAwait(false);
}
var textWithImageUrl = text + " " + imageUrlElm.Value.Trim();
- await Task.Run(() => this.tw.PostStatus(textWithImageUrl, inReplyToStatusId))
+ await this.tw.PostStatus(textWithImageUrl, inReplyToStatusId)
.ConfigureAwait(false);
}
private SettingCommon _cfgCommon;
//twitter解析部
- private Twitter tw = new Twitter();
private TwitterApi twitterApi = new TwitterApi();
+ private Twitter tw;
//Growl呼び出し部
private GrowlHelper gh = new GrowlHelper(Application.ProductName);
this._cfgCommon.AutoShortUrlFirst = MyCommon.UrlConverter.Uxnu;
HttpTwitter.TwitterUrl = this._cfgCommon.TwitterUrl;
+ this.tw = new Twitter(this.twitterApi);
//認証関連
if (string.IsNullOrEmpty(this._cfgCommon.Token)) this._cfgCommon.UserName = "";
{
if (status.mediaItems == null || status.mediaItems.Length == 0)
{
- this.tw.PostStatus(status.status, status.inReplyToId);
+ await this.tw.PostStatus(status.status, status.inReplyToId)
+ .ConfigureAwait(false);
}
else
{
//private FavoriteQueue favQueue;
private HttpTwitter twCon = new HttpTwitter();
+ private TwitterApi Api { get; }
//private List<PostClass> _deletemessages = new List<PostClass>();
- public Twitter()
+ public Twitter() : this(new TwitterApi())
{
+ }
+
+ public Twitter(TwitterApi api)
+ {
+ this.Api = api;
this.Configuration = TwitterConfiguration.DefaultConfiguration();
}
return false;
}
- public void PostStatus(string postStr, long? reply_to, List<long> mediaIds = null)
+ public async Task PostStatus(string postStr, long? reply_to, IReadOnlyList<long> mediaIds = null)
{
this.CheckAccountState();
return;
}
- HttpStatusCode res;
- var content = "";
- try
- {
- res = twCon.UpdateStatus(postStr, reply_to, mediaIds, ref content);
- }
- catch(Exception ex)
- {
- throw new WebApiException("Err:" + ex.Message, ex);
- }
-
- // 投稿に成功していても404が返ることがあるらしい: https://dev.twitter.com/discussions/1213
- if (res == HttpStatusCode.NotFound)
- return;
-
- this.CheckStatusCode(res, content);
+ var response = await this.Api.StatusesUpdate(postStr, reply_to, mediaIds)
+ .ConfigureAwait(false);
- TwitterStatus status;
- try
- {
- status = TwitterStatus.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 status = await response.LoadJsonAsync()
+ .ConfigureAwait(false);
this.UpdateUserStats(status.User);
}
}
- public void PostStatusWithMultipleMedia(string postStr, long? reply_to, IMediaItem[] mediaItems)
+ public async Task PostStatusWithMultipleMedia(string postStr, long? reply_to, IMediaItem[] mediaItems)
{
this.CheckAccountState();
if (mediaIds.Count == 0)
throw new WebApiException("Err:Invalid Files!");
- PostStatus(postStr, reply_to, mediaIds);
+ await this.PostStatus(postStr, reply_to, mediaIds)
+ .ConfigureAwait(false);
}
public long UploadMedia(IMediaItem item)