Added support for Mojang API calls in ApiClient

Added Mojang API call to retreive Player profile
This commit is contained in:
CrushedPixel
2015-05-20 16:33:40 +02:00
parent 2f057ce0b1
commit 80fbcddb87
32 changed files with 346 additions and 94 deletions

View File

@@ -0,0 +1,159 @@
package eu.crushedpixel.replaymod.api.replay;
import com.google.gson.Gson;
import eu.crushedpixel.replaymod.ReplayMod;
import eu.crushedpixel.replaymod.api.ApiException;
import eu.crushedpixel.replaymod.api.replay.holders.ApiError;
import eu.crushedpixel.replaymod.api.replay.holders.Category;
import eu.crushedpixel.replaymod.gui.online.GuiUploadFile;
import net.minecraft.client.resources.I18n;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
public class FileUploader {
private static final Gson gson = new Gson();
private boolean uploading = false;
private long filesize;
private long current;
private String attachmentName = "file";
private String attachmentFileName = "file.mcpr";
private String crlf = "\r\n";
private String twoHyphens = "--";
private boolean cancel = false;
private String boundary = "*****";
private GuiUploadFile parent;
public void uploadFile(GuiUploadFile gui, String auth, String filename, List<String> tags, File file, Category category) throws IOException, ApiException, RuntimeException {
boolean success = false;
String info = null;
try {
parent = gui;
gui.onStartUploading();
filesize = 0;
if(uploading) throw new RuntimeException("FileUploader is already uploading");
uploading = true;
String postData = "?auth=" + auth + "&category=" + category.getId();
if(tags.size() > 0) {
postData += "&tags=";
for(String tag : tags) {
postData += tag;
if(!tag.equals(tags.get(tags.size() - 1))) {
postData += ",";
}
}
}
postData += "&name=" + URLEncoder.encode(filename, "UTF-8");
System.out.println(postData);
String url = "http://ReplayMod.com/api/upload_file" + postData;
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setUseCaches(false);
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setChunkedStreamingMode(1024);
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Cache-Control", "no-cache");
con.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + this.boundary);
HashMap<String, String> params = new HashMap<String, String>();
params.put("auth", auth);
params.put("name", filename);
params.put("category", category.getId() + "");
DataOutputStream request = new DataOutputStream(con.getOutputStream());
request.writeBytes(this.twoHyphens + this.boundary + this.crlf);
request.writeBytes("Content-Disposition: form-data; name=\"" + this.attachmentName + "\";filename=\"" + this.attachmentFileName + "\"" + this.crlf);
request.writeBytes(this.crlf);
byte[] buf = new byte[1024];
FileInputStream fis = new FileInputStream(file);
filesize = fis.getChannel().size();
current = 0;
int len;
while((len = fis.read(buf)) != -1) {
request.write(buf);
current += len;
if(cancel) {
uploading = false;
current = 0;
cancel = false;
parent.onFinishUploading(false, I18n.format("replaymod.gui.upload.canceled"));
fis.close();
return;
}
}
fis.close();
request.writeBytes(this.crlf);
request.writeBytes(this.twoHyphens + this.boundary + this.twoHyphens + this.crlf);
request.flush();
request.close();
success = false;
int responseCode = con.getResponseCode();
InputStream is;
if(responseCode == 200) {
success = true;
is = con.getInputStream();
} else {
is = con.getErrorStream();
}
if(is != null) {
BufferedReader r = new BufferedReader(new InputStreamReader(is));
info = null;
if(responseCode != 200) {
String json = "";
while(r.ready()) {
json += r.readLine();
}
ApiError error = gson.fromJson(json, ApiError.class);
info = error.getTranslatedDesc();
System.out.println(info);
}
}
con.disconnect();
if(info == null) info = I18n.format("replaymod.gui.unknownerror");
ReplayMod.uploadedFileHandler.markAsUploaded(file);
success = true;
} catch(Exception e) {
success = false;
} finally {
parent.onFinishUploading(success, info);
uploading = false;
}
}
public float getUploadProgress() {
if(!uploading || filesize == 0) return 0;
return (float) ((double) current / (double) filesize);
}
public boolean isUploading() {
return uploading;
}
public void cancelUploading() {
cancel = true;
}
}

View File

@@ -0,0 +1,22 @@
package eu.crushedpixel.replaymod.api.replay;
public class ReplayModApiMethods {
public static final String REPLAYMOD_BASE_URL = "http://ReplayMod.com/api/";
public static final String register = REPLAYMOD_BASE_URL+"register";
public static final String check_authkey = REPLAYMOD_BASE_URL+"check_authkey";
public static final String login = REPLAYMOD_BASE_URL+"login";
public static final String logout = REPLAYMOD_BASE_URL+"logout";
public static final String file_details = REPLAYMOD_BASE_URL+"file_details";
public static final String upload_file = REPLAYMOD_BASE_URL+"upload_file";
public static final String download_file = REPLAYMOD_BASE_URL+"download_file";
public static final String get_thumbnail = REPLAYMOD_BASE_URL+"get_thumbnail";
public static final String remove_file = REPLAYMOD_BASE_URL+"remove_file";
public static final String rate_file = REPLAYMOD_BASE_URL+"rate_file";
public static final String fav_file = REPLAYMOD_BASE_URL+"fav_file";
public static final String get_favorites = REPLAYMOD_BASE_URL+"get_favorites";
public static final String check_auth = REPLAYMOD_BASE_URL+"check_auth";
public static final String get_language = REPLAYMOD_BASE_URL+"get_language";
public static final String search = REPLAYMOD_BASE_URL+"search";
}

View File

@@ -0,0 +1,56 @@
package eu.crushedpixel.replaymod.api.replay;
import java.lang.reflect.Field;
import java.net.URLEncoder;
public class SearchQuery {
public Boolean order, singleplayer;
public String player, tag, version, server, name, auth;
public Integer category, offset;
public SearchQuery() {
}
public SearchQuery(Boolean order, Boolean singleplayer, String player,
String tag, String version, String server, String name,
String auth, Integer category, Integer offset) {
this.order = order;
this.singleplayer = singleplayer;
this.player = player;
this.tag = tag;
this.version = version;
this.server = server;
this.name = name;
this.auth = auth;
this.category = category;
this.offset = offset;
}
public String buildQuery() {
String query = "";
boolean first = true;
//Please don't slaughter me for this code,
//even if I deserve it, which I certainly do.
for(Field f : this.getClass().getDeclaredFields()) {
try {
Object value = f.get(this);
if(value == null) continue;
query += first ? "?" : "&";
first = false;
query += f.getName() + "=";
query += URLEncoder.encode(String.valueOf(value), "UTF-8");
} catch(Exception e) {
e.printStackTrace();
}
}
return query;
}
@Override
public String toString() {
return buildQuery();
}
}

View File

@@ -0,0 +1,36 @@
package eu.crushedpixel.replaymod.api.replay.holders;
import net.minecraft.client.resources.I18n;
public class ApiError {
private int id;
private String desc;
private String key;
private String[] objects;
public ApiError(int id, String desc) {
this.id = id;
this.desc = desc;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getTranslatedDesc() {
return I18n.format(key, (Object[])objects);
}
}

View File

@@ -0,0 +1,10 @@
package eu.crushedpixel.replaymod.api.replay.holders;
public class AuthConfirmation {
private String username;
private int id;
public String getUsername() {
return username;
}
}

View File

@@ -0,0 +1,14 @@
package eu.crushedpixel.replaymod.api.replay.holders;
public class AuthKey {
private String auth;
public AuthKey(String authkey) {
this.auth = authkey;
}
public String getAuthkey() {
return auth;
}
}

View File

@@ -0,0 +1,44 @@
package eu.crushedpixel.replaymod.api.replay.holders;
import net.minecraft.client.resources.I18n;
public enum Category {
SURVIVAL(0, "replaymod.category.survival"), MINIGAME(1, "replaymod.category.minigame"),
BUILD(2, "replaymod.category.build"), MISCELLANEOUS(3, "replaymod.category.misc");
private int id;
private String name;
Category(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return this.id;
}
public Category fromId(int id) {
for(Category c : values()) {
if(c.id == id) return c;
}
return null;
}
public String toNiceString() {
return I18n.format(this.name);
}
public Category next() {
for(int i = 0; i < values().length; i++) {
if(values()[i] == this) {
if(i == values().length - 1) {
i = -1;
}
return values()[i + 1];
}
}
return this;
}
}

View File

@@ -0,0 +1,10 @@
package eu.crushedpixel.replaymod.api.replay.holders;
public class Donated {
private boolean donated = false;
public boolean hasDonated() {
return donated;
}
}

View File

@@ -0,0 +1,7 @@
package eu.crushedpixel.replaymod.api.replay.holders;
public class Favorites {
private int[] favorited;
public int[] getFavorited() { return favorited; }
}

View File

@@ -0,0 +1,77 @@
package eu.crushedpixel.replaymod.api.replay.holders;
import eu.crushedpixel.replaymod.recording.ReplayMetaData;
public class FileInfo {
private int id;
private ReplayMetaData metadata;
private String owner;
private Rating ratings;
private int size;
private int category;
private int downloads;
private int favorites;
private String name;
private boolean thumbnail;
public FileInfo(int id, ReplayMetaData metadata, String owner,
Rating ratings, int size, int category, int downloads, String name,
boolean thumbnail, int favorites) {
this.id = id;
this.metadata = metadata;
this.owner = owner;
this.ratings = ratings;
this.size = size;
this.category = category;
this.downloads = downloads;
this.name = name;
this.thumbnail = thumbnail;
this.favorites = favorites;
}
public int getId() {
return id;
}
public ReplayMetaData getMetadata() {
return metadata;
}
public String getOwner() {
return owner;
}
public Rating getRatings() {
return ratings;
}
public int getSize() {
return size;
}
public int getCategory() {
return category;
}
public int getDownloads() {
return downloads;
}
public int getFavorites() { return favorites; }
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean hasThumbnail() {
return thumbnail;
}
}

View File

@@ -0,0 +1,14 @@
package eu.crushedpixel.replaymod.api.replay.holders;
public class Rating {
private int negative, positive;
public int getNegative() {
return negative;
}
public int getPositive() {
return positive;
}
}

View File

@@ -0,0 +1,10 @@
package eu.crushedpixel.replaymod.api.replay.holders;
public class SearchResult {
private FileInfo[] results;
public FileInfo[] getResults() {
return results;
}
}

View File

@@ -0,0 +1,10 @@
package eu.crushedpixel.replaymod.api.replay.holders;
public class Success {
private boolean success = false;
public boolean isSuccess() {
return success;
}
}

View File

@@ -0,0 +1,22 @@
package eu.crushedpixel.replaymod.api.replay.holders;
public class UserFiles {
private String user;
private FileInfo[] files;
private int total_size;
public String getUser() {
return user;
}
public FileInfo[] getFiles() {
return files;
}
public int getTotal_size() {
return total_size;
}
}

View File

@@ -0,0 +1,63 @@
package eu.crushedpixel.replaymod.api.replay.pagination;
import eu.crushedpixel.replaymod.ReplayMod;
import eu.crushedpixel.replaymod.api.replay.holders.FileInfo;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class DownloadedFilePagination implements Pagination {
private int page;
private HashMap<Integer, FileInfo> files = new HashMap<Integer, FileInfo>();
public DownloadedFilePagination() {
this.page = -1;
}
@Override
public List<FileInfo> getFiles() {
return new ArrayList<FileInfo>(files.values());
}
@Override
public int getLoadedPages() {
return page;
}
@Override
public boolean fetchPage() {
page++;
HashMap<Integer, File> f = ReplayMod.downloadedFileHandler.getDownloadedFiles();
List<Integer> toAdd = new ArrayList<Integer>();
for(int i : f.keySet()) {
if(!files.containsKey(i)) {
toAdd.add(i);
if(toAdd.size() >= Pagination.PAGE_SIZE) break;
}
}
files.keySet().retainAll(f.keySet());
try {
FileInfo[] fis = ReplayMod.apiClient.getFileInfo(toAdd);
if(fis.length < 1) {
page--;
return false;
}
for(FileInfo info : fis) {
files.put(info.getId(), info);
}
return true;
} catch(Exception e) {
e.printStackTrace();
}
page--;
return false;
}
}

View File

@@ -0,0 +1,60 @@
package eu.crushedpixel.replaymod.api.replay.pagination;
import eu.crushedpixel.replaymod.ReplayMod;
import eu.crushedpixel.replaymod.api.replay.holders.FileInfo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class FavoritedFilePagination implements Pagination {
private int page;
private HashMap<Integer, FileInfo> files = new HashMap<Integer, FileInfo>();
public FavoritedFilePagination() {
this.page = -1;
}
@Override
public List<FileInfo> getFiles() {
return new ArrayList<FileInfo>(files.values());
}
@Override
public int getLoadedPages() {
return page;
}
@Override
public boolean fetchPage() {
page++;
try {
List<Integer> f = ReplayMod.favoritedFileHandler.getFavorited();
List<Integer> toAdd = new ArrayList<Integer>();
for(int i : f) {
if(!files.containsKey(i)) {
toAdd.add(i);
if(toAdd.size() >= Pagination.PAGE_SIZE) break;
}
}
FileInfo[] fis = ReplayMod.apiClient.getFileInfo(toAdd);
if(fis.length < 1) {
page--;
return false;
}
for(FileInfo info : fis) {
files.put(info.getId(), info);
}
return true;
} catch(Exception e) {
e.printStackTrace();
}
page--;
return false;
}
}

View File

@@ -0,0 +1,15 @@
package eu.crushedpixel.replaymod.api.replay.pagination;
import eu.crushedpixel.replaymod.api.replay.holders.FileInfo;
import java.util.List;
public interface Pagination {
public List<FileInfo> getFiles();
public int getLoadedPages();
public boolean fetchPage();
public static final int PAGE_SIZE = 30; //defined by the Website API
}

View File

@@ -0,0 +1,53 @@
package eu.crushedpixel.replaymod.api.replay.pagination;
import eu.crushedpixel.replaymod.ReplayMod;
import eu.crushedpixel.replaymod.api.replay.SearchQuery;
import eu.crushedpixel.replaymod.api.replay.holders.FileInfo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SearchPagination implements Pagination {
private final SearchQuery searchQuery;
private int page;
private List<FileInfo> files = new ArrayList<FileInfo>();
public SearchPagination(SearchQuery searchQuery) {
this.page = -1;
this.searchQuery = searchQuery;
}
@Override
public List<FileInfo> getFiles() {
return new ArrayList<FileInfo>(files);
}
@Override
public int getLoadedPages() {
return page;
}
@Override
public boolean fetchPage() {
page++;
searchQuery.offset = page;
try {
FileInfo[] fis = ReplayMod.apiClient.searchFiles(searchQuery);
if(fis.length < 1) {
page--;
return false;
}
files.addAll(Arrays.asList(fis));
return true;
} catch(Exception e) {
e.printStackTrace();
}
page--;
return false;
}
}