Skip to content
New issue

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

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

Already on GitHub? # to your account

Add single and multiple customer lookup and support customer search API #17

Merged
merged 8 commits into from
Sep 25, 2019
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
56 changes: 56 additions & 0 deletions src/main/java/com/shopify/ShopifySdk.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@
import com.shopify.model.ShopifyCustomerRoot;
import com.shopify.model.ShopifyCustomerUpdateRequest;
import com.shopify.model.ShopifyCustomerUpdateRoot;
import com.shopify.model.ShopifyCustomersRoot;
import com.shopify.model.ShopifyFulfillment;
import com.shopify.model.ShopifyFulfillmentCreationRequest;
import com.shopify.model.ShopifyFulfillmentRoot;
import com.shopify.model.ShopifyFulfillmentUpdateRequest;
import com.shopify.model.ShopifyGetCustomersRequest;
import com.shopify.model.ShopifyGiftCard;
import com.shopify.model.ShopifyGiftCardCreationRequest;
import com.shopify.model.ShopifyGiftCardRoot;
Expand Down Expand Up @@ -90,6 +92,7 @@
import com.shopify.model.ShopifyVariantRoot;
import com.shopify.model.ShopifyVariantUpdateRequest;


public class ShopifySdk {

private static final String MINIMUM_REQUEST_RETRY_DELAY_CANNOT_BE_LARGER_THAN_MAXIMUM_REQUEST_RETRY_DELAY_MESSAGE = "Maximum request retry delay must be larger than minimum request retry delay.";
Expand Down Expand Up @@ -132,6 +135,9 @@ public class ShopifySdk {
static final String CREATED_AT_MIN_QUERY_PARAMETER = "created_at_min";
static final String CREATED_AT_MAX_QUERY_PARAMETER = "created_at_max";
static final String ATTRIBUTION_APP_ID_QUERY_PARAMETER = "attribution_app_id";
static final String IDS_QUERY_PARAMETER = "ids";
static final String SINCE_ID_QUERY_PARAMETER = "since_id";
static final String QUERY_QUERY_PARAMETER = "query";
static final String CALCULATE = "calculate";
static final String REFUNDS = "refunds";
static final String TRANSACTIONS = "transactions";
Expand Down Expand Up @@ -176,6 +182,7 @@ public class ShopifySdk {
private static final Client CLIENT = buildClient();

private static final String CUSTOMERS = "customers";
private static final String SEARCH = "search";

public static interface OptionalsStep {

Expand Down Expand Up @@ -645,6 +652,45 @@ public ShopifyCustomer updateCustomer(final ShopifyCustomerUpdateRequest shopify
return shopifyCustomerRootResponse.getCustomer();
}

public ShopifyCustomer getCustomerById(final String id) {
final Response response = get(getWebTarget().path(CUSTOMERS).path(id));
final ShopifyCustomerRoot shopifyCustomerRootResponse = response.readEntity(ShopifyCustomerRoot.class);
return shopifyCustomerRootResponse.getCustomer();
}

public List<ShopifyCustomer> getCustomers(final ShopifyGetCustomersRequest shopifyGetCustomersRequest) {
WebTarget target = getWebTarget().path(CUSTOMERS);
if (shopifyGetCustomersRequest.getPage() != 0) {
target = target.queryParam(PAGE_QUERY_PARAMETER, shopifyGetCustomersRequest.getPage());
}
if (shopifyGetCustomersRequest.getLimit() != 0) {
target = target.queryParam(LIMIT_QUERY_PARAMETER, shopifyGetCustomersRequest.getLimit());
} else {
target = target.queryParam(LIMIT_QUERY_PARAMETER, DEFAULT_REQUEST_LIMIT);
}
if (shopifyGetCustomersRequest.getIds() != null) {
target = target.queryParam(IDS_QUERY_PARAMETER, String.join( ",", shopifyGetCustomersRequest.getIds()));
}
if (shopifyGetCustomersRequest.getSinceId() != null) {
target = target.queryParam(SINCE_ID_QUERY_PARAMETER, shopifyGetCustomersRequest.getSinceId());
}
if (shopifyGetCustomersRequest.getCreatedAtMin() != null) {
target = target.queryParam(CREATED_AT_MIN_QUERY_PARAMETER, shopifyGetCustomersRequest.getCreatedAtMin());
}
if (shopifyGetCustomersRequest.getCreatedAtMax() != null) {
target = target.queryParam(CREATED_AT_MAX_QUERY_PARAMETER, shopifyGetCustomersRequest.getCreatedAtMax());
}
Response response = get(target);
return getCustomers(response);
}

public List<ShopifyCustomer> searchCustomer(String query) {
final Response response = get(getWebTarget().path(CUSTOMERS).path(SEARCH)
.queryParam(QUERY_QUERY_PARAMETER, query)
.queryParam(LIMIT_QUERY_PARAMETER, DEFAULT_REQUEST_LIMIT));
return searchCustomer(response);
}

public ShopifyFulfillment cancelFulfillment(final String orderId, final String fulfillmentId) {
final Response response = post(
getWebTarget().path(ORDERS).path(orderId).path(FULFILLMENTS).path(fulfillmentId).path(CANCEL),
Expand Down Expand Up @@ -758,6 +804,16 @@ public ShopifyGiftCard createGiftCard(final ShopifyGiftCardCreationRequest shopi
public String getAccessToken() {
return accessToken;
}

private List<ShopifyCustomer> getCustomers(Response response) {
ShopifyCustomersRoot shopifyCustomersRootResponse = response.readEntity(ShopifyCustomersRoot.class);
return shopifyCustomersRootResponse.getCustomers();
}

private List<ShopifyCustomer> searchCustomer(Response response) {
ShopifyCustomersRoot shopifyCustomersRoot = response.readEntity(ShopifyCustomersRoot.class);
return shopifyCustomersRoot.getCustomers();
}

private ShopifyRefund calculateRefund(final ShopifyRefundCreationRequest shopifyRefundCreationRequest) {
final ShopifyRefundRoot shopifyRefundRoot = new ShopifyRefundRoot();
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/shopify/model/ShopifyCustomersRoot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.shopify.model;

import javax.xml.bind.annotation.XmlRootElement;
import java.util.LinkedList;
import java.util.List;

@XmlRootElement
public class ShopifyCustomersRoot {
public List<ShopifyCustomer> getCustomers() {
return customers;
}

public void setCustomers(List<ShopifyCustomer> customers) {
this.customers = customers;
}

private List<ShopifyCustomer> customers = new LinkedList<>();

}
135 changes: 135 additions & 0 deletions src/main/java/com/shopify/model/ShopifyGetCustomersRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package com.shopify.model;

import org.joda.time.DateTime;

import java.util.List;

public class ShopifyGetCustomersRequest {
private int page;
private int limit;
private List<String> ids;
private String sinceId;
private DateTime createdAtMin;
private DateTime createdAtMax;
public static interface OptionalsStep {
OptionalsStep withPage(int page);

OptionalsStep withLimit(int limit);

OptionalsStep withIds(List<String> ids);

OptionalsStep withSinceId(String sinceId);

OptionalsStep withCreatedAtMin(DateTime createdAtMin);

OptionalsStep withCreatedAtMax(DateTime createdAtMax);

ShopifyGetCustomersRequest build();
}

public static OptionalsStep newBuilder() {
return new Steps();
}

protected ShopifyGetCustomersRequest(final Steps steps) {
if (steps != null) {
this.page = steps.page;
this.limit = steps.limit;
this.ids = steps.ids;
this.sinceId = steps.sinceId;
this.createdAtMin = steps.createdAtMin;
this.createdAtMax = steps.createdAtMax;
}
}

protected static class Steps implements OptionalsStep {
private int page;
private int limit;
private List<String> ids;
private String sinceId;
private DateTime createdAtMin;
private DateTime createdAtMax;

@Override
public ShopifyGetCustomersRequest build() {
return new ShopifyGetCustomersRequest(this);
}
@Override
public OptionalsStep withPage(int page) {
this.page = page;
return this;
}
@Override
public OptionalsStep withLimit(int limit) {
this.limit = limit;
return this;
}
@Override
public OptionalsStep withIds(List<String> ids) {
this.ids = ids;
return this;
}
@Override
public OptionalsStep withSinceId(String sinceId) {
this.sinceId = sinceId;
return this;
}
@Override
public OptionalsStep withCreatedAtMin(DateTime createdAtMin) {
this.createdAtMin = createdAtMin;
return this;
}
@Override
public OptionalsStep withCreatedAtMax(DateTime createdAtMax) {
this.createdAtMax = createdAtMax;
return this;
}
}
public int getPage() {
return page;
}

public void setPage(int page) {
this.page = page;
}

public int getLimit() {
return limit;
}

public void setLimit(int limit) {
this.limit = limit;
}

public List<String> getIds() {
return ids;
}

public void setIds(List<String> ids) {
this.ids = ids;
}

public String getSinceId() {
return sinceId;
}

public void setSinceId(String sinceId) {
this.sinceId = sinceId;
}

public DateTime getCreatedAtMin() {
return createdAtMin;
}

public void setCreatedAtMin(DateTime createdAtMin) {
this.createdAtMin = createdAtMin;
}

public DateTime getCreatedAtMax() {
return createdAtMax;
}

public void setCreatedAtMax(DateTime createdAtMax) {
this.createdAtMax = createdAtMax;
}
}
Loading