add client for server; do some refactoring

This commit is contained in:
Greg Gauthier 2023-05-25 21:32:24 +01:00
parent 5c25e7c9f8
commit 51f9390cec
6 changed files with 94 additions and 5 deletions

View File

@ -111,6 +111,14 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,68 @@
package com.gmgauthier.client;
import org.json.JSONObject;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class CalculatorClient {
private static final String ROOT_URL = "http://localhost:8000/";
static URI targetURI;
static HttpClient httpClient;
public static void main(String[] args)
throws URISyntaxException, IOException, InterruptedException {
httpClient = HttpClient.newHttpClient();
System.out.println(getSum(10,20));
System.out.println(getProduct(123, 444));
System.out.println(getDifference(846, 233));
System.out.println(getQuotient(999, 4));
}
public static Integer getSum(Integer operanda, Integer operandb)
throws URISyntaxException, IOException, InterruptedException {
URI addUrl = new URI(ROOT_URL + "sum");
JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb});
return (Integer) new JSONObject(makeRequest(addUrl, postJson).body()).get("sum");
}
public static Integer getDifference(Integer operanda, Integer operandb)
throws URISyntaxException, IOException, InterruptedException {
URI addUrl = new URI(ROOT_URL + "difference");
JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb});
return (Integer) new JSONObject(makeRequest(addUrl, postJson).body()).get("difference");
}
public static Integer getProduct(Integer operanda, Integer operandb)
throws URISyntaxException, IOException, InterruptedException {
URI addUrl = new URI(ROOT_URL + "product");
JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb});
return (Integer) new JSONObject(makeRequest(addUrl, postJson).body()).get("product");
}
public static BigDecimal getQuotient(Integer operanda, Integer operandb)
throws URISyntaxException, IOException, InterruptedException {
URI addUrl = new URI(ROOT_URL + "quotient");
JSONObject postJson = new JSONObject().put("values", new Integer[] {operanda, operandb});
return (BigDecimal) new JSONObject(makeRequest(addUrl, postJson).body()).get("quotient");
}
private static HttpResponse<String> makeRequest(URI uri, JSONObject jsonBody)
throws IOException, InterruptedException {
HttpRequest httpRequest = HttpRequest.newBuilder()
.header("Content-Type","application/json")
.uri(uri)
.POST(HttpRequest.BodyPublishers.ofString(String.valueOf(jsonBody)))
.build();
return httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
}
}

View File

@ -1,4 +1,4 @@
package com.gmgauthier;
package com.gmgauthier.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@ -8,8 +8,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
* Spring Boot application starter class
*/
@SpringBootApplication
public class Application {
public class CalculatorServer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
SpringApplication.run(CalculatorServer.class, args);
}
}

View File

@ -1,4 +1,4 @@
package com.gmgauthier.controllers;
package com.gmgauthier.server.controllers;
import org.json.JSONObject;
import org.springframework.http.HttpStatus;

View File

@ -0,0 +1,13 @@
package com.gmgauthier.client.requests;
import org.junit.Test;
public class CalculatorClientTests {
@Test
public void testDummy() throws Exception {
assert true;
}
}

View File

@ -1,4 +1,4 @@
package com.gmgauthier.requests;
package com.gmgauthier.server.requests;
import com.hackerrank.test.utility.OrderedTestRunner;
import com.hackerrank.test.utility.TestWatchman;