/*
 * Copyright (c) 2011 Novell Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.suse.studio.client;

import java.io.IOException;
import java.util.List;

import com.suse.studio.client.data.Appliance;
import com.suse.studio.client.data.Appliances;
import com.suse.studio.client.data.User;
import com.suse.studio.client.net.StudioConnection;
import com.suse.studio.util.Base64;

/**
 * Library class for the SUSE Studio REST API.
 */
public class SUSEStudio {

    private final String user;
    private final String apiKey;

    public static final String baseURL = "http://susestudio.com/api/v2";

    public SUSEStudio(String user, String apiKey) {
        if (user == null || apiKey == null) {
            throw new RuntimeException("We need the user and API key!");
        }
        this.user = user;
        this.apiKey = apiKey;
    }

    /**
     * GET /api/v2/user/account
     *
     * @return current user
     * @throws IOException
     */
    public User getUser() throws IOException {
        StudioConnection sc = new StudioConnection("/user/account", getEncodedCredentials());
        User result = sc.get(User.class);
        return result;
    }

    /**
     * Get all appliances of the current user.
     * GET /api/v2/user/appliances
     *
     * @return list of the current user's appliances
     * @throws IOException
     */
    public List<Appliance> getAppliances() throws IOException {
        StudioConnection sc = new StudioConnection("/user/appliances", getEncodedCredentials());
        Appliances result = sc.get(Appliances.class);
        return result.getAppliances();
    }

    /**
     * Return the encoded credentials.
     *
     * @return encoded credentials as {@link String}
     */
    private String getEncodedCredentials() {
        return Base64.encodeBytes((user + ":" + apiKey).getBytes());
    }
}

@@ -1416,16 +1416,51 @@
 .Base64;
+%0Aimport com.suse.studio.util.Prefs;
 %0A%0A/**%0A *
@@ -1543,393 +1543,946 @@
     
-private final String user;%0A    private final String apiKey;%0A%0A    public static final String baseURL = %22http://susestudio.com/api/v2%22;%0A%0A    public SUSEStudio(String user, String apiKey) %7B%0A        if (user == null %7C%7C apiKey == null) %7B%0A            throw new RuntimeException(%22We need the user and API key!%22);%0A        %7D%0A        this.user = user;%0A        this.apiKey = apiKey;%0A    %7D%0A%0A    /*
+// The encoded credentials%0A    private final String credentials;%0A%0A    /**%0A     * Create a client object by providing user and API key. This client will%0A     * talk to the public SUSE Studio default URL.%0A     *%0A     * @param user%0A     * @param apiKey%0A     */%0A    public SUSEStudio(String user, String apiKey) %7B%0A        if (user == null %7C%7C apiKey == null) %7B%0A            throw new RuntimeException(%22We need the user and API key!%22);%0A        %7D%0A        // Encode the credentials%0A        this.credentials = Base64.encodeBytes((user + %22:%22 + apiKey).getBytes());%0A    %7D%0A%0A    /**%0A     * Create a client by providing user, API key and a base URL of the API.%0A     *%0A     * @param user%0A     * @param apiKey%0A     * @param url%0A     */%0A    public SUSEStudio(String user, String apiKey, String url) %7B%0A        this(user, apiKey);%0A        Prefs.put(Prefs.BASE_URL, url);%0A    %7D%0A%0A    /**%0A     * Get information about the user associated with this client.%0A     
 *%0A  
@@ -2696,27 +2696,17 @@
 count%22, 
-getEncodedC
+c
 redentia
@@ -2699,34 +2699,32 @@
 nt%22, credentials
-()
 );%0A        User 
@@ -2837,16 +2837,23 @@
 t user.%0A
+     *%0A
      * G
@@ -3109,27 +3109,17 @@
 ances%22, 
-getEncodedC
+c
 redentia
@@ -3120,18 +3120,16 @@
 dentials
-()
 );%0A     
@@ -3225,243 +3225,7 @@
    %7D
-%0A%0A    /**%0A     * Return the encoded credentials.%0A     *%0A     * @return encoded credentials as %7B@link String%7D%0A     */%0A    private String getEncodedCredentials() %7B%0A        return Base64.encodeBytes((user + %22:%22 + apiKey).getBytes());%0A    %7D
 %0A%7D%0A

Add constructor accepting base URL as parameter + pre-encode credentials