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

com.twilio.sdk.resource.Page Maven / Gradle / Ivy

The newest version!
package com.twilio.sdk.resource;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.twilio.sdk.exception.ApiConnectionException;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

public class Page {
    private final List records;
    private final String firstPageUri;
    private final String nextPageUri;
    private final String previousPageUri;
    private final String uri;
    private final int pageSize;

    private Page(Builder b) {
        this.records = b.records;
        this.firstPageUri = b.firstPageUri;
        this.nextPageUri = b.nextPageUri;
        this.previousPageUri = b.previousPageUri;
        this.uri = b.uri;
        this.pageSize = b.pageSize;
    }

    public List getRecords() {
        return records;
    }

    public String getFirstPageUri() {
        return firstPageUri;
    }

    public String getNextPageUri() {
        return nextPageUri;
    }

    public String getPreviousPageUri() {
        return previousPageUri;
    }

    public int getPageSize() {
        return pageSize;
    }

    public String getUri() {
        return uri;
    }

    /**
     * Create a new page of data from a json blob.
     *
     * @param recordKey key which holds the records
     * @param json json blob
     * @param recordType resource type
     * @param mapper json parser
     * @param  record class type
     * @return a page of records of type T
     */
    public static  Page fromJson(String recordKey, String json, Class recordType, ObjectMapper mapper) {
        try {
            List results = new ArrayList<>();
            JsonNode root = mapper.readTree(json);
            JsonNode records = root.get(recordKey);
            for (final JsonNode record : records) {
                results.add(mapper.readValue(record.toString(), recordType));
            }

            JsonNode uriNode = root.get("uri");
            if (uriNode != null) {
                return buildPage(root, results);
            } else {
                return buildNextGenPage(root, results);
            }

        } catch (final IOException e) {
            throw new ApiConnectionException(
                "Unable to deserialize response: " + e.getMessage() + "\nJSON: " + json, e
            );
        }
    }

    private static  Page buildPage(JsonNode root, List results) {
        Builder builder = new Builder()
            .uri(root.get("uri").asText());

        JsonNode nextPageNode = root.get("next_page_uri");
        if (nextPageNode != null && !nextPageNode.isNull()) {
            builder.nextPageUri(nextPageNode.asText());
        }

        JsonNode previousPageNode = root.get("previous_page_uri");
        if (previousPageNode != null && !previousPageNode.isNull()) {
            builder.previousPageUri(previousPageNode.asText());
        }

        JsonNode firstPageNode = root.get("first_page_uri");
        if (firstPageNode != null && !firstPageNode.isNull()) {
            builder.firstPageUri(firstPageNode.asText());
        }

        JsonNode pageSizeNode = root.get("page_size");
        if (pageSizeNode != null && !pageSizeNode.isNull()) {
            builder.pageSize(pageSizeNode.asInt());
        } else {
            builder.pageSize(results.size());
        }

        return builder.records(results).build();
    }

    private static  Page buildNextGenPage(JsonNode root, List results) {
        JsonNode meta = root.get("meta");
        Builder builder = new Builder()
            .uri(URI.create(meta.get("url").asText()).getPath());

        JsonNode nextPageNode = meta.get("next_page_url");
        if (!nextPageNode.isNull()) {
            builder.nextPageUri(URI.create(nextPageNode.asText()).getPath());
        }

        JsonNode previousPageNode = meta.get("previous_page_url");
        if (!previousPageNode.isNull()) {
            builder.previousPageUri(URI.create(previousPageNode.asText()).getPath());
        }

        JsonNode firstPageNode = meta.get("first_page_url");
        if (!firstPageNode.isNull()) {
            builder.firstPageUri(URI.create(firstPageNode.asText()).getPath());
        }

        JsonNode pageSizeNode = meta.get("page_size");
        if (!pageSizeNode.isNull()) {
            builder.pageSize(pageSizeNode.asInt());
        } else {
            builder.pageSize(results.size());
        }

        return builder.records(results).build();
    }

    private static class Builder {
        private List records;
        private String firstPageUri;
        private String nextPageUri;
        private String previousPageUri;
        private String uri;
        private int pageSize;

        public Builder records(List records) {
            this.records = records;
            return this;
        }

        public Builder firstPageUri(String firstPageUri) {
            this.firstPageUri = firstPageUri;
            return this;
        }

        public Builder nextPageUri(String nextPageUri) {
            this.nextPageUri = nextPageUri;
            return this;
        }

        public Builder previousPageUri(String previousPageUri) {
            this.previousPageUri = previousPageUri;
            return this;
        }

        public Builder uri(String uri) {
            this.uri = uri;
            return this;
        }

        public Builder pageSize(int pageSize) {
            this.pageSize = pageSize;
            return this;
        }

        public Page build() {
            return new Page<>(this);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy