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.
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);
}
}
Information
As you can tell in this class, there's no GET request example, because a GET request always returns a value or content of some sort, hence why it's called GET.
This is the same class as the previous example, only this time we don't serialize the response into an object, we simply ask for the status code.
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 POST request, add a new friend.
* @return A status code object of the request.
*/
public ServiceResponse addFriend(UUID uuid, UUID target) {
Body body = new Body();
body.add("uuid", uuid.toString());
body.add("target", target.toString());
return super.post("/friend_add", body);
}
/**
* Example of PUT request, update player's coins.
* @return A status code object of the request.
*/
public ServiceResponse updateCoins(UUID uuid, int amount) {
Body body = new Body();
body.add("uuid", uuid.toString());
body.add("coins", amount);
return super.put("/coin_update", body);
}
/**
* Example of DELETE request, delete player data.
* @return A status code object of the request.
*/
public ServiceResponse deletePlayer(UUID uuid) {
Query query = new Query();
query.add("uuid", uuid.toString());
return super.delete("/delete", query);
}
}
As you can see in this example, instead of returning a serialized object, it returns an object with a status code of the request. The ServiceResponse is actually an enumerator which holds the most common status codes of a request;
/**
* Most common status codes in a microservice.
*/
public enum ServiceResponse {
OK(200),
Created(201),
Accepted(202),
NoContent(204),
BadRequest(400),
Unauthorized(401),
Forbidden(403),
NotFound(404),
NotAllowed(405),
Conflict(409),
InternalServerError(500),
NotImplemented(501),
ServiceUnavailable(503),
Unknown(-1);
private final int code;
ServiceResponse(int code) {
this.code = code;
}
public int getStatusCode() {
return code;
}
}
Last updated