Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding pagination to codegen #200

Merged
merged 16 commits into from
Aug 21, 2020

Conversation

alexforsyth
Copy link
Contributor

Issue #, if available:

Description of changes:
Adding Pagination code generation.

Please be kind! This is basically the first time i've touched java code since ?highschool?. Please let me know what to change - im sure i'm missing lots of obvious stuff.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@alexforsyth alexforsyth changed the title Alexforsyth/pagination WIP: Alexforsyth/pagination Aug 17, 2020
@alexforsyth
Copy link
Contributor Author

alexforsyth commented Aug 17, 2020

For reference: When this is run on the test (ListCities) this will generate this paginator file:

import { Weather } from "../Weather";
import { WeatherClient } from "../WeatherClient";
import {
  ListCitiesCommand,
  ListCitiesCommandInput,
  ListCitiesCommandOutput,
} from "../commands/ListCitiesCommand";
import { WeatherPaginationConfiguration } from "./Interfaces";
import { Paginator } from "@aws-sdk/types";

const makePagedClientRequest = async (client: WeatherClient, input: ListCitiesCommandInput, ...additionalArguments: any): Promise<ListCitiesCommandOutput> => {
  // @ts-ignore
  return await client.send(new ListCitiesCommand(input, ...additionalArguments));
}
const makePagedRequest = async (client: Weather, input: ListCitiesCommandInput, ...additionalArguments: any): Promise<ListCitiesCommandOutput> => {
  // @ts-ignore
  return await client.listCities(input, ...additionalArguments);
}
export async function* listCitiesPaginate(config: WeatherPaginationConfiguration, input: ListCitiesCommandInput, ...additionalArguments: any): Paginator<ListCitiesCommandOutput>{
  let token = config.startingToken || '';
  let hasNext = true;
  let page:ListCitiesCommandOutput;
  while (hasNext) {
    input["nextToken"] = token;
    input["pageSize"] = config.pageSize;
    if(config.client instanceof Weather) {
      page = await makePagedRequest(config.client, input, ...additionalArguments);
    }
    else if (config.client instanceof WeatherClient) {
       page = await makePagedClientRequest(config.client, input, ...additionalArguments);
    }
    else {
       throw new Error("Invalid client, expected Weather | WeatherClient");
    }
    yield page;
    token = page["nextToken"];
    hasNext = !!(token);
  }
  return undefined;
}


@alexforsyth
Copy link
Contributor Author

This will also generate this interface:

import { Weather } from "../Weather";
import { WeatherClient } from "../WeatherClient";
import { PaginationConfiguration } from "@aws-sdk/types";

export interface WeatherPaginationConfiguration extends PaginationConfiguration {
  client: Weather | WeatherClient;
}

@alexforsyth
Copy link
Contributor Author

alexforsyth commented Aug 17, 2020

To make this work, a customer would need to write:

(async () => {
    const paginatorConfiguration: WeatherPaginationConfiguration = {
        client: new Weather({...})
    };
    const listCitiesInput: ListCitiesCommandInput = {...};
    const paginator = listCitiesPaginate(paginatorConfiguration, listCitiesInput);

    for await (const page of paginator) {
        console.log(page);
        // do stuff with the page
    }
})();

@alexforsyth alexforsyth changed the title WIP: Alexforsyth/pagination feat: adding pagination to codegen Aug 20, 2020
@kstich kstich merged commit b6ff818 into smithy-lang:master Aug 21, 2020
srchase pushed a commit to srchase/smithy-typescript that referenced this pull request Mar 17, 2023
This commit adds support for generating a paginator abstraction for service operations with the `@paginated` trait applied.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants