開発に使用するリポジトリ
Revisión | bf2677bd6dd0761de4034589419cb6abf4043570 (tree) |
---|---|
Tiempo | 2013-08-25 15:08:07 |
Autor | Kimura Youichi <kim.upsilon@bucy...> |
Commiter | Kimura Youichi |
REST APIのエラー処理を統一
https://dev.twitter.com/docs/error-codes-responses に記載されているエラーコードを考慮
@@ -5,6 +5,7 @@ | ||
5 | 5 | // (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/> |
6 | 6 | // (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow> |
7 | 7 | // (c) 2011 Egtra (@egtra) <http://dev.activebasic.com/egtra/> |
8 | +// (c) 2013 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/> | |
8 | 9 | // All rights reserved. |
9 | 10 | // |
10 | 11 | // This file is part of OpenTween. |
@@ -428,8 +429,80 @@ namespace OpenTween | ||
428 | 429 | [DataContract] |
429 | 430 | public class ErrorResponse |
430 | 431 | { |
431 | - [DataMember(Name = "request")] public string Request; | |
432 | - [DataMember(Name = "error")] public string ErrMsg; | |
432 | + [DataMember(Name = "errors")] public ErrorItem[] Errors; | |
433 | + } | |
434 | + | |
435 | + [DataContract] | |
436 | + public class ErrorItem | |
437 | + { | |
438 | + [DataMember(Name = "code")] public ErrorCode Code; | |
439 | + [DataMember(Name = "message")] public string Message; | |
440 | + | |
441 | + public override string ToString() | |
442 | + { | |
443 | + if (Enum.IsDefined(typeof(ErrorCode), this.Code)) | |
444 | + return this.Code.ToString(); | |
445 | + else | |
446 | + return this.Message; | |
447 | + } | |
448 | + } | |
449 | + | |
450 | + /// <summary> | |
451 | + /// Twitter API から返されるエラーコード | |
452 | + /// </summary> | |
453 | + /// <remarks> | |
454 | + /// https://dev.twitter.com/docs/error-codes-responses を参照 | |
455 | + /// </remarks> | |
456 | + public enum ErrorCode : int | |
457 | + { | |
458 | + /// <summary> | |
459 | + /// 不正なリクエスト等によって認証を完了できない場合に発生する。大体クライアントのせい | |
460 | + /// </summary> | |
461 | + AuthError = 32, | |
462 | + /// <summary> | |
463 | + /// 指定されたリソースが存在しません。HTTP 404 と同等 | |
464 | + /// </summary> | |
465 | + NotFound = 34, | |
466 | + /// <summary> | |
467 | + /// REST API v1 は星になりました | |
468 | + /// </summary> | |
469 | + APIv1Retired = 68, | |
470 | + /// <summary> | |
471 | + /// レートリミットに到達しました | |
472 | + /// </summary> | |
473 | + RateLimit = 88, | |
474 | + /// <summary> | |
475 | + /// アクセストークンが無効です。不正なトークンまたはユーザーによって失効されています | |
476 | + /// </summary> | |
477 | + InvalidToken = 89, | |
478 | + /// <summary> | |
479 | + /// アカウントが凍結されています | |
480 | + /// </summary> | |
481 | + SuspendedAccount = 64, | |
482 | + /// <summary> | |
483 | + /// サーバーの過負荷によって一時的にアクセスできません | |
484 | + /// </summary> | |
485 | + OverCapacity = 130, | |
486 | + /// <summary> | |
487 | + /// サーバーの内部エラー | |
488 | + /// </summary> | |
489 | + InternalError = 131, | |
490 | + /// <summary> | |
491 | + /// ユーザーからブロックされている (公式ドキュメントに記述無し) | |
492 | + /// </summary> | |
493 | + Blocked = 136, | |
494 | + /// <summary> | |
495 | + /// oauth_timestamp の時刻が無効。クライアントかサーバーの時計が大幅にずれている | |
496 | + /// </summary> | |
497 | + TimestampOutOfRange = 135, | |
498 | + /// <summary> | |
499 | + /// 投稿されたステータスが重複しています | |
500 | + /// </summary> | |
501 | + DuplicateStatus = 187, | |
502 | + /// <summary> | |
503 | + /// 認証が必要な API で認証データが含まれていない、または認証データが不正 | |
504 | + /// </summary> | |
505 | + AuthenticationRequired = 215, | |
433 | 506 | } |
434 | 507 | |
435 | 508 | [DataContract] |
@@ -1,7 +1,9 @@ | ||
1 | 1 | 更新履歴 |
2 | 2 | |
3 | 3 | ==== Ver 1.1.3-beta1(2013/xx/xx) |
4 | + * CHG: Twitter API のエラー処理を改善 | |
4 | 5 | * FIX: 「片思いユーザーリストを取得する」を無効にするとRT非表示設定が反映されない問題の修正 (thx @ui_nyan!) |
6 | + * FIX: ブロックされているユーザーのツイートをふぁぼった時にUserStreamsが切断されてしまう問題の修正 | |
5 | 7 | |
6 | 8 | ==== Ver 1.1.2(2013/07/06) |
7 | 9 | * NEW: 画像アップロード対応サービスについっぷるフォトを追加 |
@@ -2575,19 +2575,7 @@ namespace OpenTween | ||
2575 | 2575 | bw.ReportProgress(200); |
2576 | 2576 | if (string.IsNullOrEmpty(args.status.imagePath)) |
2577 | 2577 | { |
2578 | - for (int i = 0; i <= 1; i++) | |
2579 | - { | |
2580 | - ret = tw.PostStatus(args.status.status, args.status.inReplyToId); | |
2581 | - if (string.IsNullOrEmpty(ret) || | |
2582 | - ret.StartsWith("OK:") || | |
2583 | - ret.StartsWith("Outputz:") || | |
2584 | - ret.StartsWith("Warn:") || | |
2585 | - ret == "Err:Status is a duplicate." || | |
2586 | - args.status.status.StartsWith("D", StringComparison.OrdinalIgnoreCase) || | |
2587 | - args.status.status.StartsWith("DM", StringComparison.OrdinalIgnoreCase) || | |
2588 | - Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) | |
2589 | - break; | |
2590 | - } | |
2578 | + ret = tw.PostStatus(args.status.status, args.status.inReplyToId); | |
2591 | 2579 | } |
2592 | 2580 | else |
2593 | 2581 | { |
@@ -5,6 +5,7 @@ | ||
5 | 5 | // (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/> |
6 | 6 | // (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow> |
7 | 7 | // (c) 2011 Egtra (@egtra) <http://dev.activebasic.com/egtra/> |
8 | +// (c) 2013 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/> | |
8 | 9 | // All rights reserved. |
9 | 10 | // |
10 | 11 | // This file is part of OpenTween. |
@@ -28,6 +29,7 @@ using System.Diagnostics; | ||
28 | 29 | using System.IO; |
29 | 30 | using System.Linq; |
30 | 31 | using System.Net; |
32 | +using System.Runtime.CompilerServices; | |
31 | 33 | using System.Runtime.Serialization; |
32 | 34 | using System.Runtime.Serialization.Json; |
33 | 35 | using System.Text; |
@@ -191,10 +193,10 @@ namespace OpenTween | ||
191 | 193 | |
192 | 194 | public string Authenticate(string username, string password) |
193 | 195 | { |
196 | + this.ResetApiStatus(); | |
197 | + | |
194 | 198 | HttpStatusCode res; |
195 | 199 | var content = ""; |
196 | - | |
197 | - this.ResetApiStatus(); | |
198 | 200 | try |
199 | 201 | { |
200 | 202 | res = twCon.AuthUserAndPass(username, password, ref content); |
@@ -204,41 +206,12 @@ namespace OpenTween | ||
204 | 206 | return "Err:" + ex.Message; |
205 | 207 | } |
206 | 208 | |
207 | - switch (res) | |
208 | - { | |
209 | - case HttpStatusCode.OK: | |
210 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
211 | - _uname = username.ToLower(); | |
212 | - if (AppendSettingDialog.Instance.UserstreamStartup) this.ReconnectUserStream(); | |
213 | - return ""; | |
214 | - case HttpStatusCode.Unauthorized: | |
215 | - { | |
216 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
217 | - var errMsg = GetErrorMessageJson(content); | |
218 | - if (string.IsNullOrEmpty(errMsg)) | |
219 | - { | |
220 | - return Properties.Resources.Unauthorized + Environment.NewLine + content; | |
221 | - } | |
222 | - else | |
223 | - { | |
224 | - return "Auth error:" + errMsg; | |
225 | - } | |
226 | - } | |
227 | - case HttpStatusCode.Forbidden: | |
228 | - { | |
229 | - var errMsg = GetErrorMessageJson(content); | |
230 | - if (string.IsNullOrEmpty(errMsg)) | |
231 | - { | |
232 | - return "Err:Forbidden"; | |
233 | - } | |
234 | - else | |
235 | - { | |
236 | - return "Err:" + errMsg; | |
237 | - } | |
238 | - } | |
239 | - default: | |
240 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
241 | - } | |
209 | + var err = this.CheckStatusCode(res, content); | |
210 | + if (err != null) return err; | |
211 | + | |
212 | + _uname = username.ToLower(); | |
213 | + if (AppendSettingDialog.Instance.UserstreamStartup) this.ReconnectUserStream(); | |
214 | + return ""; | |
242 | 215 | } |
243 | 216 | |
244 | 217 | public string StartAuthentication(ref string pinPageUrl) |
@@ -261,10 +234,9 @@ namespace OpenTween | ||
261 | 234 | |
262 | 235 | public string Authenticate(string pinCode) |
263 | 236 | { |
264 | - HttpStatusCode res; | |
265 | - var content = ""; | |
266 | - | |
267 | 237 | this.ResetApiStatus(); |
238 | + | |
239 | + HttpStatusCode res; | |
268 | 240 | try |
269 | 241 | { |
270 | 242 | res = twCon.AuthGetAccessToken(pinCode); |
@@ -274,41 +246,12 @@ namespace OpenTween | ||
274 | 246 | return "Err:" + "Failed to access auth acc server."; |
275 | 247 | } |
276 | 248 | |
277 | - switch (res) | |
278 | - { | |
279 | - case HttpStatusCode.OK: | |
280 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
281 | - _uname = Username.ToLower(); | |
282 | - if (AppendSettingDialog.Instance.UserstreamStartup) this.ReconnectUserStream(); | |
283 | - return ""; | |
284 | - case HttpStatusCode.Unauthorized: | |
285 | - { | |
286 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
287 | - var errMsg = GetErrorMessageJson(content); | |
288 | - if (string.IsNullOrEmpty(errMsg)) | |
289 | - { | |
290 | - return "Check the PIN or retry." + Environment.NewLine + content; | |
291 | - } | |
292 | - else | |
293 | - { | |
294 | - return "Auth error:" + errMsg; | |
295 | - } | |
296 | - } | |
297 | - case HttpStatusCode.Forbidden: | |
298 | - { | |
299 | - var errMsg = GetErrorMessageJson(content); | |
300 | - if (string.IsNullOrEmpty(errMsg)) | |
301 | - { | |
302 | - return "Err:Forbidden"; | |
303 | - } | |
304 | - else | |
305 | - { | |
306 | - return "Err:" + errMsg; | |
307 | - } | |
308 | - } | |
309 | - default: | |
310 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
311 | - } | |
249 | + var err = this.CheckStatusCode(res, null); | |
250 | + if (err != null) return err; | |
251 | + | |
252 | + _uname = Username.ToLower(); | |
253 | + if (AppendSettingDialog.Instance.UserstreamStartup) this.ReconnectUserStream(); | |
254 | + return ""; | |
312 | 255 | } |
313 | 256 | |
314 | 257 | public void ClearAuthInfo() |
@@ -320,9 +263,8 @@ namespace OpenTween | ||
320 | 263 | |
321 | 264 | public void VerifyCredentials() |
322 | 265 | { |
323 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
266 | + HttpStatusCode res; | |
324 | 267 | var content = ""; |
325 | - | |
326 | 268 | try |
327 | 269 | { |
328 | 270 | res = twCon.VerifyCredentials(ref content); |
@@ -348,36 +290,6 @@ namespace OpenTween | ||
348 | 290 | } |
349 | 291 | } |
350 | 292 | |
351 | - private string GetErrorMessageJson(string content) | |
352 | - { | |
353 | - try | |
354 | - { | |
355 | - if (!string.IsNullOrEmpty(content)) | |
356 | - { | |
357 | - using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(content), XmlDictionaryReaderQuotas.Max)) | |
358 | - { | |
359 | - var xElm = XElement.Load(jsonReader); | |
360 | - if (xElm.Element("error") != null) | |
361 | - { | |
362 | - return xElm.Element("error").Value; | |
363 | - } | |
364 | - else | |
365 | - { | |
366 | - return ""; | |
367 | - } | |
368 | - } | |
369 | - } | |
370 | - else | |
371 | - { | |
372 | - return ""; | |
373 | - } | |
374 | - } | |
375 | - catch(Exception) | |
376 | - { | |
377 | - return ""; | |
378 | - } | |
379 | - } | |
380 | - | |
381 | 293 | public void Initialize(string token, string tokenSecret, string username, long userId) |
382 | 294 | { |
383 | 295 | //OAuth認証 |
@@ -556,7 +468,6 @@ namespace OpenTween | ||
556 | 468 | |
557 | 469 | public string PostStatus(string postStr, long? reply_to) |
558 | 470 | { |
559 | - | |
560 | 471 | if (MyCommon._endingFlag) return ""; |
561 | 472 | |
562 | 473 | if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return ""; |
@@ -568,7 +479,7 @@ namespace OpenTween | ||
568 | 479 | return SendDirectMessage(postStr); |
569 | 480 | } |
570 | 481 | |
571 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
482 | + HttpStatusCode res; | |
572 | 483 | var content = ""; |
573 | 484 | try |
574 | 485 | { |
@@ -579,87 +490,44 @@ namespace OpenTween | ||
579 | 490 | return "Err:" + ex.Message; |
580 | 491 | } |
581 | 492 | |
582 | - switch (res) | |
493 | + // 投稿に成功していても404が返ることがあるらしい: https://dev.twitter.com/discussions/1213 | |
494 | + if (res == HttpStatusCode.NotFound) return ""; | |
495 | + | |
496 | + var err = this.CheckStatusCode(res, content); | |
497 | + if (err != null) return err; | |
498 | + | |
499 | + TwitterDataModel.Status status; | |
500 | + try | |
583 | 501 | { |
584 | - case HttpStatusCode.OK: | |
585 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
586 | - TwitterDataModel.Status status; | |
587 | - try | |
588 | - { | |
589 | - status = MyCommon.CreateDataFromJson<TwitterDataModel.Status>(content); | |
590 | - } | |
591 | - catch(SerializationException ex) | |
592 | - { | |
593 | - MyCommon.TraceOut(ex.Message + Environment.NewLine + content); | |
594 | - return "Err:Json Parse Error(DataContractJsonSerializer)"; | |
595 | - } | |
596 | - catch(Exception ex) | |
597 | - { | |
598 | - MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content); | |
599 | - return "Err:Invalid Json!"; | |
600 | - } | |
601 | - _followersCount = status.User.FollowersCount; | |
602 | - _friendsCount = status.User.FriendsCount; | |
603 | - _statusesCount = status.User.StatusesCount; | |
604 | - _location = status.User.Location; | |
605 | - _bio = status.User.Description; | |
502 | + status = MyCommon.CreateDataFromJson<TwitterDataModel.Status>(content); | |
503 | + } | |
504 | + catch(SerializationException ex) | |
505 | + { | |
506 | + MyCommon.TraceOut(ex.Message + Environment.NewLine + content); | |
507 | + return "Err:Json Parse Error(DataContractJsonSerializer)"; | |
508 | + } | |
509 | + catch(Exception ex) | |
510 | + { | |
511 | + MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content); | |
512 | + return "Err:Invalid Json!"; | |
513 | + } | |
514 | + _followersCount = status.User.FollowersCount; | |
515 | + _friendsCount = status.User.FriendsCount; | |
516 | + _statusesCount = status.User.StatusesCount; | |
517 | + _location = status.User.Location; | |
518 | + _bio = status.User.Description; | |
606 | 519 | |
607 | - if (IsPostRestricted(status)) | |
608 | - { | |
609 | - return "OK:Delaying?"; | |
610 | - } | |
611 | - if (op.Post(postStr.Length)) | |
612 | - { | |
613 | - return ""; | |
614 | - } | |
615 | - else | |
616 | - { | |
617 | - return "Outputz:Failed"; | |
618 | - } | |
619 | - case HttpStatusCode.NotFound: | |
520 | + if (IsPostRestricted(status)) | |
521 | + { | |
522 | + return "OK:Delaying?"; | |
523 | + } | |
524 | + if (op.Post(postStr.Length)) | |
525 | + { | |
620 | 526 | return ""; |
621 | - case HttpStatusCode.Forbidden: | |
622 | - case HttpStatusCode.BadRequest: | |
623 | - { | |
624 | - var errMsg = GetErrorMessageJson(content); | |
625 | - if (string.IsNullOrEmpty(errMsg)) | |
626 | - { | |
627 | - return "Warn:" + res.ToString(); | |
628 | - } | |
629 | - else | |
630 | - { | |
631 | - return "Warn:" + errMsg; | |
632 | - } | |
633 | - } | |
634 | - case HttpStatusCode.Conflict: | |
635 | - case HttpStatusCode.ExpectationFailed: | |
636 | - case HttpStatusCode.Gone: | |
637 | - case HttpStatusCode.LengthRequired: | |
638 | - case HttpStatusCode.MethodNotAllowed: | |
639 | - case HttpStatusCode.NotAcceptable: | |
640 | - case HttpStatusCode.PaymentRequired: | |
641 | - case HttpStatusCode.PreconditionFailed: | |
642 | - case HttpStatusCode.RequestedRangeNotSatisfiable: | |
643 | - case HttpStatusCode.RequestEntityTooLarge: | |
644 | - case HttpStatusCode.RequestTimeout: | |
645 | - case HttpStatusCode.RequestUriTooLong: | |
646 | - //仕様書にない400系エラー。サーバまでは到達しているのでリトライしない | |
647 | - return "Warn:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
648 | - case HttpStatusCode.Unauthorized: | |
649 | - { | |
650 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
651 | - var errMsg = GetErrorMessageJson(content); | |
652 | - if (string.IsNullOrEmpty(errMsg)) | |
653 | - { | |
654 | - return Properties.Resources.Unauthorized; | |
655 | - } | |
656 | - else | |
657 | - { | |
658 | - return "Auth err:" + errMsg; | |
659 | - } | |
660 | - } | |
661 | - default: | |
662 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
527 | + } | |
528 | + else | |
529 | + { | |
530 | + return "Outputz:Failed"; | |
663 | 531 | } |
664 | 532 | } |
665 | 533 |
@@ -671,7 +539,7 @@ namespace OpenTween | ||
671 | 539 | |
672 | 540 | postStr = postStr.Trim(); |
673 | 541 | |
674 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
542 | + HttpStatusCode res; | |
675 | 543 | var content = ""; |
676 | 544 | try |
677 | 545 | { |
@@ -682,87 +550,44 @@ namespace OpenTween | ||
682 | 550 | return "Err:" + ex.Message; |
683 | 551 | } |
684 | 552 | |
685 | - switch (res) | |
553 | + // 投稿に成功していても404が返ることがあるらしい: https://dev.twitter.com/discussions/1213 | |
554 | + if (res == HttpStatusCode.NotFound) return ""; | |
555 | + | |
556 | + var err = this.CheckStatusCode(res, content); | |
557 | + if (err != null) return err; | |
558 | + | |
559 | + TwitterDataModel.Status status; | |
560 | + try | |
686 | 561 | { |
687 | - case HttpStatusCode.OK: | |
688 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
689 | - TwitterDataModel.Status status; | |
690 | - try | |
691 | - { | |
692 | - status = MyCommon.CreateDataFromJson<TwitterDataModel.Status>(content); | |
693 | - } | |
694 | - catch(SerializationException ex) | |
695 | - { | |
696 | - MyCommon.TraceOut(ex.Message + Environment.NewLine + content); | |
697 | - return "Err:Json Parse Error(DataContractJsonSerializer)"; | |
698 | - } | |
699 | - catch(Exception ex) | |
700 | - { | |
701 | - MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content); | |
702 | - return "Err:Invalid Json!"; | |
703 | - } | |
704 | - _followersCount = status.User.FollowersCount; | |
705 | - _friendsCount = status.User.FriendsCount; | |
706 | - _statusesCount = status.User.StatusesCount; | |
707 | - _location = status.User.Location; | |
708 | - _bio = status.User.Description; | |
562 | + status = MyCommon.CreateDataFromJson<TwitterDataModel.Status>(content); | |
563 | + } | |
564 | + catch(SerializationException ex) | |
565 | + { | |
566 | + MyCommon.TraceOut(ex.Message + Environment.NewLine + content); | |
567 | + return "Err:Json Parse Error(DataContractJsonSerializer)"; | |
568 | + } | |
569 | + catch(Exception ex) | |
570 | + { | |
571 | + MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content); | |
572 | + return "Err:Invalid Json!"; | |
573 | + } | |
574 | + _followersCount = status.User.FollowersCount; | |
575 | + _friendsCount = status.User.FriendsCount; | |
576 | + _statusesCount = status.User.StatusesCount; | |
577 | + _location = status.User.Location; | |
578 | + _bio = status.User.Description; | |
709 | 579 | |
710 | - if (IsPostRestricted(status)) | |
711 | - { | |
712 | - return "OK:Delaying?"; | |
713 | - } | |
714 | - if (op.Post(postStr.Length)) | |
715 | - { | |
716 | - return ""; | |
717 | - } | |
718 | - else | |
719 | - { | |
720 | - return "Outputz:Failed"; | |
721 | - } | |
722 | - case HttpStatusCode.NotFound: | |
580 | + if (IsPostRestricted(status)) | |
581 | + { | |
582 | + return "OK:Delaying?"; | |
583 | + } | |
584 | + if (op.Post(postStr.Length)) | |
585 | + { | |
723 | 586 | return ""; |
724 | - case HttpStatusCode.Forbidden: | |
725 | - case HttpStatusCode.BadRequest: | |
726 | - { | |
727 | - var errMsg = GetErrorMessageJson(content); | |
728 | - if (string.IsNullOrEmpty(errMsg)) | |
729 | - { | |
730 | - return "Warn:" + res.ToString(); | |
731 | - } | |
732 | - else | |
733 | - { | |
734 | - return "Warn:" + errMsg; | |
735 | - } | |
736 | - } | |
737 | - case HttpStatusCode.Conflict: | |
738 | - case HttpStatusCode.ExpectationFailed: | |
739 | - case HttpStatusCode.Gone: | |
740 | - case HttpStatusCode.LengthRequired: | |
741 | - case HttpStatusCode.MethodNotAllowed: | |
742 | - case HttpStatusCode.NotAcceptable: | |
743 | - case HttpStatusCode.PaymentRequired: | |
744 | - case HttpStatusCode.PreconditionFailed: | |
745 | - case HttpStatusCode.RequestedRangeNotSatisfiable: | |
746 | - case HttpStatusCode.RequestEntityTooLarge: | |
747 | - case HttpStatusCode.RequestTimeout: | |
748 | - case HttpStatusCode.RequestUriTooLong: | |
749 | - //仕様書にない400系エラー。サーバまでは到達しているのでリトライしない | |
750 | - return "Warn:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
751 | - case HttpStatusCode.Unauthorized: | |
752 | - { | |
753 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
754 | - var errMsg = GetErrorMessageJson(content); | |
755 | - if (string.IsNullOrEmpty(errMsg)) | |
756 | - { | |
757 | - return Properties.Resources.Unauthorized; | |
758 | - } | |
759 | - else | |
760 | - { | |
761 | - return "Auth err:" + errMsg; | |
762 | - } | |
763 | - } | |
764 | - default: | |
765 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
587 | + } | |
588 | + else | |
589 | + { | |
590 | + return "Outputz:Failed"; | |
766 | 591 | } |
767 | 592 | } |
768 | 593 |
@@ -779,11 +604,10 @@ namespace OpenTween | ||
779 | 604 | |
780 | 605 | postStr = postStr.Trim(); |
781 | 606 | |
782 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
783 | - var content = ""; | |
784 | - | |
785 | 607 | var mc = Regex.Match(postStr, "^DM? +(?<id>[a-zA-Z0-9_]+) +(?<body>.+)", RegexOptions.IgnoreCase | RegexOptions.Singleline); |
786 | 608 | |
609 | + HttpStatusCode res; | |
610 | + var content = ""; | |
787 | 611 | try |
788 | 612 | { |
789 | 613 | res = twCon.SendDirectMessage(mc.Groups["body"].Value, mc.Groups["id"].Value, ref content); |
@@ -793,82 +617,37 @@ namespace OpenTween | ||
793 | 617 | return "Err:" + ex.Message; |
794 | 618 | } |
795 | 619 | |
796 | - switch (res) | |
620 | + var err = this.CheckStatusCode(res, content); | |
621 | + if (err != null) return err; | |
622 | + | |
623 | + TwitterDataModel.Directmessage status; | |
624 | + try | |
797 | 625 | { |
798 | - case HttpStatusCode.OK: | |
799 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
800 | - TwitterDataModel.Directmessage status; | |
801 | - try | |
802 | - { | |
803 | - status = MyCommon.CreateDataFromJson<TwitterDataModel.Directmessage>(content); | |
804 | - } | |
805 | - catch(SerializationException ex) | |
806 | - { | |
807 | - MyCommon.TraceOut(ex.Message + Environment.NewLine + content); | |
808 | - return "Err:Json Parse Error(DataContractJsonSerializer)"; | |
809 | - } | |
810 | - catch(Exception ex) | |
811 | - { | |
812 | - MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content); | |
813 | - return "Err:Invalid Json!"; | |
814 | - } | |
815 | - _followersCount = status.Sender.FollowersCount; | |
816 | - _friendsCount = status.Sender.FriendsCount; | |
817 | - _statusesCount = status.Sender.StatusesCount; | |
818 | - _location = status.Sender.Location; | |
819 | - _bio = status.Sender.Description; | |
626 | + status = MyCommon.CreateDataFromJson<TwitterDataModel.Directmessage>(content); | |
627 | + } | |
628 | + catch(SerializationException ex) | |
629 | + { | |
630 | + MyCommon.TraceOut(ex.Message + Environment.NewLine + content); | |
631 | + return "Err:Json Parse Error(DataContractJsonSerializer)"; | |
632 | + } | |
633 | + catch(Exception ex) | |
634 | + { | |
635 | + MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content); | |
636 | + return "Err:Invalid Json!"; | |
637 | + } | |
638 | + _followersCount = status.Sender.FollowersCount; | |
639 | + _friendsCount = status.Sender.FriendsCount; | |
640 | + _statusesCount = status.Sender.StatusesCount; | |
641 | + _location = status.Sender.Location; | |
642 | + _bio = status.Sender.Description; | |
820 | 643 | |
821 | - if (op.Post(postStr.Length)) | |
822 | - { | |
823 | - return ""; | |
824 | - } | |
825 | - else | |
826 | - { | |
827 | - return "Outputz:Failed"; | |
828 | - } | |
829 | - case HttpStatusCode.Forbidden: | |
830 | - case HttpStatusCode.BadRequest: | |
831 | - { | |
832 | - var errMsg = GetErrorMessageJson(content); | |
833 | - if (string.IsNullOrEmpty(errMsg)) | |
834 | - { | |
835 | - return "Warn:" + res.ToString(); | |
836 | - } | |
837 | - else | |
838 | - { | |
839 | - return "Warn:" + errMsg; | |
840 | - } | |
841 | - } | |
842 | - case HttpStatusCode.Conflict: | |
843 | - case HttpStatusCode.ExpectationFailed: | |
844 | - case HttpStatusCode.Gone: | |
845 | - case HttpStatusCode.LengthRequired: | |
846 | - case HttpStatusCode.MethodNotAllowed: | |
847 | - case HttpStatusCode.NotAcceptable: | |
848 | - case HttpStatusCode.NotFound: | |
849 | - case HttpStatusCode.PaymentRequired: | |
850 | - case HttpStatusCode.PreconditionFailed: | |
851 | - case HttpStatusCode.RequestedRangeNotSatisfiable: | |
852 | - case HttpStatusCode.RequestEntityTooLarge: | |
853 | - case HttpStatusCode.RequestTimeout: | |
854 | - case HttpStatusCode.RequestUriTooLong: | |
855 | - //仕様書にない400系エラー。サーバまでは到達しているのでリトライしない | |
856 | - return "Warn:" + res.ToString(); | |
857 | - case HttpStatusCode.Unauthorized: | |
858 | - { | |
859 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
860 | - var errMsg = GetErrorMessageJson(content); | |
861 | - if (string.IsNullOrEmpty(errMsg)) | |
862 | - { | |
863 | - return Properties.Resources.Unauthorized; | |
864 | - } | |
865 | - else | |
866 | - { | |
867 | - return "Auth err:" + errMsg; | |
868 | - } | |
869 | - } | |
870 | - default: | |
871 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
644 | + if (op.Post(postStr.Length)) | |
645 | + { | |
646 | + return ""; | |
647 | + } | |
648 | + else | |
649 | + { | |
650 | + return "Outputz:Failed"; | |
872 | 651 | } |
873 | 652 | } |
874 | 653 |
@@ -878,8 +657,7 @@ namespace OpenTween | ||
878 | 657 | |
879 | 658 | if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return ""; |
880 | 659 | |
881 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
882 | - | |
660 | + HttpStatusCode res; | |
883 | 661 | try |
884 | 662 | { |
885 | 663 | res = twCon.DestroyStatus(id); |
@@ -888,20 +666,8 @@ namespace OpenTween | ||
888 | 666 | { |
889 | 667 | return "Err:" + ex.Message; |
890 | 668 | } |
891 | - | |
892 | - switch (res) | |
893 | - { | |
894 | - case HttpStatusCode.OK: | |
895 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
896 | - return ""; | |
897 | - case HttpStatusCode.Unauthorized: | |
898 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
899 | - return Properties.Resources.Unauthorized; | |
900 | - case HttpStatusCode.NotFound: | |
901 | - return ""; | |
902 | - default: | |
903 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
904 | - } | |
669 | + | |
670 | + return this.CheckStatusCode(res, null) ?? ""; | |
905 | 671 | } |
906 | 672 | |
907 | 673 | public string PostRetweet(long id, bool read) |
@@ -921,7 +687,7 @@ namespace OpenTween | ||
921 | 687 | target = TabInformations.GetInstance()[id].RetweetedId.Value; //再RTの場合は元発言をRT |
922 | 688 | } |
923 | 689 | |
924 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
690 | + HttpStatusCode res; | |
925 | 691 | var content = ""; |
926 | 692 | try |
927 | 693 | { |
@@ -996,11 +762,11 @@ namespace OpenTween | ||
996 | 762 | return "Auth Err:try to re-authorization."; |
997 | 763 | } |
998 | 764 | |
999 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
1000 | - | |
1001 | 765 | //if (post.IsMe) |
1002 | 766 | // _deletemessages.Add(post) |
1003 | 767 | //} |
768 | + | |
769 | + HttpStatusCode res; | |
1004 | 770 | try |
1005 | 771 | { |
1006 | 772 | res = twCon.DestroyDirectMessage(id); |
@@ -1010,19 +776,7 @@ namespace OpenTween | ||
1010 | 776 | return "Err:" + ex.Message; |
1011 | 777 | } |
1012 | 778 | |
1013 | - switch (res) | |
1014 | - { | |
1015 | - case HttpStatusCode.OK: | |
1016 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
1017 | - return ""; | |
1018 | - case HttpStatusCode.Unauthorized: | |
1019 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
1020 | - return Properties.Resources.Unauthorized; | |
1021 | - case HttpStatusCode.NotFound: | |
1022 | - return ""; | |
1023 | - default: | |
1024 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1025 | - } | |
779 | + return this.CheckStatusCode(res, null) ?? ""; | |
1026 | 780 | } |
1027 | 781 | |
1028 | 782 | public string PostFollowCommand(string screenName) |
@@ -1031,9 +785,8 @@ namespace OpenTween | ||
1031 | 785 | |
1032 | 786 | if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return ""; |
1033 | 787 | |
1034 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
788 | + HttpStatusCode res; | |
1035 | 789 | var content = ""; |
1036 | - | |
1037 | 790 | try |
1038 | 791 | { |
1039 | 792 | res = twCon.CreateFriendships(screenName, ref content); |
@@ -1043,27 +796,7 @@ namespace OpenTween | ||
1043 | 796 | return "Err:" + ex.Message; |
1044 | 797 | } |
1045 | 798 | |
1046 | - switch (res) | |
1047 | - { | |
1048 | - case HttpStatusCode.OK: | |
1049 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
1050 | - return ""; | |
1051 | - case HttpStatusCode.Unauthorized: | |
1052 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
1053 | - return Properties.Resources.Unauthorized; | |
1054 | - case HttpStatusCode.Forbidden: | |
1055 | - var errMsg = GetErrorMessageJson(content); | |
1056 | - if (string.IsNullOrEmpty(errMsg)) | |
1057 | - { | |
1058 | - return "Err:Forbidden(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1059 | - } | |
1060 | - else | |
1061 | - { | |
1062 | - return "Err:" + errMsg; | |
1063 | - } | |
1064 | - default: | |
1065 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1066 | - } | |
799 | + return this.CheckStatusCode(res, content) ?? ""; | |
1067 | 800 | } |
1068 | 801 | |
1069 | 802 | public string PostRemoveCommand(string screenName) |
@@ -1072,9 +805,8 @@ namespace OpenTween | ||
1072 | 805 | |
1073 | 806 | if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return ""; |
1074 | 807 | |
1075 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
808 | + HttpStatusCode res; | |
1076 | 809 | var content = ""; |
1077 | - | |
1078 | 810 | try |
1079 | 811 | { |
1080 | 812 | res = twCon.DestroyFriendships(screenName, ref content); |
@@ -1084,27 +816,7 @@ namespace OpenTween | ||
1084 | 816 | return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")"; |
1085 | 817 | } |
1086 | 818 | |
1087 | - switch (res) | |
1088 | - { | |
1089 | - case HttpStatusCode.OK: | |
1090 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
1091 | - return ""; | |
1092 | - case HttpStatusCode.Unauthorized: | |
1093 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
1094 | - return Properties.Resources.Unauthorized; | |
1095 | - case HttpStatusCode.Forbidden: | |
1096 | - var errMsg = GetErrorMessageJson(content); | |
1097 | - if (string.IsNullOrEmpty(errMsg)) | |
1098 | - { | |
1099 | - return "Err:Forbidden(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1100 | - } | |
1101 | - else | |
1102 | - { | |
1103 | - return "Err:" + errMsg; | |
1104 | - } | |
1105 | - default: | |
1106 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1107 | - } | |
819 | + return this.CheckStatusCode(res, content) ?? ""; | |
1108 | 820 | } |
1109 | 821 | |
1110 | 822 | public string PostCreateBlock(string screenName) |
@@ -1113,9 +825,8 @@ namespace OpenTween | ||
1113 | 825 | |
1114 | 826 | if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return ""; |
1115 | 827 | |
1116 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
828 | + HttpStatusCode res; | |
1117 | 829 | var content = ""; |
1118 | - | |
1119 | 830 | try |
1120 | 831 | { |
1121 | 832 | res = twCon.CreateBlock(screenName, ref content); |
@@ -1125,27 +836,7 @@ namespace OpenTween | ||
1125 | 836 | return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")"; |
1126 | 837 | } |
1127 | 838 | |
1128 | - switch (res) | |
1129 | - { | |
1130 | - case HttpStatusCode.OK: | |
1131 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
1132 | - return ""; | |
1133 | - case HttpStatusCode.Unauthorized: | |
1134 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
1135 | - return Properties.Resources.Unauthorized; | |
1136 | - case HttpStatusCode.Forbidden: | |
1137 | - var errMsg = GetErrorMessageJson(content); | |
1138 | - if (string.IsNullOrEmpty(errMsg)) | |
1139 | - { | |
1140 | - return "Err:Forbidden(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1141 | - } | |
1142 | - else | |
1143 | - { | |
1144 | - return "Err:" + errMsg; | |
1145 | - } | |
1146 | - default: | |
1147 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1148 | - } | |
839 | + return this.CheckStatusCode(res, content) ?? ""; | |
1149 | 840 | } |
1150 | 841 | |
1151 | 842 | public string PostDestroyBlock(string screenName) |
@@ -1154,9 +845,8 @@ namespace OpenTween | ||
1154 | 845 | |
1155 | 846 | if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return ""; |
1156 | 847 | |
1157 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
848 | + HttpStatusCode res; | |
1158 | 849 | var content = ""; |
1159 | - | |
1160 | 850 | try |
1161 | 851 | { |
1162 | 852 | res = twCon.DestroyBlock(screenName, ref content); |
@@ -1166,27 +856,7 @@ namespace OpenTween | ||
1166 | 856 | return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")"; |
1167 | 857 | } |
1168 | 858 | |
1169 | - switch (res) | |
1170 | - { | |
1171 | - case HttpStatusCode.OK: | |
1172 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
1173 | - return ""; | |
1174 | - case HttpStatusCode.Unauthorized: | |
1175 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
1176 | - return Properties.Resources.Unauthorized; | |
1177 | - case HttpStatusCode.Forbidden: | |
1178 | - var errMsg = GetErrorMessageJson(content); | |
1179 | - if (string.IsNullOrEmpty(errMsg)) | |
1180 | - { | |
1181 | - return "Err:Forbidden(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1182 | - } | |
1183 | - else | |
1184 | - { | |
1185 | - return "Err:" + errMsg; | |
1186 | - } | |
1187 | - default: | |
1188 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1189 | - } | |
859 | + return this.CheckStatusCode(res, content) ?? ""; | |
1190 | 860 | } |
1191 | 861 | |
1192 | 862 | public string PostReportSpam(string screenName) |
@@ -1195,9 +865,8 @@ namespace OpenTween | ||
1195 | 865 | |
1196 | 866 | if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return ""; |
1197 | 867 | |
1198 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
868 | + HttpStatusCode res; | |
1199 | 869 | var content = ""; |
1200 | - | |
1201 | 870 | try |
1202 | 871 | { |
1203 | 872 | res = twCon.ReportSpam(screenName, ref content); |
@@ -1207,27 +876,7 @@ namespace OpenTween | ||
1207 | 876 | return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")"; |
1208 | 877 | } |
1209 | 878 | |
1210 | - switch (res) | |
1211 | - { | |
1212 | - case HttpStatusCode.OK: | |
1213 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
1214 | - return ""; | |
1215 | - case HttpStatusCode.Unauthorized: | |
1216 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
1217 | - return Properties.Resources.Unauthorized; | |
1218 | - case HttpStatusCode.Forbidden: | |
1219 | - var errMsg = GetErrorMessageJson(content); | |
1220 | - if (string.IsNullOrEmpty(errMsg)) | |
1221 | - { | |
1222 | - return "Err:Forbidden(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1223 | - } | |
1224 | - else | |
1225 | - { | |
1226 | - return "Err:" + errMsg; | |
1227 | - } | |
1228 | - default: | |
1229 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1230 | - } | |
879 | + return this.CheckStatusCode(res, content) ?? ""; | |
1231 | 880 | } |
1232 | 881 | |
1233 | 882 | public string GetFriendshipInfo(string screenName, ref bool isFollowing, ref bool isFollowed) |
@@ -1236,7 +885,7 @@ namespace OpenTween | ||
1236 | 885 | |
1237 | 886 | if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return ""; |
1238 | 887 | |
1239 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
888 | + HttpStatusCode res; | |
1240 | 889 | var content = ""; |
1241 | 890 | try |
1242 | 891 | { |
@@ -1247,33 +896,25 @@ namespace OpenTween | ||
1247 | 896 | return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")"; |
1248 | 897 | } |
1249 | 898 | |
1250 | - switch (res) | |
899 | + var err = this.CheckStatusCode(res, content); | |
900 | + if (err != null) return err; | |
901 | + | |
902 | + try | |
1251 | 903 | { |
1252 | - case HttpStatusCode.OK: | |
1253 | - try | |
1254 | - { | |
1255 | - var relation = MyCommon.CreateDataFromJson<TwitterDataModel.Relationship>(content); | |
1256 | - isFollowing = relation.relationship.Source.Following; | |
1257 | - isFollowed = relation.relationship.Source.FollowedBy; | |
1258 | - return ""; | |
1259 | - } | |
1260 | - catch(SerializationException ex) | |
1261 | - { | |
1262 | - MyCommon.TraceOut(ex.Message + Environment.NewLine + content); | |
1263 | - return "Err:Json Parse Error(DataContractJsonSerializer)"; | |
1264 | - } | |
1265 | - catch(Exception ex) | |
1266 | - { | |
1267 | - MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content); | |
1268 | - return "Err:Invalid Json!"; | |
1269 | - } | |
1270 | - case HttpStatusCode.BadRequest: | |
1271 | - return "Err:API Limits?"; | |
1272 | - case HttpStatusCode.Unauthorized: | |
1273 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
1274 | - return Properties.Resources.Unauthorized; | |
1275 | - default: | |
1276 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
904 | + var relation = MyCommon.CreateDataFromJson<TwitterDataModel.Relationship>(content); | |
905 | + isFollowing = relation.relationship.Source.Following; | |
906 | + isFollowed = relation.relationship.Source.FollowedBy; | |
907 | + return ""; | |
908 | + } | |
909 | + catch(SerializationException ex) | |
910 | + { | |
911 | + MyCommon.TraceOut(ex.Message + Environment.NewLine + content); | |
912 | + return "Err:Json Parse Error(DataContractJsonSerializer)"; | |
913 | + } | |
914 | + catch(Exception ex) | |
915 | + { | |
916 | + MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content); | |
917 | + return "Err:Invalid Json!"; | |
1277 | 918 | } |
1278 | 919 | } |
1279 | 920 |
@@ -1283,9 +924,10 @@ namespace OpenTween | ||
1283 | 924 | |
1284 | 925 | if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return ""; |
1285 | 926 | |
1286 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
1287 | - var content = ""; | |
1288 | 927 | user = null; |
928 | + | |
929 | + HttpStatusCode res; | |
930 | + var content = ""; | |
1289 | 931 | try |
1290 | 932 | { |
1291 | 933 | res = twCon.ShowUserInfo(screenName, ref content); |
@@ -1295,41 +937,24 @@ namespace OpenTween | ||
1295 | 937 | return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")"; |
1296 | 938 | } |
1297 | 939 | |
1298 | - switch (res) | |
940 | + var err = this.CheckStatusCode(res, content); | |
941 | + if (err != null) return err; | |
942 | + | |
943 | + try | |
1299 | 944 | { |
1300 | - case HttpStatusCode.OK: | |
1301 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
1302 | - try | |
1303 | - { | |
1304 | - user = MyCommon.CreateDataFromJson<TwitterDataModel.User>(content); | |
1305 | - } | |
1306 | - catch (SerializationException ex) | |
1307 | - { | |
1308 | - MyCommon.TraceOut(ex.Message + Environment.NewLine + content); | |
1309 | - return "Err:Json Parse Error(DataContractJsonSerializer)"; | |
1310 | - } | |
1311 | - catch (Exception ex) | |
1312 | - { | |
1313 | - MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content); | |
1314 | - return "Err:Invalid Json!"; | |
1315 | - } | |
1316 | - return ""; | |
1317 | - case HttpStatusCode.BadRequest: | |
1318 | - return "Err:API Limits?"; | |
1319 | - case HttpStatusCode.Unauthorized: | |
1320 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
1321 | - var errMsg = GetErrorMessageJson(content); | |
1322 | - if (string.IsNullOrEmpty(errMsg)) | |
1323 | - { | |
1324 | - return Properties.Resources.Unauthorized; | |
1325 | - } | |
1326 | - else | |
1327 | - { | |
1328 | - return "Auth err:" + errMsg; | |
1329 | - } | |
1330 | - default: | |
1331 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
945 | + user = MyCommon.CreateDataFromJson<TwitterDataModel.User>(content); | |
946 | + } | |
947 | + catch (SerializationException ex) | |
948 | + { | |
949 | + MyCommon.TraceOut(ex.Message + Environment.NewLine + content); | |
950 | + return "Err:Json Parse Error(DataContractJsonSerializer)"; | |
951 | + } | |
952 | + catch (Exception ex) | |
953 | + { | |
954 | + MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content); | |
955 | + return "Err:Invalid Json!"; | |
1332 | 956 | } |
957 | + return ""; | |
1333 | 958 | } |
1334 | 959 | |
1335 | 960 | public string GetStatus_Retweeted_Count(long StatusId, ref int retweeted_count) |
@@ -1338,9 +963,8 @@ namespace OpenTween | ||
1338 | 963 | |
1339 | 964 | if (MyCommon._endingFlag) return ""; |
1340 | 965 | |
1341 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
966 | + HttpStatusCode res; | |
1342 | 967 | var content = ""; |
1343 | - | |
1344 | 968 | try |
1345 | 969 | { |
1346 | 970 | res = twCon.ShowStatuses(StatusId, ref content); |
@@ -1349,21 +973,9 @@ namespace OpenTween | ||
1349 | 973 | { |
1350 | 974 | return "Err:" + ex.Message; |
1351 | 975 | } |
1352 | - switch (res) | |
1353 | - { | |
1354 | - case HttpStatusCode.OK: | |
1355 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
1356 | - break; | |
1357 | - case HttpStatusCode.Unauthorized: | |
1358 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
1359 | - return Properties.Resources.Unauthorized; | |
1360 | - case HttpStatusCode.BadRequest: | |
1361 | - return "Err:API Limits?"; | |
1362 | - case HttpStatusCode.Forbidden: | |
1363 | - return "Err:protected user's tweet"; | |
1364 | - default: | |
1365 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1366 | - } | |
976 | + | |
977 | + var err = this.CheckStatusCode(res, content); | |
978 | + if (err != null) return err; | |
1367 | 979 | |
1368 | 980 | TwitterDataModel.Status status; |
1369 | 981 | try |
@@ -1394,7 +1006,7 @@ namespace OpenTween | ||
1394 | 1006 | |
1395 | 1007 | //if (this.favQueue.Contains(id)) this.favQueue.Remove(id) |
1396 | 1008 | |
1397 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
1009 | + HttpStatusCode res; | |
1398 | 1010 | var content = ""; |
1399 | 1011 | try |
1400 | 1012 | { |
@@ -1407,41 +1019,13 @@ namespace OpenTween | ||
1407 | 1019 | return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")"; |
1408 | 1020 | } |
1409 | 1021 | |
1410 | - switch (res) | |
1411 | - { | |
1412 | - case HttpStatusCode.OK: | |
1413 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
1414 | - //this.favQueue.FavoriteCacheStart(); | |
1415 | - if (!_restrictFavCheck) return ""; | |
1416 | - break; | |
1417 | - case HttpStatusCode.Unauthorized: | |
1418 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
1419 | - return Properties.Resources.Unauthorized; | |
1420 | - case HttpStatusCode.Forbidden: | |
1421 | - var errMsg = GetErrorMessageJson(content); | |
1422 | - if (string.IsNullOrEmpty(errMsg)) | |
1423 | - { | |
1424 | - return "Err:Forbidden(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1425 | - } | |
1426 | - else | |
1427 | - { | |
1428 | - //if (errMsg.Contains("It's great that you like so many updates")) | |
1429 | - // //this.favQueue.Add(id) | |
1430 | - // return "Err:->FavoriteQueue:" + errMsg; | |
1431 | - //} | |
1432 | - return "Err:" + errMsg; | |
1433 | - } | |
1434 | - //Case HttpStatusCode.BadGateway, HttpStatusCode.ServiceUnavailable, HttpStatusCode.InternalServerError, HttpStatusCode.RequestTimeout | |
1435 | - // //this.favQueue.Add(id) | |
1436 | - // return "Err:->FavoriteQueue:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1437 | - default: | |
1438 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1439 | - } | |
1022 | + var err = this.CheckStatusCode(res, content); | |
1023 | + if (err != null) return err; | |
1024 | + | |
1025 | + if (!_restrictFavCheck) return ""; | |
1440 | 1026 | |
1441 | 1027 | //http://twitter.com/statuses/show/id.xml APIを発行して本文を取得 |
1442 | 1028 | |
1443 | - //var content = ""; | |
1444 | - content = ""; | |
1445 | 1029 | try |
1446 | 1030 | { |
1447 | 1031 | res = twCon.ShowStatuses(id, ref content); |
@@ -1451,40 +1035,31 @@ namespace OpenTween | ||
1451 | 1035 | return "Err:" + ex.Message; |
1452 | 1036 | } |
1453 | 1037 | |
1454 | - switch (res) | |
1038 | + err = this.CheckStatusCode(res, content); | |
1039 | + if (err != null) return err; | |
1040 | + | |
1041 | + TwitterDataModel.Status status; | |
1042 | + try | |
1455 | 1043 | { |
1456 | - case HttpStatusCode.OK: | |
1457 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
1458 | - TwitterDataModel.Status status; | |
1459 | - try | |
1460 | - { | |
1461 | - status = MyCommon.CreateDataFromJson<TwitterDataModel.Status>(content); | |
1462 | - } | |
1463 | - catch (SerializationException ex) | |
1464 | - { | |
1465 | - MyCommon.TraceOut(ex.Message + Environment.NewLine + content); | |
1466 | - return "Err:Json Parse Error(DataContractJsonSerializer)"; | |
1467 | - } | |
1468 | - catch (Exception ex) | |
1469 | - { | |
1470 | - MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content); | |
1471 | - return "Err:Invalid Json!"; | |
1472 | - } | |
1473 | - if (status.Favorited) | |
1474 | - { | |
1475 | - return ""; | |
1476 | - } | |
1477 | - else | |
1478 | - { | |
1479 | - return "NG(Restricted?)"; | |
1480 | - } | |
1481 | - case HttpStatusCode.Unauthorized: | |
1482 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
1483 | - return Properties.Resources.Unauthorized; | |
1484 | - case HttpStatusCode.BadRequest: | |
1485 | - return "Err:API Limits?"; | |
1486 | - default: | |
1487 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1044 | + status = MyCommon.CreateDataFromJson<TwitterDataModel.Status>(content); | |
1045 | + } | |
1046 | + catch (SerializationException ex) | |
1047 | + { | |
1048 | + MyCommon.TraceOut(ex.Message + Environment.NewLine + content); | |
1049 | + return "Err:Json Parse Error(DataContractJsonSerializer)"; | |
1050 | + } | |
1051 | + catch (Exception ex) | |
1052 | + { | |
1053 | + MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content); | |
1054 | + return "Err:Invalid Json!"; | |
1055 | + } | |
1056 | + if (status.Favorited) | |
1057 | + { | |
1058 | + return ""; | |
1059 | + } | |
1060 | + else | |
1061 | + { | |
1062 | + return "NG(Restricted?)"; | |
1488 | 1063 | } |
1489 | 1064 | } |
1490 | 1065 |
@@ -1501,7 +1076,7 @@ namespace OpenTween | ||
1501 | 1076 | // return ""; |
1502 | 1077 | //} |
1503 | 1078 | |
1504 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
1079 | + HttpStatusCode res; | |
1505 | 1080 | var content = ""; |
1506 | 1081 | try |
1507 | 1082 | { |
@@ -1512,27 +1087,7 @@ namespace OpenTween | ||
1512 | 1087 | return "Err:" + ex.Message; |
1513 | 1088 | } |
1514 | 1089 | |
1515 | - switch (res) | |
1516 | - { | |
1517 | - case HttpStatusCode.OK: | |
1518 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
1519 | - return ""; | |
1520 | - case HttpStatusCode.Unauthorized: | |
1521 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
1522 | - return Properties.Resources.Unauthorized; | |
1523 | - case HttpStatusCode.Forbidden: | |
1524 | - var errMsg = GetErrorMessageJson(content); | |
1525 | - if (string.IsNullOrEmpty(errMsg)) | |
1526 | - { | |
1527 | - return "Err:Forbidden(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1528 | - } | |
1529 | - else | |
1530 | - { | |
1531 | - return "Err:" + errMsg; | |
1532 | - } | |
1533 | - default: | |
1534 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1535 | - } | |
1090 | + return this.CheckStatusCode(res, content) ?? ""; | |
1536 | 1091 | } |
1537 | 1092 | |
1538 | 1093 | public string PostUpdateProfile(string name, string url, string location, string description) |
@@ -1541,7 +1096,7 @@ namespace OpenTween | ||
1541 | 1096 | |
1542 | 1097 | if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return ""; |
1543 | 1098 | |
1544 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
1099 | + HttpStatusCode res; | |
1545 | 1100 | var content = ""; |
1546 | 1101 | try |
1547 | 1102 | { |
@@ -1552,27 +1107,7 @@ namespace OpenTween | ||
1552 | 1107 | return "Err:" + ex.Message; |
1553 | 1108 | } |
1554 | 1109 | |
1555 | - switch (res) | |
1556 | - { | |
1557 | - case HttpStatusCode.OK: | |
1558 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
1559 | - return ""; | |
1560 | - case HttpStatusCode.Unauthorized: | |
1561 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
1562 | - return Properties.Resources.Unauthorized; | |
1563 | - case HttpStatusCode.Forbidden: | |
1564 | - var errMsg = GetErrorMessageJson(content); | |
1565 | - if (string.IsNullOrEmpty(errMsg)) | |
1566 | - { | |
1567 | - return "Err:Forbidden(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1568 | - } | |
1569 | - else | |
1570 | - { | |
1571 | - return "Err:" + errMsg; | |
1572 | - } | |
1573 | - default: | |
1574 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1575 | - } | |
1110 | + return this.CheckStatusCode(res, content) ?? ""; | |
1576 | 1111 | } |
1577 | 1112 | |
1578 | 1113 | public string PostUpdateProfileImage(string filename) |
@@ -1581,38 +1116,18 @@ namespace OpenTween | ||
1581 | 1116 | |
1582 | 1117 | if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return ""; |
1583 | 1118 | |
1584 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
1119 | + HttpStatusCode res; | |
1585 | 1120 | var content = ""; |
1586 | 1121 | try |
1587 | 1122 | { |
1588 | - res = twCon.UpdateProfileImage(new FileInfo(filename), ref content); | |
1589 | - } | |
1590 | - catch(Exception ex) | |
1591 | - { | |
1592 | - return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1593 | - } | |
1594 | - | |
1595 | - switch (res) | |
1596 | - { | |
1597 | - case HttpStatusCode.OK: | |
1598 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
1599 | - return ""; | |
1600 | - case HttpStatusCode.Unauthorized: | |
1601 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
1602 | - return Properties.Resources.Unauthorized; | |
1603 | - case HttpStatusCode.Forbidden: | |
1604 | - var errMsg = GetErrorMessageJson(content); | |
1605 | - if (string.IsNullOrEmpty(errMsg)) | |
1606 | - { | |
1607 | - return "Err:Forbidden(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1608 | - } | |
1609 | - else | |
1610 | - { | |
1611 | - return "Err:" + errMsg; | |
1612 | - } | |
1613 | - default: | |
1614 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1123 | + res = twCon.UpdateProfileImage(new FileInfo(filename), ref content); | |
1124 | + } | |
1125 | + catch(Exception ex) | |
1126 | + { | |
1127 | + return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1615 | 1128 | } |
1129 | + | |
1130 | + return this.CheckStatusCode(res, content) ?? ""; | |
1616 | 1131 | } |
1617 | 1132 | |
1618 | 1133 | public string Username |
@@ -1919,19 +1434,9 @@ namespace OpenTween | ||
1919 | 1434 | { |
1920 | 1435 | return "Err:" + ex.Message; |
1921 | 1436 | } |
1922 | - switch (res) | |
1923 | - { | |
1924 | - case HttpStatusCode.OK: | |
1925 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
1926 | - break; | |
1927 | - case HttpStatusCode.Unauthorized: | |
1928 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
1929 | - return Properties.Resources.Unauthorized; | |
1930 | - case HttpStatusCode.BadRequest: | |
1931 | - return "Err:API Limits?"; | |
1932 | - default: | |
1933 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1934 | - } | |
1437 | + | |
1438 | + var err = this.CheckStatusCode(res, content); | |
1439 | + if (err != null) return err; | |
1935 | 1440 | |
1936 | 1441 | if (gType == MyCommon.WORKERTYPE.Timeline) |
1937 | 1442 | { |
@@ -1953,7 +1458,7 @@ namespace OpenTween | ||
1953 | 1458 | |
1954 | 1459 | if (MyCommon._endingFlag) return ""; |
1955 | 1460 | |
1956 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
1461 | + HttpStatusCode res; | |
1957 | 1462 | var content = ""; |
1958 | 1463 | |
1959 | 1464 | if (count == 0) count = 20; |
@@ -1982,19 +1487,12 @@ namespace OpenTween | ||
1982 | 1487 | { |
1983 | 1488 | return "Err:" + ex.Message; |
1984 | 1489 | } |
1985 | - switch (res) | |
1986 | - { | |
1987 | - case HttpStatusCode.OK: | |
1988 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
1989 | - break; | |
1990 | - case HttpStatusCode.Unauthorized: | |
1991 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
1992 | - return "Err:@" + userName + "'s Tweets are protected."; | |
1993 | - case HttpStatusCode.BadRequest: | |
1994 | - return "Err:API Limits?"; | |
1995 | - default: | |
1996 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
1997 | - } | |
1490 | + | |
1491 | + if (res == HttpStatusCode.Unauthorized) | |
1492 | + return "Err:@" + userName + "'s Tweets are protected."; | |
1493 | + | |
1494 | + var err = this.CheckStatusCode(res, content); | |
1495 | + if (err != null) return err; | |
1998 | 1496 | |
1999 | 1497 | List<TwitterDataModel.Status> items; |
2000 | 1498 | try |
@@ -2035,9 +1533,8 @@ namespace OpenTween | ||
2035 | 1533 | |
2036 | 1534 | if (MyCommon._endingFlag) return ""; |
2037 | 1535 | |
2038 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
1536 | + HttpStatusCode res; | |
2039 | 1537 | var content = ""; |
2040 | - | |
2041 | 1538 | try |
2042 | 1539 | { |
2043 | 1540 | res = twCon.ShowStatuses(id, ref content); |
@@ -2046,21 +1543,12 @@ namespace OpenTween | ||
2046 | 1543 | { |
2047 | 1544 | return "Err:" + ex.Message; |
2048 | 1545 | } |
2049 | - switch (res) | |
2050 | - { | |
2051 | - case HttpStatusCode.OK: | |
2052 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
2053 | - break; | |
2054 | - case HttpStatusCode.Unauthorized: | |
2055 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
2056 | - return Properties.Resources.Unauthorized; | |
2057 | - case HttpStatusCode.BadRequest: | |
2058 | - return "Err:API Limits?"; | |
2059 | - case HttpStatusCode.Forbidden: | |
2060 | - return "Err:protected user's tweet"; | |
2061 | - default: | |
2062 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
2063 | - } | |
1546 | + | |
1547 | + if (res == HttpStatusCode.Forbidden) | |
1548 | + return "Err:protected user's tweet"; | |
1549 | + | |
1550 | + var err = this.CheckStatusCode(res, content); | |
1551 | + if (err != null) return err; | |
2064 | 1552 | |
2065 | 1553 | TwitterDataModel.Status status; |
2066 | 1554 | try |
@@ -2509,19 +1997,9 @@ namespace OpenTween | ||
2509 | 1997 | { |
2510 | 1998 | return "Err:" + ex.Message; |
2511 | 1999 | } |
2512 | - switch (res) | |
2513 | - { | |
2514 | - case HttpStatusCode.OK: | |
2515 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
2516 | - break; | |
2517 | - case HttpStatusCode.Unauthorized: | |
2518 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
2519 | - return Properties.Resources.Unauthorized; | |
2520 | - case HttpStatusCode.BadRequest: | |
2521 | - return "Err:API Limits?"; | |
2522 | - default: | |
2523 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
2524 | - } | |
2000 | + | |
2001 | + var err = this.CheckStatusCode(res, content); | |
2002 | + if (err != null) return err; | |
2525 | 2003 | |
2526 | 2004 | return CreatePostsFromJson(content, MyCommon.WORKERTYPE.List, tab, read, count, ref tab.OldestId); |
2527 | 2005 | } |
@@ -2659,7 +2137,7 @@ namespace OpenTween | ||
2659 | 2137 | |
2660 | 2138 | if (MyCommon._endingFlag) return ""; |
2661 | 2139 | |
2662 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
2140 | + HttpStatusCode res; | |
2663 | 2141 | var content = ""; |
2664 | 2142 | try |
2665 | 2143 | { |
@@ -2676,19 +2154,9 @@ namespace OpenTween | ||
2676 | 2154 | { |
2677 | 2155 | return "Err:" + ex.Message; |
2678 | 2156 | } |
2679 | - switch (res) | |
2680 | - { | |
2681 | - case HttpStatusCode.OK: | |
2682 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
2683 | - break; | |
2684 | - case HttpStatusCode.Unauthorized: | |
2685 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
2686 | - return Properties.Resources.Unauthorized; | |
2687 | - case HttpStatusCode.BadRequest: | |
2688 | - return "Err:API Limits?"; | |
2689 | - default: | |
2690 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
2691 | - } | |
2157 | + | |
2158 | + var err = this.CheckStatusCode(res, content); | |
2159 | + if (err != null) return err; | |
2692 | 2160 | |
2693 | 2161 | List<TwitterDataModel.RelatedResult> items; |
2694 | 2162 | try |
@@ -2987,7 +2455,7 @@ namespace OpenTween | ||
2987 | 2455 | return "Auth Err:try to re-authorization."; |
2988 | 2456 | } |
2989 | 2457 | |
2990 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
2458 | + HttpStatusCode res; | |
2991 | 2459 | var content = ""; |
2992 | 2460 | |
2993 | 2461 | try |
@@ -3020,19 +2488,8 @@ namespace OpenTween | ||
3020 | 2488 | return "Err:" + ex.Message; |
3021 | 2489 | } |
3022 | 2490 | |
3023 | - switch (res) | |
3024 | - { | |
3025 | - case HttpStatusCode.OK: | |
3026 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
3027 | - break; | |
3028 | - case HttpStatusCode.Unauthorized: | |
3029 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
3030 | - return Properties.Resources.Unauthorized; | |
3031 | - case HttpStatusCode.BadRequest: | |
3032 | - return "Err:API Limits?"; | |
3033 | - default: | |
3034 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
3035 | - } | |
2491 | + var err = this.CheckStatusCode(res, content); | |
2492 | + if (err != null) return err; | |
3036 | 2493 | |
3037 | 2494 | return CreateDirectMessagesFromJson(content, gType, read); |
3038 | 2495 | } |
@@ -3046,8 +2503,6 @@ namespace OpenTween | ||
3046 | 2503 | |
3047 | 2504 | if (MyCommon._endingFlag) return ""; |
3048 | 2505 | |
3049 | - HttpStatusCode res; | |
3050 | - var content = ""; | |
3051 | 2506 | var count = AppendSettingDialog.Instance.CountApi; |
3052 | 2507 | if (AppendSettingDialog.Instance.UseAdditionalCount && |
3053 | 2508 | AppendSettingDialog.Instance.FavoritesCountApi != 0) |
@@ -3065,6 +2520,8 @@ namespace OpenTween | ||
3065 | 2520 | page_ = 1; |
3066 | 2521 | } |
3067 | 2522 | |
2523 | + HttpStatusCode res; | |
2524 | + var content = ""; | |
3068 | 2525 | try |
3069 | 2526 | { |
3070 | 2527 | res = twCon.Favorites(count, page_, ref content); |
@@ -3074,23 +2531,10 @@ namespace OpenTween | ||
3074 | 2531 | return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")"; |
3075 | 2532 | } |
3076 | 2533 | |
3077 | - switch (res) | |
3078 | - { | |
3079 | - case HttpStatusCode.OK: | |
3080 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
3081 | - break; | |
3082 | - case HttpStatusCode.Unauthorized: | |
3083 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
3084 | - return Properties.Resources.Unauthorized; | |
3085 | - case HttpStatusCode.BadRequest: | |
3086 | - return "Err:API Limits?"; | |
3087 | - default: | |
3088 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
3089 | - } | |
2534 | + var err = this.CheckStatusCode(res, content); | |
2535 | + if (err != null) return err; | |
3090 | 2536 | |
3091 | - var serializer = new DataContractJsonSerializer(typeof(List<TwitterDataModel.Status>)); | |
3092 | 2537 | List<TwitterDataModel.Status> item; |
3093 | - | |
3094 | 2538 | try |
3095 | 2539 | { |
3096 | 2540 | item = MyCommon.CreateDataFromJson<List<TwitterDataModel.Status>>(content); |
@@ -3271,7 +2715,7 @@ namespace OpenTween | ||
3271 | 2715 | { |
3272 | 2716 | if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return ""; |
3273 | 2717 | |
3274 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
2718 | + HttpStatusCode res; | |
3275 | 2719 | var content = ""; |
3276 | 2720 | try |
3277 | 2721 | { |
@@ -3282,19 +2726,8 @@ namespace OpenTween | ||
3282 | 2726 | return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")"; |
3283 | 2727 | } |
3284 | 2728 | |
3285 | - switch (res) | |
3286 | - { | |
3287 | - case HttpStatusCode.OK: | |
3288 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
3289 | - break; | |
3290 | - case HttpStatusCode.Unauthorized: | |
3291 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
3292 | - return Properties.Resources.Unauthorized; | |
3293 | - case HttpStatusCode.BadRequest: | |
3294 | - return "Err:API Limits?"; | |
3295 | - default: | |
3296 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
3297 | - } | |
2729 | + var err = this.CheckStatusCode(res, content); | |
2730 | + if (err != null) return err; | |
3298 | 2731 | |
3299 | 2732 | try |
3300 | 2733 | { |
@@ -3341,7 +2774,7 @@ namespace OpenTween | ||
3341 | 2774 | { |
3342 | 2775 | if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return ""; |
3343 | 2776 | |
3344 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
2777 | + HttpStatusCode res; | |
3345 | 2778 | var content = ""; |
3346 | 2779 | try |
3347 | 2780 | { |
@@ -3352,19 +2785,8 @@ namespace OpenTween | ||
3352 | 2785 | return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")"; |
3353 | 2786 | } |
3354 | 2787 | |
3355 | - switch (res) | |
3356 | - { | |
3357 | - case HttpStatusCode.OK: | |
3358 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
3359 | - break; | |
3360 | - case HttpStatusCode.Unauthorized: | |
3361 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
3362 | - return Properties.Resources.Unauthorized; | |
3363 | - case HttpStatusCode.BadRequest: | |
3364 | - return "Err:API Limits?"; | |
3365 | - default: | |
3366 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
3367 | - } | |
2788 | + var err = this.CheckStatusCode(res, content); | |
2789 | + if (err != null) return err; | |
3368 | 2790 | |
3369 | 2791 | try |
3370 | 2792 | { |
@@ -3406,19 +2828,8 @@ namespace OpenTween | ||
3406 | 2828 | return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")"; |
3407 | 2829 | } |
3408 | 2830 | |
3409 | - switch (res) | |
3410 | - { | |
3411 | - case HttpStatusCode.OK: | |
3412 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
3413 | - break; | |
3414 | - case HttpStatusCode.Unauthorized: | |
3415 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
3416 | - return Properties.Resources.Unauthorized; | |
3417 | - case HttpStatusCode.BadRequest: | |
3418 | - return "Err:API Limits?"; | |
3419 | - default: | |
3420 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
3421 | - } | |
2831 | + var err = this.CheckStatusCode(res, content); | |
2832 | + if (err != null) return err; | |
3422 | 2833 | |
3423 | 2834 | try |
3424 | 2835 | { |
@@ -3446,7 +2857,7 @@ namespace OpenTween | ||
3446 | 2857 | { |
3447 | 2858 | if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return ""; |
3448 | 2859 | |
3449 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
2860 | + HttpStatusCode res; | |
3450 | 2861 | var content = ""; |
3451 | 2862 | long cursor = -1; |
3452 | 2863 |
@@ -3462,19 +2873,8 @@ namespace OpenTween | ||
3462 | 2873 | return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")"; |
3463 | 2874 | } |
3464 | 2875 | |
3465 | - switch (res) | |
3466 | - { | |
3467 | - case HttpStatusCode.OK: | |
3468 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
3469 | - break; | |
3470 | - case HttpStatusCode.Unauthorized: | |
3471 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
3472 | - return Properties.Resources.Unauthorized; | |
3473 | - case HttpStatusCode.BadRequest: | |
3474 | - return "Err:API Limits?"; | |
3475 | - default: | |
3476 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
3477 | - } | |
2876 | + var err = this.CheckStatusCode(res, content); | |
2877 | + if (err != null) return err; | |
3478 | 2878 | |
3479 | 2879 | try |
3480 | 2880 | { |
@@ -3495,7 +2895,6 @@ namespace OpenTween | ||
3495 | 2895 | } while (cursor != 0); |
3496 | 2896 | |
3497 | 2897 | cursor = -1; |
3498 | - content = ""; | |
3499 | 2898 | do |
3500 | 2899 | { |
3501 | 2900 | try |
@@ -3507,19 +2906,8 @@ namespace OpenTween | ||
3507 | 2906 | return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")"; |
3508 | 2907 | } |
3509 | 2908 | |
3510 | - switch (res) | |
3511 | - { | |
3512 | - case HttpStatusCode.OK: | |
3513 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
3514 | - break; | |
3515 | - case HttpStatusCode.Unauthorized: | |
3516 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
3517 | - return Properties.Resources.Unauthorized; | |
3518 | - case HttpStatusCode.BadRequest: | |
3519 | - return "Err:API Limits?"; | |
3520 | - default: | |
3521 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
3522 | - } | |
2909 | + var err = this.CheckStatusCode(res, content); | |
2910 | + if (err != null) return err; | |
3523 | 2911 | |
3524 | 2912 | try |
3525 | 2913 | { |
@@ -3547,7 +2935,7 @@ namespace OpenTween | ||
3547 | 2935 | { |
3548 | 2936 | if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return ""; |
3549 | 2937 | |
3550 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
2938 | + HttpStatusCode res; | |
3551 | 2939 | IEnumerable<ListElement> lists; |
3552 | 2940 | var content = ""; |
3553 | 2941 |
@@ -3560,19 +2948,8 @@ namespace OpenTween | ||
3560 | 2948 | return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")"; |
3561 | 2949 | } |
3562 | 2950 | |
3563 | - switch (res) | |
3564 | - { | |
3565 | - case HttpStatusCode.OK: | |
3566 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
3567 | - break; | |
3568 | - case HttpStatusCode.Unauthorized: | |
3569 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
3570 | - return Properties.Resources.Unauthorized; | |
3571 | - case HttpStatusCode.BadRequest: | |
3572 | - return "Err:API Limits?"; | |
3573 | - default: | |
3574 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
3575 | - } | |
2951 | + var err = this.CheckStatusCode(res, content); | |
2952 | + if (err != null) return err; | |
3576 | 2953 | |
3577 | 2954 | try |
3578 | 2955 | { |
@@ -3590,7 +2967,6 @@ namespace OpenTween | ||
3590 | 2967 | return "Err:Invalid Json!"; |
3591 | 2968 | } |
3592 | 2969 | |
3593 | - content = ""; | |
3594 | 2970 | try |
3595 | 2971 | { |
3596 | 2972 | res = twCon.GetListsSubscriptions(this.Username, null, ref content); |
@@ -3600,19 +2976,8 @@ namespace OpenTween | ||
3600 | 2976 | return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")"; |
3601 | 2977 | } |
3602 | 2978 | |
3603 | - switch (res) | |
3604 | - { | |
3605 | - case HttpStatusCode.OK: | |
3606 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
3607 | - break; | |
3608 | - case HttpStatusCode.Unauthorized: | |
3609 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
3610 | - return Properties.Resources.Unauthorized; | |
3611 | - case HttpStatusCode.BadRequest: | |
3612 | - return "Err:API Limits?"; | |
3613 | - default: | |
3614 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
3615 | - } | |
2979 | + err = this.CheckStatusCode(res, content); | |
2980 | + if (err != null) return err; | |
3616 | 2981 | |
3617 | 2982 | try |
3618 | 2983 | { |
@@ -3636,7 +3001,7 @@ namespace OpenTween | ||
3636 | 3001 | |
3637 | 3002 | public string DeleteList(string list_id) |
3638 | 3003 | { |
3639 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
3004 | + HttpStatusCode res; | |
3640 | 3005 | var content = ""; |
3641 | 3006 | |
3642 | 3007 | try |
@@ -3648,26 +3013,15 @@ namespace OpenTween | ||
3648 | 3013 | return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")"; |
3649 | 3014 | } |
3650 | 3015 | |
3651 | - switch (res) | |
3652 | - { | |
3653 | - case HttpStatusCode.OK: | |
3654 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
3655 | - break; | |
3656 | - case HttpStatusCode.Unauthorized: | |
3657 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
3658 | - return Properties.Resources.Unauthorized; | |
3659 | - case HttpStatusCode.BadRequest: | |
3660 | - return "Err:API Limits?"; | |
3661 | - default: | |
3662 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
3663 | - } | |
3016 | + var err = this.CheckStatusCode(res, content); | |
3017 | + if (err != null) return err; | |
3664 | 3018 | |
3665 | 3019 | return ""; |
3666 | 3020 | } |
3667 | 3021 | |
3668 | 3022 | public string EditList(string list_id, string new_name, bool isPrivate, string description, ref ListElement list) |
3669 | 3023 | { |
3670 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
3024 | + HttpStatusCode res; | |
3671 | 3025 | var content = ""; |
3672 | 3026 | |
3673 | 3027 | try |
@@ -3679,19 +3033,8 @@ namespace OpenTween | ||
3679 | 3033 | return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")"; |
3680 | 3034 | } |
3681 | 3035 | |
3682 | - switch (res) | |
3683 | - { | |
3684 | - case HttpStatusCode.OK: | |
3685 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
3686 | - break; | |
3687 | - case HttpStatusCode.Unauthorized: | |
3688 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
3689 | - return Properties.Resources.Unauthorized; | |
3690 | - case HttpStatusCode.BadRequest: | |
3691 | - return "Err:API Limits?"; | |
3692 | - default: | |
3693 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
3694 | - } | |
3036 | + var err = this.CheckStatusCode(res, content); | |
3037 | + if (err != null) return err; | |
3695 | 3038 | |
3696 | 3039 | try |
3697 | 3040 | { |
@@ -3726,10 +3069,8 @@ namespace OpenTween | ||
3726 | 3069 | { |
3727 | 3070 | if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return ""; |
3728 | 3071 | |
3729 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
3072 | + HttpStatusCode res; | |
3730 | 3073 | var content = ""; |
3731 | - | |
3732 | - //Do | |
3733 | 3074 | try |
3734 | 3075 | { |
3735 | 3076 | res = twCon.GetListMembers(this.Username, list_id, cursor, ref content); |
@@ -3739,19 +3080,8 @@ namespace OpenTween | ||
3739 | 3080 | return "Err:" + ex.Message; |
3740 | 3081 | } |
3741 | 3082 | |
3742 | - switch (res) | |
3743 | - { | |
3744 | - case HttpStatusCode.OK: | |
3745 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
3746 | - break; | |
3747 | - case HttpStatusCode.Unauthorized: | |
3748 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
3749 | - return Properties.Resources.Unauthorized; | |
3750 | - case HttpStatusCode.BadRequest: | |
3751 | - return "Err:API Limits?"; | |
3752 | - default: | |
3753 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
3754 | - } | |
3083 | + var err = this.CheckStatusCode(res, content); | |
3084 | + if (err != null) return err; | |
3755 | 3085 | |
3756 | 3086 | try |
3757 | 3087 | { |
@@ -3778,9 +3108,8 @@ namespace OpenTween | ||
3778 | 3108 | { |
3779 | 3109 | if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return ""; |
3780 | 3110 | |
3781 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
3111 | + HttpStatusCode res; | |
3782 | 3112 | var content = ""; |
3783 | - | |
3784 | 3113 | try |
3785 | 3114 | { |
3786 | 3115 | res = twCon.CreateLists(listName, isPrivate, description, ref content); |
@@ -3790,19 +3119,8 @@ namespace OpenTween | ||
3790 | 3119 | return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")"; |
3791 | 3120 | } |
3792 | 3121 | |
3793 | - switch (res) | |
3794 | - { | |
3795 | - case HttpStatusCode.OK: | |
3796 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
3797 | - break; | |
3798 | - case HttpStatusCode.Unauthorized: | |
3799 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
3800 | - return Properties.Resources.Unauthorized; | |
3801 | - case HttpStatusCode.BadRequest: | |
3802 | - return "Err:API Limits?"; | |
3803 | - default: | |
3804 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
3805 | - } | |
3122 | + var err = this.CheckStatusCode(res, content); | |
3123 | + if (err != null) return err; | |
3806 | 3124 | |
3807 | 3125 | try |
3808 | 3126 | { |
@@ -3828,7 +3146,7 @@ namespace OpenTween | ||
3828 | 3146 | |
3829 | 3147 | if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return ""; |
3830 | 3148 | |
3831 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
3149 | + HttpStatusCode res; | |
3832 | 3150 | var content = ""; |
3833 | 3151 | |
3834 | 3152 | try |
@@ -3840,23 +3158,15 @@ namespace OpenTween | ||
3840 | 3158 | return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")"; |
3841 | 3159 | } |
3842 | 3160 | |
3843 | - switch (res) | |
3161 | + if (res == HttpStatusCode.NotFound) | |
3844 | 3162 | { |
3845 | - case HttpStatusCode.OK: | |
3846 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
3847 | - break; | |
3848 | - case HttpStatusCode.Unauthorized: | |
3849 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
3850 | - return Properties.Resources.Unauthorized; | |
3851 | - case HttpStatusCode.BadRequest: | |
3852 | - return "Err:API Limits?"; | |
3853 | - case HttpStatusCode.NotFound: | |
3854 | - value = false; | |
3855 | - return ""; | |
3856 | - default: | |
3857 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
3163 | + value = false; | |
3164 | + return ""; | |
3858 | 3165 | } |
3859 | 3166 | |
3167 | + var err = this.CheckStatusCode(res, content); | |
3168 | + if (err != null) return err; | |
3169 | + | |
3860 | 3170 | try |
3861 | 3171 | { |
3862 | 3172 | var u = MyCommon.CreateDataFromJson<TwitterDataModel.User>(content); |
@@ -3872,8 +3182,8 @@ namespace OpenTween | ||
3872 | 3182 | |
3873 | 3183 | public string AddUserToList(string listId, string user) |
3874 | 3184 | { |
3185 | + HttpStatusCode res; | |
3875 | 3186 | var content = ""; |
3876 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
3877 | 3187 | |
3878 | 3188 | try |
3879 | 3189 | { |
@@ -3884,28 +3194,16 @@ namespace OpenTween | ||
3884 | 3194 | return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")"; |
3885 | 3195 | } |
3886 | 3196 | |
3887 | - switch (res) | |
3888 | - { | |
3889 | - case HttpStatusCode.OK: | |
3890 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
3891 | - break; | |
3892 | - case HttpStatusCode.Unauthorized: | |
3893 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
3894 | - return Properties.Resources.Unauthorized; | |
3895 | - case HttpStatusCode.BadRequest: | |
3896 | - return "Err:" + GetErrorMessageJson(content); | |
3897 | - default: | |
3898 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
3899 | - } | |
3197 | + var err = this.CheckStatusCode(res, content); | |
3198 | + if (err != null) return err; | |
3900 | 3199 | |
3901 | 3200 | return ""; |
3902 | 3201 | } |
3903 | 3202 | |
3904 | 3203 | public string RemoveUserToList(string listId, string user) |
3905 | 3204 | { |
3906 | - | |
3205 | + HttpStatusCode res; | |
3907 | 3206 | var content = ""; |
3908 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
3909 | 3207 | |
3910 | 3208 | try |
3911 | 3209 | { |
@@ -3916,19 +3214,8 @@ namespace OpenTween | ||
3916 | 3214 | return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")"; |
3917 | 3215 | } |
3918 | 3216 | |
3919 | - switch (res) | |
3920 | - { | |
3921 | - case HttpStatusCode.OK: | |
3922 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
3923 | - break; | |
3924 | - case HttpStatusCode.Unauthorized: | |
3925 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
3926 | - return Properties.Resources.Unauthorized; | |
3927 | - case HttpStatusCode.BadRequest: | |
3928 | - return "Err:" + GetErrorMessageJson(content); | |
3929 | - default: | |
3930 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
3931 | - } | |
3217 | + var err = this.CheckStatusCode(res, content); | |
3218 | + if (err != null) return err; | |
3932 | 3219 | |
3933 | 3220 | return ""; |
3934 | 3221 | } |
@@ -4231,7 +3518,7 @@ namespace OpenTween | ||
4231 | 3518 | |
4232 | 3519 | if (MyCommon._endingFlag) return null; |
4233 | 3520 | |
4234 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
3521 | + HttpStatusCode res; | |
4235 | 3522 | var content = ""; |
4236 | 3523 | try |
4237 | 3524 | { |
@@ -4243,7 +3530,8 @@ namespace OpenTween | ||
4243 | 3530 | return null; |
4244 | 3531 | } |
4245 | 3532 | |
4246 | - if (res != HttpStatusCode.OK) return null; | |
3533 | + var err = this.CheckStatusCode(res, content); | |
3534 | + if (err != null) return null; | |
4247 | 3535 | |
4248 | 3536 | try |
4249 | 3537 | { |
@@ -4266,7 +3554,7 @@ namespace OpenTween | ||
4266 | 3554 | |
4267 | 3555 | if (MyCommon._endingFlag) return null; |
4268 | 3556 | |
4269 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
3557 | + HttpStatusCode res; | |
4270 | 3558 | var content = ""; |
4271 | 3559 | try |
4272 | 3560 | { |
@@ -4278,7 +3566,8 @@ namespace OpenTween | ||
4278 | 3566 | return null; |
4279 | 3567 | } |
4280 | 3568 | |
4281 | - if (res != HttpStatusCode.OK) return null; | |
3569 | + var err = this.CheckStatusCode(res, content); | |
3570 | + if (err != null) return null; | |
4282 | 3571 | |
4283 | 3572 | try |
4284 | 3573 | { |
@@ -4297,7 +3586,7 @@ namespace OpenTween | ||
4297 | 3586 | { |
4298 | 3587 | if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return ""; |
4299 | 3588 | |
4300 | - HttpStatusCode res = HttpStatusCode.BadRequest; | |
3589 | + HttpStatusCode res; | |
4301 | 3590 | var content = ""; |
4302 | 3591 | |
4303 | 3592 | try |
@@ -4309,19 +3598,8 @@ namespace OpenTween | ||
4309 | 3598 | return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")"; |
4310 | 3599 | } |
4311 | 3600 | |
4312 | - switch (res) | |
4313 | - { | |
4314 | - case HttpStatusCode.OK: | |
4315 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
4316 | - break; | |
4317 | - case HttpStatusCode.Unauthorized: | |
4318 | - Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
4319 | - return Properties.Resources.Unauthorized; | |
4320 | - case HttpStatusCode.BadRequest: | |
4321 | - return "Err:API Limits?"; | |
4322 | - default: | |
4323 | - return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")"; | |
4324 | - } | |
3601 | + var err = this.CheckStatusCode(res, content); | |
3602 | + if (err != null) return err; | |
4325 | 3603 | |
4326 | 3604 | try |
4327 | 3605 | { |
@@ -4370,6 +3648,48 @@ namespace OpenTween | ||
4370 | 3648 | } |
4371 | 3649 | } |
4372 | 3650 | |
3651 | + [MethodImpl(MethodImplOptions.NoInlining)] // 呼び出し元の取得に必要 | |
3652 | + private string CheckStatusCode(HttpStatusCode httpStatus, string responseText) | |
3653 | + { | |
3654 | + if (httpStatus == HttpStatusCode.OK) | |
3655 | + { | |
3656 | + Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid; | |
3657 | + return null; | |
3658 | + } | |
3659 | + | |
3660 | + // 404エラーの挙動が変なので無視: https://dev.twitter.com/discussions/1213 | |
3661 | + if (httpStatus == HttpStatusCode.NotFound) return null; | |
3662 | + | |
3663 | + var callerMethod = new StackTrace(false).GetFrame(1).GetMethod().Name; | |
3664 | + | |
3665 | + if (string.IsNullOrWhiteSpace(responseText)) | |
3666 | + { | |
3667 | + if (httpStatus == HttpStatusCode.Unauthorized) | |
3668 | + Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
3669 | + | |
3670 | + return "Err:" + httpStatus + "(" + callerMethod + ")"; | |
3671 | + } | |
3672 | + | |
3673 | + try | |
3674 | + { | |
3675 | + var errors = MyCommon.CreateDataFromJson<TwitterDataModel.ErrorResponse>(responseText).Errors; | |
3676 | + | |
3677 | + foreach (var error in errors) | |
3678 | + { | |
3679 | + if (error.Code == TwitterDataModel.ErrorCode.InvalidToken || | |
3680 | + error.Code == TwitterDataModel.ErrorCode.SuspendedAccount) | |
3681 | + { | |
3682 | + Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid; | |
3683 | + } | |
3684 | + } | |
3685 | + | |
3686 | + return "Err:" + string.Join(",", errors.Select(x => x.ToString())) + "(" + callerMethod + ")"; | |
3687 | + } | |
3688 | + catch (SerializationException) { } | |
3689 | + | |
3690 | + return "Err:" + responseText + "(" + callerMethod + ")"; | |
3691 | + } | |
3692 | + | |
4373 | 3693 | #region "UserStream" |
4374 | 3694 | private string trackWord_ = ""; |
4375 | 3695 | public string TrackWord |