package com.khmelenko.lab.travisclient.task.github;

import android.text.TextUtils;

import com.khmelenko.lab.travisclient.event.github.CreateAuthorizationSuccessEvent;
import com.khmelenko.lab.travisclient.event.github.GithubAuthorizationFailEvent;
import com.khmelenko.lab.travisclient.network.request.AuthorizationRequest;
import com.khmelenko.lab.travisclient.network.response.Authorization;
import com.khmelenko.lab.travisclient.network.retrofit.GithubApiService;
import com.khmelenko.lab.travisclient.task.Task;
import com.khmelenko.lab.travisclient.task.TaskError;
import com.khmelenko.lab.travisclient.task.TaskException;

import retrofit.client.Header;
import retrofit.client.Response;

/**
 * The task for creation new authorization
 *
 * @author Dmytro Khmelenko
 */
public final class CreateAuthorizationTask extends Task<Authorization> {

    private final String mBasicAuth;
    private final AuthorizationRequest mAuthorizationRequest;
    private String mTwoFactorCode;

    public CreateAuthorizationTask(String basicAuth, AuthorizationRequest authorizationRequest) {
        mBasicAuth = basicAuth;
        mAuthorizationRequest = authorizationRequest;
    }

    public CreateAuthorizationTask(String basicAuth, AuthorizationRequest authorizationRequest, String twoFactorCode) {
        mBasicAuth = basicAuth;
        mAuthorizationRequest = authorizationRequest;
        mTwoFactorCode = twoFactorCode;
    }

    @Override
    public Authorization execute() throws TaskException {

        Authorization authorization;
        try {
            if (!TextUtils.isEmpty(mTwoFactorCode)) {
                authorization = mRestClient.getGithubApiService().createNewAuthorization(mBasicAuth,
                        mTwoFactorCode, mAuthorizationRequest);
            } else {
                authorization = mRestClient.getGithubApiService()
                        .createNewAuthorization(mBasicAuth, mAuthorizationRequest);
            }
        } catch (TaskException error) {

            Response response = error.getTaskError().getResponse();
            boolean twoFactorAuthRequired = false;
            for (Header header : response.getHeaders()) {
                if (GithubApiService.TWO_FACTOR_HEADER.equals(header.getName())) {
                    twoFactorAuthRequired = true;
                    break;
                }
            }
            if (response.getStatus() == 401 && twoFactorAuthRequired) {
                TaskError taskError = new TaskError(TaskError.TWO_FACTOR_AUTH_REQUIRED, "");
                throw new TaskException(taskError);
            } else {
                throw error;
            }
        }

        return authorization;
    }

    @Override
    public void onSuccess(Authorization result) {
        CreateAuthorizationSuccessEvent event = new CreateAuthorizationSuccessEvent(result);
        mEventBus.post(event);
    }

    @Override
    public void onFail(TaskError error) {
        GithubAuthorizationFailEvent event = new GithubAuthorizationFailEvent(error);
        mEventBus.post(event);
    }
}

@@ -2061,16 +2061,55 @@
 onse();%0A
+            if(response != null) %7B%0A    
         
@@ -2151,16 +2151,20 @@
  false;%0A
+    
         
@@ -2225,24 +2225,28 @@
             
+    
 if (GithubAp
@@ -2320,16 +2320,20 @@
         
+ 
+   
 twoFacto
@@ -2374,16 +2374,20 @@
         
+    
 break;%0A 
@@ -2393,34 +2393,42 @@
                 
+  
-%7D%0A
+  %7D%0A    
             %7D%0A  
@@ -2417,32 +2417,36 @@
               %7D%0A
+    
             if (
@@ -2517,16 +2517,20 @@
         
+    
 TaskErro
@@ -2610,24 +2610,28 @@
             
+    
 throw new Ta
@@ -2650,24 +2650,100 @@
 taskError);%0A
+                %7D else %7B%0A                    throw error;%0A                %7D%0A
             

Handle NPE in case of error during authorization task