Hook into Booster API

This commit is contained in:
Shaun Bennett 2016-05-20 16:58:58 +10:00
parent 012edfed45
commit efb264dd75
12 changed files with 402 additions and 11 deletions

View File

@ -16,6 +16,16 @@
<groupId>com.mineplex</groupId>
<artifactId>spigot</artifactId>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client</artifactId>
<version>1.20.0</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-gson</artifactId>
<version>1.20.0</version>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,25 @@
package mineplex.core.common.api;
import com.google.gson.Gson;
/**
* @author Shaun Bennett
*/
public class ApiEndpoint
{
private static final String API_HOST = "localhost";
private static final int API_PORT = 3000;
private ApiWebCall _webCall;
public ApiEndpoint(String path, Gson gson)
{
String url = "http://" + API_HOST + ":" + API_PORT + path;
_webCall = new ApiWebCall(url, gson);
}
protected ApiWebCall getWebCall()
{
return _webCall;
}
}

View File

@ -0,0 +1,36 @@
package mineplex.core.common.api;
import java.util.Date;
/**
* @author Shaun Bennett
*/
public class ApiResponse
{
private boolean success;
private String error;
public ApiResponse()
{
}
public boolean isSuccess()
{
return success;
}
public String getError()
{
return error;
}
@Override
public String toString()
{
return "ApiResponse{" +
"success=" + success +
", error='" + error + '\'' +
'}';
}
}

View File

@ -0,0 +1,95 @@
package mineplex.core.common.api;
import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
/**
* @author Shaun Bennett
*/
public class ApiWebCall
{
private String _url;
private Gson _gson;
private PoolingHttpClientConnectionManager _cm;
private CloseableHttpClient _httpClient;
public ApiWebCall(String url, Gson gson)
{
_url = url;
_gson = gson;
_cm = new PoolingHttpClientConnectionManager();
_cm.setMaxTotal(200);
_cm.setDefaultMaxPerRoute(20);
_httpClient = HttpClients.custom().setConnectionManager(_cm).build();
}
public <T> T get(String resource, Class<T> clazz)
{
T returnData = null;
HttpGet httpGet = new HttpGet(_url + resource);
try (CloseableHttpResponse response = _httpClient.execute(httpGet)) {
returnData = getFromResponse(response, clazz);
} catch (IOException e)
{
e.printStackTrace();
}
return returnData;
}
public <T> T post(String resource, Class<T> clazz, Object data)
{
T returnData = null;
HttpPost httpPost = new HttpPost(_url + resource);
try
{
if (data != null)
{
StringEntity params = new StringEntity(_gson.toJson(data));
params.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(params);
}
try (CloseableHttpResponse response = _httpClient.execute(httpPost))
{
returnData = getFromResponse(response, clazz);
} catch (ClientProtocolException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
return returnData;
}
private <T> T getFromResponse(CloseableHttpResponse response, Class<T> clazz) throws IOException
{
HttpEntity entity = response.getEntity();
return _gson.fromJson(new InputStreamReader(entity.getContent()), clazz);
}
}

View File

@ -0,0 +1,36 @@
package mineplex.core.common.api;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
/**
* @author Shaun Bennett
*/
public class ListResponseType<Data> implements ParameterizedType
{
private Class<Data> _wrapped;
public ListResponseType(Class<Data> wrapped)
{
_wrapped = wrapped;
}
@Override
public Type[] getActualTypeArguments()
{
return new Type[] { _wrapped };
}
@Override
public Type getRawType()
{
return List.class;
}
@Override
public Type getOwnerType()
{
return null;
}
}

View File

@ -0,0 +1,71 @@
package mineplex.core.boosters;
import java.util.Date;
import java.util.UUID;
/**
* @author Shaun Bennett
*/
public class Booster
{
private String playerName;
private UUID uuid;
private int accountId;
private int duration;
private Date startTime;
private Date endTime;
private Date activationTime;
public Booster()
{
}
public String getPlayerName()
{
return playerName;
}
public UUID getUuid()
{
return uuid;
}
public int getAccountId()
{
return accountId;
}
public int getDuration()
{
return duration;
}
public Date getStartTime()
{
return startTime;
}
public Date getEndTime()
{
return endTime;
}
public Date getActivationTime()
{
return activationTime;
}
@Override
public String toString()
{
return "Booster{" +
"playerName='" + playerName + '\'' +
", uuid=" + uuid +
", accountId=" + accountId +
", duration=" + duration +
", startTime=" + startTime +
", endTime=" + endTime +
", activationTime=" + activationTime +
'}';
}
}

View File

@ -0,0 +1,19 @@
package mineplex.core.boosters;
import mineplex.core.common.api.ApiResponse;
/**
* @author Shaun Bennett
*/
public class BoosterApiResponse extends ApiResponse
{
public String startTime;
@Override
public String toString()
{
return "BoosterApiResponse{" +
"startTime='" + startTime + '\'' +
"} " + super.toString();
}
}

View File

@ -0,0 +1,61 @@
package mineplex.core.boosters;
import mineplex.core.MiniPlugin;
import mineplex.core.common.util.Callback;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.UUID;
/**
* @author Shaun Bennett
*/
public class BoosterManager extends MiniPlugin
{
private BoosterRepository _repository;
public BoosterManager(JavaPlugin plugin)
{
super("Booster Manager", plugin);
_repository = new BoosterRepository();
}
public void getBoosters(Callback<List<Booster>> callback)
{
}
public void getBoosters(String serverGroup, Callback<List<Booster>> callback)
{
runAsync(() -> {
try
{
List<Booster> boosters = _repository.getBoosters(serverGroup);
if (callback != null) runSync(() -> callback.run(boosters));
}
catch (Exception e)
{
System.err.println("Failed to grab boosters for servergroup: " + serverGroup);
e.printStackTrace();
}
});
}
public void activateBooster(String serverGroup, Player player)
{
}
public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException
{
BoosterRepository repository = new BoosterRepository();
System.out.println("Adding Booster...");
System.out.println(repository.addBooster("Testing", "Phinary", UUID.randomUUID(), 100, 3600));
System.out.println("Getting Boosters");
System.out.println(repository.getBoosters("Testing"));
}
}

View File

@ -0,0 +1,38 @@
package mineplex.core.boosters;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import mineplex.core.common.api.ApiEndpoint;
import mineplex.core.common.api.ApiResponse;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
/**
* @author Shaun Bennett
*/
public class BoosterRepository extends ApiEndpoint
{
public BoosterRepository()
{
super("/booster", new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX").create());
}
public List<Booster> getBoosters(String serverGroup)
{
return Arrays.asList(getWebCall().get("/" + serverGroup, Booster[].class));
}
public ApiResponse addBooster(String serverGroup, String playerName, UUID uuid, int accountId, int duration)
{
JsonObject body = new JsonObject();
body.addProperty("playerName", playerName);
body.addProperty("uuid", uuid.toString());
body.addProperty("accountId", accountId);
body.addProperty("duration", duration);
return getWebCall().post("/" + serverGroup, BoosterApiResponse.class, body);
}
}

View File

@ -69,7 +69,7 @@ public class JsonWebCall
}
catch (Exception ex)
{
System.out.println("Error executing JsonWebCall: \n" + ex.getMessage());
System.out.println("Error executing ApiWebCall: \n" + ex.getMessage());
System.out.println("Result: \n" + result);
for (StackTraceElement trace : ex.getStackTrace())
@ -123,7 +123,7 @@ public class JsonWebCall
}
catch (Exception ex)
{
System.out.println("JsonWebCall.Execute() Error:\n" + ex.getMessage());
System.out.println("ApiWebCall.Execute() Error:\n" + ex.getMessage());
for (StackTraceElement trace : ex.getStackTrace())
{
@ -186,7 +186,7 @@ public class JsonWebCall
}
catch (Exception ex)
{
System.out.println("Error executing JsonWebCall: \n" + ex.getMessage());
System.out.println("Error executing ApiWebCall: \n" + ex.getMessage());
System.out.println("Result: \n" + result);
for (StackTraceElement trace : ex.getStackTrace())
@ -247,7 +247,7 @@ public class JsonWebCall
}
catch (Exception ex)
{
System.out.println("Error executing JsonWebCall: \n" + ex.getMessage());
System.out.println("Error executing ApiWebCall: \n" + ex.getMessage());
System.out.println("Result: \n" + result);
for (StackTraceElement trace : ex.getStackTrace())
@ -312,7 +312,7 @@ public class JsonWebCall
}
catch (Exception ex)
{
System.out.println("Error executing JsonWebCall: \n" + ex.getMessage());
System.out.println("Error executing ApiWebCall: \n" + ex.getMessage());
UtilSystem.printStackTrace(ex.getStackTrace());
System.out.println("Result: \n" + result);
}

View File

@ -69,7 +69,7 @@ public class JsonWebCall
}
catch (Exception ex)
{
System.out.println("Error executing JsonWebCall: \n" + ex.getMessage());
System.out.println("Error executing ApiWebCall: \n" + ex.getMessage());
System.out.println("Result: \n" + result);
for (StackTraceElement trace : ex.getStackTrace())
@ -123,7 +123,7 @@ public class JsonWebCall
}
catch (Exception ex)
{
System.out.println("JsonWebCall.Execute() Error:\n" + ex.getMessage());
System.out.println("ApiWebCall.Execute() Error:\n" + ex.getMessage());
for (StackTraceElement trace : ex.getStackTrace())
{
@ -186,7 +186,7 @@ public class JsonWebCall
}
catch (Exception ex)
{
System.out.println("Error executing JsonWebCall: \n" + ex.getMessage());
System.out.println("Error executing ApiWebCall: \n" + ex.getMessage());
System.out.println("Result: \n" + result);
for (StackTraceElement trace : ex.getStackTrace())
@ -247,7 +247,7 @@ public class JsonWebCall
}
catch (Exception ex)
{
System.out.println("Error executing JsonWebCall: \n" + ex.getMessage());
System.out.println("Error executing ApiWebCall: \n" + ex.getMessage());
System.out.println("Result: \n" + result);
for (StackTraceElement trace : ex.getStackTrace())
@ -312,7 +312,7 @@ public class JsonWebCall
}
catch (Exception ex)
{
System.out.println("Error executing JsonWebCall: \n" + ex.getMessage());
System.out.println("Error executing ApiWebCall: \n" + ex.getMessage());
System.out.println("Result: \n" + result);
}
finally

View File

@ -115,7 +115,7 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2</version>
<version>4.5.2</version>
<scope>compile</scope>
</dependency>
<dependency>