Skip to content

Instantly share code, notes, and snippets.

@bgoetzmann
bgoetzmann / SwapiService.java
Created April 13, 2023 18:50
Interface to call SWAPI in restclient application
package odelia.restclient;
import javax.json.JsonObject;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
@bgoetzmann
bgoetzmann / GreetingResource.java
Created April 13, 2023 11:54
REST resource for restclient application
package odelia.restclient;
import javax.inject.Inject;
import javax.json.JsonObject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.eclipse.microprofile.rest.client.inject.RestClient;
SwapiService swapiService =
RestClientBuilder.newBuilder()
.baseUrl(new URL("https://swapi.dev/api"))
.build(SwapiService.class);
JsonObject character = swapiService.getCharacter(id);
JsonObject character = swapiService.getCharacter("1");
String name = character.getString("name");
@Inject
@RestClient
private SwapiService swapiService;
@RegisterRestClient
@Produces(MediaType.APPLICATION_JSON)
public interface SwapiService {
@GET
@Path("/people/{id}")
JsonObject getCharacter(@PathParam("id") String id);
@GET
@Path("/people")
JsonObject getPeople();
def _makeAddItemCommand(Item item) {
{ Cart cart -> cart.addItem(item) }
}
def _makeRemoveItemCommand(Item item) {
{ Cart cart -> cart.removeItem(item) }
}
def actions = [
_makeAddItemCommand(new Item(name: 'Item1', price: 1)),
def fpCart = new Cart()
// Create command
def addItem1 = makeAddItemCommand(fpCart, new Item(name: 'Item1', price: 1))
// Invoke command
addItem1()
assert fpCart.items.keySet() == ['Item1'] as Set
def makeAddItemCommand(Cart cart, Item item) {
{ -> cart.addItem(item) }
}
def makeRemoveItemCommand(Cart cart, Item item) {
{ -> cart.removeItem(item) }
}
class MacroCartCommand implements CartCommand {
def commands = []
MacroCartCommand leftShift(CartCommand command) {
commands << command
this
}
void setReceiver(Cart cart) {
commands*.cart = cart