Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
chtrembl committed Nov 9, 2023
1 parent 0e9c59f commit 9e112cf
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,22 @@ protected CompletableFuture<Void> onMessageActivity(TurnContext turnContext) {
}
break;
case VIEW_SHOPPING_CART:
dpResponse.setDpResponseText("Once I get your session information, I will be able to display your shopping cart.");
if(azurePetStoreSessionInfo != null)
{
dpResponse = this.azurePetStore.viewCart(azurePetStoreSessionInfo);
}
else {
dpResponse.setDpResponseText("Once I get your session information, I will be able to display your shopping cart.");
}
break;
case PLACE_ORDER:
dpResponse.setDpResponseText("Once I get your session information, I will be able to place your order.");
if(azurePetStoreSessionInfo != null)
{
dpResponse = this.azurePetStore.completeCart(azurePetStoreSessionInfo);
}
else {
dpResponse.setDpResponseText("Once I get your session information, I will be able to place your order.");
}
break;
case SEARCH_FOR_PRODUCTS:
dpResponse = this.azureOpenAI.completion(text, dpResponse.getClassification());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

@Service
public class AzurePetStore implements IAzurePetStore {
Expand All @@ -24,6 +25,9 @@ public class AzurePetStore implements IAzurePetStore {
private OkHttpClient client = new OkHttpClient().newBuilder().build();

private String UPDATE_CART_URL = "https://azurepetstore.com/api/updatecart";
private String VIEW_CART_URL = "https://azurepetstore.com/api/viewcart";
private String COMPLETE_CART_URL = "https://azurepetstore.com/api/completecart";


@Override
public DPResponse updateCart(AzurePetStoreSessionInfo azurePetStoreSessionInfo, String productId) {
Expand Down Expand Up @@ -63,10 +67,68 @@ public DPResponse updateCart(AzurePetStoreSessionInfo azurePetStoreSessionInfo,
return dpResponse;
}

@Override
public DPResponse viewCart(AzurePetStoreSessionInfo azurePetStoreSessionInfo) {
DPResponse dpResponse = new DPResponse();

try {
Request request = new Request.Builder()
.url(this.VIEW_CART_URL)
.method("GET", null)
.addHeader("Cookie", "JSESSIONID=" + azurePetStoreSessionInfo.getSessionID())
.addHeader("Content-Type", "text/html")
.build();

Response response = client.newCall(request).execute();

LOGGER.info("Retrieved cart items for session id: "
+ azurePetStoreSessionInfo.getSessionID() + " csrf: "
+ azurePetStoreSessionInfo.getCsrfToken());

dpResponse.setDpResponseText(response.body().string());

dpResponse.setUpdateCart(true);
} catch (Exception e) {
LOGGER.error("Error retrieving cart items for session id: "
+ azurePetStoreSessionInfo.getSessionID() + " " + e.getMessage());
dpResponse.setDpResponseText("I'm sorry, I wasn't able to retrieve your shopping cart. "+azurePetStoreSessionInfo.getSessionID()+"|"+azurePetStoreSessionInfo.getCsrfToken());
}

return dpResponse;
}

@Override
public DPResponse completeCart(AzurePetStoreSessionInfo azurePetStoreSessionInfo) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'completeCart'");
DPResponse dpResponse = new DPResponse();

try {

MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType,
"_csrf=" + azurePetStoreSessionInfo.getCsrfToken());
Request request = new Request.Builder()
.url(this.UPDATE_CART_URL)
.method("POST", body)
.addHeader("Cookie", "JSESSIONID=" + azurePetStoreSessionInfo.getSessionID())
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.build();

Response response = client.newCall(request).execute();

LOGGER.info("Completed cart for session id: "
+ azurePetStoreSessionInfo.getSessionID() + " csrf: "
+ azurePetStoreSessionInfo.getCsrfToken());

dpResponse.setDpResponseText(response.body().string());

dpResponse.setCompleteCart(true);
} catch (Exception e) {
LOGGER.error("Error completing cart for session id: "
+ azurePetStoreSessionInfo.getSessionID() + " " + e.getMessage());
dpResponse.setDpResponseText("I'm sorry, I wasn't able to place your order. "+azurePetStoreSessionInfo.getSessionID()+"|"+azurePetStoreSessionInfo.getCsrfToken());
}

return dpResponse;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@

public interface IAzurePetStore {
public DPResponse updateCart(AzurePetStoreSessionInfo azurePetStoreSessionInfo, String productId);
public DPResponse completeCart(AzurePetStoreSessionInfo azurePetStoreSessionInfo);
public DPResponse viewCart(AzurePetStoreSessionInfo azurePetStoreSessionInfo);
public DPResponse completeCart(AzurePetStoreSessionInfo azurePetStoreSessionInfo);
}

0 comments on commit 9e112cf

Please sign in to comment.