Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 111 additions & 95 deletions src/main/java/me/jay/AnonAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;

import java.io.*;
import java.net.URL;
import me.jay.exception.GetRequestException;
import me.jay.exception.InvalidTokenException;
import me.jay.exception.PostRequestException;
import org.json.JSONObject;

import java.io.*;
import java.net.URL;

/**
* @author Jay, 2018
*
Expand All @@ -22,107 +20,125 @@
* to send 'get' and 'post' requests with ease.
*/
public class AnonAPI {
/*
* API Token for account uploading or anything of the sort.
*/
private String apiToken;
/*
* API Token for account uploading or anything of the sort.
*/
private String apiToken;

/**
* A variable which is assigned to true if the programmer is using an account token.
*/
private boolean usingAccount;
/**
* A variable which is assigned to true if the programmer is using an account
* token.
*/
private boolean usingAccount;

/**
* GSON instantiation
*/
private Gson gson = new GsonBuilder().setPrettyPrinting().create();
/**
* GSON instantiation
*/
private Gson gson = new GsonBuilder().setPrettyPrinting().create();

/**
* Constructor which passes in the APIToken if you want to upload straight from your account.
* @param apiToken
/**
* Constructor which passes in the APIToken if you want to upload straight
* from your account.
* @param apiToken
*/
public AnonAPI(String apiToken) {
/*
* If token is equal to null.
*/
public AnonAPI(String apiToken) {
/*
* If token is equal to null.
*/
if (apiToken == null || apiToken == "") {
this.usingAccount = false;
throw new InvalidTokenException();
} else {
this.usingAccount = true;
this.apiToken = apiToken;
}
if (apiToken == null || apiToken == "") {
this.usingAccount = false;
throw new InvalidTokenException();
} else {
this.usingAccount = true;
this.apiToken = apiToken;
}
}

/**
* Contact's AnonAPI with a 'post' request to upload the specified file.
* @param file - File to upload
* @return - URL which can be reformed into a string.
*/
public String upload(File file) {
try {
/* Creating response */
HttpResponse<JsonNode> response = Unirest.post("https://anonfile.com/api/upload").field("file", file).asJson();
/* Instantiating body as a JSONObject */
JSONObject object = response.getBody().getObject();
/* Creating a new string combining the data, file, url, with the string short */
String url = object.getJSONObject("data")
.getJSONObject("file")
.getJSONObject("url")
.getString("short");
/* returning the body */
return new URL(url).toString();
}catch(Exception e) {
throw new PostRequestException();
}
/**
* Contact's AnonAPI with a 'post' request to upload the specified file.
* @param file - File to upload
* @return - URL which can be reformed into a string.
*/
public String upload(File file) {
try {
/* Creating response */
HttpResponse<JsonNode> response =
Unirest.post("https://api.anonfiles.com/upload")
.field("file", file)
.asJson();
/* Instantiating body as a JSONObject */
JSONObject object = response.getBody().getObject();
/* Creating a new string combining the data, file, url, with the string
* short */
String url = object.getJSONObject("data")
.getJSONObject("file")
.getJSONObject("url")
.getString("short");
/* returning the body */
return new URL(url).toString();
} catch (Exception e) {
throw new PostRequestException();
}
}

/**X
* Contact's AnonAPI with a 'get' request to return a STRING based javascript object notation based text about the specified file.
* @param id - File id
* @return - Response body
*/
public String get(String id) {
try {
/* Create response */
HttpResponse<JsonNode> response = Unirest.get("https://anonfile.com/api/v2/file/{id}/info".replace("{id}", id)).asJson();
/* Instantiate a new variable with the body as the value. */
String body = response.getBody().toString();
/* Instantiate our parser */
JsonParser parser = new JsonParser();
/* Instantiate our JsonElement which parses through the body using JsonParser */
JsonElement element = parser.parse(body);
/* Return the body but with pretty formatting as stated in the GSON variable #setPrettyPrinting() */
return gson.toJson(element);
}catch(Exception e) {
e.printStackTrace();
throw new GetRequestException();
}
/**X
* Contact's AnonAPI with a 'get' request to return a STRING based javascript
* object notation based text about the specified file.
* @param id - File id
* @return - Response body
*/
public String get(String id) {
try {
/* Create response */
HttpResponse<JsonNode> response =
Unirest
.get("https://api.anonfiles.com/v2/file/{id}/info".replace("{id}",
id))
.asJson();
/* Instantiate a new variable with the body as the value. */
String body = response.getBody().toString();
/* Instantiate our parser */
JsonParser parser = new JsonParser();
/* Instantiate our JsonElement which parses through the body using
* JsonParser */
JsonElement element = parser.parse(body);
/* Return the body but with pretty formatting as stated in the GSON
* variable #setPrettyPrinting() */
return gson.toJson(element);
} catch (Exception e) {
e.printStackTrace();
throw new GetRequestException();
}
}

/**
* Contact's AnonAPI with a 'get' request to return the javascript object notation based text but in the form of a JSONObject, may be easier for people who want to
* select out the values they want/
* @param id - File id
* @return - JSONObject
*/
public JSONObject getAsObject(String id) {
try {
/* Create response */
HttpResponse<JsonNode> response = Unirest.get("https://anonfile.com/api/v2/file/{id}/info".replace("{id}", id)).asJson();
/* Return the body as a JSONObject */
return response.getBody().getObject();
}catch(Exception e) {
throw new GetRequestException();
}
/**
* Contact's AnonAPI with a 'get' request to return the javascript object
* notation based text but in the form of a JSONObject, may be easier for
* people who want to select out the values they want/
* @param id - File id
* @return - JSONObject
*/
public JSONObject getAsObject(String id) {
try {
/* Create response */
HttpResponse<JsonNode> response =
Unirest
.get("https://api.anonfiles.com/v2/file/{id}/info".replace("{id}",
id))
.asJson();
/* Return the body as a JSONObject */
return response.getBody().getObject();
} catch (Exception e) {
throw new GetRequestException();
}
}

/**
* Simple substring to return ONLY the id of the file.
* @param link - Link
* @return - The ID
*/
public String getID(String link) {
return link.substring("https://anonfile.com/".length());
}
/**
* Simple substring to return ONLY the id of the file.
* @param link - Link
* @return - The ID
*/
public String getID(String link) {
return link.substring("https://anonfile.com/".length());
}
}