Skip to content
Closed
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
95 changes: 95 additions & 0 deletions examples/test_spec/twilio_pagination_v1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# This spec tests the token pagination strategy as per Twilio API Standards V1.0

info:
contact:
email: [email protected]
name: Twilio Support
url: https://support.twilio.com
description: This is the public Twilio REST API.
license:
name: Apache 2.0
url: https://www.apache.org/licenses/LICENSE-2.0.html
termsOfService: https://www.twilio.com/legal/tos
title: Twilio - REQUEST BODY TEST
version: 1.11.0
x-twilio:
apiStandards: v1.0
openapi: 3.0.1

components:
securitySchemes:
accountSid_authToken:
scheme: basic
type: http
schemas:
SampleResponseObject:
type: object
properties:
id:
type: string
name:
type: string
paths:
/v2/Services:
servers:
- url: https://testparameter.twilio.com
get:
operationId: ListService
description: Retrieve a list of all Services
parameters:
- name: pageSize
in: query
description: Maximum number of items to return in a single response
required: false
schema:
type: integer
minimum: 1
maximum: 1000
example: 50
- name: pageToken
in: query
description: A URL-safe, base64-encoded token representing the page of results to return
required: false
schema:
type: string
example: "eyJwYWdlIjoyLCJxdWVyeSI6ImJvb2tzIn0="
security:
- accountSid_authToken: [ ]
responses:
'200':
description: OK
content:
application/json:
schema:
type: object
required:
- services
- meta
properties:
services:
type: array
minItems: 0
items:
$ref: '#/components/schemas/SampleResponseObject'
meta:
type: object
required:
- key
- pageSize
properties:
key:
type: string
description: The key of the list property contains the actual data items
example: "services"
pageSize:
type: integer
description: The actual number of items returned in this response
example: 20
previousToken:
type: string
description: Token to fetch the previous page of results
example: "eyJwYWdlIjowLCJxdWVyeSI6ImJvb2tzIn0="
nextToken:
type: string
description: Token to fetch the next page of results
example: "eyJwYWdlIjoyLCJxdWVyeSI6ImJvb2tzIn0="
4 changes: 3 additions & 1 deletion src/main/resources/twilio-java/reader.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package com.twilio.rest.{{domainPackage}}.{{apiVersion}}{{namespaceSubPart}};
{{>common/imports}}
import com.twilio.base.Page;
{{#isApiV1}}import com.twilio.base.TokenPaginationPage;{{/isApiV1}}
import com.twilio.base.ResourceSet;

{{#operations}}
Expand All @@ -13,6 +14,7 @@ public class {{resourceName}}Reader extends Reader<{{resourceName}}> {
{{>common/constructors}}
{{>reader/setters}}
{{>reader/operationMethod}}
{{>reader/paginationMethods}}
{{#queryParams.0}}
{{>common/addQueryParams}}
{{/queryParams.0}}
Expand All @@ -22,4 +24,4 @@ public class {{resourceName}}Reader extends Reader<{{resourceName}}> {
}
{{/vendorExtensions.x-list-operation}}
{{/operations}}
{{/resources}}
{{/resources}}
46 changes: 3 additions & 43 deletions src/main/resources/twilio-java/reader/operationMethod.mustache
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{{!
{{!
resourceName: Api Name as identified by Directory Structure service
recordKey:
recordKey:
httpMethod: http method associated in the current operation.
domainName: example api, video, chat, etc. These can be found in Domains.java in twilio-java
}}
@Override
public ResourceSet<{{resourceName}}> read(final TwilioRestClient client) {
return new ResourceSet<>(this, client, firstPage(client));
}

public Page<{{resourceName}}> firstPage(final TwilioRestClient client) {
{{>common/generateUri}}
Request request = new Request(
Expand All @@ -28,43 +28,3 @@ domainName: example api, video, chat, etc. These can be found in Domains.java in

return pageForRequest(client, request);
}

private Page<{{resourceName}}> pageForRequest(final TwilioRestClient client, final Request request) {
Response response = client.request(request);
if (response == null) {
throw new ApiConnectionException("{{resourceName}} read failed: Unable to connect to server");
} else if (!TwilioRestClient.SUCCESS.test(response.getStatusCode())) {
RestException restException = RestException.fromJson(
response.getStream(),
client.getObjectMapper());

if (restException == null) {
throw new ApiException("Server Error, no content", response.getStatusCode());
}
throw new ApiException(restException);
} {{! end of else if }}

return Page.fromJson(
"{{recordKey}}",
response.getContent(),
{{resourceName}}.class,
client.getObjectMapper());
}

@Override
public Page<{{resourceName}}> previousPage(final Page<{{resourceName}}> page, final TwilioRestClient client ) {
Request request = new Request(HttpMethod.GET, page.getPreviousPageUrl(Domains.API.toString()));
return pageForRequest(client, request);
}

@Override
public Page<{{resourceName}}> nextPage(final Page<{{resourceName}}> page, final TwilioRestClient client) {
Request request = new Request(HttpMethod.GET, page.getNextPageUrl(Domains.API.toString()));
return pageForRequest(client, request);
}

@Override
public Page<{{resourceName}}> getPage(final String targetUrl, final TwilioRestClient client) {
Request request = new Request(HttpMethod.GET, targetUrl);
return pageForRequest(client, request);
}
57 changes: 57 additions & 0 deletions src/main/resources/twilio-java/reader/paginationMethods.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
private Page<{{resourceName}}> pageForRequest(final TwilioRestClient client, final Request request) {
Response response = client.request(request);
if (response == null) {
throw new ApiConnectionException("{{resourceName}} read failed: Unable to connect to server");
} else if (!TwilioRestClient.SUCCESS.test(response.getStatusCode())) {
RestException restException = RestException.fromJson(
response.getStream(),
client.getObjectMapper());

if (restException == null) {
throw new ApiException("Server Error, no content", response.getStatusCode());
}
throw new ApiException(restException);
} {{! end of else if }}

return {{#isApiV1}}TokenPaginationPage{{/isApiV1}}{{^isApiV1}}Page{{/isApiV1}}.fromJson(
"{{recordKey}}",
response.getContent(),
{{resourceName}}.class,
client.getObjectMapper());
}

@Override
public Page<{{resourceName}}> previousPage(final Page<{{resourceName}}> page, final TwilioRestClient client ) {
{{#isApiV1}}
{{>common/generateUri}}
path = path + page.previousQueryString();
Request request = new Request(
HttpMethod.{{httpMethod}},
Domains.{{#lambda.uppercase}}{{domainName}}{{/lambda.uppercase}}.toString(),
path
);
{{/isApiV1}}
{{^isApiV1}}Request request = new Request(HttpMethod.GET, page.getPreviousPageUrl(Domains.API.toString()));{{/isApiV1}}
return pageForRequest(client, request);
}

@Override
public Page<{{resourceName}}> nextPage(final Page<{{resourceName}}> page, final TwilioRestClient client) {
{{#isApiV1}}
{{>common/generateUri}}
path = path + page.nextQueryString();
Request request = new Request(
HttpMethod.{{httpMethod}},
Domains.{{#lambda.uppercase}}{{domainName}}{{/lambda.uppercase}}.toString(),
path
);
{{/isApiV1}}
{{^isApiV1}}Request request = new Request(HttpMethod.GET, page.getNextPageUrl(Domains.API.toString()));{{/isApiV1}}
return pageForRequest(client, request);
}

@Override
public Page<{{resourceName}}> getPage(final String targetUrl, final TwilioRestClient client) {
Request request = new Request(HttpMethod.GET, targetUrl);
return pageForRequest(client, request);
}
Loading