Skip to content

Instantly share code, notes, and snippets.

@jreece1567
Created February 11, 2016 00:00
Show Gist options
  • Save jreece1567/83d8dc893c387e8f5bfd to your computer and use it in GitHub Desktop.
Save jreece1567/83d8dc893c387e8f5bfd to your computer and use it in GitHub Desktop.
Example of getting store-details for all stores in a country.
/**
*
*/
package com.westfieldlabs.loadtest;
import java.util.ArrayList;
import java.util.List;
import com.westfieldlabs.restsdk.model.store.StoreInstance;
import com.westfieldlabs.restsdk.model.store.StoresListResponse;
import com.westfieldlabs.restsdk.services.RestSdkApiService;
import com.westfieldlabs.restsdk.services.RestSdkApiServiceBuilder;
/**
* @author jreece
*
*/
public class Main {
/**
* The API-key, obtained from the WestfieldLabs 'API' team.
* <p>
* &nbsp;
* </p>
* <b>Note:</b> The API-key hard-coded into this sample application is not
* valid and will not work.
*/
private static String API_KEY = "123456789012345678901234";
/**
* The WestfieldLabs REST API service-facade.
*/
private final RestSdkApiService service;
public Main() {
// create a service-facade, using the API key obtained from
// Westfield. Take the defaults for all other configurable features.
this.service = (new RestSdkApiServiceBuilder.Builder())
.setApiKey(API_KEY).setBaseUrl("https://api.uat.westfield.io")
.build();
}
/**
* @param country
* the country code (us, uk, au, nz)
* @return a List<Integer> containing store-ids
*/
private List<Integer> getStoreIds(final String country) {
final List<Integer> storeIds = new ArrayList<Integer>();
final StoresListResponse response = service.store.getStores(null, null,
country, null, true, null, 1, 100, null, null, null, null,
null, null, null, null);
final List<StoreInstance> stores = response.getData();
for (final StoreInstance store : stores) {
storeIds.add(store.getStore_id());
}
final int count = response.getMeta().getPage_count();
int page = 1;
while (page < count) {
page = page + 1;
final StoresListResponse resp = service.store.getStores(null, null,
country, null, true, null, page, 100, null, null, null,
null, null, null, null, null);
final List<StoreInstance> list = resp.getData();
for (final StoreInstance store : list) {
storeIds.add(store.getStore_id());
}
}
return storeIds;
}
private void getStoresForCountry(final String country) {
final List<Integer> storeIds = getStoreIds(country);
for (final Integer id : storeIds) {
service.store.getStore(id);
}
System.out.println("Fetched details for " + storeIds.size()
+ " stores in country: " + country);
}
/**
* @param args
*/
private void instanceMain(final String[] args) {
getStoresForCountry("nz");
}
/**
* @param args
*/
public static void main(final String[] args) {
if (args.length < 1) {
System.out
.println("Please provide an Api-key on the command-line.");
} else {
API_KEY = args[0]; // set the ApiKey
final Main main = new Main();
main.instanceMain(args);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment