Java

View on Github | Download

String apiKey = "****";
String apiSecret = "****";
SailthruClient client = new SailthruClient(apiKey, apiSecret);

//submit update job
try {
    UpdateJob job = new UpdateJob();
    job.setReportEmail("support@sailthru.com");
    job.setFile("/tmp/test.json");
    JsonResponse response = client.processUpdateJob(job);
    if (response.isOK()) {
        // do something here
    } else {
        // this will probably not happen because all API related errors are handled by ApiException class
    }
} catch (ApiException e) {
    // handle API exceptions here
} catch (IOException e) {
    // handle some unexpected, mostly non-API related exceptions here
}
try {
            Purchase purchase = new Purchase();
            PurchaseItem firstpurchaseitem = new PurchaseItem(1, "example purchase item", 1999, "example id", "http://www.sailthru.com/example/purchase/url");
            List items = new ArrayList();
            items.add(firstpurchaseitem);
            purchase.setItems(items);

            Map adjustmentItem = new HashMap();
            adjustmentItem.put("title", "bar");
            adjustmentItem.put("price", 1000);
            ArrayList adjustments = new ArrayList();
            adjustments.add(adjustmentItem);
            purchase.setAdjustments(adjustments);

            Map TenderItem = new HashMap();
            TenderItem.put("title", "bar");
            TenderItem.put("price", 1000);
            ArrayList tenders = new ArrayList();
            tenders.add(TenderItem);
            purchase.setTenders(tenders);

            Map vars = new HashMap();
            vars.put("name", "Prajwal Tuladhar");
            Map addressVars = new HashMap();
            addressVars.put("state", "NY");
            addressVars.put("city", "Jackson Heights");
            addressVars.put("zip", "11372");
            vars.put("address", addressVars);

            purchase.setPurchaseLevelVars(vars);

            purchase.setSendTemplate("test");

            purchase.setEmail("support@sailthru.com");

            JsonResponse response = client.purchase(purchase);
            if (response.isOK()) {
                System.out.println(response.getResponse());
            } else {
                System.out.println(response.getResponse().get("error").toString());
            }
        } catch (ApiException e) {
            // handle exception
        } catch (IOException e) {
            // handle exception
        }

Rate Limits

LastRateLimitInfo lastRateLimitInfo = sailthruClient.getLastRateLimitInfo(ApiAction.user, AbstractSailthruClient.HttpRequestMethod.POST);

// getLastRateLimitInfo returns null if given endpoint/method wasn't triggered previously
int limit, remaining;
Date reset;
if (lastRateLimitInfo != null) {
    limit = lastRateLimitInfo.getLimit();
    remaining = lastRateLimitInfo.getRemaining();
    reset = lastRateLimitInfo.getReset();

    // throttle api calls based on last rate limit info
    if (remaining <= 0) {
        long milliseconds_till_reset = reset.getTime() - System.currentTimeMillis();
        // sleep or perform other business logic before next api call
        try {
            Thread.sleep(milliseconds_till_reset); 
        catch (InterruptedException e) {
            // handle interruption
        }
    }
}
Top