Skip to content

Commit 3808826

Browse files
feat: API
1 parent 75ec58b commit 3808826

27 files changed

Lines changed: 2443 additions & 1 deletion

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

commons/src/main/java/net/swofty/commons/ServiceType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ public enum ServiceType {
44
AUCTION_HOUSE,
55
BAZAAR,
66
ITEM_TRACKER,
7+
API,
78
;
89
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package net.swofty.commons.protocol.objects.api;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.NoArgsConstructor;
5+
import net.swofty.commons.protocol.ProtocolObject;
6+
import net.swofty.commons.protocol.Serializer;
7+
import net.swofty.commons.protocol.objects.bazaar.BazaarBuyProtocolObject;
8+
import org.json.JSONObject;
9+
10+
import java.util.UUID;
11+
12+
public class APIAuthenticateCodeProtocolObject extends ProtocolObject
13+
<APIAuthenticateCodeProtocolObject.AuthenticateCodeMessage, APIAuthenticateCodeProtocolObject.AuthenticateCodeResponse> {
14+
15+
@Override
16+
public Serializer<AuthenticateCodeMessage> getSerializer() {
17+
return new Serializer<AuthenticateCodeMessage>() {
18+
@Override
19+
public String serialize(AuthenticateCodeMessage value) {
20+
JSONObject json = new JSONObject();
21+
json.put("auth-code", value.authCode);
22+
json.put("player-name", value.playerName);
23+
json.put("player-uuid", value.playerUUID);
24+
return json.toString();
25+
}
26+
27+
@Override
28+
public AuthenticateCodeMessage deserialize(String json) {
29+
JSONObject jsonObject = new JSONObject(json);
30+
AuthenticateCodeMessage message = new AuthenticateCodeMessage();
31+
message.authCode = jsonObject.getString("auth-code");
32+
message.playerName = jsonObject.getString("player-name");
33+
message.playerUUID = UUID.fromString(jsonObject.getString("player-uuid"));
34+
return message;
35+
}
36+
37+
@Override
38+
public AuthenticateCodeMessage clone(AuthenticateCodeMessage value) {
39+
return new AuthenticateCodeMessage(value.authCode, value.playerName, value.playerUUID);
40+
}
41+
};
42+
}
43+
44+
@Override
45+
public Serializer<AuthenticateCodeResponse> getReturnSerializer() {
46+
return new Serializer<AuthenticateCodeResponse>() {
47+
public String serialize(AuthenticateCodeResponse value) {
48+
JSONObject json = new JSONObject();
49+
json.put("successful", value.successful);
50+
return json.toString();
51+
}
52+
53+
public AuthenticateCodeResponse deserialize(String json) {
54+
JSONObject jsonObject = new JSONObject(json);
55+
AuthenticateCodeResponse message = new AuthenticateCodeResponse();
56+
message.successful = jsonObject.getBoolean("successful");
57+
return message;
58+
}
59+
60+
public AuthenticateCodeResponse clone(AuthenticateCodeResponse value) {
61+
return new AuthenticateCodeResponse(value.successful);
62+
}
63+
};
64+
}
65+
66+
@AllArgsConstructor
67+
@NoArgsConstructor
68+
public static class AuthenticateCodeMessage {
69+
public String authCode;
70+
public String playerName;
71+
public UUID playerUUID;
72+
}
73+
74+
@AllArgsConstructor
75+
@NoArgsConstructor
76+
public static class AuthenticateCodeResponse {
77+
public boolean successful;
78+
}
79+
}
80+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package net.swofty.service.api;
2+
3+
import com.mongodb.ConnectionString;
4+
import com.mongodb.MongoClientSettings;
5+
import com.mongodb.client.*;
6+
import com.mongodb.client.model.Filters;
7+
import com.mongodb.client.model.Updates;
8+
import net.swofty.commons.protocol.objects.api.APIAuthenticateCodeProtocolObject;
9+
import net.swofty.service.generic.MongoDB;
10+
import org.bson.Document;
11+
import org.jetbrains.annotations.Nullable;
12+
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
16+
public record APIAdminDatabase() {
17+
public static MongoClient client;
18+
public static MongoDatabase database;
19+
public static MongoCollection<Document> collection;
20+
21+
public void connect(String connectionString) {
22+
ConnectionString cs = new ConnectionString(connectionString);
23+
MongoClientSettings settings = MongoClientSettings.builder().applyConnectionString(cs).build();
24+
client = MongoClients.create(settings);
25+
26+
database = client.getDatabase("Minestom");
27+
collection = database.getCollection("api-admin");
28+
}
29+
30+
public static void replaceOrInsert(APIAdminDatabaseObject object) {
31+
Document document = object.toDocument();
32+
if (collection.find(new Document("_id", object.sessionId)).first() != null) {
33+
collection.replaceOne(new Document("_id", object.sessionId), document);
34+
} else {
35+
collection.insertOne(document);
36+
}
37+
}
38+
39+
public static @Nullable APIAdminDatabaseObject getFromCode(String authCode) {
40+
Document document = collection.find(new Document("authCode", authCode)).first();
41+
if (document == null) {
42+
return null;
43+
}
44+
return APIAdminDatabaseObject.fromDocument(document);
45+
}
46+
47+
public @Nullable APIAdminDatabaseObject getFromSessionId(String sessionId) {
48+
Document document = collection.find(new Document("_id", sessionId)).first();
49+
if (document == null) {
50+
return null;
51+
}
52+
return APIAdminDatabaseObject.fromDocument(document);
53+
}
54+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package net.swofty.service.api;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
6+
import lombok.Setter;
7+
import org.bson.Document;
8+
9+
import java.util.Map;
10+
import java.util.UUID;
11+
12+
@AllArgsConstructor
13+
@NoArgsConstructor
14+
@Getter
15+
@Setter
16+
public class APIAdminDatabaseObject {
17+
public String sessionId;
18+
public String authCode;
19+
public String authenticatorName;
20+
public UUID authenticatorUUID;
21+
22+
public Document toDocument() {
23+
return new Document(Map.of(
24+
"_id", sessionId,
25+
"authCode", authCode,
26+
"authenticatorName", authenticatorName,
27+
"authenticatorUUID", authenticatorUUID.toString()
28+
));
29+
}
30+
31+
public boolean isAuthenticated() {
32+
return authenticatorName != null && !authenticatorName.isEmpty();
33+
}
34+
35+
public static APIAdminDatabaseObject fromDocument(Document document) {
36+
APIAdminDatabaseObject object = new APIAdminDatabaseObject();
37+
object.setSessionId(document.getString("_id"));
38+
object.setAuthCode(document.getString("authCode"));
39+
object.setAuthenticatorName(document.getString("authenticatorName"));
40+
object.setAuthenticatorUUID(UUID.fromString(document.getString("authenticatorUUID")));
41+
return object;
42+
}
43+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package net.swofty.service.api;
2+
3+
import com.mongodb.ConnectionString;
4+
import com.mongodb.MongoClientSettings;
5+
import com.mongodb.client.*;
6+
import com.mongodb.client.model.Filters;
7+
import com.mongodb.client.model.Updates;
8+
import net.swofty.service.generic.MongoDB;
9+
import org.bson.Document;
10+
import org.jetbrains.annotations.Nullable;
11+
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
public record APIKeyDatabase(String key) {
16+
public static MongoClient client;
17+
public static MongoDatabase database;
18+
public static MongoCollection<Document> collection;
19+
20+
public void connect(String connectionString) {
21+
ConnectionString cs = new ConnectionString(connectionString);
22+
MongoClientSettings settings = MongoClientSettings.builder().applyConnectionString(cs).build();
23+
client = MongoClients.create(settings);
24+
25+
database = client.getDatabase("Minestom");
26+
collection = database.getCollection("api-key");
27+
}
28+
29+
public @Nullable APIKeyDatabaseObject fetch() {
30+
Document document = collection.find(new Document("_id", key)).first();
31+
if (document == null) {
32+
return null;
33+
}
34+
return APIKeyDatabaseObject.fromDocument(document);
35+
}
36+
37+
public void delete() {
38+
collection.deleteOne(new Document("_id", key));
39+
}
40+
41+
public static void insert(APIKeyDatabaseObject object) {
42+
collection.insertOne(object.toDocument());
43+
}
44+
45+
public static List<APIKeyDatabaseObject> getAll() {
46+
FindIterable<Document> results = collection.find();
47+
List<Document> list = new ArrayList<>();
48+
for (Document doc : results) {
49+
list.add(doc);
50+
}
51+
return list.stream().map(APIKeyDatabaseObject::fromDocument).toList();
52+
}
53+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package net.swofty.service.api;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
6+
import lombok.Setter;
7+
import org.bson.Document;
8+
9+
import java.util.Map;
10+
import java.util.UUID;
11+
12+
@AllArgsConstructor
13+
@NoArgsConstructor
14+
@Getter
15+
@Setter
16+
public class APIKeyDatabaseObject {
17+
public String key;
18+
public String description;
19+
public long requestsPerDay;
20+
21+
public Document toDocument() {
22+
return new Document(Map.of(
23+
"_id", key,
24+
"description", description,
25+
"requestsPerDay", requestsPerDay
26+
));
27+
}
28+
29+
public static APIKeyDatabaseObject fromDocument(Document document) {
30+
APIKeyDatabaseObject object = new APIKeyDatabaseObject();
31+
object.setKey(document.getString("_id"));
32+
object.setDescription(document.getString("description"));
33+
object.setRequestsPerDay(document.getLong("requestsPerDay"));
34+
return object;
35+
}
36+
}

0 commit comments

Comments
 (0)