Skip to content
Open
Show file tree
Hide file tree
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
28 changes: 25 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
<version>1.2</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<gson.version>2.10.1</gson.version>
<picocli.version>4.7.4</picocli.version>
<okhttp.version>4.10.0</okhttp.version>
<guava.version>32.1.2-jre</guava.version>
</properties>

<build>
Expand Down Expand Up @@ -109,7 +113,25 @@
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
<version>${gson.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.picocli/picocli -->
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>${picocli.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp/okhttp -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
</dependencies>

Expand Down
27 changes: 27 additions & 0 deletions src/main/java/net/socketconnection/jva/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.socketconnection.jva;

import net.socketconnection.jva.commands.*;
import picocli.CommandLine;


public class Main {
public static void main(String... args) {

/**Commands**/
CommandLine cmd = new CommandLine(new MainCommands())
.addSubcommand("PlayerRank", new PlayerCommand())
.addSubcommand("PlayerMatchHistory", new PlayerMatchHistoryCommand())
.addSubcommand("MatchById", new MatchWithIdCommand())
.addSubcommand("LeaderBoard", new LeaderBoardCommand())
.addSubcommand("RegionVersion", new RegionVersionCommand())
.addSubcommand("LatestValorantArticle", new LatestArticleCommand())
.addSubcommand("RegionUpdates", new RegionUpdatesCommand())
.addSubcommand("ValorantStoreItems", new StoreItemCommand())
.addSubcommand("ValorantStoreBundle", new StoreBundleCommand());

cmd.setExecutionStrategy(new CommandLine.RunAll());
int exitCode = cmd.execute(args);
System.exit(exitCode);

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.socketconnection.jva;
package net.socketconnection.jva.api;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
Expand All @@ -19,25 +19,29 @@
import net.socketconnection.jva.models.status.Update;
import net.socketconnection.jva.player.LeaderboardPlayer;
import net.socketconnection.jva.utils.GsonUtils;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import static java.net.HttpURLConnection.*;
import static net.socketconnection.jva.utils.RiotUtils.BASE_URL;

public class ValorantAPI {

private final URL baseUrl;
private final String apiKey;
private final OkHttpClient okHttpClient;

public ValorantAPI(String apiKey) throws IOException {
baseUrl = new URL("https://api.henrikdev.xyz/valorant");
this.apiKey = apiKey;
this.okHttpClient = new OkHttpClient();
}

public ValorantAPI() throws IOException {
Expand All @@ -47,10 +51,10 @@ public ValorantAPI() throws IOException {
public List<LeaderboardPlayer> getLeaderboard(Region region, String riotId) throws IOException {
JsonArray leaderboardData;

if(riotId == null) {
if (riotId == null) {
leaderboardData = sendRestRequest("/v1/leaderboard/" + region.getQuery()).getAsJsonArray();
} else {
if(!riotId.contains("#") || riotId.split("#").length < 2) {
if (!riotId.contains("#") || riotId.split("#").length < 2) {
throw new InvalidRiotIdentificationException("Unknown format (right format: NAME#TAG)");
}

Expand Down Expand Up @@ -81,15 +85,15 @@ public ServerStatus getServerStatus(Region region) throws IOException {
List<StatusEntry> maintenances = new LinkedList<>();
List<StatusEntry> incidents = new LinkedList<>();

for(JsonElement maintenanceElement : maintenancesData) {
for (JsonElement maintenanceElement : maintenancesData) {
JsonObject maintenanceObject = maintenanceElement.getAsJsonObject();
List<Update> updates = new LinkedList<>();

for(JsonElement updateElement : maintenanceObject.getAsJsonArray("updates")) {
for (JsonElement updateElement : maintenanceObject.getAsJsonArray("updates")) {
JsonObject updateObject = updateElement.getAsJsonObject();
Map<Language, String> translations = new LinkedHashMap<>();

for(JsonElement translationElement : updateObject.getAsJsonArray("translations")) {
for (JsonElement translationElement : updateObject.getAsJsonArray("translations")) {
JsonObject translationObject = translationElement.getAsJsonObject();

translations.put(Language.getFromLocale(GsonUtils.getAsString(translationObject.get("locale"))),
Expand All @@ -98,7 +102,7 @@ public ServerStatus getServerStatus(Region region) throws IOException {

List<String> publishLocations = new LinkedList<>();

for(JsonElement publishElement : updateObject.getAsJsonArray("publish_locations")) {
for (JsonElement publishElement : updateObject.getAsJsonArray("publish_locations")) {
publishLocations.add(GsonUtils.getAsString(publishElement));
}

Expand All @@ -109,13 +113,13 @@ public ServerStatus getServerStatus(Region region) throws IOException {

List<String> platforms = new LinkedList<>();

for(JsonElement platformElement : maintenanceObject.getAsJsonArray("platforms")) {
for (JsonElement platformElement : maintenanceObject.getAsJsonArray("platforms")) {
platforms.add(GsonUtils.getAsString(platformElement));
}

Map<Language, String> titles = new LinkedHashMap<>();

for(JsonElement titleElement : maintenanceObject.getAsJsonArray("titles")) {
for (JsonElement titleElement : maintenanceObject.getAsJsonArray("titles")) {
JsonObject titleObject = titleElement.getAsJsonObject();

titles.put(Language.getFromLocale(GsonUtils.getAsString(titleObject.get("locale"))), GsonUtils.getAsString(titleObject.get("content")));
Expand All @@ -127,23 +131,23 @@ public ServerStatus getServerStatus(Region region) throws IOException {
GsonUtils.getAsString(maintenanceObject.get("incident_severity"))));
}

for(JsonElement incidentElement : incidentsData) {
for (JsonElement incidentElement : incidentsData) {
JsonObject incidentObject = incidentElement.getAsJsonObject();
List<Update> updates = new LinkedList<>();

for(JsonElement updateElement : incidentObject.getAsJsonArray("updates")) {
for (JsonElement updateElement : incidentObject.getAsJsonArray("updates")) {
JsonObject updateObject = updateElement.getAsJsonObject();
Map<Language, String> translations = new LinkedHashMap<>();

for(JsonElement translationElement : updateObject.getAsJsonArray("translations")) {
for (JsonElement translationElement : updateObject.getAsJsonArray("translations")) {
JsonObject translationObject = translationElement.getAsJsonObject();

translations.put(Language.getFromLocale(GsonUtils.getAsString(translationObject.get("locale"))), GsonUtils.getAsString(translationObject.get("content")));
}

List<String> publishLocations = new LinkedList<>();

for(JsonElement publishElement : updateObject.getAsJsonArray("publish_locations")) {
for (JsonElement publishElement : updateObject.getAsJsonArray("publish_locations")) {
publishLocations.add(GsonUtils.getAsString(publishElement));
}

Expand All @@ -154,13 +158,13 @@ public ServerStatus getServerStatus(Region region) throws IOException {

List<String> platforms = new LinkedList<>();

for(JsonElement platformElement : incidentObject.getAsJsonArray("platforms")) {
for (JsonElement platformElement : incidentObject.getAsJsonArray("platforms")) {
platforms.add(GsonUtils.getAsString(platformElement));
}

Map<Language, String> titles = new LinkedHashMap<>();

for(JsonElement titleElement : incidentObject.getAsJsonArray("titles")) {
for (JsonElement titleElement : incidentObject.getAsJsonArray("titles")) {
JsonObject titleObject = titleElement.getAsJsonObject();

titles.put(Language.getFromLocale(GsonUtils.getAsString(titleObject.get("locale"))), GsonUtils.getAsString(titleObject.get("content")));
Expand All @@ -187,7 +191,7 @@ public List<WebsiteArticle> getWebsiteArticles(Language language) throws IOExcep

List<WebsiteArticle> websiteArticles = new LinkedList<>();

for(JsonElement articleElement : articleData) {
for (JsonElement articleElement : articleData) {
JsonObject articleObject = articleElement.getAsJsonObject();

websiteArticles.add(new WebsiteArticle(GsonUtils.getAsString(articleObject.get("banner_url")), WebsiteArticle.Category.getFromQuery(GsonUtils.getAsString(articleObject.get("category"))),
Expand All @@ -203,13 +207,13 @@ public List<Bundle> getStoreBundles() throws IOException {

List<Bundle> bundles = new LinkedList<>();

for(JsonElement bundleElement : bundlesData) {
for (JsonElement bundleElement : bundlesData) {
JsonObject bundleObject = bundleElement.getAsJsonObject();
JsonArray itemData = bundleObject.getAsJsonArray("items");

List<BundleItem> items = new LinkedList<>();

for(JsonElement itemElement : itemData) {
for (JsonElement itemElement : itemData) {
JsonObject itemObject = itemElement.getAsJsonObject();

items.add(new BundleItem(GsonUtils.getAsString(itemObject.get("uuid")), GsonUtils.getAsString(itemObject.get("name")),
Expand All @@ -232,12 +236,12 @@ public List<OfferItem> getStoreOffers() throws IOException {

List<OfferItem> items = new LinkedList<>();

for(JsonElement offerElement : offersData) {
for (JsonElement offerElement : offersData) {
JsonObject offerObject = offerElement.getAsJsonObject();

ContentTier contentTier = null;

if(!offerObject.get("content_tier").isJsonNull()) {
if (!offerObject.get("content_tier").isJsonNull()) {
JsonObject contentTierObject = offerObject.getAsJsonObject("content_tier");

contentTier = new ContentTier(GsonUtils.getAsString(contentTierObject.get("name")),
Expand All @@ -253,36 +257,30 @@ public List<OfferItem> getStoreOffers() throws IOException {
}

public JsonElement sendRestRequest(String uriPath) throws IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(baseUrl + uriPath).openConnection();

if(apiKey != null) {
connection.setRequestProperty("Authorization", apiKey);
}

connection.setRequestProperty("User-Agent", "Java Valorant API (JVA)");
connection.setDoInput(true);
var request = constructURLBuilder(uriPath, apiKey != null);
var call = okHttpClient.newCall(request);
var response = call.execute();

switch (connection.getResponseCode()) {
case 200:
switch (response.code()) {
case HTTP_OK:
break;
case 403:
throw new InvalidAuthenticationException(connection.getResponseMessage());
case 404:
throw new IncorrectDataException(connection.getResponseMessage());
case HTTP_BAD_REQUEST:
throw new InvalidRequestException(response.message());
case HTTP_FORBIDDEN:
throw new InvalidAuthenticationException(response.message());
case HTTP_NOT_FOUND:
throw new IncorrectDataException(response.message());
case 429:
throw new RateLimitedException(connection.getResponseMessage());
throw new RateLimitedException(response.message());
default:
throw new FetchException("Rest API returned unknown error code: " + connection.getResponseMessage());
throw new FetchException("Rest API returned unknown error code: " + response.message());
}

StringBuilder builder = new StringBuilder();
var builder = new StringBuilder();

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String msg;
var reader = new BufferedReader(new InputStreamReader(response.body().byteStream()));

while ((msg = reader.readLine()) != null) {
builder.append(msg).append("\n");
}
reader.lines().forEach(builder::append);

return new Gson().fromJson(builder.toString(), JsonElement.class);
}
Expand All @@ -291,7 +289,23 @@ public String getApiKey() {
return apiKey;
}

public URL getBaseUrl() {
return baseUrl;
public Request constructURLBuilder(String uriPath, boolean hasApiKey) {
var url = BASE_URL + "valorant" + uriPath;
var urlBuilder = HttpUrl.parse(url).newBuilder();
var targetURL = urlBuilder.build().toString();
if (hasApiKey) {
return new Request.Builder()
.url(targetURL)
.addHeader("Authorization", apiKey)
.addHeader("User-Agent", "Java Valorant API (JVA)")
.build();
}

Request request = new Request.Builder()
.url(targetURL)
.addHeader("User-Agent", "Java Valorant API (JVA)")
.build();

return request;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.socketconnection.jva.commands;


import net.socketconnection.jva.api.ValorantAPI;
import net.socketconnection.jva.enums.Language;
import net.socketconnection.jva.models.WebsiteArticle;
import picocli.CommandLine;

import java.util.List;
import java.util.concurrent.Callable;

@CommandLine.Command(name = "LatestArticle", mixinStandardHelpOptions = true, version = "1.0",
description = "Get the last articles from the official VALORANT page.")
public class LatestArticleCommand implements Callable<Integer> {

@CommandLine.Option(names = {"-lg", "--language"}, description = "The language of the article", defaultValue = "en-en")
private String language;
@CommandLine.Option(names = {"-ak", "--apikey"}, description = "The api key")
private String apiKey;

@Override
public Integer call() throws Exception {

// Initialize the main instance (API key is NOT required)
ValorantAPI valorantAPI = new ValorantAPI(apiKey);
// Get the last articles from the valorant page in a specific language using the Language enu
List<WebsiteArticle> websiteArticles = valorantAPI.getWebsiteArticles(Language.getFromName(language));
WebsiteArticle websiteArticle = websiteArticles.get(0);

// Print out title and url of the last article
System.out.println(websiteArticle.getTitle());
System.out.println(websiteArticle.getUrl());
return 0;
}
}
Loading