Skip to content

Commit

Permalink
Use enum for user/group search by node type
Browse files Browse the repository at this point in the history
  • Loading branch information
lucko committed Nov 9, 2023
1 parent 1fc0bd6 commit a35f475
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 48 deletions.
22 changes: 20 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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()
}
Expand All @@ -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
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@
*
* @see <a href="https://github.com/LuckPerms/rest-api">rest-api GitHub repo</a>
*/
public interface LuckPermsClient {
public interface LuckPermsRestClient extends AutoCloseable {

/**
* Creates a new client builder.
*
* @return the new builder
*/
static Builder builder() {
return new LuckPermsClientImpl.BuilderImpl();
return new LuckPermsRestClientImpl.BuilderImpl();
}

/**
Expand Down Expand Up @@ -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 {

Expand All @@ -108,6 +114,6 @@ interface Builder {
*
* @return a client
*/
LuckPermsClient build();
LuckPermsRestClient build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}

Expand Down
34 changes: 34 additions & 0 deletions src/main/java/net/luckperms/rest/model/NodeType.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
3 changes: 2 additions & 1 deletion src/main/java/net/luckperms/rest/service/GroupService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,7 +66,7 @@ public interface GroupService {
Call<List<GroupSearchResult>> searchNodesByMetaKey(@Query("metaKey") String metaKey);

@GET("/group/search")
Call<List<GroupSearchResult>> searchNodesByType(@Query("type") String type);
Call<List<GroupSearchResult>> searchNodesByType(@Query("type") NodeType type);

@GET("/group/{name}")
Call<Group> get(@Path("name") String name);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/luckperms/rest/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,7 +79,7 @@ public interface UserService {
Call<List<UserSearchResult>> searchNodesByMetaKey(@Query("metaKey") String metaKey);

@GET("/user/search")
Call<List<UserSearchResult>> searchNodesByType(@Query("type") String type);
Call<List<UserSearchResult>> searchNodesByType(@Query("type") NodeType type);

@GET("/user/{uniqueId}")
Call<User> get(@Path("uniqueId") UUID uniqueId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/me/lucko/luckperms/rest/ActionServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,7 +39,7 @@ public class ActionServiceTest extends AbstractIntegrationTest {

@Test
public void testSubmit() throws IOException {
LuckPermsClient client = createClient();
LuckPermsRestClient client = createClient();

Response<Void> resp = client.actions().submit(new Action(
System.currentTimeMillis() / 1000L,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Set<UUID>> resp = client.users().list().execute();
assertFalse(resp.isSuccessful());
Expand All @@ -81,7 +81,7 @@ public void testUnauthorized() throws IOException {

@Test
public void testAuthorized() throws IOException {
LuckPermsClient client = createClient("test1");
LuckPermsRestClient client = createClient("test1");

Response<Set<UUID>> resp = client.users().list().execute();
assertTrue(resp.isSuccessful());
Expand Down
17 changes: 9 additions & 8 deletions src/test/java/me/lucko/luckperms/rest/GroupServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -57,7 +58,7 @@ public class GroupServiceTest extends AbstractIntegrationTest {

@Test
public void testGroupCrud() throws IOException {
LuckPermsClient client = createClient();
LuckPermsRestClient client = createClient();

String name = randomName();

Expand Down Expand Up @@ -91,7 +92,7 @@ public void testGroupCrud() throws IOException {

@Test
public void testGroupList() throws IOException {
LuckPermsClient client = createClient();
LuckPermsRestClient client = createClient();

String name = randomName();

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

@Test
public void testGroupNodes() throws IOException {
LuckPermsClient client = createClient();
LuckPermsRestClient client = createClient();

String name = randomName();

Expand Down Expand Up @@ -211,7 +212,7 @@ public void testGroupNodes() throws IOException {

@Test
public void testGroupMetadata() throws IOException {
LuckPermsClient client = createClient();
LuckPermsRestClient client = createClient();

String name = randomName();

Expand Down Expand Up @@ -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())) {
Expand Down Expand Up @@ -298,7 +299,7 @@ public void testGroupSearch() throws IOException {
), resp3.body());

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

@Test
public void testGroupPermissionCheck() throws IOException {
LuckPermsClient client = createClient();
LuckPermsRestClient client = createClient();

String name = randomName();

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/me/lucko/luckperms/rest/MiscServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,7 +39,7 @@ public class MiscServiceTest extends AbstractIntegrationTest {

@Test
public void testHealth() throws IOException {
LuckPermsClient client = createClient();
LuckPermsRestClient client = createClient();

Response<Health> resp = client.misc().health().execute();
assertTrue(resp.isSuccessful());
Expand Down
Loading

0 comments on commit a35f475

Please sign in to comment.