Open j2h30728 opened 1 week ago
marvel API 는 url param에 public key 와 private key, hash(ts + private key + public key)를 주입하여 요청해야하기 때문에 클라이언트에 위험이 따른다.
MARVEL API는 param으로 전달하는 값은 hash 처리하기 때문에 private key은 숨겨지고, 환경변수를 사용하면 된다. 코드샌드 박스 제출시 환경변수를 주입하게 되면 동작하지 않는 상황이 발생하고 이미 서버리스 API 구축 했기떄문에 학습차 해당 api 를 사용한다.
Authentication for Server-Side Applications Server-side applications must pass two parameters in addition to the apikey parameter:
ts - a timestamp (or other long string which can change on a request-by-request basis) hash - a md5 digest of the ts parameter, your private key and your public key (e.g. md5(ts+privateKey+publicKey) For example, a user with a public key of "1234" and a private key of "abcd" could construct a valid call as follows: http://gateway.marvel.com/v1/public/comics?ts=1&apikey=1234&hash=ffd275c5130566a2916217b101f26150 (the hash value is the md5 digest of 1abcd1234)
ec2 서버를 띄우지 않고 AWS Lambda + API Gateway로 Serverless API 환경 구성하여 위험이 될 수 있는 값들을 숨김처리한다.
AWS Lambda Layers를 사용하면 여러 Lambda 함수에서 공통 라이브러리를 쉽게 공유 가능
/* global fetch */
문구 추가 필요
.js
/.mjs
파일 확장자를 쓰며 import 패키지
/ export const handler
사용.js
/.cjs
파일 확장자를 쓰며 require('패키지')/
export.handler` 사용캐릭터 리스트의 전체 데이터를 get 요청으로 가져오기 때문에 timeout 3 seconds를 초과한다.
const url =
http://gateway.marvel.com/v1/public/characters?ts=${ts}&apikey=${publicKey}&hash=${hash};
offset 과 limit를 추가하여 페이지네이션 처리 합니다.
offset, limit 추가 한뒤, 0.1 second 가 줄어들었지만 최소 응답이 3 seconds로 되어있기에 time out 발생한다.
lamdba 함수 테스트가 아닌 실제 api 호출시에는 time out 에러 없이 응답이 오는 것을 확인했다.
추후. 예측할 할 수 없는 에러를 방지하여 timeout 을 4 seconds로 늘려놓는다.