
package com.battlelancer.seriesguide.getglueapi;

import com.battlelancer.seriesguide.Constants;

import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.SharedPreferences;

import java.net.URLEncoder;

public class GetGlue {

    // private static final String TAG = GetGlue.class.getName();

    private static final String GETGLUE_APIPATH_V2 = "http://api.getglue.com/v2/";

    private static final String GETGLUE_SOURCE = "&source=http://seriesguide.uwetrottmann.com/&app=SeriesGuide";

    public static final String REQUEST_URL = "http://api.getglue.com/oauth/request_token";

    public static final String ACCESS_URL = "http://api.getglue.com/oauth/access_token";

    public static final String AUTHORIZE_URL = "http://getglue.com/oauth/authorize?style=mobile";

    public static final String OAUTH_CALLBACK_SCHEME = "seriesguide-oauth-getglue";

    public static final String OAUTH_CALLBACK_HOST = "callback";

    public static final String OAUTH_CALLBACK_URL = OAUTH_CALLBACK_SCHEME + "://"
            + OAUTH_CALLBACK_HOST;

    public static boolean isAuthenticated(SharedPreferences prefs) {

        String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
        String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");

        if (token == "" || secret == "") {
            return false;
        }

        return true;
    }

    public static void checkIn(SharedPreferences prefs, String imdbId, String comment)
            throws Exception {

        String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
        String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");

        comment = URLEncoder.encode(comment, "UTF-8");
        String requestString = GETGLUE_APIPATH_V2;
        requestString += "user/addCheckin?objectId=http://www.imdb.com/title/" + imdbId
                + GETGLUE_SOURCE + "&comment=" + comment;

        // create a consumer object and configure it with the access
        // token and token secret obtained from the service provider
        OAuthConsumer consumer = new CommonsHttpOAuthConsumer(Constants.CONSUMER_KEY,
                Constants.CONSUMER_SECRET);
        consumer.setTokenWithSecret(token, secret);

        // create an HTTP request to a protected resource
        HttpGet request = new HttpGet(requestString);

        // sign the request
        consumer.sign(request);

        // send the request
        HttpClient httpClient = new DefaultHttpClient();
        httpClient.execute(request);
        // HttpResponse response = httpClient.execute(request);

        // TODO: improve error handling
    }

    public static void clearCredentials(SharedPreferences prefs) {
        prefs.edit().putString(OAuth.OAUTH_TOKEN, "").putString(OAuth.OAUTH_TOKEN_SECRET, "")
                .commit();
    }
}

@@ -224,24 +224,98 @@
 onsumer;%0D%0A%0D%0A
+import org.apache.http.HttpResponse;%0D%0Aimport org.apache.http.HttpStatus;%0D%0A
 import org.a
@@ -2830,16 +2830,40 @@
 %0A       
+ HttpResponse response =
  httpCli
@@ -2885,24 +2885,26 @@
 uest);%0D%0A
+%0D%0A
         
 // HttpR
@@ -2899,106 +2899,274 @@
     
-// HttpResponse response = httpClient.execute(request);%0D%0A%0D%0A        // TODO: improve error handling
+int statuscode = response.getStatusLine().getStatusCode();%0D%0A        if (statuscode != HttpStatus.SC_OK) %7B%0D%0A            throw new Exception(%22Unexpected server response %22 + response.getStatusLine() + %22 for %22%0D%0A                    + request.getRequestLine());%0D%0A        %7D
 %0D%0A  

Improve GetGlue request error handling a little.