Skip to content

Commit

Permalink
[feature] 🆕 Adicionado Suporte a versão V1 da API
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristian-Sknz committed Jan 12, 2021
1 parent 17e667e commit bd1ea72
Show file tree
Hide file tree
Showing 36 changed files with 1,230 additions and 292 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
plugins {
id 'com.github.johnrengelman.shadow' version '6.1.0'
id 'java-library'
}

Expand All @@ -19,5 +20,4 @@ dependencies {
implementation group: 'com.google.code.findbugs', name: 'findbugs', version: '1.3.9'
implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '3.9.0'
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.6'

}
110 changes: 16 additions & 94 deletions src/main/java/me/skiincraft/api/osu/OsuAPI.java
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();
}
}
24 changes: 12 additions & 12 deletions src/main/java/me/skiincraft/api/osu/entity/beatmap/Beatmap.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,42 @@

public interface Beatmap extends BeatmapCompact {

float getAccuracy();
float getAccuracy(); //

float getAR();
float getAR(); //

float getBPM();

int getCircles(); //
int getCircles();

int getSliders(); //
int getSliders();

int getSpinners(); //
int getSpinners();

float getDrain();

int getHitLength(); //
int getHitLength();

long getBeatmapSetId(); //
long getBeatmapSetId();

long getPassCount();

long getPlayCount();

boolean isConverted();
boolean isConverted(); //

boolean isScoreable(); //
boolean isScoreable();

float getCS();
float getCS(); //

@Nullable
OffsetDateTime wasDeletedAt(); //

OffsetDateTime getLastUpdated(); //
OffsetDateTime getLastUpdated();

Approval getStatus();

FailTimes getFailTimes();
FailTimes getFailTimes(); //

BeatmapSetCompact getBeatmapSet();

Expand Down
39 changes: 24 additions & 15 deletions src/main/java/me/skiincraft/api/osu/entity/beatmap/BeatmapSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,57 @@
public interface BeatmapSet extends BeatmapSetCompact {

/* <?> getHype();*/
boolean canBeHyped(); //
@Nullable
Boolean canBeHyped(); //

boolean hasDownloadDisabled(); //
boolean hasDownloadDisabled();

boolean hasDiscussionEnabled(); //
@Nullable
Boolean hasDiscussionEnabled(); //

boolean hasDiscussionLocked(); //
@Nullable
Boolean hasDiscussionLocked(); //

boolean hasStoryboard();

boolean isScoreable();//
boolean isScoreable();

OffsetDateTime getLastUpdated(); //
OffsetDateTime getLastUpdated();

@Nullable
String getLegacyThreadUrl(); //

@Nullable
OffsetDateTime getRankedDate(); //
OffsetDateTime getRankedDate();

@Nullable
OffsetDateTime getSubmittedDate(); //
OffsetDateTime getSubmittedDate();

String getTags();

List<Beatmap> getBeatmaps();

List<Beatmap> getConverts();
@Nullable
List<Beatmap> getConverts(); //

String getDescription();
@Nullable
String getDescription(); //

Genre getGenre(); //
Genre getGenre();

Language getLanguage();

List<UserCompact> getRecentFavourites();
@Nullable
List<UserCompact> getRecentFavourites(); //

UserCompact getUser();
@Nullable
UserCompact getUser(); //

int getNominationsCurrent(); //
@Nullable
Integer getNominationsCurrent(); //

int getNominationsRequired(); //
@Nullable
Integer getNominationsRequired(); //


}
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ public interface BeatmapSetCompact {

String getTitle();

String getTitleUnicode(); //
String getTitleUnicode();

String getArtist();

String getArtistUnicode(); //
String getArtistUnicode();

String getCreator();

long getUserId(); //
long getUserId();

long getBeatmapSetId(); //
long getBeatmapSetId();

long getPlayCount(); //

long getFavourites(); //
long getFavourites();

float getBPM();

Expand All @@ -34,9 +34,9 @@ public interface BeatmapSetCompact {
@Nullable
String getSource();

Approval getStatus(); //
Approval getStatus();

long[] getRatings();
long[] getRatings(); //

default String getPreviewURL() {
return String.format("http://b.ppy.sh/preview/%s.mp3", getBeatmapSetId());
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/me/skiincraft/api/osu/entity/score/Score.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,26 @@

import javax.annotation.Nullable;
import java.time.OffsetDateTime;
import java.util.Objects;

public interface Score {

default long getBeatmapSetId() {
return getBeatmapSet().getBeatmapSetId();
default Long getBeatmapSetId() {
return Objects.requireNonNull(getBeatmapSet()).getBeatmapSetId();
}

default long getBeatmapId() {
return getBeatmap().getBeatmapId();
return Objects.requireNonNull(getBeatmap()).getBeatmapId();
}

@Nullable
default String getUsername() {
return getUser().getUsername();
}

long getScoreId(); //
long getBestId(); //
@Nullable
Long getBestId(); //
long getUserId(); //
float getAccuracy();
Mods[] getMods();
Expand All @@ -35,7 +39,9 @@ default String getUsername() {
String getRank();
OffsetDateTime getCreatedDate(); //
boolean hasReplay();
@Nullable
Beatmap getBeatmap();
@Nullable
BeatmapSetCompact getBeatmapSet();
UserCompact getUser();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package me.skiincraft.api.osu.entity.score;

import me.skiincraft.api.osu.impl.score.ScoreImpl;
import me.skiincraft.api.osu.impl.v2.score.ScoreImpl;

public class UserScore {

Expand Down
19 changes: 19 additions & 0 deletions src/main/java/me/skiincraft/api/osu/entity/user/SimpleUser.java
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");
}

}
Loading

0 comments on commit bd1ea72

Please sign in to comment.