task 2 in progress
This commit is contained in:
44
week5_TinsaeGhilay/client/pom.xml
Normal file
44
week5_TinsaeGhilay/client/pom.xml
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>net.tinsae</groupId>
|
||||||
|
<artifactId>client</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>21</maven.compiler.source>
|
||||||
|
<maven.compiler.target>21</maven.compiler.target>
|
||||||
|
<junit.version>5.10.0</junit.version>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>jakarta.enterprise</groupId>
|
||||||
|
<artifactId>jakarta.enterprise.cdi-api</artifactId>
|
||||||
|
<version>4.1.0</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>jakarta.ws.rs</groupId>
|
||||||
|
<artifactId>jakarta.ws.rs-api</artifactId>
|
||||||
|
<version>4.0.0</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
package net.tinsae;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
|
||||||
|
public class Client {
|
||||||
|
private String url;
|
||||||
|
private String table = "/items";
|
||||||
|
|
||||||
|
public Client(String url){
|
||||||
|
this.url = url+table;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final HttpClient client = HttpClient.newHttpClient();
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public int postItem(String json) throws Exception {
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(new URI(url))
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.POST(HttpRequest.BodyPublishers.ofString(json))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
return response.statusCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int putItem(int id, String json) throws Exception {
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(new URI(url + "/" + id))
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.PUT(HttpRequest.BodyPublishers.ofString(json))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
return response.statusCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int deleteItem(int id) throws Exception {
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(new URI(url + "/" + id))
|
||||||
|
.DELETE()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
return response.statusCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public int getItems() throws Exception {
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(new URI(url))
|
||||||
|
.GET()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
System.out.println(response.body());
|
||||||
|
return response.statusCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getItem(int id) throws Exception {
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(new URI(url+"/"+id))
|
||||||
|
.GET()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
System.out.println(response.body());
|
||||||
|
return response.statusCode();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
package net.tinsae;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Delegator {
|
||||||
|
|
||||||
|
private final Client client;
|
||||||
|
private final Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
|
public Delegator(String path) {
|
||||||
|
client = new Client(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void executeCommand(String command) {
|
||||||
|
switch (command) {
|
||||||
|
case "delete" -> delegateDelete();
|
||||||
|
case "insert" -> delegateInsert();
|
||||||
|
case "read" -> delegateRead();
|
||||||
|
case "update" -> delegateUpdate();
|
||||||
|
default -> System.err.println("Unknown command, please repeat");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void delegateDelete() {
|
||||||
|
try {
|
||||||
|
System.out.print("ID of the item you want to delete: ");
|
||||||
|
int id = sc.nextInt();
|
||||||
|
sc.nextLine(); // consume newline
|
||||||
|
int response = client.deleteItem(id);
|
||||||
|
System.out.println("Command executed with response code " + response);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("Error in executing delete command: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void delegateInsert() {
|
||||||
|
try {
|
||||||
|
System.out.print("Name: ");
|
||||||
|
String name = sc.nextLine().strip();
|
||||||
|
|
||||||
|
System.out.print("Description: ");
|
||||||
|
String description = sc.nextLine();
|
||||||
|
|
||||||
|
System.out.print("Price: ");
|
||||||
|
float price = sc.nextFloat();
|
||||||
|
sc.nextLine(); // consume newline
|
||||||
|
|
||||||
|
int response = client.postItem(makeJson(name, description, price));
|
||||||
|
System.out.println("Command executed with response code " + response);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("Error in executing insert command: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String makeJson(String name, String desc, float price) {
|
||||||
|
return String.format("""
|
||||||
|
{
|
||||||
|
"name": "%s",
|
||||||
|
"description": "%s",
|
||||||
|
"price": %s
|
||||||
|
}
|
||||||
|
""", name, desc, price);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void delegateRead() {
|
||||||
|
try {
|
||||||
|
System.out.print("Read All or One? (A/O): ");
|
||||||
|
String input = sc.nextLine();
|
||||||
|
|
||||||
|
if (input.equalsIgnoreCase("A")) {
|
||||||
|
client.getItems();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.print("Enter ID: ");
|
||||||
|
int id = sc.nextInt();
|
||||||
|
sc.nextLine(); // consume newline
|
||||||
|
client.getItem(id);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("Error in executing read command: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void delegateUpdate() {
|
||||||
|
System.out.println("Update not implemented yet");
|
||||||
|
}
|
||||||
|
}
|
||||||
24
week5_TinsaeGhilay/client/src/main/java/net/tinsae/Main.java
Normal file
24
week5_TinsaeGhilay/client/src/main/java/net/tinsae/Main.java
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package net.tinsae;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String path = "http://localhost:8080/shop-1.0-SNAPSHOT/api";
|
||||||
|
Delegator delegator = new Delegator(path);
|
||||||
|
System.out.println("Hello! What do you want to do? allowed actions are \n"+
|
||||||
|
"Insert, update, delete and read");
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
while(sc.hasNext()){
|
||||||
|
String response = sc.nextLine().strip();
|
||||||
|
if(response.equalsIgnoreCase("exit")){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
delegator.executeCommand(response);
|
||||||
|
|
||||||
|
}
|
||||||
|
sc.close();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
week5_TinsaeGhilay/client/target/classes/net/tinsae/Client.class
Normal file
BIN
week5_TinsaeGhilay/client/target/classes/net/tinsae/Client.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
week5_TinsaeGhilay/client/target/classes/net/tinsae/Main.class
Normal file
BIN
week5_TinsaeGhilay/client/target/classes/net/tinsae/Main.class
Normal file
Binary file not shown.
BIN
week5_TinsaeGhilay/client/target/client-1.0-SNAPSHOT.jar
Normal file
BIN
week5_TinsaeGhilay/client/target/client-1.0-SNAPSHOT.jar
Normal file
Binary file not shown.
@@ -0,0 +1,3 @@
|
|||||||
|
artifactId=client
|
||||||
|
groupId=net.tinsae
|
||||||
|
version=1.0-SNAPSHOT
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
net/tinsae/Client.class
|
||||||
|
net/tinsae/Main.class
|
||||||
|
net/tinsae/Delegator.class
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
/home/tgk/Repos/Trusted/DistributedSystems/week5_TinsaeGhilay/client/src/main/java/net/tinsae/Client.java
|
||||||
|
/home/tgk/Repos/Trusted/DistributedSystems/week5_TinsaeGhilay/client/src/main/java/net/tinsae/Delegator.java
|
||||||
|
/home/tgk/Repos/Trusted/DistributedSystems/week5_TinsaeGhilay/client/src/main/java/net/tinsae/Main.java
|
||||||
Reference in New Issue
Block a user