public TwitterAccountState AccountState { get; private set; } = new();
public TwitterApi()
- => this.ApiConnection = new TwitterApiConnection(new TwitterCredentialNone());
+ => this.ApiConnection = new TwitterApiConnection();
public void Initialize(ITwitterCredential credential, TwitterAccountState accountState)
{
this.AuthType = credential.AuthType;
- var newInstance = new TwitterApiConnection(credential);
+ var newInstance = new TwitterApiConnection(credential, accountState);
var oldInstance = Interlocked.Exchange(ref this.ApiConnection, newInstance);
oldInstance?.Dispose();
using System.Web;
using OpenTween.Api;
using OpenTween.Api.DataModel;
+using OpenTween.SocialProtocol.Twitter;
namespace OpenTween.Connection
{
internal ITwitterCredential Credential { get; }
+ private readonly TwitterAccountState accountState;
+
public TwitterApiConnection()
- : this(new TwitterCredentialNone())
+ : this(new TwitterCredentialNone(), new())
{
}
- public TwitterApiConnection(ITwitterCredential credential)
+ public TwitterApiConnection(ITwitterCredential credential, TwitterAccountState accountState)
{
this.Credential = credential;
+ this.accountState = accountState;
this.InitializeHttpClients();
Networking.WebProxyChanged += this.Networking_WebProxyChanged;
if (endpointName != null)
MyCommon.TwitterApiInfo.UpdateFromHeader(responseMessage.Headers, endpointName);
- await TwitterApiConnection.CheckStatusCode(responseMessage)
+ await TwitterApiConnection.CheckStatusCode(responseMessage, this.accountState)
.ConfigureAwait(false);
var response = new ApiResponse(responseMessage);
return await task;
}
- protected static async Task CheckStatusCode(HttpResponseMessage response)
+ protected static async Task CheckStatusCode(HttpResponseMessage response, TwitterAccountState? accountState)
{
var statusCode = response.StatusCode;
if ((int)statusCode >= 200 && (int)statusCode <= 299)
{
- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
+ if (accountState != null)
+ accountState.HasUnrecoverableError = false;
return;
}
if (string.IsNullOrWhiteSpace(responseText))
{
if (statusCode == HttpStatusCode.Unauthorized)
- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
+ {
+ if (accountState != null)
+ accountState.HasUnrecoverableError = true;
+ }
throw new TwitterApiException(statusCode, responseText);
}
var errorCodes = error.Errors.Select(x => x.Code);
if (errorCodes.Any(x => x == TwitterErrorCode.InternalError || x == TwitterErrorCode.SuspendedAccount))
{
- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
+ if (accountState != null)
+ accountState.HasUnrecoverableError = true;
}
throw new TwitterApiException(statusCode, error, responseText);
var responseText = await content.ReadAsStringAsync()
.ConfigureAwait(false);
- await TwitterApiConnection.CheckStatusCode(response)
+ await TwitterApiConnection.CheckStatusCode(response, accountState: null)
.ConfigureAwait(false);
var responseParams = HttpUtility.ParseQueryString(responseText);
private bool CheckAccountValid()
{
- if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
+ if (this.tw.Api.AccountState.HasUnrecoverableError)
{
this.errorCount += 1;
if (this.errorCount > 5)
{
this.errorCount = 0;
- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
+ this.tw.Api.AccountState.HasUnrecoverableError = false;
return true;
}
return false;
}
}
- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
+ this.tw.Api.AccountState.HasUnrecoverableError = false;
this.TopMost = this.settings.Common.AlwaysTop;
this.SaveConfigsAll(false);
public void ClearAuthInfo()
{
- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
+ this.Api.AccountState.HasUnrecoverableError = true;
this.ResetApiStatus();
}
{
// OAuth認証
if (credential is TwitterCredentialNone)
- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
+ this.Api.AccountState.HasUnrecoverableError = true;
this.ResetApiStatus();
this.Api.Initialize(credential, accountState);
public long UserId
=> this.Api.CurrentUserId;
- public static MyCommon.ACCOUNT_STATE AccountState { get; set; } = MyCommon.ACCOUNT_STATE.Valid;
-
public bool RestrictFavCheck { get; set; }
public int? FollowersCount
public async Task<TwitterApiStatus?> GetInfoApi()
{
- if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return null;
+ if (this.Api.AccountState.HasUnrecoverableError)
+ return null;
if (MyCommon.EndingFlag) return null;
internal void CheckAccountState()
{
- if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
+ if (this.Api.AccountState.HasUnrecoverableError)
throw new WebApiException("Auth error. Check your account");
}