diff --git a/build.gradle b/build.gradle index f2d92f0..d5a701b 100644 --- a/build.gradle +++ b/build.gradle @@ -1,11 +1,20 @@ plugins { - id 'java' id 'java-library' + id 'maven-publish' } -group = 'net.luckperms.rest' +group = 'net.luckperms' version = '0.1-SNAPSHOT' + +java { + toolchain { + languageVersion = JavaLanguageVersion.of(8) + } + withJavadocJar() + withSourcesJar() +} + repositories { mavenCentral() } @@ -32,3 +41,12 @@ dependencies { testImplementation 'org.slf4j:slf4j-simple:1.7.36' testImplementation 'com.google.guava:guava:32.1.3-jre' } + +publishing { + publications { + mavenJava(MavenPublication) { + from components.java + } + } +} + diff --git a/src/main/java/net/luckperms/rest/LuckPermsClient.java b/src/main/java/net/luckperms/rest/LuckPermsRestClient.java similarity index 90% rename from src/main/java/net/luckperms/rest/LuckPermsClient.java rename to src/main/java/net/luckperms/rest/LuckPermsRestClient.java index c4b4b27..8db4c65 100644 --- a/src/main/java/net/luckperms/rest/LuckPermsClient.java +++ b/src/main/java/net/luckperms/rest/LuckPermsRestClient.java @@ -36,7 +36,7 @@ * * @see rest-api GitHub repo */ -public interface LuckPermsClient { +public interface LuckPermsRestClient extends AutoCloseable { /** * Creates a new client builder. @@ -44,7 +44,7 @@ public interface LuckPermsClient { * @return the new builder */ static Builder builder() { - return new LuckPermsClientImpl.BuilderImpl(); + return new LuckPermsRestClientImpl.BuilderImpl(); } /** @@ -83,7 +83,13 @@ static Builder builder() { MiscService misc(); /** - * A builder for {@link LuckPermsClient} + * Close the underlying resources used by the client. + */ + @Override + void close(); + + /** + * A builder for {@link LuckPermsRestClient} */ interface Builder { @@ -108,6 +114,6 @@ interface Builder { * * @return a client */ - LuckPermsClient build(); + LuckPermsRestClient build(); } } diff --git a/src/main/java/net/luckperms/rest/LuckPermsClientImpl.java b/src/main/java/net/luckperms/rest/LuckPermsRestClientImpl.java similarity index 88% rename from src/main/java/net/luckperms/rest/LuckPermsClientImpl.java rename to src/main/java/net/luckperms/rest/LuckPermsRestClientImpl.java index e624580..9047f9d 100644 --- a/src/main/java/net/luckperms/rest/LuckPermsClientImpl.java +++ b/src/main/java/net/luckperms/rest/LuckPermsRestClientImpl.java @@ -40,22 +40,26 @@ import java.io.IOException; import java.util.Objects; -class LuckPermsClientImpl implements LuckPermsClient { +class LuckPermsRestClientImpl implements LuckPermsRestClient { + private final OkHttpClient httpClient; + private final UserService userService; private final GroupService groupService; private final TrackService trackService; private final ActionService actionService; private final MiscService miscService; - LuckPermsClientImpl(String baseUrl, String apiKey) { + LuckPermsRestClientImpl(String baseUrl, String apiKey) { OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); if (apiKey != null && !apiKey.isEmpty()) { clientBuilder.addInterceptor(new AuthInterceptor(apiKey)); } + this.httpClient = clientBuilder.build(); + Retrofit retrofit = new Retrofit.Builder() - .client(clientBuilder.build()) + .client(this.httpClient) .baseUrl(baseUrl) .addConverterFactory(GsonConverterFactory.create()) .build(); @@ -91,6 +95,12 @@ public MiscService misc() { return this.miscService; } + @Override + public void close() { + this.httpClient.dispatcher().executorService().shutdown(); + this.httpClient.connectionPool().evictAll(); + } + static final class BuilderImpl implements Builder { private String baseUrl = null; private String apiKey = null; @@ -112,9 +122,9 @@ public Builder apiKey(String apiKey) { } @Override - public LuckPermsClient build() { + public LuckPermsRestClient build() { Objects.requireNonNull(this.baseUrl, "baseUrl must be configured!"); - return new LuckPermsClientImpl(this.baseUrl, this.apiKey); + return new LuckPermsRestClientImpl(this.baseUrl, this.apiKey); } } diff --git a/src/main/java/net/luckperms/rest/model/NodeType.java b/src/main/java/net/luckperms/rest/model/NodeType.java new file mode 100644 index 0000000..9501244 --- /dev/null +++ b/src/main/java/net/luckperms/rest/model/NodeType.java @@ -0,0 +1,34 @@ +package net.luckperms.rest.model; + +import com.google.gson.annotations.SerializedName; + +import java.util.Locale; + +public enum NodeType { + + @SerializedName("regex_permission") + REGEX_PERMISSION, + + @SerializedName("inheritance") + INHERITANCE, + + @SerializedName("prefix") + PREFIX, + + @SerializedName("suffix") + SUFFIX, + + @SerializedName("meta") + META, + + @SerializedName("weight") + WEIGHT, + + @SerializedName("display_name") + DISPLAY_NAME; + + @Override + public String toString() { + return this.name().toLowerCase(Locale.ROOT); + } +} diff --git a/src/main/java/net/luckperms/rest/service/GroupService.java b/src/main/java/net/luckperms/rest/service/GroupService.java index a61c001..0a7eace 100644 --- a/src/main/java/net/luckperms/rest/service/GroupService.java +++ b/src/main/java/net/luckperms/rest/service/GroupService.java @@ -30,6 +30,7 @@ import net.luckperms.rest.model.GroupSearchResult; import net.luckperms.rest.model.Metadata; import net.luckperms.rest.model.Node; +import net.luckperms.rest.model.NodeType; import net.luckperms.rest.model.PermissionCheckRequest; import net.luckperms.rest.model.PermissionCheckResult; import net.luckperms.rest.model.TemporaryNodeMergeStrategy; @@ -65,7 +66,7 @@ public interface GroupService { Call> searchNodesByMetaKey(@Query("metaKey") String metaKey); @GET("/group/search") - Call> searchNodesByType(@Query("type") String type); + Call> searchNodesByType(@Query("type") NodeType type); @GET("/group/{name}") Call get(@Path("name") String name); diff --git a/src/main/java/net/luckperms/rest/service/UserService.java b/src/main/java/net/luckperms/rest/service/UserService.java index a891c78..e6a6153 100644 --- a/src/main/java/net/luckperms/rest/service/UserService.java +++ b/src/main/java/net/luckperms/rest/service/UserService.java @@ -29,6 +29,7 @@ import net.luckperms.rest.model.DemotionResult; import net.luckperms.rest.model.Metadata; import net.luckperms.rest.model.Node; +import net.luckperms.rest.model.NodeType; import net.luckperms.rest.model.PermissionCheckRequest; import net.luckperms.rest.model.PermissionCheckResult; import net.luckperms.rest.model.PlayerSaveResult; @@ -78,7 +79,7 @@ public interface UserService { Call> searchNodesByMetaKey(@Query("metaKey") String metaKey); @GET("/user/search") - Call> searchNodesByType(@Query("type") String type); + Call> searchNodesByType(@Query("type") NodeType type); @GET("/user/{uniqueId}") Call get(@Path("uniqueId") UUID uniqueId); diff --git a/src/test/java/me/lucko/luckperms/rest/AbstractIntegrationTest.java b/src/test/java/me/lucko/luckperms/rest/AbstractIntegrationTest.java index beeb015..e8d5564 100644 --- a/src/test/java/me/lucko/luckperms/rest/AbstractIntegrationTest.java +++ b/src/test/java/me/lucko/luckperms/rest/AbstractIntegrationTest.java @@ -25,7 +25,7 @@ package me.lucko.luckperms.rest; -import net.luckperms.rest.LuckPermsClient; +import net.luckperms.rest.LuckPermsRestClient; import org.slf4j.LoggerFactory; import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.output.Slf4jLogConsumer; @@ -51,14 +51,14 @@ public class AbstractIntegrationTest { .withStrategy(Wait.forLogMessage(".*Successfully enabled.*", 1)) ); - protected LuckPermsClient createClient() { + protected LuckPermsRestClient createClient() { assertTrue(container.isRunning()); String host = container.getHost(); Integer port = container.getFirstMappedPort(); String baseUrl = "http://" + host + ":" + port + "/"; - return LuckPermsClient.builder().baseUrl(baseUrl).build(); + return LuckPermsRestClient.builder().baseUrl(baseUrl).build(); } protected static String randomName() { diff --git a/src/test/java/me/lucko/luckperms/rest/ActionServiceTest.java b/src/test/java/me/lucko/luckperms/rest/ActionServiceTest.java index a78543c..2002655 100644 --- a/src/test/java/me/lucko/luckperms/rest/ActionServiceTest.java +++ b/src/test/java/me/lucko/luckperms/rest/ActionServiceTest.java @@ -25,7 +25,7 @@ package me.lucko.luckperms.rest; -import net.luckperms.rest.LuckPermsClient; +import net.luckperms.rest.LuckPermsRestClient; import net.luckperms.rest.model.Action; import org.junit.jupiter.api.Test; import retrofit2.Response; @@ -39,7 +39,7 @@ public class ActionServiceTest extends AbstractIntegrationTest { @Test public void testSubmit() throws IOException { - LuckPermsClient client = createClient(); + LuckPermsRestClient client = createClient(); Response resp = client.actions().submit(new Action( System.currentTimeMillis() / 1000L, diff --git a/src/test/java/me/lucko/luckperms/rest/ClientAuthenticationTest.java b/src/test/java/me/lucko/luckperms/rest/ClientAuthenticationTest.java index 53dd662..dd22e93 100644 --- a/src/test/java/me/lucko/luckperms/rest/ClientAuthenticationTest.java +++ b/src/test/java/me/lucko/luckperms/rest/ClientAuthenticationTest.java @@ -25,7 +25,7 @@ package me.lucko.luckperms.rest; -import net.luckperms.rest.LuckPermsClient; +import net.luckperms.rest.LuckPermsRestClient; import org.junit.jupiter.api.Test; import org.slf4j.LoggerFactory; import org.testcontainers.containers.GenericContainer; @@ -59,19 +59,19 @@ public class ClientAuthenticationTest { .withStrategy(Wait.forLogMessage(".*Successfully enabled.*", 1)) ); - private LuckPermsClient createClient(String apiKey) { + private LuckPermsRestClient createClient(String apiKey) { assertTrue(container.isRunning()); String host = container.getHost(); Integer port = container.getFirstMappedPort(); String baseUrl = "http://" + host + ":" + port + "/"; - return LuckPermsClient.builder().baseUrl(baseUrl).apiKey(apiKey).build(); + return LuckPermsRestClient.builder().baseUrl(baseUrl).apiKey(apiKey).build(); } @Test public void testUnauthorized() throws IOException { - LuckPermsClient client = createClient(null); + LuckPermsRestClient client = createClient(null); Response> resp = client.users().list().execute(); assertFalse(resp.isSuccessful()); @@ -81,7 +81,7 @@ public void testUnauthorized() throws IOException { @Test public void testAuthorized() throws IOException { - LuckPermsClient client = createClient("test1"); + LuckPermsRestClient client = createClient("test1"); Response> resp = client.users().list().execute(); assertTrue(resp.isSuccessful()); diff --git a/src/test/java/me/lucko/luckperms/rest/GroupServiceTest.java b/src/test/java/me/lucko/luckperms/rest/GroupServiceTest.java index a61e235..b0e1bb0 100644 --- a/src/test/java/me/lucko/luckperms/rest/GroupServiceTest.java +++ b/src/test/java/me/lucko/luckperms/rest/GroupServiceTest.java @@ -25,13 +25,14 @@ package me.lucko.luckperms.rest; -import net.luckperms.rest.LuckPermsClient; +import net.luckperms.rest.LuckPermsRestClient; import net.luckperms.rest.model.Context; import net.luckperms.rest.model.CreateGroupRequest; import net.luckperms.rest.model.Group; import net.luckperms.rest.model.GroupSearchResult; import net.luckperms.rest.model.Metadata; import net.luckperms.rest.model.Node; +import net.luckperms.rest.model.NodeType; import net.luckperms.rest.model.PermissionCheckRequest; import net.luckperms.rest.model.PermissionCheckResult; import net.luckperms.rest.model.QueryOptions; @@ -57,7 +58,7 @@ public class GroupServiceTest extends AbstractIntegrationTest { @Test public void testGroupCrud() throws IOException { - LuckPermsClient client = createClient(); + LuckPermsRestClient client = createClient(); String name = randomName(); @@ -91,7 +92,7 @@ public void testGroupCrud() throws IOException { @Test public void testGroupList() throws IOException { - LuckPermsClient client = createClient(); + LuckPermsRestClient client = createClient(); String name = randomName(); @@ -106,7 +107,7 @@ public void testGroupList() throws IOException { @Test public void testGroupNodes() throws IOException { - LuckPermsClient client = createClient(); + LuckPermsRestClient client = createClient(); String name = randomName(); @@ -211,7 +212,7 @@ public void testGroupNodes() throws IOException { @Test public void testGroupMetadata() throws IOException { - LuckPermsClient client = createClient(); + LuckPermsRestClient client = createClient(); String name = randomName(); @@ -242,7 +243,7 @@ public void testGroupMetadata() throws IOException { @Test public void testGroupSearch() throws IOException { - LuckPermsClient client = createClient(); + LuckPermsRestClient client = createClient(); // clear existing groups for (String g : Objects.requireNonNull(client.groups().list().execute().body())) { @@ -298,7 +299,7 @@ public void testGroupSearch() throws IOException { ), resp3.body()); // searchNodesByType - Response> resp4 = client.groups().searchNodesByType("prefix").execute(); + Response> resp4 = client.groups().searchNodesByType(NodeType.PREFIX).execute(); assertTrue(resp4.isSuccessful()); assertNotNull(resp4.body()); assertEquals(ImmutableList.of( @@ -308,7 +309,7 @@ public void testGroupSearch() throws IOException { @Test public void testGroupPermissionCheck() throws IOException { - LuckPermsClient client = createClient(); + LuckPermsRestClient client = createClient(); String name = randomName(); diff --git a/src/test/java/me/lucko/luckperms/rest/MiscServiceTest.java b/src/test/java/me/lucko/luckperms/rest/MiscServiceTest.java index 04c27c2..aaa4af3 100644 --- a/src/test/java/me/lucko/luckperms/rest/MiscServiceTest.java +++ b/src/test/java/me/lucko/luckperms/rest/MiscServiceTest.java @@ -25,7 +25,7 @@ package me.lucko.luckperms.rest; -import net.luckperms.rest.LuckPermsClient; +import net.luckperms.rest.LuckPermsRestClient; import net.luckperms.rest.model.Health; import org.junit.jupiter.api.Test; import retrofit2.Response; @@ -39,7 +39,7 @@ public class MiscServiceTest extends AbstractIntegrationTest { @Test public void testHealth() throws IOException { - LuckPermsClient client = createClient(); + LuckPermsRestClient client = createClient(); Response resp = client.misc().health().execute(); assertTrue(resp.isSuccessful()); diff --git a/src/test/java/me/lucko/luckperms/rest/TrackServiceTest.java b/src/test/java/me/lucko/luckperms/rest/TrackServiceTest.java index 3bb3965..51de426 100644 --- a/src/test/java/me/lucko/luckperms/rest/TrackServiceTest.java +++ b/src/test/java/me/lucko/luckperms/rest/TrackServiceTest.java @@ -25,7 +25,7 @@ package me.lucko.luckperms.rest; -import net.luckperms.rest.LuckPermsClient; +import net.luckperms.rest.LuckPermsRestClient; import net.luckperms.rest.model.CreateGroupRequest; import net.luckperms.rest.model.CreateTrackRequest; import net.luckperms.rest.model.Group; @@ -47,7 +47,7 @@ public class TrackServiceTest extends AbstractIntegrationTest { @Test public void testTrackCrud() throws IOException { - LuckPermsClient client = createClient(); + LuckPermsRestClient client = createClient(); String name = randomName(); @@ -98,7 +98,7 @@ public void testTrackCrud() throws IOException { @Test public void testTrackList() throws IOException { - LuckPermsClient client = createClient(); + LuckPermsRestClient client = createClient(); String name = randomName(); diff --git a/src/test/java/me/lucko/luckperms/rest/UserServiceTest.java b/src/test/java/me/lucko/luckperms/rest/UserServiceTest.java index 337519c..5f48112 100644 --- a/src/test/java/me/lucko/luckperms/rest/UserServiceTest.java +++ b/src/test/java/me/lucko/luckperms/rest/UserServiceTest.java @@ -25,7 +25,7 @@ package me.lucko.luckperms.rest; -import net.luckperms.rest.LuckPermsClient; +import net.luckperms.rest.LuckPermsRestClient; import net.luckperms.rest.model.Context; import net.luckperms.rest.model.CreateGroupRequest; import net.luckperms.rest.model.CreateTrackRequest; @@ -34,13 +34,13 @@ import net.luckperms.rest.model.Group; import net.luckperms.rest.model.Metadata; import net.luckperms.rest.model.Node; +import net.luckperms.rest.model.NodeType; import net.luckperms.rest.model.PermissionCheckRequest; import net.luckperms.rest.model.PermissionCheckResult; import net.luckperms.rest.model.PlayerSaveResult; import net.luckperms.rest.model.PromotionResult; import net.luckperms.rest.model.QueryOptions; import net.luckperms.rest.model.TemporaryNodeMergeStrategy; -import net.luckperms.rest.model.Track; import net.luckperms.rest.model.TrackRequest; import net.luckperms.rest.model.UpdateTrackRequest; import net.luckperms.rest.model.UpdateUserRequest; @@ -69,7 +69,7 @@ public class UserServiceTest extends AbstractIntegrationTest { @Test public void testUserCrud() throws IOException { - LuckPermsClient client = createClient(); + LuckPermsRestClient client = createClient(); UUID uuid = UUID.randomUUID(); String username = randomName(); @@ -100,7 +100,7 @@ public void testUserCrud() throws IOException { @Test public void testUserCreate() throws IOException { - LuckPermsClient client = createClient(); + LuckPermsRestClient client = createClient(); UUID uuid = UUID.randomUUID(); String username = randomName(); @@ -150,7 +150,7 @@ public void testUserCreate() throws IOException { @Test public void testUserList() throws IOException { - LuckPermsClient client = createClient(); + LuckPermsRestClient client = createClient(); UUID uuid = UUID.randomUUID(); String username = randomName(); @@ -167,7 +167,7 @@ public void testUserList() throws IOException { @Test public void testUserLookup() throws IOException { - LuckPermsClient client = createClient(); + LuckPermsRestClient client = createClient(); UUID uuid = UUID.randomUUID(); String username = randomName(); @@ -194,7 +194,7 @@ public void testUserLookup() throws IOException { @Test public void testUserNodes() throws IOException { - LuckPermsClient client = createClient(); + LuckPermsRestClient client = createClient(); UUID uuid = UUID.randomUUID(); String username = randomName(); @@ -310,7 +310,7 @@ public void testUserNodes() throws IOException { @Test public void testUserMetadata() throws IOException { - LuckPermsClient client = createClient(); + LuckPermsRestClient client = createClient(); UUID uuid = UUID.randomUUID(); String username = randomName(); @@ -342,7 +342,7 @@ public void testUserMetadata() throws IOException { @Test public void testUserSearch() throws IOException { - LuckPermsClient client = createClient(); + LuckPermsRestClient client = createClient(); // clear existing users Set existingUsers = client.users().list().execute().body(); @@ -400,7 +400,7 @@ public void testUserSearch() throws IOException { ), resp3.body()); // searchNodesByType - Response> resp4 = client.users().searchNodesByType("prefix").execute(); + Response> resp4 = client.users().searchNodesByType(NodeType.PREFIX).execute(); assertTrue(resp4.isSuccessful()); assertNotNull(resp4.body()); assertEquals(ImmutableList.of( @@ -410,7 +410,7 @@ public void testUserSearch() throws IOException { @Test public void testUserPermissionCheck() throws IOException { - LuckPermsClient client = createClient(); + LuckPermsRestClient client = createClient(); UUID uuid = UUID.randomUUID(); String username = randomName(); @@ -461,7 +461,7 @@ public void testUserPermissionCheck() throws IOException { @Test public void testUserPromoteDemote() throws IOException { - LuckPermsClient client = createClient(); + LuckPermsRestClient client = createClient(); // create a user UUID uuid = UUID.randomUUID();