Skip to content

Commit

Permalink
Add javadocs and more accurate api locale support
Browse files Browse the repository at this point in the history
  • Loading branch information
zvyap committed May 9, 2023
1 parent aedcfe3 commit d5c09ab
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 10 deletions.
50 changes: 50 additions & 0 deletions src/main/java/com/zvyap/hoyoapi/APILocale.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.zvyap.hoyoapi;

import java.util.List;

//Followed https://bbs-api-os-static.hoyolab.com/community/misc/wapi/langs?lang2022=true
public enum APILocale {
ZH_CN("简体中文","zh-cn","简","zh-cn","CHS"),
ZH_TW("繁體中文","zh-tw","繁","zh-tw","CHT"),
DE("Deutsch","de-de","DE","de-de","DE"),
EN_US("English","en-us","EN","en-us","EN"),
ES("Español","es-es","ES","es-es","ES"),
FR("Français","fr-fr","FR","fr-fr","FR"),
ID("Indonesia","id-id","ID","id-id","ID"),
IT("Italiano","it-it","IT","it-it","IT"),
JP("日本語","ja-jp","JP","ja-jp","JP","JA"),
KR("한국어","ko-kr","KR","ko-kr","KR","KO"),
PT("Português","pt-pt","PT","pt-pt","PT"),
RU("Pусский","ru-ru","RU","ru-ru","RU"),
TH("ภาษาไทย","th-th","TH","th-th","TH"),
TR("Türkçe","tr-tr","TR","tr-tr","TR"),
VI_VN("Tiếng Việt","vi-vn","VN","vi-vn","VN","VI");

private final String name;
private final String value;
private final String label;
private final List<String> alias;

APILocale(String name, String value, String label, String... alias) {
this.name = name;
this.value = value;
this.label = label;
this.alias = List.of(alias);
}

public String getName() {
return name;
}

public String getValue() {
return value;
}

public String getLabel() {
return label;
}

public List<String> getAlias() {
return alias;
}
}
88 changes: 82 additions & 6 deletions src/main/java/com/zvyap/hoyoapi/HoyoverseAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.CompletableFuture;
Expand All @@ -47,7 +46,7 @@ public static HoyoverseAPI getGlobalInstance() {
}

@NotNull
public static HoyoverseAPI buildGlobalInstance(APIEnvironment environment, Locale locale) {
public static HoyoverseAPI buildGlobalInstance(APIEnvironment environment, APILocale locale) {
if (globalInstance == null) {
globalInstance = new HoyoverseAPI(environment, locale);
}
Expand All @@ -63,21 +62,29 @@ public static HoyoverseAPI buildGlobalInstance(APIEnvironment environment, Local
@Getter
private boolean isHeaderSafe;

/**
* @param environment APIEnvironment
*/
public HoyoverseAPI(APIEnvironment environment) {
this(environment, Locale.US);
this(environment, APILocale.EN_US);
}

public HoyoverseAPI(APIEnvironment environment, Locale locale) {

/**
* @param environment APIEnvironment
* @param locale APILocale
*/
public HoyoverseAPI(APIEnvironment environment, APILocale locale) {
tryToAllowRestricedHeader();
this.httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.cookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_NONE))
.build();
this.environment = environment;
if (locale == null) {
locale = Locale.ENGLISH;
locale = APILocale.EN_US;
}
this.lang = Locale.US.toString().replace("_", "-").toLowerCase();
this.lang = locale.getValue();

}

Expand Down Expand Up @@ -120,16 +127,37 @@ private void tryToAllowRestricedHeader() {
}
}

/**
* Get game roles with some filter
*
* @param token HoyoToken
* @return A list of all game roles in this account
*/
@NotNull
public List<HoyoGameRole> getGameRoles(@NotNull HoyoToken token) {
return getGameRoles(token, (GameType) null);
}

/**
* Get game roles with some filter
*
* @param token HoyoToken
* @param type GameType
* @return A list of game roles of the game type with all region
*/
@NotNull
public List<HoyoGameRole> getGameRoles(@NotNull HoyoToken token, @Nullable GameType type) {
return Utils.ifNullGetEmptyList(Utils.getUserGameRoles(this, token, type, null).body().get().getData(), HoyoGetUserGameRolesResponse.Data::getRoles);
}

/**
* Get game role by UID with GameType filter
*
* @param token HoyoToken
* @param type GameType
* @param uid In game UID
* @return Game role information
*/
@Nullable
public HoyoGameRole getGameRoles(@NotNull HoyoToken token, @Nullable GameType type, String uid) {
for (HoyoGameRole role : getGameRoles(token, type)) {
Expand All @@ -140,11 +168,26 @@ public HoyoGameRole getGameRoles(@NotNull HoyoToken token, @Nullable GameType ty
return null;
}

/**
* Get game role by UID
*
* @param token HoyoToken
* @param uid In game UID
* @return Game role information
*/
@Nullable
public HoyoGameRole getGameRoles(@NotNull HoyoToken token, @Nullable String uid) { //uid null will return null
return getGameRoles(token, null, uid);
}

/**
* Get game role with filter
*
* @param token HoyoToken
* @param type GameType
* @param region ServerRegion
* @return Game role information
*/
@Nullable
public HoyoGameRole getGameRoles(@NotNull HoyoToken token, @Nullable GameType type, @Nullable ServerRegion region) {
for (HoyoGameRole role : getGameRoles(token, type)) {
Expand All @@ -155,6 +198,13 @@ public HoyoGameRole getGameRoles(@NotNull HoyoToken token, @Nullable GameType ty
return null;
}

/**
* Get game role with filter
*
* @param token HoyoToken
* @param region ServerRegion
* @return
*/
@NotNull
public List<HoyoGameRole> getGameRoles(@NotNull HoyoToken token, @Nullable ServerRegion region) {
List<HoyoGameRole> roles = new ArrayList<>();
Expand All @@ -166,16 +216,42 @@ public List<HoyoGameRole> getGameRoles(@NotNull HoyoToken token, @Nullable Serve
return roles;
}

/**
* Get user forum profile via account Id
*
* WARN: This request is fat, be careful
*
* @param accountId You can get this in url when you visit a user profile in forum, same as {@link HoyoToken#getLtuid()}
* @return
*/
@NotNull
public HoyoGetForumFullUserResponse getForumUser(@NotNull String accountId) {
return Utils.getForumUser(this, accountId).body().get();
}

/**
* Get user forum profile via token object
*
* WARN: This request is fat, be careful
*
* @param token HoyoToken, see also {@link this#getForumUser(String)}
* @return
*/
@NotNull
public HoyoGetForumFullUserResponse getForumUser(@NotNull HoyoToken token) {
return Utils.getForumUser(this, token.getLtuid()).body().get();
}

/**
* Get account information of this token
* Note: Some sensitive data such as: [username, email] will be blur
*
* For example: abc*****@gmail.com, ab****g
*
* @param token
* @return
*/
@NotNull
public HoyoGetUserAccountInfoResponse getAccountInfo(HoyoToken token) {
return makeRequest(token,
buildRequest(URI.create(getEnvironment().getAccountAPIConstant().getUserAccountInfoEndpoint()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,35 @@ private <T> T fetchDailyEndpoint(HoyoToken token, HttpMethod method, GameType ty
return body;
}

/**
* Get daily information of the account, contains day checked in, day missing etc
* Check out - {@link HoyoDailyCheckInInfoResponse}
*
* @param type GameType
* @param token HoyoToken
* @return The information of daily status
*/
public HoyoDailyCheckInInfoResponse getDailyInfo(@NotNull GameType type, @NotNull HoyoToken token) {
return fetchDailyEndpoint(token, HttpMethod.GET, type, "info", HoyoDailyCheckInInfoResponse.class);
}

/**
* Check in daily
*
* @param type GameType
* @param token HoyoToken
* @return The response of official API
*/
public HoyoDailyCheckInSignResponse signDaily(@NotNull GameType type, @NotNull HoyoToken token) {
return fetchDailyEndpoint(token, HttpMethod.POST, type, "sign", HoyoDailyCheckInSignResponse.class);
}

/**
* Get all reward of each day
*
* @param type GameType
* @return A response object contains all the reward
*/
public HoyoDailyCheckInRewardResponse getAllReward(@NotNull GameType type) {
return fetchDailyEndpoint(null, HttpMethod.GET, type, "home", HoyoDailyCheckInRewardResponse.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
public class GenshinDailyCheckInTest {

private HoyoverseAPI osAPI = new HoyoverseAPI(APIEnvironment.OVERSEA);
private HoyoverseAPI osCHINA = new HoyoverseAPI(APIEnvironment.CHINA, Locale.CHINA);
private HoyoToken token = HoyoToken.of(TestConstant.GENSHIN_TOKEN_ID, TestConstant.GENSHIN_TOKEN);

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
@EnabledIf("com.zvyap.hoyoapi.test.TestConstant#isHonkaiEnabled")
public class HonkaiImpactDailyCheckInTest {
private HoyoverseAPI osAPI = new HoyoverseAPI(APIEnvironment.OVERSEA);
private HoyoverseAPI osCHINA = new HoyoverseAPI(APIEnvironment.CHINA, Locale.CHINA);
private HoyoToken token = HoyoToken.of(TestConstant.HONKAI_TOKEN_ID, TestConstant.HONKAI_TOKEN); //04

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
public class HonkaiStarRailDailyCheckInTest {

private HoyoverseAPI osAPI = new HoyoverseAPI(APIEnvironment.OVERSEA);
private HoyoverseAPI osCHINA = new HoyoverseAPI(APIEnvironment.CHINA, Locale.CHINA);
private HoyoToken token = HoyoToken.of(TestConstant.HSR_TOKEN_ID, TestConstant.HSR_TOKEN);

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
public class TearsOfThemisDailyCheckInTest {

private HoyoverseAPI osAPI = new HoyoverseAPI(APIEnvironment.OVERSEA);
private HoyoverseAPI osCHINA = new HoyoverseAPI(APIEnvironment.CHINA, Locale.CHINA);
private HoyoToken token = HoyoToken.of(TestConstant.TOT_TOKEN_ID, TestConstant.TOT_TOKEN); //ys

@Test
Expand Down

0 comments on commit d5c09ab

Please sign in to comment.