Skip to content

Commit

Permalink
add new getBeatmap function
Browse files Browse the repository at this point in the history
  • Loading branch information
kane50613 committed Jun 12, 2021
1 parent c860ddb commit d821d62
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>tw.kane</groupId>
<artifactId>osu4j</artifactId>
<version>v2.2</version>
<version>v2.3</version>

<distributionManagement>
<repository>
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/tw/kane/osu4j/API.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tw.kane.osu4j;

import com.sun.org.apache.bcel.internal.generic.PUSH;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

import static tw.kane.osu4j.OsuClient.httpClient;
import static tw.kane.osu4j.OsuClient.oauth;

public class API {

public static String ENDPOINT = "https://osu.ppy.sh/api/v2";

public static String makeRequest(String url, Object ...args) throws IOException {
Request request = new Request.Builder()
.url(String.format(ENDPOINT + url, args))
.headers(oauth.header())
.build();
Response response = httpClient.newCall(request).execute();
return response.body().string();
}
}
44 changes: 20 additions & 24 deletions src/main/java/tw/kane/osu4j/OsuClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONObject;
import tw.kane.osu4j.Base.Beatmap;
import tw.kane.osu4j.Base.Score;
import tw.kane.osu4j.Base.User;
import tw.kane.osu4j.Exception.InvalidTokenException;
Expand Down Expand Up @@ -38,16 +39,8 @@ public User getUser(String id, Mode mode, boolean allowCached) throws IOExceptio
if(allowCached && Cache.userCache.containsKey(id))
return Cache.userCache.get(id);

Request request = new Request.Builder()
.url(String.format("https://osu.ppy.sh/api/v2/users/%s/%s",
id,
mode.getName()
))
.headers(oauth.header())
.build();
Response response = httpClient.newCall(request).execute();
String responseString = response.body().string();
JSONObject result = new JSONObject(responseString);
String response = API.makeRequest("/users/%s/%s", id, mode);
JSONObject result = new JSONObject(response);
if(!result.isNull("authentication"))
throw new InvalidTokenException("token wrong");
if(!result.isNull("error"))
Expand All @@ -68,24 +61,17 @@ public User getUser(String id, Mode mode, boolean allowCached) throws IOExceptio
* @throws NotFoundException if user not found
*/
public Score[] getUserScores(String id, ScoreType type, boolean includeFails, Mode mode) throws IOException, InvalidTokenException, NotFoundException {
Request request = new Request.Builder()
.url(String.format("https://osu.ppy.sh/api/v2/users/%s/scores/%s?include_fails=%c&mode=%s",
id,
type.getName(),
includeFails ? 1 : 0,
mode.getName()
))
.headers(oauth.header())
.build();
Response response = httpClient.newCall(request).execute();
String responseString = response.body().string();
if(responseString.startsWith("{")) {
JSONObject result = new JSONObject(responseString);
String response = API.makeRequest("/users/%s/scores/%s?include_fails=%c&mode=%s", id,
type.getName(),
includeFails ? 1 : 0,
mode.getName());
if(response.startsWith("{")) {
JSONObject result = new JSONObject(response);
if(!result.isNull("authentication"))
throw new InvalidTokenException("token wrong");
}
else {
JSONArray resultArray = new JSONArray(responseString);
JSONArray resultArray = new JSONArray(response);
if(resultArray.length() == 0)
throw new NotFoundException();
List<Score> scores = new ArrayList<>();
Expand All @@ -96,4 +82,14 @@ public Score[] getUserScores(String id, ScoreType type, boolean includeFails, Mo
}
return null;
}

public Beatmap getBeatmap(String id) throws IOException, InvalidTokenException, NotFoundException {
String response = API.makeRequest("/beatmaps/%s", id);
JSONObject result = new JSONObject(response);
if(!result.isNull("authentication"))
throw new InvalidTokenException("token wrong");
if(!result.isNull("error"))
throw new NotFoundException();
return new Beatmap(result);
}
}

0 comments on commit d821d62

Please sign in to comment.