Skip to content

Commit

Permalink
update Request module
Browse files Browse the repository at this point in the history
  • Loading branch information
GIGABAIT93 committed Mar 14, 2024
1 parent 248da4b commit 0c8aba1
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 18 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,20 @@ Reads and outputs the contents of text files located in the "text" folder to pla
Performs HTTP requests to specified URLs, supporting both GET and POST requests. Configuration files are used to manage request parameters. Allows you to execute commands depending on the HTTP request response. Each individual configuration file corresponds to a single request.
```yaml
# The URL of the API to be queried.
url: "https://domain.com/api/minecraft/link"
# Available placeholders: %player_name%, %player_uuid%, %player_ip%, %server%, %arg1%, %arg2%, %arg[n]%
url: "https://api.mojang.com/users/profiles/minecraft/%player_name%"
# HTTP request method. Can be "GET" or "POST".
method: "GET"
# Query parameters. They will be sent to the API with the request. If the parameters are not used, this field can be deleted.
# You can use placeholders that will be automatically replaced with the appropriate values when the query is executed.
# Available placeholders: %player_name%, %player_uuid%, %server%, %arg1%, %arg2%, %arg[n]%
# Available placeholders: %player_name%, %player_uuid%, %player_ip%, %server%, %arg1%, %arg2%, %arg[n]%
parameters:
api_key: "******************************"
player_name: "%player_name%"
player_uuid: "%player_uuid%"
secret: "lmksfdjlfjsffsdfjkljklgjkljsieiweiefdls"
player: "%player_name%"
server: "%server%"
code: "%arg1%"
user_code: "%arg1%"
# Commands that cause the request to be executed. When one of these commands is entered, a request to the API will be executed.
triggers:
Expand All @@ -148,12 +148,15 @@ permission: "account.link"
response:
# Reply with successful status (200)
success:
- "m %player_name% %response%"
- "alert Player %player_name% is uuid %id%"
- "Player %player_name% has successfully linked his account to the site %json_resp_key_1%"
- "msg %player_name% You have successfully linked your account to site. %json_resp_key_2%"
# Response in case of failed status (not 200)
failure:
- "m %player_name% %response%"
- "msg %player_name% It was not possible to link the account to the site."
debug: false
# If this option is enabled, all response options from the api/site will be sent to the sender
debug: true
```

### EventsManager:
Expand Down
49 changes: 44 additions & 5 deletions src/main/java/com/vcmi/modules/requests/RequestCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,8 @@ private void runCommand(String[] args, CommandSource sender) throws Exception {
config.getConfigurationSection("parameters").getMapValues(true),
params
);
HttpRequest req = new HttpRequest(
config.getString("url"), config.getString("method"),
parameters
);

String url = parsePlaceholder(config.getString("url"), params);
HttpRequest req = new HttpRequest(url, config.getString("method"), parameters);
JsonElement resp = req.send();


Expand Down Expand Up @@ -93,6 +90,13 @@ private void runCommand(String[] args, CommandSource sender) throws Exception {
}
}

/**
* This method prepares the placeholders for the command.
* It collects the necessary information from the command sender and arguments.
* @param args The command arguments.
* @param sender The command sender.
* @return A map of placeholders and their corresponding values.
*/
private Map<String, String> placeholderPrepare(String[] args, CommandSource sender) {
Map<String, String> params = new HashMap<>();
if (sender instanceof Player) {
Expand All @@ -115,6 +119,13 @@ private Map<String, String> placeholderPrepare(String[] args, CommandSource send
return params;
}

/**
* This method parses the placeholders in a map.
* It replaces each placeholder in the map values with its corresponding value from the params map.
* @param map The map containing the placeholders.
* @param params The map containing the placeholder values.
* @return A map with the parsed placeholders.
*/
private Map<String, String> parsePlaceholders(Map<String, Object> map, Map<String, String> params) {
Map<String, String> stringMap = new HashMap<>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
Expand All @@ -127,6 +138,34 @@ private Map<String, String> parsePlaceholders(Map<String, Object> map, Map<Strin
return stringMap;
}

/**
* This method parses a single placeholder in a text.
* It replaces the placeholder in the text with its corresponding value from the params map.
* @param text The text containing the placeholder.
* @param params The map containing the placeholder values.
* @return The text with the parsed placeholder.
*/
private String parsePlaceholder(String text, Map<String, String> params) {
if (text == null || params == null) {
return text;
}
String parsedText = text;
for (Map.Entry<String, String> param : params.entrySet()) {
if (param.getKey() == null || param.getValue() == null) {
continue;
}
parsedText = parsedText.replace("%" + param.getKey() + "%", param.getValue());
}
return parsedText;
}

/**
* This method parses the placeholders in a list.
* It replaces each placeholder in the list items with its corresponding value from the params map.
* @param list The list containing the placeholders.
* @param params The map containing the placeholder values.
* @return A list with the parsed placeholders.
*/
private List<String> parsePlaceholdersInList(List<String> list, Map<String, String> params) {
if (list == null || params == null) {
return Collections.emptyList();
Expand Down
10 changes: 6 additions & 4 deletions src/main/resources/linkaccount.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# The URL of the API to be queried.
url: "http://example.com/api"
# Available placeholders: %player_name%, %player_uuid%, %player_ip%, %server%, %arg1%, %arg2%, %arg[n]%
url: "https://api.mojang.com/users/profiles/minecraft/%player_name%"

# HTTP request method. Can be "GET" or "POST".
method: "POST"
method: "GET"

# Query parameters. They will be sent to the API with the request. If the parameters are not used, this field can be deleted.
# You can use placeholders that will be automatically replaced with the appropriate values when the query is executed.
Expand All @@ -28,11 +29,12 @@ permission: "account.link"
response:
# Reply with successful status (200)
success:
- "alert Player %player_name% has successfully linked his account to the site %json_resp_key_1%"
- "alert Player %player_name% is uuid %id%"
- "Player %player_name% has successfully linked his account to the site %json_resp_key_1%"
- "msg %player_name% You have successfully linked your account to site. %json_resp_key_2%"
# Response in case of failed status (not 200)
failure:
- "msg %player_name% It was not possible to link the account to the site."

# If this option is enabled, all response options from the api/site will be sent to the sender
debug: false
debug: true

0 comments on commit 0c8aba1

Please sign in to comment.