-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] 🆕 Adicionado Suporte a versão V1 da API
- Loading branch information
1 parent
17e667e
commit bd1ea72
Showing
36 changed files
with
1,230 additions
and
292 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,31 @@ | ||
package me.skiincraft.api.osu; | ||
|
||
import com.google.gson.Gson; | ||
import me.skiincraft.api.osu.exceptions.TokenException; | ||
import me.skiincraft.api.osu.oauth.OAuthApplication; | ||
import me.skiincraft.api.osu.impl.v1.OsuAPIV1; | ||
import me.skiincraft.api.osu.impl.v2.OsuAPIV2; | ||
import me.skiincraft.api.osu.object.OAuthApplication; | ||
import me.skiincraft.api.osu.requests.APIRequest; | ||
import me.skiincraft.api.osu.requests.impl.EndpointImpl; | ||
import me.skiincraft.api.osu.requests.Token; | ||
import me.skiincraft.api.osu.requests.impl.DefaultAPIRequest; | ||
import me.skiincraft.api.osu.requests.impl.FakeAPIRequest; | ||
import me.skiincraft.api.osu.requests.impl.TokenResponseParser; | ||
import me.skiincraft.api.osu.util.ReflectionUtil; | ||
import okhttp3.*; | ||
import okhttp3.OkHttpClient; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.function.Function; | ||
|
||
public class OsuAPI { | ||
public interface OsuAPI { | ||
|
||
private static final String URL_V2 = "https://osu.ppy.sh/api/v2/"; | ||
private static OkHttpClient client = new OkHttpClient(); | ||
private final OAuthApplication authApplication; | ||
private final List<Token> tokens = new ArrayList<>(); | ||
|
||
public OsuAPI(OAuthApplication osuAuthApplication) { | ||
this.authApplication = osuAuthApplication; | ||
OsuAPI.client = (client == null) ? new OkHttpClient() : client; | ||
} | ||
|
||
public APIRequest<Token> createToken(@Nonnull String code) throws TokenException { | ||
if (code.length() < 600) | ||
throw new TokenException("Your code is not valid, as it has fewer characters than normal."); | ||
|
||
Request request = new Request.Builder() | ||
.url("https://osu.ppy.sh/oauth/token") | ||
.method("POST", makeAuthUrl(code)) | ||
.addHeader("Accept", "application/json") | ||
.addHeader("Content-Type", "application/json") | ||
.build(); | ||
|
||
DefaultAPIRequest<Token> token = new DefaultAPIRequest<>(request, this); | ||
Function<Response, Token> function = response -> { | ||
try { | ||
Token item = new Gson().fromJson(Objects.requireNonNull(response.body()).string(), Token.class); | ||
ReflectionUtil.setField(item, "api", this); | ||
ReflectionUtil.setField(item, "endpoint", new EndpointImpl(item)); | ||
|
||
tokens.add(item); | ||
return item; | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
}; | ||
token.setFunction(function); | ||
token.setResponseParser((r) -> new TokenResponseParser<>(r, null)); | ||
return token; | ||
} | ||
|
||
public APIRequest<Boolean> checkToken(@Nonnull String bearerToken) throws TokenException { | ||
if (bearerToken.length() < 800) | ||
throw new TokenException("Your token is not valid as it has fewer characters than normal."); | ||
|
||
Request request = new Request.Builder() | ||
.url(URL_V2 + "me/osu") | ||
.addHeader("Accept", "application/json") | ||
.addHeader("Content-Type", "application/json") | ||
.addHeader("Authorization", "Bearer " + bearerToken).build(); | ||
|
||
return new DefaultAPIRequest<>(request, (response) -> true, this); | ||
} | ||
|
||
public APIRequest<Token> resumeToken(@Nonnull String bearerToken) throws TokenException { | ||
if (bearerToken.length() < 800) | ||
throw new TokenException("Your token is not valid as it has fewer characters than normal."); | ||
|
||
if (!checkToken(bearerToken).get()) { | ||
return null; | ||
} | ||
Token exists = tokens.stream().filter(token -> token.getToken().equalsIgnoreCase(bearerToken)).findFirst().orElse(null); | ||
if (exists == null) { | ||
tokens.add(exists = new Token(this, bearerToken)); | ||
} | ||
|
||
return new FakeAPIRequest<>(exists, 200); | ||
APIRequest<Token> createToken(@Nonnull String token); | ||
default APIRequest<Boolean> checkToken(@Nonnull String token) { | ||
throw new UnsupportedOperationException("This method is not compatible with this version of the API"); | ||
} | ||
|
||
public OAuthApplication getAuthApplication() { | ||
return authApplication; | ||
default APIRequest<Token> resumeToken(@Nonnull String token){ | ||
return createToken(token); | ||
} | ||
List<Token> getTokens(); | ||
|
||
public OkHttpClient getClient() { | ||
return client; | ||
OkHttpClient getClient(); | ||
static OsuAPI newAPIV2(OAuthApplication oAuthApplication){ | ||
return new OsuAPIV2(oAuthApplication); | ||
} | ||
|
||
private RequestBody makeAuthUrl(String code) { | ||
return new MultipartBody.Builder().setType(MultipartBody.FORM) | ||
.addFormDataPart("grant_type", "authorization_code") | ||
.addFormDataPart("client_id", String.valueOf(getAuthApplication().getClientId())) | ||
.addFormDataPart("client_secret", getAuthApplication().getClientSecret()) | ||
.addFormDataPart("redirect_uri", getAuthApplication().getRedirectUri()) | ||
.addFormDataPart("code", code) | ||
.build(); | ||
static OsuAPI newAPIV1() { | ||
return new OsuAPIV1(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/main/java/me/skiincraft/api/osu/entity/score/UserScore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/me/skiincraft/api/osu/entity/user/SimpleUser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package me.skiincraft.api.osu.entity.user; | ||
|
||
import me.skiincraft.api.osu.object.user.UserStatistics; | ||
|
||
import java.time.OffsetDateTime; | ||
|
||
public interface SimpleUser extends UserCompact { | ||
|
||
OffsetDateTime getJoinDate(); | ||
UserStatistics getStatistics(); | ||
boolean isCompleteUser(); | ||
default User getUser() { | ||
if (isCompleteUser()){ | ||
return (User) this; | ||
} | ||
throw new UnsupportedOperationException("This method is not compatible with this version of the API"); | ||
} | ||
|
||
} |
Oops, something went wrong.