All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lob.model.Postcard Maven / Gradle / Ivy

package com.lob.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.lob.exception.APIException;
import com.lob.exception.AuthenticationException;
import com.lob.exception.InvalidRequestException;
import com.lob.exception.RateLimitException;
import com.lob.net.APIResource;
import com.lob.net.LobResponse;
import com.lob.net.RequestOptions;
import org.joda.time.DateTime;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Postcard extends APIResource {

    public static final String RESOURCE = "postcards";

    @JsonProperty private final String id;
    @JsonProperty private final String description;
    @JsonProperty private final Address to;
    @JsonProperty private final Address from;
    @JsonProperty private final String url;
    @JsonProperty private final String frontTemplateId;
    @JsonProperty private final String backTemplateId;
    @JsonProperty private final String frontTemplateVersionId;
    @JsonProperty private final String backTemplateVersionId;
    @JsonProperty private final String carrier;
    @JsonProperty private final List trackingEvents;
    @JsonProperty private final List thumbnails;
    @JsonProperty private final String size;
    @JsonProperty private final String mailType;
    @JsonProperty private final DateTime expectedDeliveryDate;
    @JsonProperty private final DateTime dateCreated;
    @JsonProperty private final DateTime dateModified;
    @JsonProperty private final DateTime sendDate;
    @JsonProperty private final Map metadata;
    @JsonProperty private final boolean deleted;
    @JsonProperty private final String object;

    @JsonCreator
    public Postcard(
            @JsonProperty("id") final String id,
            @JsonProperty("description") final String description,
            @JsonProperty("to") final Address to,
            @JsonProperty("from") final Address from,
            @JsonProperty("url") final String url,
            @JsonProperty("front_template_id") final String frontTemplateId,
            @JsonProperty("back_template_id") final String backTemplateId,
            @JsonProperty("front_template_version_id") final String frontTemplateVersionId,
            @JsonProperty("back_template_version_id") final String backTemplateVersionId,
            @JsonProperty("carrier") final String carrier,
            @JsonProperty("tracking_events") final List trackingEvents,
            @JsonProperty("thumbnails") final List thumbnails,
            @JsonProperty("size") final String size,
            @JsonProperty("mail_type") final String mailType,
            @JsonProperty("expected_delivery_date") final DateTime expectedDeliveryDate,
            @JsonProperty("date_created") final DateTime dateCreated,
            @JsonProperty("date_modified") final DateTime dateModified,
            @JsonProperty("send_date") final DateTime sendDate,
            @JsonProperty("metadata") final Map metadata,
            @JsonProperty("deleted") final boolean deleted,
            @JsonProperty("object") final String object) {
        this.id = id;
        this.description = description;
        this.to = to;
        this.from = from;
        this.url = url;
        this.frontTemplateId = frontTemplateId;
        this.backTemplateId = backTemplateId;
        this.frontTemplateVersionId = frontTemplateVersionId;
        this.backTemplateVersionId = backTemplateVersionId;
        this.carrier = carrier;
        this.trackingEvents = trackingEvents;
        this.thumbnails = thumbnails;
        this.size = size;
        this.mailType = mailType;
        this.expectedDeliveryDate = expectedDeliveryDate;
        this.dateCreated = dateCreated;
        this.dateModified = dateModified;
        this.sendDate = sendDate;
        this.metadata = metadata;
        this.deleted = deleted;
        this.object = object;
    }

    public String getId() {
        return id;
    }

    public String getDescription() {
        return description;
    }

    public Address getTo() {
        return to;
    }

    public Address getFrom() {
        return from;
    }

    public String getUrl() {
        return url;
    }

    public String getFrontTemplateId() {
        return frontTemplateId;
    }

    public String getBackTemplateId() {
        return backTemplateId;
    }

    public String getFrontTemplateVersionId() {
        return frontTemplateVersionId;
    }

    public String getBackTemplateVersionId() {
        return backTemplateVersionId;
    }

    public String getCarrier() {
        return carrier;
    }

    public List getTrackingEvents() {
        return trackingEvents;
    }

    public List getThumbnails() {
        return thumbnails;
    }

    public String getSize() {
        return size;
    }

    public String getMailType() {
        return mailType;
    }

    public DateTime getExpectedDeliveryDate() {
        return expectedDeliveryDate;
    }

    public DateTime getDateCreated() {
        return dateCreated;
    }

    public DateTime getDateModified() {
        return dateModified;
    }

    public DateTime getSendDate() {
        return sendDate;
    }

    public Map getMetadata() {
        return metadata;
    }

    public boolean isDeleted() {
        return deleted;
    }

    public String getObject() {
        return object;
    }

    @Override
    public String toString() {
        return "Postcard{" +
                "id='" + id + '\'' +
                ", description='" + description + '\'' +
                ", to=" + to +
                ", from=" + from +
                ", url='" + url + '\'' +
                ", frontTemplateId='" + frontTemplateId + '\'' +
                ", backTemplateId='" + backTemplateId + '\'' +
                ", frontTemplateVersionId='" + frontTemplateVersionId + '\'' +
                ", backTemplateVersionId='" + backTemplateVersionId + '\'' +
                ", carrier='" + carrier + '\'' +
                ", trackingEvents='" + trackingEvents + '\'' +
                ", thumbnails='" + thumbnails + '\'' +
                ", size='" + size + '\'' +
                ", mailType='" + mailType + '\'' +
                ", expectedDeliveryDate=" + expectedDeliveryDate +
                ", dateCreated=" + dateCreated +
                ", dateModified=" + dateModified +
                ", sendDate=" + sendDate +
                ", mailType='" + mailType + '\'' +
                ", metadata=" + metadata +
                ", deleted=" + deleted +
                ", object='" + object + '\'' +
                '}';
    }

    public static final class RequestBuilder {
        private Map params = new HashMap();
        private boolean isMultipart = false;

        public RequestBuilder() {
        }

        public RequestBuilder setDescription(String description) {
            params.put("description", description);
            return this;
        }

        public RequestBuilder setTo(String to) {
            params.put("to", to);
            return this;
        }

        public RequestBuilder setTo(Address.RequestBuilder to) {
            params.put("to", to.build());
            return this;
        }

        public RequestBuilder setFrom(String from) {
            params.put("from", from);
            return this;
        }

        public RequestBuilder setFrom(Address.RequestBuilder from) {
            params.put("from", from.build());
            return this;
        }

        public RequestBuilder setFront(String front) {
            params.put("front", front);
            return this;
        }

        public RequestBuilder setFront(File front) {
            isMultipart = true;
            params.put("front", front);
            return this;
        }

        public RequestBuilder setBack(String back) {
            params.put("back", back);
            return this;
        }

        public RequestBuilder setBack(File back) {
            isMultipart = true;
            params.put("back", back);
            return this;
        }

        public RequestBuilder setMergeVariables(Map mergeVariables) {
            params.put("merge_variables", mergeVariables);
            return this;
        }

        public RequestBuilder setSize(String size) {
            params.put("size", size);
            return this;
        }

        public RequestBuilder setMailType(String mailType) {
            params.put("mail_type", mailType);
            return this;
        }

        public RequestBuilder setSendDate(String sendDate) {
            params.put("send_date", sendDate);
            return this;
        }

        public RequestBuilder setSendDate(DateTime sendDate) {
            params.put("send_date", sendDate);
            return this;
        }

        public RequestBuilder setMetadata(Map metadata) {
            params.put("metadata", metadata);
            return this;
        }


        public LobResponse create() throws APIException, IOException, AuthenticationException, InvalidRequestException, RateLimitException {
            return create(null);
        }

        public LobResponse create(RequestOptions options) throws APIException, IOException, AuthenticationException, InvalidRequestException, RateLimitException  {
            if (isMultipart) {
                return request(RequestMethod.POST, RequestType.MULTIPART, RESOURCE, params, Postcard.class, options);
            }

            return request(RequestMethod.POST, RequestType.NORMAL, RESOURCE, params, Postcard.class, options);
        }

    }

    public static LobResponse retrieve(String id) throws APIException, IOException, AuthenticationException, InvalidRequestException, RateLimitException {
        return retrieve(id, null);
    }

    public static LobResponse retrieve(String id, RequestOptions options) throws APIException, IOException, AuthenticationException, InvalidRequestException, RateLimitException {
        return request(RequestMethod.GET, RequestType.NORMAL, String.format("%s/%s", RESOURCE, id), null, Postcard.class, options);
    }

    public static LobResponse list() throws APIException, IOException, AuthenticationException, InvalidRequestException, RateLimitException {
        return list(null, null);
    }

    public static LobResponse list(Map params) throws APIException, IOException, AuthenticationException, InvalidRequestException, RateLimitException {
        return list(params, null);
    }

    public static LobResponse list(RequestOptions options) throws APIException, IOException, AuthenticationException, InvalidRequestException, RateLimitException {
        return list(null, options);
    }

    public static LobResponse list(Map params, RequestOptions options) throws APIException, IOException, AuthenticationException, InvalidRequestException, RateLimitException {
        return request(RequestMethod.GET, RequestType.NORMAL, RESOURCE, params, PostcardCollection.class, options);
    }

    public static LobResponse delete(String id) throws APIException, IOException, AuthenticationException, InvalidRequestException, RateLimitException {
        return delete(id, null);
    }

    public static LobResponse delete(String id, RequestOptions options) throws APIException, IOException, AuthenticationException, InvalidRequestException, RateLimitException {
        return request(RequestMethod.DELETE, RequestType.NORMAL, String.format("%s/%s", RESOURCE, id), null, Postcard.class, options);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy