Skip to content

Commit 2d16e5a

Browse files
committed
2 parents 04a2be0 + 2c6f183 commit 2d16e5a

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/ImageClientImpl.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,25 @@
2323

2424
package com.iluwatar.api.gateway;
2525

26+
import static org.slf4j.LoggerFactory.getLogger;
27+
2628
import java.io.IOException;
2729
import java.net.URI;
2830
import java.net.http.HttpClient;
2931
import java.net.http.HttpRequest;
32+
import java.net.http.HttpResponse;
3033
import java.net.http.HttpResponse.BodyHandlers;
34+
35+
import org.slf4j.Logger;
3136
import org.springframework.stereotype.Component;
3237

3338
/**
3439
* An adapter to communicate with the Image microservice.
3540
*/
3641
@Component
3742
public class ImageClientImpl implements ImageClient {
43+
private static final Logger LOGGER = getLogger(ImageClientImpl.class);
44+
3845
/**
3946
* Makes a simple HTTP Get request to the Image microservice.
4047
*
@@ -49,12 +56,26 @@ public String getImagePath() {
4956
.build();
5057

5158
try {
59+
LOGGER.info("Sending request to fetch image path");
5260
var httpResponse = httpClient.send(httpGet, BodyHandlers.ofString());
61+
logResponse(httpResponse);
5362
return httpResponse.body();
5463
} catch (IOException | InterruptedException e) {
55-
e.printStackTrace();
64+
LOGGER.error("Failure occurred while getting image path", e);
5665
}
5766

5867
return null;
5968
}
69+
70+
private void logResponse(HttpResponse<String> httpResponse) {
71+
if (isSuccessResponse(httpResponse.statusCode())) {
72+
LOGGER.info("Image path received successfully");
73+
} else {
74+
LOGGER.warn("Image path request failed");
75+
}
76+
}
77+
78+
private boolean isSuccessResponse(int responseCode) {
79+
return responseCode >= 200 && responseCode <= 299;
80+
}
6081
}

api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/PriceClientImpl.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,26 @@
2323

2424
package com.iluwatar.api.gateway;
2525

26+
import static org.slf4j.LoggerFactory.getLogger;
27+
2628
import java.io.IOException;
2729
import java.net.URI;
2830
import java.net.http.HttpClient;
2931
import java.net.http.HttpRequest;
32+
import java.net.http.HttpResponse;
3033
import java.net.http.HttpResponse.BodyHandlers;
34+
35+
import org.slf4j.Logger;
3136
import org.springframework.stereotype.Component;
3237

38+
3339
/**
3440
* An adapter to communicate with the Price microservice.
3541
*/
3642
@Component
3743
public class PriceClientImpl implements PriceClient {
44+
private static final Logger LOGGER = getLogger(PriceClientImpl.class);
45+
3846
/**
3947
* Makes a simple HTTP Get request to the Price microservice.
4048
*
@@ -49,12 +57,26 @@ public String getPrice() {
4957
.build();
5058

5159
try {
60+
LOGGER.info("Sending request to fetch price info");
5261
var httpResponse = httpClient.send(httpGet, BodyHandlers.ofString());
62+
logResponse(httpResponse);
5363
return httpResponse.body();
5464
} catch (IOException | InterruptedException e) {
55-
e.printStackTrace();
65+
LOGGER.error("Failure occurred while getting price info", e);
5666
}
5767

5868
return null;
5969
}
70+
71+
private void logResponse(HttpResponse<String> httpResponse) {
72+
if (isSuccessResponse(httpResponse.statusCode())) {
73+
LOGGER.info("Price info received successfully");
74+
} else {
75+
LOGGER.warn("Price info request failed");
76+
}
77+
}
78+
79+
private boolean isSuccessResponse(int responseCode) {
80+
return responseCode >= 200 && responseCode <= 299;
81+
}
6082
}

api-gateway/image-microservice/src/main/java/com/iluwatar/image/microservice/ImageController.java

+6
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,20 @@
2323

2424
package com.iluwatar.image.microservice;
2525

26+
import static org.slf4j.LoggerFactory.getLogger;
27+
28+
import org.slf4j.Logger;
2629
import org.springframework.web.bind.annotation.RequestMapping;
2730
import org.springframework.web.bind.annotation.RequestMethod;
2831
import org.springframework.web.bind.annotation.RestController;
2932

33+
3034
/**
3135
* Exposes the Image microservice's endpoints.
3236
*/
3337
@RestController
3438
public class ImageController {
39+
private static final Logger LOGGER = getLogger(ImageController.class);
3540

3641
/**
3742
* An endpoint for a user to retrieve an image path.
@@ -40,6 +45,7 @@ public class ImageController {
4045
*/
4146
@RequestMapping(value = "/image-path", method = RequestMethod.GET)
4247
public String getImagePath() {
48+
LOGGER.info("Successfully found image path");
4349
return "/product-image.png";
4450
}
4551
}

api-gateway/price-microservice/src/main/java/com/iluwatar/price/microservice/PriceController.java

+6
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,20 @@
2323

2424
package com.iluwatar.price.microservice;
2525

26+
import static org.slf4j.LoggerFactory.getLogger;
27+
28+
import org.slf4j.Logger;
2629
import org.springframework.web.bind.annotation.RequestMapping;
2730
import org.springframework.web.bind.annotation.RequestMethod;
2831
import org.springframework.web.bind.annotation.RestController;
2932

33+
3034
/**
3135
* Exposes the Price microservice's endpoints.
3236
*/
3337
@RestController
3438
public class PriceController {
39+
private static final Logger LOGGER = getLogger(PriceController.class);
3540

3641
/**
3742
* An endpoint for a user to retrieve a product's price.
@@ -40,6 +45,7 @@ public class PriceController {
4045
*/
4146
@RequestMapping(value = "/price", method = RequestMethod.GET)
4247
public String getPrice() {
48+
LOGGER.info("Successfully found price info");
4349
return "20";
4450
}
4551
}

0 commit comments

Comments
 (0)