Skip to content

Commit 72f221e

Browse files
renovate[bot]yannicklamprechtrainbowdashlabs
authored
Update dependency de.chojo:cjda-util to v2.10.3+jda-5.1.0 (#628)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Yannick Lamprecht <[email protected]> Co-authored-by: Lilly <[email protected]>
1 parent cb22bc4 commit 72f221e

13 files changed

+586
-385
lines changed

build.gradle.kts

+7-1
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,16 @@ spotless {
2525

2626
dependencies {
2727
//discord
28-
implementation("de.chojo", "cjda-util", "2.9.8+jda-5.0.0") {
28+
implementation("de.chojo", "cjda-util", "2.10.3+jda-5.1.0") {
2929
exclude(group = "club.minnced", module = "opus-java")
3030
}
3131

32+
val openapi = "6.3.0"
33+
34+
annotationProcessor("io.javalin.community.openapi:openapi-annotation-processor:$openapi")
35+
implementation("io.javalin.community.openapi:javalin-openapi-plugin:$openapi") // for /openapi route with JSON scheme
36+
implementation("io.javalin.community.openapi:javalin-swagger-plugin:$openapi") // for Swagger UI
37+
3238
// database
3339
implementation("org.postgresql", "postgresql", "42.7.5")
3440
implementation(libs.bundles.sadu)

settings.gradle.kts

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ dependencyResolutionManagement {
2525
// plugins
2626
plugin("spotless", "com.diffplug.spotless").version("7.0.2")
2727
plugin("shadow", "com.gradleup.shadow").version("8.3.5")
28-
2928
}
3029
}
3130
}

src/main/java/de/chojo/repbot/core/Web.java

+39-14
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
import de.chojo.jdautil.botlist.BotlistService;
99
import de.chojo.repbot.config.Configuration;
1010
import de.chojo.repbot.web.Api;
11+
import de.chojo.repbot.web.error.ApiException;
1112
import io.javalin.Javalin;
12-
import io.javalin.plugin.openapi.OpenApiOptions;
13-
import io.javalin.plugin.openapi.OpenApiPlugin;
14-
import io.javalin.plugin.openapi.ui.ReDocOptions;
15-
import io.javalin.plugin.openapi.ui.SwaggerOptions;
16-
import io.javalin.plugin.openapi.utils.OpenApiVersionUtil;
17-
import io.swagger.v3.oas.models.info.License;
13+
import io.javalin.openapi.OpenApiLicense;
14+
import io.javalin.openapi.plugin.OpenApiPlugin;
15+
import io.javalin.openapi.plugin.OpenApiPluginConfiguration;
16+
import io.javalin.openapi.plugin.swagger.SwaggerConfiguration;
17+
import io.javalin.openapi.plugin.swagger.SwaggerPlugin;
1818
import net.dv8tion.jda.api.entities.User;
1919
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
2020
import net.dv8tion.jda.api.requests.ErrorResponse;
@@ -51,19 +51,44 @@ public void init() {
5151
private void initApi() {
5252
var api = configuration.api();
5353

54-
var info = new io.swagger.v3.oas.models.info.Info().version("1.0").title("Reputation Bot API")
55-
.description("Documentation for the Reputation Bot API")
56-
.license(new License().name("GNU Affero General Public License v3.0")
57-
.url("https://github.com/RainbowDashLabs/reputation-bot/blob/master/LICENSE.md"));
54+
/*
5855
var options = new OpenApiOptions(info)
5956
.path("/json-docs")
6057
.reDoc(new ReDocOptions("/redoc")) // endpoint for redoc
6158
.swagger(new SwaggerOptions("/docs").title("Reputation Bot API"));
62-
OpenApiVersionUtil.INSTANCE.setLogWarnings(false);
59+
OpenApiVersionUtil.INSTANCE.setLogWarnings(false);*/
6360

64-
javalin = Javalin.create(config -> config.registerPlugin(new OpenApiPlugin(options)))
65-
.start(api.host(), api.port());
66-
new Api(javalin, data.metrics()).init();
61+
javalin = Javalin.create(config -> {
62+
config.registerPlugin(new OpenApiPlugin(this::configureOpenApi));
63+
config.registerPlugin(new SwaggerPlugin(this::configureSwagger));
64+
config.router.apiBuilder(() -> new Api(data.metrics()).init());
65+
})
66+
.start(api.host(), api.port());
67+
javalin.exception(ApiException.class, (err, ctx) -> ctx.result(err.getMessage()).status(err.status()));
68+
}
69+
70+
private void configureSwagger(SwaggerConfiguration swaggerConfiguration) {
71+
swaggerConfiguration.setDocumentationPath("/docs");
72+
swaggerConfiguration.setUiPath("/swagger-ui");
73+
}
74+
75+
private void configureOpenApi(OpenApiPluginConfiguration config) {
76+
config.withDocumentationPath("/docs")
77+
.withDefinitionConfiguration((version, definition) -> {
78+
definition.withInfo(info -> {
79+
info.setTitle("Reputation Bot API");
80+
info.setVersion("1.0");
81+
info.setDescription("Documentation for the Reputation Bot API");
82+
info.setLicense(new OpenApiLicense()
83+
.name("GNU Affero General Public License v3.0")
84+
.url("https://github.com/RainbowDashLabs/reputation-bot/blob/master/LICENSE.md")
85+
);
86+
});
87+
definition.withServer(openApiServer -> {
88+
openApiServer.setUrl("https://repbot.rainbowdashlabs.de");
89+
openApiServer.setDescription("Main server");
90+
});
91+
});
6792
}
6893

6994
private void initBotList() {

src/main/java/de/chojo/repbot/web/Api.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
package de.chojo.repbot.web;
77

88
import de.chojo.repbot.dao.provider.Metrics;
9-
import de.chojo.repbot.web.erros.ApiException;
9+
import de.chojo.repbot.web.error.ApiException;
1010
import de.chojo.repbot.web.routes.v1.MetricsRoute;
1111
import io.javalin.Javalin;
12+
import io.javalin.apibuilder.ApiBuilder;
13+
import io.javalin.apibuilder.EndpointGroup;
14+
import io.javalin.router.Endpoint;
1215
import org.slf4j.Logger;
1316

1417
import static io.javalin.apibuilder.ApiBuilder.before;
@@ -17,20 +20,14 @@
1720

1821
public class Api {
1922
private static final Logger log = getLogger(Api.class);
20-
private final Javalin javalin;
2123
private final MetricsRoute metricsRoute;
2224

23-
public Api(Javalin javalin, Metrics metrics) {
24-
this.javalin = javalin;
25+
public Api(Metrics metrics) {
2526
metricsRoute = new MetricsRoute(metrics);
2627
}
2728

2829
public void init() {
29-
javalin.exception(ApiException.class, (err, ctx) -> ctx.result(err.getMessage()).status(err.status()));
30-
javalin.routes(() -> {
31-
before(ctx -> log.debug("Received request on {}.", ctx.path()));
32-
33-
path("v1", () -> path("metrics", metricsRoute::buildRoutes));
34-
});
30+
before(ctx -> log.debug("Received request on {}.", ctx.path()));
31+
path("v1", () -> path("metrics", metricsRoute::buildRoutes));
3532
}
3633
}

src/main/java/de/chojo/repbot/web/error/ApiException.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33
*
44
* Copyright (C) RainbowDashLabs and Contributor
55
*/
6-
package de.chojo.repbot.web.erros;
6+
package de.chojo.repbot.web.error;
77

8-
import io.javalin.http.HttpCode;
8+
9+
import io.javalin.http.HttpStatus;
910

1011
public class ApiException extends RuntimeException {
11-
private final HttpCode status;
12+
private final HttpStatus status;
1213

13-
public ApiException(HttpCode status, String message) {
14+
public ApiException(HttpStatus status, String message) {
1415
super(message);
1516
this.status = status;
1617
}
1718

18-
public HttpCode status() {
19+
public HttpStatus status() {
1920
return status;
2021
}
2122
}

src/main/java/de/chojo/repbot/web/routes/v1/MetricsHolder.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
package de.chojo.repbot.web.routes.v1;
77

88
import de.chojo.repbot.dao.provider.Metrics;
9-
import de.chojo.repbot.web.erros.ApiException;
9+
import de.chojo.repbot.web.error.ApiException;
1010
import de.chojo.repbot.web.routes.RoutesBuilder;
1111
import de.chojo.repbot.web.routes.v1.metrics.MetricCache;
1212
import io.javalin.http.Context;
1313
import io.javalin.http.Handler;
14-
import io.javalin.http.HttpCode;
15-
import org.eclipse.jetty.http.HttpStatus;
14+
import io.javalin.http.HttpStatus;
1615
import org.slf4j.Logger;
1716

1817
import static org.slf4j.LoggerFactory.getLogger;
@@ -36,7 +35,7 @@ protected void writeImage(Context ctx, byte[] png) {
3635
ctx.header("X-Content-Type-Options", "nosniff");
3736
ctx.contentType("image/png");
3837

39-
ctx.result(png).status(HttpStatus.OK_200);
38+
ctx.result(png).status(HttpStatus.OK);
4039
}
4140

4241
protected int offset(Context context, int max) {
@@ -46,7 +45,7 @@ protected int offset(Context context, int max) {
4645
assertSize(offset, 0, max);
4746
return offset;
4847
} catch (NumberFormatException e) {
49-
throw new ApiException(HttpCode.BAD_REQUEST, "Offset is not a number, Got: " + param);
48+
throw new ApiException(HttpStatus.BAD_REQUEST, "Offset is not a number, Got: " + param);
5049
}
5150
}
5251

@@ -57,16 +56,16 @@ protected int count(Context context, int max) {
5756
assertSize(offset, 2, max);
5857
return offset;
5958
} catch (NumberFormatException e) {
60-
throw new ApiException(HttpCode.BAD_REQUEST, "Count is not a number, Got: " + param);
59+
throw new ApiException(HttpStatus.BAD_REQUEST, "Count is not a number, Got: " + param);
6160
}
6261
}
6362

6463
private void assertSize(int value, int min, int max) {
6564
if (value < min) {
66-
throw new ApiException(HttpCode.BAD_REQUEST, String.format("Value %s is too small. Min: %s", value, min));
65+
throw new ApiException(HttpStatus.BAD_REQUEST, String.format("Value %s is too small. Min: %s", value, min));
6766
}
6867
if (value > max) {
69-
throw new ApiException(HttpCode.BAD_REQUEST, String.format("Value %s is too large. Max: %s", value, max));
68+
throw new ApiException(HttpStatus.BAD_REQUEST, String.format("Value %s is too large. Max: %s", value, max));
7069
}
7170
}
7271

src/main/java/de/chojo/repbot/web/routes/v1/MetricsRoute.java

-49
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import de.chojo.repbot.web.routes.v1.metrics.Reputation;
1313
import de.chojo.repbot.web.routes.v1.metrics.Service;
1414
import de.chojo.repbot.web.routes.v1.metrics.Users;
15-
import io.swagger.v3.oas.models.parameters.Parameter;
1615

1716
public class MetricsRoute implements RoutesBuilder {
1817

@@ -52,52 +51,4 @@ public void buildRoutes() {
5251
users.buildRoutes();
5352
service.buildRoutes();
5453
}
55-
56-
private static void offsetDoc(Parameter parameter, String resolution, int maxValue) {
57-
setParameter(parameter, "%s offset. 0 is current %s. Max value is %s".formatted(resolution, resolution.toLowerCase(), maxValue));
58-
}
59-
60-
public static void offsetDayDoc(Parameter p) {
61-
offsetDoc(p, "Day", MAX_DAY_OFFSET);
62-
}
63-
64-
public static void offsetHourDoc(Parameter p) {
65-
offsetDoc(p, "Hour", MAX_HOUR_OFFSET);
66-
}
67-
68-
public static void offsetWeekDoc(Parameter p) {
69-
offsetDoc(p, "Week", MAX_WEEK_OFFSET);
70-
}
71-
72-
public static void offsetMonthDoc(Parameter p) {
73-
offsetDoc(p, "Month", MAX_MONTH_OFFSET);
74-
}
75-
76-
public static void offsetYearDoc(Parameter p) {
77-
offsetDoc(p, "Year", MAX_YEAR_OFFSET);
78-
}
79-
80-
private static void countDoc(Parameter parameter, String resolution, int maxValue) {
81-
setParameter(parameter, "%s count. Amount of previously %s in the chart. Max value is %s".formatted(resolution, resolution.toLowerCase(), maxValue));
82-
}
83-
84-
public static void countHourDoc(Parameter p) {
85-
countDoc(p, "Hours", MAX_HOURS);
86-
}
87-
88-
public static void countDayDoc(Parameter p) {
89-
countDoc(p, "Days", MAX_DAYS);
90-
}
91-
92-
public static void countWeekDoc(Parameter p) {
93-
countDoc(p, "Weeks", MAX_WEEKS);
94-
}
95-
96-
public static void countMonthDoc(Parameter p) {
97-
countDoc(p, "Months", MAX_MONTH);
98-
}
99-
100-
private static void setParameter(Parameter p, String description) {
101-
p.setDescription(description);
102-
}
10354
}

0 commit comments

Comments
 (0)