使用 JavaScript SDK由API網關生成的 REST API - Amazon API Gateway

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 JavaScript SDK由API網關生成的 REST API

下列程序顯示如何使用API閘道 JavaScript SDK產生的。

注意

這些說明假設您已完成RESTAPIs在API閘SDKs道中產生中的指示。

重要

如果您API只定義了ANY方法,則生成的SDK包將不包含apigClient.js文件,並且您需要自己定義ANY方法。

要安裝,啟動和調用由API網關 JavaScript SDK生成的 REST API
  1. 解壓縮您之前下載的API閘道產生 .zip 檔案的內容。

  2. 為 API Gateway 將呼叫的所有方法啟用跨來源資源共用 (CORS)。SDK如需說明,請參閱CORSRESTAPIs在API閘道中

  3. 在您的網頁中,包含下列指令碼的參考。

    <script type="text/javascript" src="lib/axios/dist/axios.standalone.js"></script> <script type="text/javascript" src="lib/CryptoJS/rollups/hmac-sha256.js"></script> <script type="text/javascript" src="lib/CryptoJS/rollups/sha256.js"></script> <script type="text/javascript" src="lib/CryptoJS/components/hmac.js"></script> <script type="text/javascript" src="lib/CryptoJS/components/enc-base64.js"></script> <script type="text/javascript" src="lib/url-template/url-template.js"></script> <script type="text/javascript" src="lib/apiGatewayCore/sigV4Client.js"></script> <script type="text/javascript" src="lib/apiGatewayCore/apiGatewayClient.js"></script> <script type="text/javascript" src="lib/apiGatewayCore/simpleHttpClient.js"></script> <script type="text/javascript" src="lib/apiGatewayCore/utils.js"></script> <script type="text/javascript" src="apigClient.js"></script>
  4. 在您的程式碼中,使用類似下列的程式碼,初始化 API Gateway 所SDK產生的程式碼。

    var apigClient = apigClientFactory.newClient();

    若要使用 AWS 認證初始化 API Gateway SDK 產生的,請使用類似下列的程式碼。如果您使用 AWS 認證,則API會簽署對的所有要求。

    var apigClient = apigClientFactory.newClient({ accessKey: 'ACCESS_KEY', secretKey: 'SECRET_KEY', });

    若要搭配 API Gateway SDK 產生的API金鑰使用,請使用類似下列的程式碼,將API金鑰做為參數傳遞至Factory物件。如果您使用一個API密鑰,它被指定為x-api-key標頭的一部分,並且所有的請求都API將被簽名。這表示您必須為每個要求設定適當的 CORS Accept 標頭。

    var apigClient = apigClientFactory.newClient({ apiKey: 'API_KEY' });

  5. 使用類似下列的程API式碼呼叫 API Gateway 中的方法。每個呼叫會傳回成功與失敗回呼的結果。

    var params = { // This is where any modeled request parameters should be added. // The key is the parameter name, as it is defined in the API in API Gateway. param0: '', param1: '' }; var body = { // This is where you define the body of the request, }; var additionalParams = { // If there are any unmodeled query parameters or headers that must be // sent with the request, add them here. headers: { param0: '', param1: '' }, queryParams: { param0: '', param1: '' } }; apigClient.methodName(params, body, additionalParams) .then(function(result){ // Add success callback code here. }).catch( function(result){ // Add error callback code here. });

    在這裡,methodName 是從方法請求的資源路徑和HTTP動詞構造的。對於 SimpleCalc API,SDK方法的API方法

    1. GET /?a=...&b=...&op=... 2. POST / { "a": ..., "b": ..., "op": ...} 3. GET /{a}/{b}/{op}

    相應的SDK方法如下:

    1. rootGet(params); // where params={"a": ..., "b": ..., "op": ...} is resolved to the query parameters 2. rootPost(null, body); // where body={"a": ..., "b": ..., "op": ...} 3. aBOpGet(params); // where params={"a": ..., "b": ..., "op": ...} is resolved to the path parameters