Skip to content

Commit 998854c

Browse files
committed
Split the api client to smaller parts
1 parent 7ce8b88 commit 998854c

File tree

4 files changed

+299
-254
lines changed

4 files changed

+299
-254
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package nl.ikoodi.external
2+
3+
import nl.ikoodi.bol.openapi.OpenApiClient
4+
import spock.lang.Specification
5+
6+
class OpenApiUserSpec extends Specification {
7+
8+
private String apiKey
9+
10+
def setup() {
11+
apiKey = System.getenv('OPENAPI_KEY')
12+
if (!apiKey) {
13+
apiKey = System.getProperty('OPENAPI_KEY')
14+
}
15+
Objects.requireNonNull(apiKey, '''
16+
Running from your IDE? Set the OPENAPI_KEY property to your OpenAPI API key
17+
Running with Gradle, then the 'openapi.properties' should exist in the root project directory with your OpenAPI API key'''.stripIndent()
18+
)
19+
}
20+
21+
def 'Can use searchBuilder outside the api package'() {
22+
def openApi = OpenApiClient.withDefaultClient(apiKey)
23+
24+
expect:
25+
def results = openApi.searchBuilder().term('harry potter').search()
26+
results.products
27+
}
28+
29+
def 'Can use listBuilder outside the api package'() {
30+
def openApi = OpenApiClient.withDefaultClient(apiKey)
31+
32+
expect:
33+
def results = openApi.listBuilder().list()
34+
results.products
35+
}
36+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package nl.ikoodi.bol.openapi;
2+
3+
import com.bol.api.openapi_4_0.ListResults;
4+
5+
import static nl.ikoodi.bol.openapi.QueryOfferType.OfferType.*;
6+
import static nl.ikoodi.bol.openapi.QueryProductListType.ProductListType.*;
7+
import static nl.ikoodi.bol.openapi.QuerySortingMethod.SortingBy.PRICE;
8+
import static nl.ikoodi.bol.openapi.QuerySortingMethod.SortingOrder.ASCENDING;
9+
import static nl.ikoodi.bol.openapi.QuerySortingMethod.SortingOrder.DESCENDING;
10+
11+
class ListBuilder {
12+
private static final int OFFSET_DEFAULT = 0;
13+
private static final int LIMIT_DEFAULT = 10;
14+
private final OpenApiClient client;
15+
private QueryProductListType productListType = QueryProductListType.builder().list(DEFAULT);
16+
private QueryCategoryId.Builder categoryIds = QueryCategoryId.builder();
17+
private QueryDataType.Builder dataTypes = QueryDataType.builder();
18+
private QueryOfferType.Builder offerTypes = QueryOfferType.builder();
19+
private QuerySortingMethod sortingMethod = QuerySortingMethod.builder().by(PRICE).order(ASCENDING);
20+
private QueryOffset offset = QueryOffset.builder().offset(OFFSET_DEFAULT);
21+
private QueryLimit limit = QueryLimit.builder().limit(LIMIT_DEFAULT);
22+
private QueryIncludeAttribute includeAttributes = QueryIncludeAttribute.builder().exclude();
23+
24+
ListBuilder(OpenApiClient client) {
25+
this.client = client;
26+
}
27+
28+
ListBuilder(ListBuilder builder) {
29+
this.client = builder.client;
30+
this.productListType = builder.productListType;
31+
this.categoryIds = builder.categoryIds;
32+
this.dataTypes = builder.dataTypes;
33+
this.offerTypes = builder.offerTypes;
34+
this.sortingMethod = builder.sortingMethod;
35+
this.offset = builder.offset;
36+
this.limit = builder.limit;
37+
this.includeAttributes = builder.includeAttributes;
38+
}
39+
40+
public ListBuilder defaultTopSelling() {
41+
productListType = QueryProductListType.builder().list(DEFAULT);
42+
return new ListBuilder(this);
43+
}
44+
45+
public ListBuilder lastWeeksTopSelling() {
46+
productListType = QueryProductListType.builder().list(LAST_WEEK);
47+
return new ListBuilder(this);
48+
}
49+
50+
public ListBuilder lastTwoMonthsTopSelling() {
51+
productListType = QueryProductListType.builder().list(LAST_TWO_MONTHS);
52+
return new ListBuilder(this);
53+
}
54+
55+
public ListBuilder newTopSelling() {
56+
productListType = QueryProductListType.builder().list(QueryProductListType.ProductListType.NEW);
57+
return new ListBuilder(this);
58+
}
59+
60+
public ListBuilder preOrderTopSelling() {
61+
productListType = QueryProductListType.builder().list(PRE_ORDER);
62+
return new ListBuilder(this);
63+
}
64+
65+
public ListBuilder category(String id) {
66+
categoryIds.add(id);
67+
return new ListBuilder(this);
68+
}
69+
70+
public ListBuilder allOffers() {
71+
offerTypes.add(ALL);
72+
return new ListBuilder(this);
73+
}
74+
75+
public ListBuilder cheapestOffer() {
76+
offerTypes.add(CHEAPEST);
77+
return new ListBuilder(this);
78+
}
79+
80+
public ListBuilder bolOffer() {
81+
offerTypes.add(BOLCOM);
82+
return new ListBuilder(this);
83+
}
84+
85+
public ListBuilder newOffer() {
86+
offerTypes.add(QueryOfferType.OfferType.NEW);
87+
return new ListBuilder(this);
88+
}
89+
90+
public ListBuilder secondHandOffer() {
91+
offerTypes.add(SECONDHAND);
92+
return new ListBuilder(this);
93+
}
94+
95+
public ListBuilder sort(QuerySortingMethod.SortingBy by) {
96+
sortingMethod = QuerySortingMethod.builder().by(by).order(ASCENDING);
97+
return new ListBuilder(this);
98+
}
99+
100+
public ListBuilder sortDescending(QuerySortingMethod.SortingBy by) {
101+
sortingMethod = QuerySortingMethod.builder().by(by).order(DESCENDING);
102+
return new ListBuilder(this);
103+
}
104+
105+
public ListBuilder offset(int amount) {
106+
offset = QueryOffset.builder().offset(amount);
107+
return new ListBuilder(this);
108+
}
109+
110+
public ListBuilder limit(int amount) {
111+
limit = QueryLimit.builder().limit(amount);
112+
return new ListBuilder(this);
113+
}
114+
115+
public ListBuilder includeAttributes() {
116+
includeAttributes = QueryIncludeAttribute.builder().include();
117+
return new ListBuilder(this);
118+
}
119+
120+
public ListResults list() {
121+
return client.getApi().list(
122+
productListType
123+
, categoryIds.create()
124+
, dataTypes.create()
125+
, offerTypes.create()
126+
, sortingMethod
127+
, offset
128+
, limit
129+
, includeAttributes
130+
);
131+
}
132+
}

0 commit comments

Comments
 (0)