API Gateway에서 생성한 REST API용 JavaScript SDK 사용 - Amazon API Gateway

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

API Gateway에서 생성한 REST API용 JavaScript SDK 사용

참고

이러한 지침에서는 API Gateway 콘솔을 사용하여 API용 SDK 생성의 지침을 이미 완료한 것으로 가정합니다.

중요

API에 ANY 메서드만 정의된 경우 생성된 SDK 패키지에는 apigClient.js 파일이 포함되지 않으므로 ANY 메서드를 직접 정의할 필요가 없습니다.

API Gateway가 생성한 REST API용 JavaScript SDK를 설치, 시작, 호출하려면
  1. 앞서 다운로드한 API Gateway가 생성한 .zip 파일의 압축을 풉니다.

  2. API Gateway에서 생성한 SDK에 의해 생성된 모든 메서드에 대한 CORS(cross-origin 리소스 공유)를 활성화합니다. 지침은 REST API 리소스에 대한 CORS 활성화 단원을 참조하세요.

  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 키를 API Gateway에 의해 생성된 SDK와 함께 사용하려면 다음과 비슷한 코드를 사용하여 API 키를 Factory 객체에 파라미터로 전달합니다. API 키를 사용하는 경우 키는 x-api-key 헤더의 일부로 지정되며 API에 대한 모든 요청이 서명됩니다. 이는 각 요청에 대해 적절한 CORS 수락 헤더를 설정해야 한다는 의미입니다.

    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