SDK Generation ============== The ``@app.route(...)`` information you provide chalice allows it to create corresponding routes in API Gateway. One of the benefits of this approach is that we can leverage API Gateway's SDK generation process. Chalice offers a ``chalice generate-sdk`` command that will automatically generate an SDK based on your declared routes. .. note:: The only supported language at this time is javascript. Keep in mind that chalice itself does not have any logic for generating SDKs. The SDK generation happens service side in `API Gateway`_, the ``chalice generate-sdk`` is just a high level wrapper around that functionality. To generate an SDK for a chalice app, run this command from the project directory:: $ chalice generate-sdk /tmp/sdk You should now have a generated javascript sdk in ``/tmp/sdk``. API Gateway includes a ``README.md`` as part of its SDK generation which contains details on how to use the javascript SDK. Example ------- Suppose we have the following chalice app: .. code-block:: python from chalice import Chalice app = Chalice(app_name='sdktest') @app.route('/', cors=True) def index(): return {'hello': 'world'} @app.route('/foo', cors=True) def foo(): return {'foo': True} @app.route('/hello/{name}', cors=True) def hello_name(name): return {'hello': name} @app.route('/users/{user_id}', methods=['PUT'], cors=True) def update_user(user_id): return {"msg": "fake updated user", "userId": user_id} Let's generate a javascript SDK and test it out in the browser. Run the following command from the project dir:: $ chalice generate-sdk /tmp/sdkdemo $ cd /tmp/sdkdemo $ ls -la -rw-r--r-- 1 jamessar r 3227 Nov 21 17:06 README.md -rw-r--r-- 1 jamessar r 9243 Nov 21 17:06 apigClient.js drwxr-xr-x 6 jamessar r 204 Nov 21 17:06 lib You should now be able to follow the instructions from API Gateway in the ``README.md`` file. Below is a snippet that shows how the generated javascript SDK methods correspond to the ``@app.route()`` calls in chalice. .. code-block:: html Example HTML File ~~~~~~~~~~~~~~~~~ If you want to try out the example above, you can use the following index.html page to test: .. code-block:: html SDK Test
result of rootGet()
result of fooGet()
result of helloNameGet({name: 'jimmy'})
result of usersUserIdPut({user_id: '123'})
.. _API Gateway: https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-generate-sdk.html