Skip to content
Open
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
67 changes: 67 additions & 0 deletions src/main/java/com/twilio/base/CreatorV1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.twilio.base;

import com.twilio.Twilio;
import com.twilio.http.TwilioRestClient;

import java.util.concurrent.CompletableFuture;

/**
* Executor for creation of a resource.
*
* @param <T> type of the resource
*/
public abstract class CreatorV1<T extends Resource> {

/**
* Execute an async request using default client.
*
* @return future that resolves to requested object
*/
public CompletableFuture<T> createAsync() {
return createAsync(Twilio.getRestClient());
}

/**
* Execute an async request using specified client.
*
* @param client client used to make request
* @return future that resolves to requested object
*/
public CompletableFuture<T> createAsync(final TwilioRestClient client) {
return CompletableFuture.supplyAsync(() -> create(client), Twilio.getExecutorService());
}

/**
* Execute a request using default client.
*
* @return Requested object
*/
public T create() {
return create(Twilio.getRestClient());
}

/**
* Execute a request using specified client.
*
* @param client client used to make request
* @return Requested object
*/
public abstract T create(final TwilioRestClient client);

/**
* Execute a request and return result with response metadata (headers, status code).
*
* @return ResponseMetadata containing resource and HTTP metadata
*/
public ResponseMetadata<T> createWithMetadata() {
return createWithMetadata(Twilio.getRestClient());
}

/**
* Execute a request using specified client and return result with response metadata.
*
* @param client client used to make request
* @return ResponseMetadata containing resource and HTTP metadata
*/
public abstract ResponseMetadata<T> createWithMetadata(final TwilioRestClient client);
}
117 changes: 117 additions & 0 deletions src/main/java/com/twilio/base/PageMetadata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.twilio.base;

import com.twilio.http.TwilioRestClient;
import java.util.Iterator;
import java.util.Map;

/**
* Wrapper around ResourceSet that includes HTTP response metadata from first page.
* Provides access to headers and status code while supporting pagination.
*
* @param <E> type of the resource
*/
public class PageMetadata<E extends Resource> implements Iterable<E> {

private final ResourceSet<E> resourceSet;
private final Map<String, String> headers;
private final int statusCode;

/**
* Create page metadata wrapper.
*
* @param resourceSet the resource set with pagination
* @param statusCode HTTP status code from first page
* @param headers HTTP headers from first page
*/
public PageMetadata(
final ResourceSet<E> resourceSet,
final int statusCode,
final Map<String, String> headers
) {
this.resourceSet = resourceSet;
this.statusCode = statusCode;
this.headers = headers;
}

/**
* Get HTTP headers from first page response.
*
* @return map of header name to value
*/
public Map<String, String> getHeaders() {
return headers;
}

/**
* Get HTTP status code from first page response.
*
* @return status code
*/
public int getStatusCode() {
return statusCode;
}

/**
* Get specific header value.
*
* @param headerName name of the header
* @return header value or null if not present
*/
public String getHeader(final String headerName) {
return headers != null ? headers.get(headerName) : null;
}

/**
* Get underlying ResourceSet for advanced operations.
*
* @return the wrapped resource set
*/
public ResourceSet<E> getResourceSet() {
return resourceSet;
}

public boolean isAutoPaging() {
return resourceSet.isAutoPaging();
}

public PageMetadata<E> setAutoPaging(final boolean autoPaging) {
resourceSet.setAutoPaging(autoPaging);
return this;
}

public Integer getPageSize() {
return resourceSet.getPageSize();
}

public PageMetadata<E> setPageSize(final int pageSize) {
resourceSet.setPageSize(pageSize);
return this;
}

public Long getLimit() {
return resourceSet.getLimit();
}

public PageMetadata<E> setLimit(final long limit) {
resourceSet.setLimit(limit);
return this;
}

public long getPageLimit() {
return resourceSet.getPageLimit();
}

@Override
public Iterator<E> iterator() {
return resourceSet.iterator();
}

@Override
public String toString() {
return "PageMetadata{" +
"statusCode=" + statusCode +
", headers=" + headers +
", resourceSet=" + resourceSet +
'}';
}
}
175 changes: 175 additions & 0 deletions src/main/java/com/twilio/base/ReaderV1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package com.twilio.base;

import com.twilio.Twilio;
import com.twilio.http.TwilioRestClient;
import com.twilio.rest.api.v2010.account.Message;

import java.util.concurrent.CompletableFuture;

/**
* Executor for listing of a resource.
*
* @param <T> type of the resource
*/
public abstract class ReaderV1<T extends Resource> {

private Integer pageSize;
private Long limit;

/**
* Execute a request using default client.
*
* @return ResourceSet of objects
*/
public ResourceSet<T> read() {
return read(Twilio.getRestClient());
}

/**
* Execute a request using specified client.
*
* @param client client used to make request
* @return ResourceSet of objects
*/
public abstract ResourceSet<T> read(final TwilioRestClient client);

/**
* Execute an async request using default client.
*
* @return future that resolves to the ResourceSet of objects
*/
public CompletableFuture<ResourceSet<T>> readAsync() {
return readAsync(Twilio.getRestClient());
}

/**
* Execute an async request using specified client.
*
* @param client client used to make request
* @return future that resolves to the ResourceSet of objects
*/
public CompletableFuture<ResourceSet<T>> readAsync(final TwilioRestClient client) {
return CompletableFuture.supplyAsync(() -> read(client), Twilio.getExecutorService());
}

/**
* Fetch the first page of resources.
*
* @return Page containing the first pageSize of resources
*/
public Page<T> firstPage() {
return firstPage(Twilio.getRestClient());
}

/**
* Fetch the first page of resources using specified client.
*
* @param client client used to fetch
* @return Page containing the first pageSize of resources
*/
public abstract Page<T> firstPage(final TwilioRestClient client);

/**
* Retrieve the target page of resources.
*
* @param targetUrl API-generated URL for the requested results page
* @return Page containing the target pageSize of resources
*/
public Page<T> getPage(final String targetUrl) {
return getPage(targetUrl, Twilio.getRestClient());
}

/**
* Retrieve the target page of resources.
*
* @param targetUrl API-generated URL for the requested results page
* @param client client used to fetch
* @return Page containing the target pageSize of resources
*/
public abstract Page<T> getPage(final String targetUrl, final TwilioRestClient client);

/**
* Fetch the following page of resources.
*
* @param page current page of resources
* @return Page containing the next pageSize of resources
*/
public Page<T> nextPage(final Page<T> page) {
return nextPage(page, Twilio.getRestClient());
}

/**
* Fetch the following page of resources using specified client.
*
* @param page current page of resources
* @param client client used to fetch
* @return Page containing the next pageSize of resources
*/
public abstract Page<T> nextPage(final Page<T> page, final TwilioRestClient client);

/**
* Fetch the prior page of resources.
*
* @param page current page of resources
* @return Page containing the previous pageSize of resources
*/
public Page<T> previousPage(final Page<T> page) {
return previousPage(page, Twilio.getRestClient());
}


/**
* Execute list request with response metadata (headers, status code).
*
* @return PageMetadata containing resource set and HTTP metadata
*/
public PageMetadata<T> readWithMetadata() {
return readWithMetadata(Twilio.getRestClient());
}

/**
* Execute list request with response metadata using specified client.
*
* @param client client used to make request
* @return PageMetadata containing resource set and HTTP metadata
*/
public abstract PageMetadata<T> readWithMetadata(final TwilioRestClient client);

/**
* Fetch the prior page of resources using specified client.
*
* @param page current page of resources
* @param client client used to fetch
* @return Page containing the previous pageSize of resources
*/
public abstract Page<T> previousPage(final Page<T> page, final TwilioRestClient client);

public Integer getPageSize() {
return pageSize;
}

public ReaderV1<T> pageSize(final int pageSize) {
this.pageSize = pageSize;
return this;
}

public Long getLimit() {
return limit;
}

/**
* Sets the max number of records to read.
*
* @param limit max number of records to read
* @return this reader
*/
public ReaderV1<T> limit(final long limit) {
this.limit = limit;

if (this.pageSize == null) {
this.pageSize = this.limit.intValue();
}

return this;
}
}
Loading
Loading