diff --git a/examples/test_spec/twilio_pagination_v1.yaml b/examples/test_spec/twilio_pagination_v1.yaml new file mode 100644 index 000000000..fd929f372 --- /dev/null +++ b/examples/test_spec/twilio_pagination_v1.yaml @@ -0,0 +1,95 @@ +# This spec tests the token pagination strategy as per Twilio API Standards V1.0 + +info: + contact: + email: support@twilio.com + 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=" diff --git a/src/main/resources/twilio-java/reader.mustache b/src/main/resources/twilio-java/reader.mustache index 74ae0f226..a27e103a5 100644 --- a/src/main/resources/twilio-java/reader.mustache +++ b/src/main/resources/twilio-java/reader.mustache @@ -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}} @@ -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}} @@ -22,4 +24,4 @@ public class {{resourceName}}Reader extends Reader<{{resourceName}}> { } {{/vendorExtensions.x-list-operation}} {{/operations}} -{{/resources}} \ No newline at end of file +{{/resources}} diff --git a/src/main/resources/twilio-java/reader/operationMethod.mustache b/src/main/resources/twilio-java/reader/operationMethod.mustache index 7dea9b5d8..87bb9ccb3 100644 --- a/src/main/resources/twilio-java/reader/operationMethod.mustache +++ b/src/main/resources/twilio-java/reader/operationMethod.mustache @@ -1,6 +1,6 @@ -{{! +{{! 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 }} @@ -8,7 +8,7 @@ domainName: example api, video, chat, etc. These can be found in Domains.java in 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( @@ -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); - } \ No newline at end of file diff --git a/src/main/resources/twilio-java/reader/paginationMethods.mustache b/src/main/resources/twilio-java/reader/paginationMethods.mustache index e69de29bb..2a8bfda3d 100644 --- a/src/main/resources/twilio-java/reader/paginationMethods.mustache +++ b/src/main/resources/twilio-java/reader/paginationMethods.mustache @@ -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); + }