/*
* Copyright (c) 2012, Soren Davidsen
*
* 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 scrumdo;
import com.google.gson.*;
import scrumdo.meta.GsonBuilderCallback;
import scrumdo.meta.Tastypie;
import scrumdo.model.Login;
import scrumdo.model.Points;
import scrumdo.transport.HttpClientTransport;
import scrumdo.transport.ITransport;
import scrumdo.transport.Response;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
/**
* Constructs the ScrumDoApi by performing login procedure and constructing tastypie interface using user_key credentials.
*
* @author Soren <soren@tanesha.net>
*/
public class ScrumDoApiFactory {
/**
* The base url of the scrumdo API.
*/
public static final String BASE_URL = "http://www.scrumdo.com/api/v1/";
/**
* Create a ScrumDoApi using default transport (http client).
*
* @param developerKey The devleoper key
* @param username The username
* @param password The password
* @return New scrumdo api instance.
*/
public static ScrumDoApi newScrumDo(String developerKey, String username, String password) {
return newScrumDo(developerKey, username, password, new HttpClientTransport());
}
/**
* Create a ScrumDoApi by using a specified transport.
*
* @param developerKey
* @param username
* @param password
* @param transport
* @return
*/
public static ScrumDoApi newScrumDo(String developerKey, String username, String password, ITransport transport) {
try {
URL base = new URL(BASE_URL);
StringBuffer url = new StringBuffer(new URL(base, "login").toExternalForm())
.append("?format=json")
.append("&developer_key=").append(URLEncoder.encode(developerKey))
.append("&username=").append(URLEncoder.encode(username))
.append("&password=").append(URLEncoder.encode(password));
Response response = transport.get(url.toString());
Login login = new Gson().fromJson(response.content, Login.class);
GsonBuilderCallback gbc = new GsonBuilderCallback() {
@Override
public void doWithBuilder(GsonBuilder builder) {
builder.registerTypeAdapter(Points.class, new PointsHandler());
}
};
Tastypie pie = new Tastypie(gbc, transport, base,
"user_key", login.key,
"user_name", username,
"format", "json");
return new ScrumDoApi(pie);
} catch (MalformedURLException mfe) {
throw new RuntimeException("Malformed urls: " + mfe.getMessage(), mfe);
}
}
/**
* Used for resolving scrumdo specific "points" objects.
*/
private static class PointsHandler implements JsonSerializer<Points>, JsonDeserializer<Points> {
public JsonElement serialize(Points src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.points);
}
public Points deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return new Points(json.getAsJsonPrimitive().getAsString());
}
}
}