Skip to content

Commit 097ce5f

Browse files
authored
[CSL-2299] Add on_missing parameter support (#117)
* Add on missing support * Add default
1 parent 4ae89a9 commit 097ce5f

File tree

5 files changed

+127
-10
lines changed

5 files changed

+127
-10
lines changed

constructorio-client/src/main/java/io/constructor/client/CatalogRequest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@
55

66
/** Constructor.io Catalog Request */
77
public class CatalogRequest {
8+
public enum OnMissing {
9+
FAIL,
10+
IGNORE,
11+
CREATE,
12+
}
13+
814
private Map<String, File> files;
915
private String section;
1016
private String notificationEmail;
17+
private OnMissing onMissing;
1118
private Boolean force;
1219

1320
/**
@@ -26,6 +33,24 @@ public CatalogRequest(Map<String, File> files, String section) {
2633

2734
this.files = files;
2835
this.section = section;
36+
this.onMissing = OnMissing.FAIL;
37+
}
38+
39+
/**
40+
* @param onMissing Either "FAIL", "CREATE", "IGNORE", indicating what to do when a missing item
41+
* is uploaded for patch requests. "FAIL" will fail the patch catalog request if an item
42+
* does not exist. "CREATE" will create the item if it does not exist. "IGNORE" will ignore
43+
* items that do not exist. Defaults to "FAIL". Only applicable to patchCatalog requests.
44+
*/
45+
public void setOnMissing(OnMissing onMissing) {
46+
this.onMissing = onMissing;
47+
}
48+
49+
/**
50+
* @return the onMissing strategy
51+
*/
52+
public OnMissing getOnMissing() {
53+
return onMissing;
2954
}
3055

3156
/**

constructorio-client/src/main/java/io/constructor/client/ConstructorIO.java

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -469,13 +469,21 @@ public boolean deleteVariations(
469469
* @param section the section of the autocomplete that you're modifying the item for.
470470
* @param force whether or not the system should process the request even if it will invalidate
471471
* a large number of existing variations.
472+
* @param onMissing Either "FAIL", "IGNORE", "CREATE", indicating how the system will handle
473+
* updating items that don't exist. "FAIL" fails the ingestion if there are items that don't
474+
* exist. "IGNORE" ignores items that don't exist. "CREATE" creates items that don't exist.
475+
* Defaults to "FAIL".
472476
* @param notificationEmail An email address where you'd like to receive an email notification
473477
* in case the task fails.
474478
* @return true if successfully modified
475479
* @throws ConstructorException if the request is invalid.
476480
*/
477481
public boolean updateItems(
478-
ConstructorItem[] items, String section, Boolean force, String notificationEmail)
482+
ConstructorItem[] items,
483+
String section,
484+
Boolean force,
485+
String notificationEmail,
486+
CatalogRequest.OnMissing onMissing)
479487
throws ConstructorException {
480488
try {
481489
HttpUrl url = this.makeUrl(Arrays.asList("v2", "items"));
@@ -492,6 +500,10 @@ public boolean updateItems(
492500
.build();
493501
}
494502

503+
if (onMissing != null && onMissing != CatalogRequest.OnMissing.FAIL) {
504+
url = url.newBuilder().addQueryParameter("on_missing", onMissing.name()).build();
505+
}
506+
495507
Map<String, Object> data = new HashMap<String, Object>();
496508
List<Object> itemsAsJSON = new ArrayList<Object>();
497509
for (ConstructorItem item : items) {
@@ -511,18 +523,36 @@ public boolean updateItems(
511523
}
512524
}
513525

526+
/**
527+
* Updates items from your index.
528+
*
529+
* @param items the items that you're updating
530+
* @param section the section of the autocomplete that you're modifying the item for.
531+
* @param force whether or not the system should process the request even if it will invalidate
532+
* a large number of existing variations.
533+
* @param notificationEmail An email address where you'd like to receive an email notification
534+
* in case the task fails.
535+
* @return true if successfully modified
536+
* @throws ConstructorException if the request is invalid.
537+
*/
538+
public boolean updateItems(
539+
ConstructorItem[] items, String section, Boolean force, String notificationEmail)
540+
throws ConstructorException {
541+
return updateItems(items, section, force, notificationEmail, null);
542+
}
543+
514544
public boolean updateItems(ConstructorItem[] items) throws ConstructorException {
515-
return updateItems(items, "Products", false, null);
545+
return updateItems(items, "Products", false, null, null);
516546
}
517547

518548
public boolean updateItems(ConstructorItem[] items, String section)
519549
throws ConstructorException {
520-
return updateItems(items, section, false, null);
550+
return updateItems(items, section, false, null, null);
521551
}
522552

523553
public boolean updateItems(ConstructorItem[] items, String section, Boolean force)
524554
throws ConstructorException {
525-
return updateItems(items, section, force, null);
555+
return updateItems(items, section, force, null, null);
526556
}
527557

528558
/**
@@ -534,14 +564,19 @@ public boolean updateItems(ConstructorItem[] items, String section, Boolean forc
534564
* a large number of existing variations.
535565
* @param notificationEmail An email address where you'd like to receive an email notification
536566
* in case the task fails.
567+
* @param onMissing Either "FAIL", "IGNORE", "CREATE", indicating how the system will handle
568+
* updating variations that don't exist. "FAIL" fails the ingestion if there are items that
569+
* don't exist. "IGNORE" ignores variations that don't exist. "CREATE" creates items that
570+
* don't exist. Defaults to "FAIL".
537571
* @return true if successfully modified
538572
* @throws ConstructorException if the request is invalid.
539573
*/
540574
public boolean updateVariations(
541575
ConstructorVariation[] variations,
542576
String section,
543577
Boolean force,
544-
String notificationEmail)
578+
String notificationEmail,
579+
CatalogRequest.OnMissing onMissing)
545580
throws ConstructorException {
546581
try {
547582
HttpUrl url = this.makeUrl(Arrays.asList("v2", "variations"));
@@ -557,6 +592,9 @@ public boolean updateVariations(
557592
.addQueryParameter("notification_email", notificationEmail)
558593
.build();
559594
}
595+
if (onMissing != null && onMissing != CatalogRequest.OnMissing.FAIL) {
596+
url = url.newBuilder().addQueryParameter("on_missing", onMissing.name()).build();
597+
}
560598

561599
Map<String, Object> data = new HashMap<String, Object>();
562600
List<Object> variationsAsJSON = new ArrayList<Object>();
@@ -577,19 +615,44 @@ public boolean updateVariations(
577615
}
578616
}
579617

618+
/**
619+
* Update variations from your index.
620+
*
621+
* @param variations the variations that you're updating.
622+
* @param section the section of the autocomplete that you're modifying the item for.
623+
* @param force whether or not the system should process the request even if it will invalidate
624+
* a large number of existing variations.
625+
* @param notificationEmail An email address where you'd like to receive an email notification
626+
* in case the task fails.
627+
* @return true if successfully modified
628+
* @throws ConstructorException if the request is invalid.
629+
*/
630+
public boolean updateVariations(
631+
ConstructorVariation[] variations,
632+
String section,
633+
Boolean force,
634+
String notificationEmail)
635+
throws ConstructorException {
636+
try {
637+
return updateVariations(variations, section, force, notificationEmail, null);
638+
} catch (Exception exception) {
639+
throw new ConstructorException(exception);
640+
}
641+
}
642+
580643
public boolean updateVariations(ConstructorVariation[] variations) throws ConstructorException {
581-
return updateVariations(variations, "Products", false, null);
644+
return updateVariations(variations, "Products", false, null, null);
582645
}
583646

584647
public boolean updateVariations(ConstructorVariation[] variations, String section)
585648
throws ConstructorException {
586-
return updateVariations(variations, section, false, null);
649+
return updateVariations(variations, section, false, null, null);
587650
}
588651

589652
public boolean updateVariations(
590653
ConstructorVariation[] variations, String section, Boolean force)
591654
throws ConstructorException {
592-
return updateVariations(variations, section, force, null);
655+
return updateVariations(variations, section, force, null, null);
593656
}
594657

595658
/**
@@ -2251,6 +2314,9 @@ public String patchCatalog(CatalogRequest req) throws ConstructorException {
22512314
if (force != null) {
22522315
urlBuilder.addQueryParameter("force", Boolean.toString(force));
22532316
}
2317+
if (req.getOnMissing() != CatalogRequest.OnMissing.FAIL) {
2318+
urlBuilder.addQueryParameter("on_missing", req.getOnMissing().name());
2319+
}
22542320

22552321
urlBuilder.addQueryParameter("patch_delta", Boolean.toString(true));
22562322

constructorio-client/src/test/java/io/constructor/client/ConstructorIOCatalogTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,23 @@ public void PatchCatalogWithItemsAndForceShouldReturnTaskInfo() throws Exception
355355
assertTrue("task_status_path exists", jsonObj.has("task_status_path") == true);
356356
}
357357

358+
@Test
359+
public void PatchCatalogWithItemsAndOnMissingShouldReturnTaskInfo() throws Exception {
360+
ConstructorIO constructor = new ConstructorIO(token, apiKey, true, null);
361+
Map<String, File> files = new HashMap<String, File>();
362+
files.put("items", new File("src/test/resources/csv/items.csv"));
363+
364+
CatalogRequest req = new CatalogRequest(files, "Products");
365+
366+
req.setOnMissing(CatalogRequest.OnMissing.CREATE);
367+
368+
String response = constructor.patchCatalog(req);
369+
JSONObject jsonObj = new JSONObject(response);
370+
371+
assertTrue("task_id exists", jsonObj.has("task_id") == true);
372+
assertTrue("task_status_path exists", jsonObj.has("task_status_path") == true);
373+
}
374+
358375
@Test
359376
public void PatchCatalogWithItemsAndVariationsFilesShouldReturnTaskInfo() throws Exception {
360377
ConstructorIO constructor = new ConstructorIO(token, apiKey, true, null);

constructorio-client/src/test/java/io/constructor/client/ConstructorIOItemsTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@ public void updateItemsShouldReturnTrueWithAllParameters() throws Exception {
9595

9696
assertTrue(
9797
"update succeeds",
98-
constructor.updateItems(itemsNew, "Products", true, "test@constructor.io"));
98+
constructor.updateItems(
99+
itemsNew,
100+
"Products",
101+
true,
102+
"test@constructor.io",
103+
CatalogRequest.OnMissing.CREATE));
99104
addItemsToCleanUpArray(itemsNew);
100105
}
101106

constructorio-client/src/test/java/io/constructor/client/ConstructorIOVariationsTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ public void updateVariationsShouldReturnTrueWithAllParameters() throws Exception
107107
assertTrue(
108108
"update succeeds",
109109
constructor.updateVariations(
110-
variationsNew, "Products", true, "test@constructor.io"));
110+
variationsNew,
111+
"Products",
112+
true,
113+
"test@constructor.io",
114+
CatalogRequest.OnMissing.CREATE));
111115
addVariationsToCleanUpArray(variationsNew);
112116
}
113117

0 commit comments

Comments
 (0)