# SDK 초기화 플로우

SDK가 초기화되는 과정을 단계별로 보여줍니다.

> **📊 다이어그램 안내** 이 문서의 다이어그램은 Mermaid 형식으로 작성되어 있습니다.
>
> * **GitHub에서 보기**: 자동으로 렌더링됩니다.
> * **GitBook에서 보기**: 아래 코드를 복사하여 [Mermaid Live Editor](https://mermaid.live)에 붙여넣으면 확인할 수 있습니다.

## 전체 초기화 흐름

```mermaid
sequenceDiagram
    participant App as 앱
    participant SDK as AdchainSdk
    participant Server as 애드체인 서버
    participant Storage as 로컬 저장소

    App->>SDK: initialize(config)
    SDK->>SDK: config 검증

    alt 유효하지 않은 config
        SDK-->>App: onFailure(에러)
    else 유효한 config
        SDK->>Server: POST /api/init<br/>(appKey, appSecret)

        alt 서버 에러
            Server-->>SDK: 401/500 에러
            SDK-->>App: onFailure(에러)
        else 성공
            Server-->>SDK: 200 OK<br/>(설정 데이터)
            SDK->>Storage: 설정 저장
            SDK->>SDK: 내부 모듈 초기화
            SDK-->>App: onSuccess()
        end
    end
```

## 설명

1. **앱에서 initialize() 호출**: appKey와 appSecret을 포함한 config로 초기화 시작
2. **config 검증**: SDK가 필수 값들이 있는지 확인
3. **서버 인증**: 애드체인 서버에 appKey/appSecret 전송하여 인증
4. **설정 다운로드**: 애드체인 서버에서 오퍼월 URL 등을 받아옴
5. **로컬 저장**: 받은 설정을 로컬에 캐싱 (다음 실행 시 빠른 시작)
6. **내부 모듈 초기화**: 이벤트 큐, WebView 설정 등 준비
7. **콜백 호출**: onSuccess() 또는 onFailure() 호출

## 초기화 후 로그인 플로우

```mermaid
sequenceDiagram
    participant App as 앱
    participant SDK as AdchainSdk
    participant Server as 애드체인 서버

    App->>SDK: login(user)

    alt SDK 미초기화
        SDK-->>App: onFailure("SDK not initialized")
    else SDK 초기화됨
        SDK->>Server: POST /api/user/login<br/>(userId, gender, birthYear)

        alt 로그인 성공
            Server-->>SDK: 200 OK<br/>(사용자 토큰)
            SDK->>SDK: 토큰 저장
            SDK->>SDK: isLoggedIn = true
            SDK-->>App: onSuccess()
        else 로그인 실패
            Server-->>SDK: 에러
            SDK-->>App: onFailure(에러)
        end
    end
```

## 주의사항

* 초기화는 앱 시작 시 한 번만 수행합니다
* 초기화 완료 전에는 다른 SDK 기능을 쓸 수 없습니다
* 네트워크가 안 되면 초기화가 실패합니다 (캐시된 설정이 있어도 첫 실행에는 네트워크 필요)
* 로그인도 초기화가 완료된 후에만 가능합니다


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://adchain-doc.1self.world/undefined-4/initialization-flow.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
