# 完整调用示例

从零创建平台、导入小说与分页读取的 PowerShell 示例。

以下示例要求 Go 服务已运行，并将创建时返回的个人 Key 保存到调用端 `ATHUB_API_TOKEN` 环境变量中。它不是 Go 服务的全局配置。
不要将真实密钥写进文档或提交到 Git。

### 10.1 PowerShell：从零创建平台并上传

```powershell
$base = 'https://api.albatextus.com' # 自托管时改为实际 Go 服务地址
$headers = @{ Authorization = "Bearer $env:ATHUB_API_TOKEN" }

$platformJson = @{ name = 'My platform'; url = $null } | ConvertTo-Json
$platform = Invoke-RestMethod "$base/api/v2/platforms" -Method Post -Headers $headers `
  -ContentType 'application/json; charset=utf-8' `
  -Body ([Text.Encoding]::UTF8.GetBytes($platformJson))

$upload = @{
  novel = @{
    platformId = $platform.id
    title = 'Example novel'
    authorId = $null
    summary = 'Introduction'
    coverUrl = $null
    status = 'ONGOING'
  }
  volumes = @(@{ order = 1; title = 'Volume 1' })
  chapters = @(
    @{ order = 1; volumeOrder = 1; title = 'Chapter 1'; content = "Paragraph 1.`nParagraph 2." }
  )
} | ConvertTo-Json -Depth 10

$result = Invoke-RestMethod "$base/api/v2/upload" -Method Post -Headers $headers `
  -ContentType 'application/json; charset=utf-8' `
  -Body ([Text.Encoding]::UTF8.GetBytes($upload))

$novelId = $result.novel.id
Invoke-RestMethod "$base/api/v2/novelinfo?id=$novelId" -Headers $headers
Invoke-RestMethod "$base/api/v2/volumes?novelId=$novelId" -Headers $headers
Invoke-RestMethod "$base/api/v2/toc?novelId=$novelId" -Headers $headers
Invoke-RestMethod "$base/api/v2/content?novelId=$novelId&order=1" -Headers $headers
```

### 10.2 上传 JSON 文件

把第 8.1 节 JSON 保存为 UTF-8 无 BOM 文件，替换真实平台/作者 ID。
下面假设文件在 `temp/api/v2/upload.json`。这只是请求格式示例，不会自动读取本机文件路径。

```powershell
curl.exe --request POST 'https://api.albatextus.com/api/v2/upload' `
  --header "Authorization: Bearer $env:ATHUB_API_TOKEN" `
  --header 'Content-Type: application/json; charset=utf-8' `
  --data-binary '@temp/api/v2/upload.json'
```

不要使用 `-F file=@...`。接口接收 JSON 内容，不接受 `file`、`filePath`、`filename`、`encoding` 等额外字段。

### 10.3 续传下一批

```powershell
$nextBatch = @{
  novelId = $novelId
  chapters = @(
    @{ order = 2; volumeOrder = 1; title = 'Chapter 2'; content = 'Next chapter body.' }
  )
} | ConvertTo-Json -Depth 10

Invoke-RestMethod "$base/api/v2/upload" -Method Post -Headers $headers `
  -ContentType 'application/json; charset=utf-8' `
  -Body ([Text.Encoding]::UTF8.GetBytes($nextBatch))
```

重发这个续传请求会覆盖同一本书的第 2 章，不会创建第二本小说或重复章节。

### 10.4 分页获取全部章节

```powershell
$limit = 100
$offset = 0
do {
  $page = Invoke-RestMethod "$base/api/v2/content?novelId=$novelId&limit=$limit&offset=$offset" -Headers $headers
  foreach ($chapter in $page) {
    # 在这里消费 chapter.id / volumeId / order / title / content / updatedAt
    $chapter
  }
  $offset += $limit
} while ($page.Count -eq $limit)
```

上传完成后再开始全量分页读取，可以避免边上传边分页产生的数据变化。
