Added OSRS request logic

This commit is contained in:
Salmonllama 2020-09-08 23:39:55 -04:00
parent 9d85f8e6d6
commit eafdc7fbe6

View File

@ -5,13 +5,7 @@
package dev.salmonllama.fsbot.endpoints.scapefashion; package dev.salmonllama.fsbot.endpoints.scapefashion;
import okhttp3.OkHttpClient; import com.google.gson.Gson;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URI; import java.net.URI;
import java.net.URLEncoder; import java.net.URLEncoder;
@ -19,7 +13,6 @@ import java.net.http.HttpClient;
import java.net.http.HttpRequest; import java.net.http.HttpRequest;
import java.net.http.HttpResponse; import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;
public class ScapeFashionConnection { public class ScapeFashionConnection {
private final String RS3_REQUEST_URL = "https://api.rune.scape.fashion"; private final String RS3_REQUEST_URL = "https://api.rune.scape.fashion";
@ -28,36 +21,44 @@ public class ScapeFashionConnection {
private final String OSRS_REQUEST_URL = "https://api.scape.fashion"; private final String OSRS_REQUEST_URL = "https://api.scape.fashion";
private final String OSRS_LINK_URL = "https://scape.fashion"; private final String OSRS_LINK_URL = "https://scape.fashion";
private final String USER_AGENT = "Fashionscape-Bot github.com/salmonllama/fashionscape-bot"; public ScapeFashionConnection() {}
private final OkHttpClient client;
private final Request.Builder requestBuilder;
public ScapeFashionConnection() {
client = new OkHttpClient().newBuilder().build();
requestBuilder = new Request.Builder();
}
// Uses the color endpoint to search for items // Uses the color endpoint to search for items
// Returns an object with a list of the top results, and a link redirect to see full list // Returns an object with a list of the top results, and a link redirect to see full list
public void osrsColor(String color) throws IOException { public ScapeFashionResult osrsColor(String color) throws Exception {
String url = OSRS_REQUEST_URL + "/colors/" + encode(color); String uri = OSRS_REQUEST_URL + "/colors/" + encode(color);
System.out.println(url); String link = OSRS_LINK_URL + "/colors/" + encode(color);
makeRequestNEW(url); var response = makeRequest(uri);
response.setLink(link);
return response;
} }
private void osrsColor(String color, String slot) { private ScapeFashionResult osrsColor(String color, String slot) throws Exception {
String uri = OSRS_REQUEST_URL + "/colors/" + encode(color) + "?slot=" + encode(slot);
String link = OSRS_LINK_URL + "/colors/" + encode(color) + "?slot=" + encode(slot);
var response = makeRequest(uri);
response.setLink(link);
return response;
} }
private void osrsItem(String item) { private ScapeFashionResult osrsItem(String item) throws Exception {
String uri = OSRS_REQUEST_URL + "/items/" + encode(item);
String link = OSRS_LINK_URL + "/items/" + encode(item);
var response = makeRequest(uri);
response.setLink(link);
return response;
} }
private void osrsItem(String item, String slot) { private ScapeFashionResult osrsItem(String item, String slot) throws Exception {
String uri = OSRS_REQUEST_URL + "/items/" + encode(item) + "?slot=" + encode(slot);
String link = OSRS_LINK_URL + "/items/" + encode(item) + "?slot=" + encode(slot);
var response = makeRequest(uri);
response.setLink(link);
return response;
} }
private void rs3Color(String color) { private void rs3Color(String color) {
@ -76,35 +77,24 @@ public class ScapeFashionConnection {
} }
private JSONArray makeRequest(String url) throws IOException { private ScapeFashionResult makeRequest(String url) throws Exception {
// Returns the items JSONObject
Request request = requestBuilder.get().url(url).addHeader("User-Agent", USER_AGENT).build();
Response response = client.newCall(request).execute();
// returns a JSONArray of JSONObjects
System.out.println(response.body().string());
return new JSONObject(response.body().string()).getJSONArray("items");
}
private void makeRequestNEW(String url) {
var client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_2).build(); var client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_2).build();
String USER_AGENT = "Fashionscape-Bot github.com/salmonllama/fashionscape-bot";
HttpRequest request = HttpRequest.newBuilder(URI.create(url)) HttpRequest request = HttpRequest.newBuilder(URI.create(url))
.header("Content-Type", "application/json") .header("Content-Type", "application/json")
.header("User-Agent", USER_AGENT) .header("User-Agent", USER_AGENT)
.GET() .GET()
.build(); .build();
CompletableFuture<HttpResponse<String>> response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()); Gson gson = new Gson();
response.thenAcceptAsync(res -> System.out.println(res.body())); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
var json = response.body();
return gson.fromJson(json, ScapeFashionResult.class);
} }
private String encode(String value) throws UnsupportedEncodingException { private String encode(String value) throws UnsupportedEncodingException {
return URLEncoder.encode(value, StandardCharsets.UTF_8.toString()); return URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
} }
private ScapeFashionResult extract(JSONObject json) {
return null;
}
} }