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

Fetching products in collections, custom collections, and smart collections #42

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 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.channelape</groupId>
<artifactId>shopify-sdk</artifactId>
<version>2.3.0</version>
<version>2.3.0-SNAPSHOT</version>

<name>Shopify SDK</name>
<description>Java SDK for Shopify REST API.</description>
Expand Down Expand Up @@ -99,7 +99,7 @@
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>

<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
Expand Down Expand Up @@ -349,4 +349,4 @@
</profile>
</profiles>

</project>
</project>
92 changes: 85 additions & 7 deletions src/main/java/com/shopify/ShopifySdk.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.apache.commons.lang3.StringUtils;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import com.github.rholder.retry.Attempt;
Expand Down Expand Up @@ -94,12 +87,21 @@
import com.shopify.model.ShopifyRefundCreationRequest;
import com.shopify.model.ShopifyRefundRoot;
import com.shopify.model.ShopifyShop;
import com.shopify.model.ShopifySmartCollection;
import com.shopify.model.ShopifySmartCollectionRoot;
import com.shopify.model.ShopifySmartCollectionsRoot;
import com.shopify.model.ShopifyTransaction;
import com.shopify.model.ShopifyTransactionsRoot;
import com.shopify.model.ShopifyVariant;
import com.shopify.model.ShopifyVariantMetafieldCreationRequest;
import com.shopify.model.ShopifyVariantRoot;
import com.shopify.model.ShopifyVariantUpdateRequest;
import org.apache.commons.lang3.StringUtils;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ShopifySdk {

Expand All @@ -124,8 +126,10 @@ public class ShopifySdk {
static final String REVOKE = "revoke";
static final String ACCESS_TOKEN = "access_token";
static final String PRODUCTS = "products";
static final String COLLECTIONS = "collections";
static final String VARIANTS = "variants";
static final String CUSTOM_COLLECTIONS = "custom_collections";
static final String SMART_COLLECTIONS = "smart_collections";
static final String RECURRING_APPLICATION_CHARGES = "recurring_application_charges";
static final String ORDERS = "orders";
static final String FULFILLMENTS = "fulfillments";
Expand Down Expand Up @@ -468,12 +472,46 @@ public ShopifyProducts getProducts() {
return new ShopifyProducts(shopifyProducts);
}

public ShopifyPage<ShopifyProduct> getCollectionProducts(final String collectionId, final int pageSize) {
return this.getCollectionProducts(collectionId, null, pageSize);
}

public ShopifyPage<ShopifyProduct> getCollectionProducts(final String collectionId, final String pageInfo, final int pageSize) {
final String productsJson = new StringBuilder().append(PRODUCTS).append(JSON).toString();
final Response response = get(getWebTarget().path(COLLECTIONS).path(collectionId).path(productsJson)
.queryParam(LIMIT_QUERY_PARAMETER, pageSize)
.queryParam(PAGE_INFO_QUERY_PARAMETER, pageInfo));
final ShopifyProductsRoot shopifyProductsRoot = response.readEntity(ShopifyProductsRoot.class);
return mapPagedResponse(shopifyProductsRoot.getProducts(), response);
}

public ShopifyProducts getCollectionProducts(final String collectionId) {
final List<ShopifyProduct> shopifyProducts = new LinkedList<>();

ShopifyPage<ShopifyProduct> shopifyProductsPage = getCollectionProducts(collectionId, DEFAULT_REQUEST_LIMIT);
LOGGER.info("Retrieved {} products for collection {} from first page", shopifyProductsPage.size(), collectionId);
shopifyProducts.addAll(shopifyProductsPage);
while (shopifyProductsPage.getNextPageInfo() != null) {
shopifyProductsPage = getCollectionProducts(collectionId, shopifyProductsPage.getNextPageInfo(), DEFAULT_REQUEST_LIMIT);
LOGGER.info("Retrieved {} products for collection {} from page {}", shopifyProductsPage.size(),
collectionId, shopifyProductsPage.getNextPageInfo());
shopifyProducts.addAll(shopifyProductsPage);
}
return new ShopifyProducts(shopifyProducts);
}

public int getProductCount() {
final Response response = get(getWebTarget().path(PRODUCTS).path(COUNT));
final Count count = response.readEntity(Count.class);
return count.getCount();
}

public ShopifyCustomCollection getCustomCollection(final String collectionId) {
final Response response = get(getWebTarget().path(CUSTOM_COLLECTIONS).path(collectionId));
final ShopifyCustomCollectionRoot shopifyCsutomCollectionRootResponse = response.readEntity(ShopifyCustomCollectionRoot.class);
return shopifyCsutomCollectionRootResponse.getCustomCollection();
}

public ShopifyPage<ShopifyCustomCollection> getCustomCollections(final int pageSize) {
final Response response = get(
getWebTarget().path(CUSTOM_COLLECTIONS).queryParam(LIMIT_QUERY_PARAMETER, pageSize));
Expand Down Expand Up @@ -519,6 +557,46 @@ public ShopifyCustomCollection createCustomCollection(
return shopifyCustomCollectionRootResponse.getCustomCollection();
}

public ShopifySmartCollection getSmartCollection(final String collectionId) {
final Response response = get(getWebTarget().path(SMART_COLLECTIONS).path(collectionId));
final ShopifySmartCollectionRoot shopifySmartCollectionRootResponse = response.readEntity(ShopifySmartCollectionRoot.class);
return shopifySmartCollectionRootResponse.getSmartCollection();
}

public ShopifyPage<ShopifySmartCollection> getSmartCollections(final int pageSize) {
final Response response = get(
getWebTarget().path(SMART_COLLECTIONS).queryParam(LIMIT_QUERY_PARAMETER, pageSize));
final ShopifySmartCollectionsRoot shopifySmartCollectionsRoot = response
.readEntity(ShopifySmartCollectionsRoot.class);
return mapPagedResponse(shopifySmartCollectionsRoot.getSmartCollections(), response);
}

public ShopifyPage<ShopifySmartCollection> getSmartCollections(final String pageInfo, final int pageSize) {
final Response response = get(getWebTarget().path(SMART_COLLECTIONS)
.queryParam(LIMIT_QUERY_PARAMETER, pageSize).queryParam(PAGE_INFO_QUERY_PARAMETER, pageInfo));
final ShopifySmartCollectionsRoot shopifySmartCollectionsRoot = response
.readEntity(ShopifySmartCollectionsRoot.class);
return mapPagedResponse(shopifySmartCollectionsRoot.getSmartCollections(), response);
}

public List<ShopifySmartCollection> getSmartCollections() {
final List<ShopifySmartCollection> shopifySmartCollections = new LinkedList<>();

ShopifyPage<ShopifySmartCollection> smartCollectionsPage = getSmartCollections(DEFAULT_REQUEST_LIMIT);
LOGGER.info("Retrieved {} custom collections from first page", smartCollectionsPage.size());
shopifySmartCollections.addAll(smartCollectionsPage);

while (smartCollectionsPage.getNextPageInfo() != null) {
smartCollectionsPage = getSmartCollections(smartCollectionsPage.getNextPageInfo(),
DEFAULT_REQUEST_LIMIT);
LOGGER.info("Retrieved {} smart collections from page info {}", smartCollectionsPage.size(),
smartCollectionsPage.getNextPageInfo());
shopifySmartCollections.addAll(smartCollectionsPage);
}

return shopifySmartCollections;
}

public ShopifyShop getShop() {
final Response response = get(getWebTarget().path(SHOP));
return response.readEntity(ShopifyShop.class);
Expand Down
142 changes: 142 additions & 0 deletions src/main/java/com/shopify/model/ShopifySmartCollection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package com.shopify.model;

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import com.shopify.model.adapters.DateTimeAdapter;
import org.joda.time.DateTime;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ShopifySmartCollection {
private String id;
private String title;
private String handle;

@XmlElement(name = "body_html")
private String bodyHtml;

@XmlElement(name = "published_at")
@XmlJavaTypeAdapter(DateTimeAdapter.class)
private DateTime publishedAt;

@XmlElement(name = "published_scope")
private String publishedScope;

@XmlElement(name = "updated_at")
@XmlJavaTypeAdapter(DateTimeAdapter.class)
private DateTime updatedAt;

private List<ShopifySmartCollectionRule> rules;
private boolean disjunctive;

@XmlElement(name = "sort_order")
private String sortOrder;

@XmlElement(name = "template_suffix")
private String templateSuffix;

public String getId() {
return id;
}

public ShopifySmartCollection setId(String id) {
this.id = id;
return this;
}

public String getTitle() {
return title;
}

public ShopifySmartCollection setTitle(String title) {
this.title = title;
return this;
}

public String getHandle() {
return handle;
}

public ShopifySmartCollection setHandle(String handle) {
this.handle = handle;
return this;
}

public String getBodyHtml() {
return bodyHtml;
}

public ShopifySmartCollection setBodyHtml(String bodyHtml) {
this.bodyHtml = bodyHtml;
return this;
}

public DateTime getPublishedAt() {
return publishedAt;
}

public ShopifySmartCollection setPublishedAt(DateTime publishedAt) {
this.publishedAt = publishedAt;
return this;
}

public String getPublishedScope() {
return publishedScope;
}

public ShopifySmartCollection setPublishedScope(String publishedScope) {
this.publishedScope = publishedScope;
return this;
}

public DateTime getUpdatedAt() {
return updatedAt;
}

public ShopifySmartCollection setUpdatedAt(DateTime updatedAt) {
this.updatedAt = updatedAt;
return this;
}

public List<ShopifySmartCollectionRule> getRules() {
return rules;
}

public ShopifySmartCollection setRules(List<ShopifySmartCollectionRule> rules) {
this.rules = rules;
return this;
}

public boolean isDisjunctive() {
return disjunctive;
}

public ShopifySmartCollection setDisjunctive(boolean disjunctive) {
this.disjunctive = disjunctive;
return this;
}

public String getSortOrder() {
return sortOrder;
}

public ShopifySmartCollection setSortOrder(String sortOrder) {
this.sortOrder = sortOrder;
return this;
}

public String getTemplateSuffix() {
return templateSuffix;
}

public ShopifySmartCollection setTemplateSuffix(String templateSuffix) {
this.templateSuffix = templateSuffix;
return this;
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/shopify/model/ShopifySmartCollectionRoot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.shopify.model;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class ShopifySmartCollectionRoot {

@XmlElement(name = "smart_collection")
private ShopifySmartCollection smartCollection;

public ShopifySmartCollection getSmartCollection() {
return smartCollection;
}

public ShopifySmartCollectionRoot setSmartCollection(ShopifySmartCollection smartCollection) {
this.smartCollection = smartCollection;
return this;
}
}
34 changes: 34 additions & 0 deletions src/main/java/com/shopify/model/ShopifySmartCollectionRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.shopify.model;

public class ShopifySmartCollectionRule {
private String column;
private String relation;
private String condition;

public String getColumn() {
return column;
}

public ShopifySmartCollectionRule setColumn(String column) {
this.column = column;
return this;
}

public String getRelation() {
return relation;
}

public ShopifySmartCollectionRule setRelation(String relation) {
this.relation = relation;
return this;
}

public String getCondition() {
return condition;
}

public ShopifySmartCollectionRule setCondition(String condition) {
this.condition = condition;
return this;
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/shopify/model/ShopifySmartCollectionsRoot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.shopify.model;

import java.util.LinkedList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class ShopifySmartCollectionsRoot {

@XmlElement(name = "smart_collections")
private List<ShopifySmartCollection> smartCollections = new LinkedList<>();

public List<ShopifySmartCollection> getSmartCollections() {
return smartCollections;
}

public void setSmartCollections(List<ShopifySmartCollection> smartCollections) {
this.smartCollections = smartCollections;
}
}
Loading