一个 Swift 客户端,以及 BBC News API 的 非官方文档。
本项目仅用于教育和研究目的。
bbc-news-swift 可以使用 Swift Package Manager 安装,只需将以下内容添加到您的 Package.swift
文件中
.package(url: "https://github.com/bilaalrashid/bbc-news-swift.git", .upToNextMajor(from: "1.1.2"))
完整文档可在 Swift Package Index 上找到。
import BbcNews
let bbcNews = BbcNews(modelIdentifier: "iPhone15,2", systemName: "iOS", systemVersion: "17.0")
// Get results from the home page
let results = await bbcNews.fetchIndexDiscoveryPage(postcode: "W1A")
// Get results from a topic page
let results = await bbcNews.fetchTopicDiscoveryPage(for: "c50znx8v8y4t")
// Check if the network call was a success or not
switch result {
case .success(let result):
// Parse story promo from a set of discovery results and fetch the full contents of that story
for item in result.data.items {
if case .storyPromo(let storyPromo) = item {
let url = storyPromo.link.destinations[0].url
// Get the full contents of the story
let story = await bbcNews.fetch(url: url)
}
}
case .failure(let error):
print(error)
}
所有方法都有支持 Result
类型和传统 try
的等效方法。您可以通过在方法名称末尾添加 Throwing
来使用 try
等效方法。
Result
:
let result = await bbcNews.fetchIndexDiscoveryPage(postcode: "W1A")
switch result {
case .success(let results):
print(results) // [...]
case .failure(let error):
print(error)
}
try
:
let results = try await bbcNews.fetchIndexDiscoveryPageThrowing(postcode: "W1A")
print(results) // [...]
import BbcNews
// Check if a URL is part of the BBC News API
BbcNews.isApiUrl(url: URL(string: "https://bbc.co.uk")!) // false
// Convert a webpage URL to a URL for the API
BbcNews.convertWebUrlToApi(url: URL(string: "https://www.bbc.com/news/articles/c289n8m4j19o")!) // https://news-app.api.bbc.co.uk/fd/app-article-api?clientName=Chrysalis&clientVersion=pre-7&page=https://www.bbc.com/news/articles/c289n8m4j19o
本项目使用 SwiftLint 来强制执行编码风格。您可以使用以下命令检查任何风格违规并尝试修复它们:
swiftlint --config .swiftlint.yml --strict [--fix]
测试套件使用测试资源包中的本地文件。这会导致测试在 Xcode 中失败,因此必须从命令行运行测试。
swift test
完整的贡献指南可以在 CONTRIBUTING.md 中找到。
API 的文档使用 OpenAPI 定义,并使用 CI 自动构建(可在 https://bilaalrashid.github.io/bbc-news-swift 访问)。
可以使用 Redocly 检查这些模式文件。
redocly lint openapi/*/openapi.yaml
可以通过生成 pageset 可视化对这些文件的更改
redocly build-docs openapi/<version>/openapi.yaml
在 main
分支上
make release # Defaults to patch
make release-<major|minor|patch>
make release-version VERSION=<version>
git push --follow-tags