package com.alwaysallthetime.adnlib;

import android.os.AsyncTask;
import android.os.Build;

import com.alwaysallthetime.adnlib.request.AppDotNetRequest;

public class AppDotNetClient {
    public static final String METHOD_DELETE = "DELETE";
    public static final String METHOD_GET = "GET";
    public static final String METHOD_POST = "POST";
    public static final String METHOD_PUT = "PUT";

    protected String authHeader;

    public AppDotNetClient() {}

    public AppDotNetClient(String token) {
        setToken(token);
    }

    public void setToken(String token) {
        authHeader = "Bearer " + token;
    }

    public boolean hasToken() {
        return authHeader != null;
    }

    protected void execute(AppDotNetRequest request) {
        if (request.isAuthenticated() && !hasToken()) {
            throw new IllegalStateException("authentication token not set");
        }

        final AppDotNetClientTask task = new AppDotNetClientTask(authHeader);

        // AsyncTask was changed in Honeycomb to execute in serial by default, at which time
        // executeOnExecutor was added to specify parallel execution.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, request);
        } else {
            task.execute(request);
        }
    }
}
@@ -138,21 +138,367 @@
 ppDotNet
-Reque
+OAuthRequest;%0Aimport com.alwaysallthetime.adnlib.request.AppDotNetRequest;%0Aimport com.alwaysallthetime.adnlib.response.AccessTokenResponseHandler;%0Aimport com.alwaysallthetime.adnlib.response.LoginResponseHandler;%0A%0Aimport org.apache.http.NameValuePair;%0Aimport org.apache.http.message.BasicNameValuePair;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Li
 st;%0A%0Apub
@@ -770,16 +770,62 @@
 hHeader;
+%0A    protected List%3CNameValuePair%3E authParams;
 %0A%0A    pu
@@ -941,150 +941,1636 @@
 lic 
-void setToken(String token) %7B%0A        authHeader = %22Bearer %22 + token;%0A    %7D%0A%0A    public boolean hasToken() %7B%0A        return authHeader != null
+AppDotNetClient(String clientId, String passwordGrantSecret) %7B%0A        authParams = new ArrayList%3CNameValuePair%3E(3);%0A        authParams.add(new BasicNameValuePair(%22client_id%22, clientId));%0A        authParams.add(new BasicNameValuePair(%22password_grant_secret%22, passwordGrantSecret));%0A        authParams.add(new BasicNameValuePair(%22grant_type%22, %22password%22));%0A    %7D%0A%0A    public void setToken(String token) %7B%0A        authHeader = %22Bearer %22 + token;%0A    %7D%0A%0A    public boolean hasToken() %7B%0A        return authHeader != null;%0A    %7D%0A%0A    /*%0A     * OAUTH%0A     */%0A%0A    public void authenticateWithPassword(String username, String password, String scope, LoginResponseHandler responseHandler) %7B%0A        if (authParams == null)%0A            throw new IllegalStateException(%22client must be constructed with client ID and password grant secret%22);%0A%0A        final List%3CNameValuePair%3E params = getAuthenticationParams(username, password, scope);%0A        final AccessTokenResponseHandler tokenResponseHandler = new AccessTokenResponseHandler(this, responseHandler);%0A        execute(new AppDotNetOAuthRequest(tokenResponseHandler, params, %22access_token%22));%0A    %7D%0A%0A    protected List%3CNameValuePair%3E getAuthenticationParams(String username, String password, String scope) %7B%0A        final List%3CNameValuePair%3E params = new ArrayList%3CNameValuePair%3E(authParams.size() + 3);%0A        params.addAll(authParams);%0A        params.add(new BasicNameValuePair(%22username%22, username));%0A        params.add(new BasicNameValuePair(%22password%22, password));%0A%0A        if (scope != null)%0A            params.add(new BasicNameValuePair(%22scope%22, scope));%0A%0A        return params
 ;%0A  

add password flow authentication support