Make request: Spigot

Discover the steps to integrate the library into your plugin, facilitating communication with your microservice hosted on a Node.js application.

Example for making your first request

In this class, I will walk you through the process of utilizing all four HTTP methods. If you have prior experience with microservices or basic HTTP requests, the concepts should be straightforward to understand.

These methods direct requests to specific endpoints. The final step is to implement the logic for these methods within your microservice application.

PlayerRepository.java
import micro.api.service.*;
import micro.api.payload.*;

public class PlayerRepository extends ServiceEndpoint {
   /**
    * This points to `http://localhost:5000/api/player`
    */
    public PlayerRepository(ServiceHost host) {
       super("/player", host);
    }

    /**
     * Example of GET request, get player's data.
     * @return a new PlayerClient object.
     */
    public PlayerClient loadPlayer(UUID uuid) {
        Query query = new Query();
        query.add("uuid", uuid.toString());

        return super.get("/load", PlayerClient.class, query);
    }
    
    /**
     * Example of POST request, add a new friend.
     * @return The updated PlayerClient object.
     */
    public PlayerClient addFriend(UUID uuid, UUID target) {
        Body body = new Body();
        body.add("uuid", uuid.toString());
        body.add("target", target.toString());

        return super.post("/friend_add", PlayerClient.class, body);
    }
    
    /**
     * Example of PUT request, update player's coins.
     * @return The updated PlayerClient object.
     */
    public PlayerClient updateCoins(UUID uuid, int amount) {
        Body body = new Body();
        body.add("uuid", uuid.toString());
        body.add("coins", amount);

        return super.put("/coin_update", PlayerClient.class, body);
    }
    
    /**
     * Example of DELETE request, delete player data.
     * @return The deleted PlayerClient object.
     */
    public PlayerClient deletePlayer(UUID uuid) {
        Query query = new Query();
        query.add("uuid", uuid.toString());
        
        return super.delete("/delete", PlayerClient.class, query);
    }
}

Last updated