authorization fix

This commit is contained in:
Aleksandr Zelenin 2019-12-30 18:52:13 +03:00
parent 9e9b0f6083
commit b1a5e5a5b9
2 changed files with 12 additions and 7 deletions

View file

@ -16,15 +16,16 @@ type AuthorizationStateHandler interface {
func Authorize(client *Client, authorizationStateHandler AuthorizationStateHandler) error {
defer authorizationStateHandler.Close()
var authorizationError error
for {
state, err := client.GetAuthorizationState()
if err != nil {
return err
}
err = authorizationStateHandler.Handle(client, state)
if err != nil {
return err
if state.AuthorizationStateType() == TypeAuthorizationStateClosed {
return authorizationError
}
if state.AuthorizationStateType() == TypeAuthorizationStateReady {
@ -32,6 +33,12 @@ func Authorize(client *Client, authorizationStateHandler AuthorizationStateHandl
time.Sleep(1 * time.Second)
return nil
}
err = authorizationStateHandler.Handle(client, state)
if err != nil {
authorizationError = err
client.Close()
}
}
}
@ -100,10 +107,10 @@ func (stateHandler *clientAuthorizer) Handle(client *Client, state Authorization
return ErrNotSupportedAuthorizationState
case TypeAuthorizationStateClosing:
return ErrNotSupportedAuthorizationState
return nil
case TypeAuthorizationStateClosed:
return ErrNotSupportedAuthorizationState
return nil
}
return ErrNotSupportedAuthorizationState

View file

@ -71,8 +71,6 @@ func NewClient(authorizationStateHandler AuthorizationStateHandler, options ...O
err := Authorize(client, authorizationStateHandler)
if err != nil {
client.Stop()
return nil, err
}