Skip to content

Commit a35f475

Browse files
committed
Use enum for user/group search by node type
1 parent 1fc0bd6 commit a35f475

13 files changed

+119
-48
lines changed

build.gradle

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
plugins {
2-
id 'java'
32
id 'java-library'
3+
id 'maven-publish'
44
}
55

6-
group = 'net.luckperms.rest'
6+
group = 'net.luckperms'
77
version = '0.1-SNAPSHOT'
88

9+
10+
java {
11+
toolchain {
12+
languageVersion = JavaLanguageVersion.of(8)
13+
}
14+
withJavadocJar()
15+
withSourcesJar()
16+
}
17+
918
repositories {
1019
mavenCentral()
1120
}
@@ -32,3 +41,12 @@ dependencies {
3241
testImplementation 'org.slf4j:slf4j-simple:1.7.36'
3342
testImplementation 'com.google.guava:guava:32.1.3-jre'
3443
}
44+
45+
publishing {
46+
publications {
47+
mavenJava(MavenPublication) {
48+
from components.java
49+
}
50+
}
51+
}
52+

src/main/java/net/luckperms/rest/LuckPermsClient.java renamed to src/main/java/net/luckperms/rest/LuckPermsRestClient.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@
3636
*
3737
* @see <a href="https://github.com/LuckPerms/rest-api">rest-api GitHub repo</a>
3838
*/
39-
public interface LuckPermsClient {
39+
public interface LuckPermsRestClient extends AutoCloseable {
4040

4141
/**
4242
* Creates a new client builder.
4343
*
4444
* @return the new builder
4545
*/
4646
static Builder builder() {
47-
return new LuckPermsClientImpl.BuilderImpl();
47+
return new LuckPermsRestClientImpl.BuilderImpl();
4848
}
4949

5050
/**
@@ -83,7 +83,13 @@ static Builder builder() {
8383
MiscService misc();
8484

8585
/**
86-
* A builder for {@link LuckPermsClient}
86+
* Close the underlying resources used by the client.
87+
*/
88+
@Override
89+
void close();
90+
91+
/**
92+
* A builder for {@link LuckPermsRestClient}
8793
*/
8894
interface Builder {
8995

@@ -108,6 +114,6 @@ interface Builder {
108114
*
109115
* @return a client
110116
*/
111-
LuckPermsClient build();
117+
LuckPermsRestClient build();
112118
}
113119
}

src/main/java/net/luckperms/rest/LuckPermsClientImpl.java renamed to src/main/java/net/luckperms/rest/LuckPermsRestClientImpl.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,26 @@
4040
import java.io.IOException;
4141
import java.util.Objects;
4242

43-
class LuckPermsClientImpl implements LuckPermsClient {
43+
class LuckPermsRestClientImpl implements LuckPermsRestClient {
44+
private final OkHttpClient httpClient;
45+
4446
private final UserService userService;
4547
private final GroupService groupService;
4648
private final TrackService trackService;
4749
private final ActionService actionService;
4850
private final MiscService miscService;
4951

50-
LuckPermsClientImpl(String baseUrl, String apiKey) {
52+
LuckPermsRestClientImpl(String baseUrl, String apiKey) {
5153
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
5254

5355
if (apiKey != null && !apiKey.isEmpty()) {
5456
clientBuilder.addInterceptor(new AuthInterceptor(apiKey));
5557
}
5658

59+
this.httpClient = clientBuilder.build();
60+
5761
Retrofit retrofit = new Retrofit.Builder()
58-
.client(clientBuilder.build())
62+
.client(this.httpClient)
5963
.baseUrl(baseUrl)
6064
.addConverterFactory(GsonConverterFactory.create())
6165
.build();
@@ -91,6 +95,12 @@ public MiscService misc() {
9195
return this.miscService;
9296
}
9397

98+
@Override
99+
public void close() {
100+
this.httpClient.dispatcher().executorService().shutdown();
101+
this.httpClient.connectionPool().evictAll();
102+
}
103+
94104
static final class BuilderImpl implements Builder {
95105
private String baseUrl = null;
96106
private String apiKey = null;
@@ -112,9 +122,9 @@ public Builder apiKey(String apiKey) {
112122
}
113123

114124
@Override
115-
public LuckPermsClient build() {
125+
public LuckPermsRestClient build() {
116126
Objects.requireNonNull(this.baseUrl, "baseUrl must be configured!");
117-
return new LuckPermsClientImpl(this.baseUrl, this.apiKey);
127+
return new LuckPermsRestClientImpl(this.baseUrl, this.apiKey);
118128
}
119129
}
120130

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package net.luckperms.rest.model;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
5+
import java.util.Locale;
6+
7+
public enum NodeType {
8+
9+
@SerializedName("regex_permission")
10+
REGEX_PERMISSION,
11+
12+
@SerializedName("inheritance")
13+
INHERITANCE,
14+
15+
@SerializedName("prefix")
16+
PREFIX,
17+
18+
@SerializedName("suffix")
19+
SUFFIX,
20+
21+
@SerializedName("meta")
22+
META,
23+
24+
@SerializedName("weight")
25+
WEIGHT,
26+
27+
@SerializedName("display_name")
28+
DISPLAY_NAME;
29+
30+
@Override
31+
public String toString() {
32+
return this.name().toLowerCase(Locale.ROOT);
33+
}
34+
}

src/main/java/net/luckperms/rest/service/GroupService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import net.luckperms.rest.model.GroupSearchResult;
3131
import net.luckperms.rest.model.Metadata;
3232
import net.luckperms.rest.model.Node;
33+
import net.luckperms.rest.model.NodeType;
3334
import net.luckperms.rest.model.PermissionCheckRequest;
3435
import net.luckperms.rest.model.PermissionCheckResult;
3536
import net.luckperms.rest.model.TemporaryNodeMergeStrategy;
@@ -65,7 +66,7 @@ public interface GroupService {
6566
Call<List<GroupSearchResult>> searchNodesByMetaKey(@Query("metaKey") String metaKey);
6667

6768
@GET("/group/search")
68-
Call<List<GroupSearchResult>> searchNodesByType(@Query("type") String type);
69+
Call<List<GroupSearchResult>> searchNodesByType(@Query("type") NodeType type);
6970

7071
@GET("/group/{name}")
7172
Call<Group> get(@Path("name") String name);

src/main/java/net/luckperms/rest/service/UserService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import net.luckperms.rest.model.DemotionResult;
3030
import net.luckperms.rest.model.Metadata;
3131
import net.luckperms.rest.model.Node;
32+
import net.luckperms.rest.model.NodeType;
3233
import net.luckperms.rest.model.PermissionCheckRequest;
3334
import net.luckperms.rest.model.PermissionCheckResult;
3435
import net.luckperms.rest.model.PlayerSaveResult;
@@ -78,7 +79,7 @@ public interface UserService {
7879
Call<List<UserSearchResult>> searchNodesByMetaKey(@Query("metaKey") String metaKey);
7980

8081
@GET("/user/search")
81-
Call<List<UserSearchResult>> searchNodesByType(@Query("type") String type);
82+
Call<List<UserSearchResult>> searchNodesByType(@Query("type") NodeType type);
8283

8384
@GET("/user/{uniqueId}")
8485
Call<User> get(@Path("uniqueId") UUID uniqueId);

src/test/java/me/lucko/luckperms/rest/AbstractIntegrationTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
package me.lucko.luckperms.rest;
2727

28-
import net.luckperms.rest.LuckPermsClient;
28+
import net.luckperms.rest.LuckPermsRestClient;
2929
import org.slf4j.LoggerFactory;
3030
import org.testcontainers.containers.GenericContainer;
3131
import org.testcontainers.containers.output.Slf4jLogConsumer;
@@ -51,14 +51,14 @@ public class AbstractIntegrationTest {
5151
.withStrategy(Wait.forLogMessage(".*Successfully enabled.*", 1))
5252
);
5353

54-
protected LuckPermsClient createClient() {
54+
protected LuckPermsRestClient createClient() {
5555
assertTrue(container.isRunning());
5656

5757
String host = container.getHost();
5858
Integer port = container.getFirstMappedPort();
5959
String baseUrl = "http://" + host + ":" + port + "/";
6060

61-
return LuckPermsClient.builder().baseUrl(baseUrl).build();
61+
return LuckPermsRestClient.builder().baseUrl(baseUrl).build();
6262
}
6363

6464
protected static String randomName() {

src/test/java/me/lucko/luckperms/rest/ActionServiceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
package me.lucko.luckperms.rest;
2727

28-
import net.luckperms.rest.LuckPermsClient;
28+
import net.luckperms.rest.LuckPermsRestClient;
2929
import net.luckperms.rest.model.Action;
3030
import org.junit.jupiter.api.Test;
3131
import retrofit2.Response;
@@ -39,7 +39,7 @@ public class ActionServiceTest extends AbstractIntegrationTest {
3939

4040
@Test
4141
public void testSubmit() throws IOException {
42-
LuckPermsClient client = createClient();
42+
LuckPermsRestClient client = createClient();
4343

4444
Response<Void> resp = client.actions().submit(new Action(
4545
System.currentTimeMillis() / 1000L,

src/test/java/me/lucko/luckperms/rest/ClientAuthenticationTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
package me.lucko.luckperms.rest;
2727

28-
import net.luckperms.rest.LuckPermsClient;
28+
import net.luckperms.rest.LuckPermsRestClient;
2929
import org.junit.jupiter.api.Test;
3030
import org.slf4j.LoggerFactory;
3131
import org.testcontainers.containers.GenericContainer;
@@ -59,19 +59,19 @@ public class ClientAuthenticationTest {
5959
.withStrategy(Wait.forLogMessage(".*Successfully enabled.*", 1))
6060
);
6161

62-
private LuckPermsClient createClient(String apiKey) {
62+
private LuckPermsRestClient createClient(String apiKey) {
6363
assertTrue(container.isRunning());
6464

6565
String host = container.getHost();
6666
Integer port = container.getFirstMappedPort();
6767
String baseUrl = "http://" + host + ":" + port + "/";
6868

69-
return LuckPermsClient.builder().baseUrl(baseUrl).apiKey(apiKey).build();
69+
return LuckPermsRestClient.builder().baseUrl(baseUrl).apiKey(apiKey).build();
7070
}
7171

7272
@Test
7373
public void testUnauthorized() throws IOException {
74-
LuckPermsClient client = createClient(null);
74+
LuckPermsRestClient client = createClient(null);
7575

7676
Response<Set<UUID>> resp = client.users().list().execute();
7777
assertFalse(resp.isSuccessful());
@@ -81,7 +81,7 @@ public void testUnauthorized() throws IOException {
8181

8282
@Test
8383
public void testAuthorized() throws IOException {
84-
LuckPermsClient client = createClient("test1");
84+
LuckPermsRestClient client = createClient("test1");
8585

8686
Response<Set<UUID>> resp = client.users().list().execute();
8787
assertTrue(resp.isSuccessful());

src/test/java/me/lucko/luckperms/rest/GroupServiceTest.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@
2525

2626
package me.lucko.luckperms.rest;
2727

28-
import net.luckperms.rest.LuckPermsClient;
28+
import net.luckperms.rest.LuckPermsRestClient;
2929
import net.luckperms.rest.model.Context;
3030
import net.luckperms.rest.model.CreateGroupRequest;
3131
import net.luckperms.rest.model.Group;
3232
import net.luckperms.rest.model.GroupSearchResult;
3333
import net.luckperms.rest.model.Metadata;
3434
import net.luckperms.rest.model.Node;
35+
import net.luckperms.rest.model.NodeType;
3536
import net.luckperms.rest.model.PermissionCheckRequest;
3637
import net.luckperms.rest.model.PermissionCheckResult;
3738
import net.luckperms.rest.model.QueryOptions;
@@ -57,7 +58,7 @@ public class GroupServiceTest extends AbstractIntegrationTest {
5758

5859
@Test
5960
public void testGroupCrud() throws IOException {
60-
LuckPermsClient client = createClient();
61+
LuckPermsRestClient client = createClient();
6162

6263
String name = randomName();
6364

@@ -91,7 +92,7 @@ public void testGroupCrud() throws IOException {
9192

9293
@Test
9394
public void testGroupList() throws IOException {
94-
LuckPermsClient client = createClient();
95+
LuckPermsRestClient client = createClient();
9596

9697
String name = randomName();
9798

@@ -106,7 +107,7 @@ public void testGroupList() throws IOException {
106107

107108
@Test
108109
public void testGroupNodes() throws IOException {
109-
LuckPermsClient client = createClient();
110+
LuckPermsRestClient client = createClient();
110111

111112
String name = randomName();
112113

@@ -211,7 +212,7 @@ public void testGroupNodes() throws IOException {
211212

212213
@Test
213214
public void testGroupMetadata() throws IOException {
214-
LuckPermsClient client = createClient();
215+
LuckPermsRestClient client = createClient();
215216

216217
String name = randomName();
217218

@@ -242,7 +243,7 @@ public void testGroupMetadata() throws IOException {
242243

243244
@Test
244245
public void testGroupSearch() throws IOException {
245-
LuckPermsClient client = createClient();
246+
LuckPermsRestClient client = createClient();
246247

247248
// clear existing groups
248249
for (String g : Objects.requireNonNull(client.groups().list().execute().body())) {
@@ -298,7 +299,7 @@ public void testGroupSearch() throws IOException {
298299
), resp3.body());
299300

300301
// searchNodesByType
301-
Response<List<GroupSearchResult>> resp4 = client.groups().searchNodesByType("prefix").execute();
302+
Response<List<GroupSearchResult>> resp4 = client.groups().searchNodesByType(NodeType.PREFIX).execute();
302303
assertTrue(resp4.isSuccessful());
303304
assertNotNull(resp4.body());
304305
assertEquals(ImmutableList.of(
@@ -308,7 +309,7 @@ public void testGroupSearch() throws IOException {
308309

309310
@Test
310311
public void testGroupPermissionCheck() throws IOException {
311-
LuckPermsClient client = createClient();
312+
LuckPermsRestClient client = createClient();
312313

313314
String name = randomName();
314315

0 commit comments

Comments
 (0)