Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce "product details page" api endpoint support #54

Merged
merged 12 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
name: Submit a New Feature Pull Request
about: Introduce a new feature, refactor code or fix a bug with a pull request
title: 'Feature(<location>): Refactor(<location>): Fix(<location>):'
labels: ''
assignees: ''

---
<!-- Thanks for submitting a PR! -->
## Purpose of this Pull Request
<!-- ie 'addresses issue #7689075'. Be sure to preface the pr title appropriately with this purpose in mind -->

## How does this Pull Request address its purpose?
<!-- how are your code changes pertinent to this PR's goal? -->

## Iterate what tests have been added to maintain complete code coverage.
<!-- ie 'unit tests for the new "BatCarLocation" class are added in the new test class of the same name' -->



<!-- note: the sign of consistently well written pull requests are ones that don't require further discussion from
reviewers. Make everything the reviewer will want to see prior to approval easy to find and easy to
understand your rationale -->
1 change: 1 addition & 0 deletions .github/workflows/mvn-package_pr,push_main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main", "release/*"]

jobs:
build:
Expand Down
23 changes: 2 additions & 21 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.${owner}</groupId>
<artifactId>threshr</artifactId>
<version>0.0.4</version>
<version>0.0.5-SNAPSHOT</version>
<packaging>${packaging}</packaging>

<parent>
Expand Down Expand Up @@ -44,6 +44,7 @@
<url>https://repo.maven.apache.org/maven2</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
Expand All @@ -64,31 +65,11 @@
<artifactId>micronaut-retry</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.data</groupId>
<artifactId>micronaut-data-jdbc</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.flyway</groupId>
<artifactId>micronaut-flyway</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.serde</groupId>
<artifactId>micronaut-serde-jackson</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.sql</groupId>
<artifactId>micronaut-jdbc-hikari</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
Expand Down
22 changes: 3 additions & 19 deletions src/main/java/com/graqr/threshr/ThreshrClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import com.graqr.threshr.model.TargetStore;
import com.graqr.threshr.model.TargetStorePdpSearch;
import com.graqr.threshr.model.Tcin;
import com.graqr.threshr.model.redsky.products.pdp.client.pdpClientRoot;
import com.graqr.threshr.model.redsky.products.plp.search.plpSearchRoot;
import com.graqr.threshr.model.redsky.products.pdp.client.PdpClientRoot;
import com.graqr.threshr.model.redsky.products.summary.ProductSummaryRoot;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.annotation.Get;
Expand Down Expand Up @@ -36,7 +35,7 @@
@Client(id = "redsky-api")
@Header(name = USER_AGENT, value = "Micronaut HTTP Client")
@Header(name = ACCEPT, value = "application/vnd.github.v3+json, application/json")
public interface ThreshrClient {
interface ThreshrClient {


/**
Expand All @@ -58,21 +57,6 @@ HttpResponse<ProductSummaryRoot> productSummaryWithFulfillment(
Tcin tcins);

/**
* Submits a search query to target's redsky api. {@link plpSearchRoot} results
* include pricing.
* <p>
* Uses the following environment variables (See {@link ThreshrClient}).
* <ul>
* <li>THRESHR_KEY</li>
* <li>THRESHR_VISITOR</li>
* <li>THRESHR_CATEGORY</li>
* <li>THRESHR_CHANNEL</li>
* <li>THRESHR_NEW-SEARCH</li>
* <li>THRESHR_OFFSET</li>
* <li>THRESHR_PAGE</li>
* <li>THRESHR_PLATFORM</li>
* </ul>
*
* @param tcin tcin ID's for products to query
* @param targetStorePdpSearch store from which the product summaries are to be queried.
*/
Expand All @@ -81,7 +65,7 @@ HttpResponse<ProductSummaryRoot> productSummaryWithFulfillment(
"?key=${threshr.key}" +
"{&tcin}" +
"{&targetStorePdpSearch*}")
HttpResponse<pdpClientRoot> pdpClient(
HttpResponse<PdpClientRoot> productDetails(
TargetStorePdpSearch targetStorePdpSearch,
@Pattern(regexp = "(\\d{8})|(\\d{9})")
String tcin);
Expand Down
37 changes: 32 additions & 5 deletions src/main/java/com/graqr/threshr/ThreshrController.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,53 @@
package com.graqr.threshr;

import com.graqr.threshr.model.TargetStore;
import com.graqr.threshr.model.TargetStorePdpSearch;
import com.graqr.threshr.model.Tcin;
import com.graqr.threshr.model.redsky.products.summary.ProductSummaryRoot;
import com.graqr.threshr.model.redsky.products.Product;
import com.graqr.threshr.model.redsky.products.ProductSummary;
import com.graqr.threshr.model.redsky.products.pdp.client.PdpClientRoot;
import io.micronaut.core.async.annotation.SingleResult;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import jakarta.inject.Inject;

import java.util.List;

@Controller("/product")
public class ThreshrController {

@Inject
ThreshrClient threshrClient;


@Get("/summary")
@Get("/summary-with-fulfillment")
@SingleResult
public HttpResponse<ProductSummaryRoot> fetchProductSummaries(
public List<ProductSummary> fetchProductSummariesWithFulfillment(
TargetStore targetStore,
Tcin tcin) {
return threshrClient.productSummaryWithFulfillment(targetStore, tcin);
return threshrClient
.productSummaryWithFulfillment(targetStore, tcin)
.body()
.data()
.productSummary();
}

public List<ProductSummary> fetchProductSummariesWithFulfillment(
TargetStore targetStore,
String... tcin) throws ThreshrException {
return fetchProductSummariesWithFulfillment(targetStore,
new Tcin(tcin));
}

@Get("/details")
@SingleResult
public Product fetchProductDetails(TargetStore targetStore, String tcin) throws ThreshrException {
HttpResponse<PdpClientRoot> response = threshrClient.productDetails(new TargetStorePdpSearch(targetStore), tcin);
if (response.code() != 200) {
throw new ThreshrException("Query from pdp client endpoint didn't return 200.\n" + response.reason());
}
return response.body()
.data()
.product();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
@Serdeable
public record Product(
@JsonProperty("__typename") String typename,
String tcin, Category category,
@JsonProperty("ratings_and_reviews") RatingsAndReviews ratingsAndReviews, Item item,
String tcin,
Category category,
@JsonProperty("ratings_and_reviews") RatingsAndReviews ratingsAndReviews,
Item item,
@JsonProperty("finds_stories")
List<FindsStory> findsStories,
@JsonProperty("finds_posts")
List<FindsPost> findsPosts,
Price price, List<?> promotions) {
Price price,
List<?> promotions) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
import io.micronaut.serde.annotation.Serdeable;

@Serdeable
public record pdpClientRoot(Data data) {
public record PdpClientRoot(Data data) {
}
14 changes: 1 addition & 13 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,4 @@ micronaut:
---
threshr:
key: 9f36aeafbe60771e321a7cc95a78140772ab3e96
CHANNEL: WEB
---
datasources:
default:
url: jdbc:h2:mem:default;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
username: sa
password: ""
driverClassName: org.h2.Driver
---
flyway:
datasources:
default:
enabled: true
CHANNEL: WEB
4 changes: 2 additions & 2 deletions src/test/groovy/com/graqr/threshr/ThreshrClientTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.graqr.threshr


import com.graqr.threshr.model.TargetStorePdpSearch
import com.graqr.threshr.model.redsky.products.pdp.client.pdpClientRoot
import com.graqr.threshr.model.redsky.products.pdp.client.PdpClientRoot
import com.graqr.threshr.model.redsky.products.summary.ProductSummaryRoot
import io.micronaut.http.HttpResponse
/**
Expand All @@ -24,7 +24,7 @@ class ThreshrClientTest extends ThreshrTest {

void "no error calling pdp client search"() {
when:
HttpResponse<pdpClientRoot> response = threshrClient.pdpClient(
HttpResponse<PdpClientRoot> response = threshrClient.productDetails(
new TargetStorePdpSearch(targetStore),
tcin.getTcins().split(",")[0])

Expand Down
34 changes: 31 additions & 3 deletions src/test/groovy/com/graqr/threshr/ThreshrControllerTest.groovy
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
package com.graqr.threshr


import com.graqr.threshr.model.Tcin
import io.micronaut.test.extensions.spock.annotation.MicronautTest

@MicronautTest
class ThreshrControllerTest extends ThreshrTest {

void "query product summaries with no error"() {
void "query product summaries with no error with tcin string(s)"() {
when:
threshrController.fetchProductSummariesWithFulfillment(targetStore, tcinArg)

then:
noExceptionThrown()

where:
tcinArg | _
tcin.getTcins().split(",")[0] | _
tcin.getTcins().split(",") | _
}

void "query product summaries with no error tcin type arg"() {
when:
threshrController.fetchProductSummariesWithFulfillment(targetStore, tcinArg)

then:
noExceptionThrown()

where:
tcinArg | _
tcin | _
new Tcin(tcin.getTcins().split(",")[0]) | _
}

void "query product details with no error"() {
when:
threshrController.fetchProductSummaries(targetStore, tcin)
threshrController.fetchProductDetails(targetStore,
tcin.getTcins().split(",")[0]
)

then:
noExceptionThrown()
Expand Down
21 changes: 1 addition & 20 deletions src/test/groovy/com/graqr/threshr/ThreshrTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ package com.graqr.threshr

import com.graqr.threshr.model.TargetStore
import com.graqr.threshr.model.Tcin
import io.micronaut.context.ApplicationContext
import io.micronaut.context.env.Environment
import io.micronaut.http.HttpResponse
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Shared
import spock.lang.Specification

import java.util.function.Predicate

@MicronautTest
class ThreshrTest extends Specification {

Expand All @@ -23,10 +18,7 @@ class ThreshrTest extends Specification {
@Shared
ThreshrClient threshrClient

@Shared
ApplicationContext ctx

@Shared
@Shared // TODO configure test data to be provided, not hardcoded
TargetStore targetStore = new TargetStore(
"1750",
"UT",
Expand All @@ -39,15 +31,4 @@ class ThreshrTest extends Specification {
new String[]{"82691535", "12953464"} //corn & coke https://bit.ly/45V8dui https://bit.ly/40j4A0e
)

@Shared
Predicate<HttpResponse<?>> okResponse = response -> response.code() >= 200 && response.code() < 300

void setupSpec() {
ctx = ApplicationContext.run(Environment.CLI, Environment.TEST)
}

void cleanupSpec() {
ctx.stop()
}

}
Loading