[STK1-02] 토스 시세 Gateway

작업 내용 (설계 의도)

변경 사항

  • PriceGateway interface(domain) + TossPriceGateway 구현(infrastructure).
  • OAuth client_credentials 토큰 관리, 시세 일괄(batch) 조회.
  • 401 응답 시 토큰 갱신 후 1회 재시도. 재시도도 실패 시 TossAuthException.
  • 토스 API가 WebSocket/Push를 미제공하므로 HTTP polling으로 구현.

의존

  • 선행: STK1-01
  • 후행: STK1-04

롤백

  • 외부 연동 — 토스 키 환경변수 미설정 시 호출 비활성.

다이어그램

처리 흐름

sequenceDiagram
    participant DS as PriceAlertDomainService
    participant GW as TossPriceGateway
    participant Toss as Toss OAuth API
    participant Price as Toss Price API

    DS->>GW: getPrices(symbols)
    GW->>Price: GET /price?symbols=...
    alt 401 Unauthorized
        Price-->>GW: 401
        GW->>Toss: POST /oauth/token
        Toss-->>GW: access_token
        GW->>Price: GET /price?symbols=... (재시도)
        alt 재시도도 401
            Price-->>GW: 401
            GW-->>DS: throw TossAuthException
        end
    end
    Price-->>GW: {"AAPL": 185.20, ...}
    GW-->>DS: Map<String, BigDecimal>

클래스 의존

flowchart LR
    subgraph Domain["domain"]
        PGW[PriceGateway]
    end
    subgraph Infra["infrastructure"]
        TPG[TossPriceGateway]
        TokenCache[TokenCache]
    end
    TPG -.->|implements| PGW
    TPG --> TokenCache

테스트 케이스

  • 유효한 토큰으로 시세 조회 시 symbol별 가격 Map을 반환한다.
  • 빈 symbol 목록으로 호출 시 API를 호출하지 않고 빈 Map을 반환한다.
  • 401 응답 시 토큰을 재발급하고 시세 조회를 1회 재시도한다.
  • 재시도도 401이면 TossAuthException을 던진다.
  • 토스 API가 5xx를 반환하면 PriceGatewayException을 던진다.