PokemonAPI 6.0.2

PokemonAPI 6.0.2

Christopher Jennewein 维护。



PokemonAPI

Build Status License Platform Swift Package Manager compatible Version Carthage compatible

这是什么?

PokemonAPI 是 PokéAPI (pokeapi.co) 的 Swift 封装。轻松调用网络服务以获取关于宝可梦和宝可梦游戏的信息。

框架支持分页响应,允许您快速获取下一个结果或特定页面。通过泛型 fetch(_ resource:) 函数,可以快速将关联资源转换为相关类。

所有代码均为 Swift 原生,无需第三方框架。

用法

使用 PokemonAPI 类来访问在 pokapi.co 上找到的按类别组织的网络服务类(如果实、宝可梦、游戏等)。

响应

所有网络服务函数都支持与 iOS 13 及以后的 Combine 框架兼容。

组合函数返回一个 AnyPublisher。非组合函数使用类型为 Result 的完成处理器。这些函数的成功值都包含从 JSON 响应解码的自定义类。错误值是自定义枚举类型 HTTPError

资源

返回对象的一些属性类型为 PKMNamedAPIResource<T>PKMAPIResource<T>,其中 T 是另一个包含额外信息的类。将此属性传递给 PokemonAPI.resourceService.fetch(_ resource:) 将返回底层数据。以下是一个示例。

列表

获取列表将返回一个包含一页结果的、以及总数、页数以及下一个/上一个页面是否可用的 PagedObject<T>。结果中的 results 将是一个包含 PKMNamedAPIResource<T>PKMAPIResource<T> 的数组,因此通常需要在必要时立即获取资源。

列表的 Web 服务函数接受一个 PaginationState 枚举参数。此枚举有两种情况:对于第一次调用的是 .initial(pageLimit: Int),后续调用是 .continuing(PKMPagedObject<T>, PaginationRelationship)。每个函数默认值为 .initial(pageLimit: 20),但您也可以传入自己的页数限制。在第一次调用后,您可以使用 .continuing() 与来自上一个响应的 PagedObject,以及一个 PaginationRelationship.next.previous.first.last 或特定 .page(Int))进行导航。

网络调用

通过创建一个 App Transport Security 域异常,允许您的应用通过 PokéAPI (pokeapi.co) 进行调用。

在您的 Info.plist 中,添加以下内容:

<key>NSAppTransportSecurity</key>
<dict>
	<key>NSExceptionDomains</key>
	<dict>
		<key>pokeapi.co</key>
		<dict>
			<key>NSIncludesSubdomains</key>
			<true/>
			<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
			<true/>
			<key>NSTemporaryExceptionMinimumTLSVersion</key>
			<string>TLSv1.2</string>
		</dict>
	</dict>
</dict>

示例

import PokemonAPI

// Example of calling a web service using an ID
PokemonAPI().berryService.fetchBerry(1) { result in
    switch result {
    case .success(let berry):
        self.berryLabel.text = berry.name // cheri
    case .failure(let error):
        print(error.localizedDescription)
    }
}

// Same example using Combine. Don't forget to store your cancellable.
let cancellable = PokemonAPI().berryService.fetchBerry(1)
	.sink(receiveCompletion: { completion in
        if case .failure(let error) = completion {
            print(error.localizedDescription)
        }
	}, receiveValue: { berry in
		self.berryName = berry.name! // cheri
	})
// Example of calling a web service using a name
PokemonAPI().pokemonService.fetchPokemon("bulbasaur") { result in
    switch result {
    case .success(let pokemon):
        self.pokemonLabel.text = pokemon.name // bulbasaur
    case .failure(let error):
        print(error.localizedDescription)
    }
}
// Example of fetching a PKMNamedAPIResource (or PKMAPIResource)
PokemonAPI().gameService.fetchPokedex(14) { result in
    switch result {
    case .success(let pokedex):
        print(pokedex.name!) // kalos-mountain
        
        PokemonAPI().resourceService.fetch(pokedex.region!) { result in
            switch result {
            case .success(let region):
                print(region.name!) // kalos
            case .failure(let error):
                print(error.localizedDescription)
            }
        }
        
    case .failure(let error):
        print(error.localizedDescription)
    }
}
// Example of calling a paginated web service with a pageLimit, then using the pagedObject to fetch the next page in the list
PokemonAPI().utilityService.fetchLanguageList(paginationState: .initial(pageLimit: 5)) { result in
    switch result {
    case .success(let pagedLanguages):
        print("\(pagedLanguages.count!)") // 13

        PokemonAPI().utilityService.fetchLanguageList(paginationState: .continuing(pagedLanguages, .next)) { result in
            switch result {
            case .success(let pagedLanguagesNext):
                print("Page: \(pagedLanguagesNext.currentPage)") // Page: 1
            case .failure(let error):
                print(error.localizedDescription)
            }
        }
    case .failure(let error):
        print(error.localizedDescription)
    }
}

待办事项

  • 已完整<文档
  • 已全面测试

安装

Swift包管理器

在Xcode中搜索此存储库链接

文件 -> Swift包 -> 添加包依赖...

https://github.com/kinkofer/PokemonAPI

CocoaPods

PokemonAPI可通过CocoaPods获取。要安装,只需将以下行添加到您的Podfile

pod 'PokemonAPI'

Carthage

如果您使用Carthage,可以通过将其添加到Cartfile来添加PokemonAPI

github "kinkofer/PokemonAPI" ~> 6.0.0

作者

Christopher Jennewein, [email protected]

从PokemonKit分支,由Yeung Yiu Hung,http://github.com/ContinuousLearning/PokemonKit

许可证

PokemonAPI受MIT许可证的约束。查看阅读许可证文件获取更多信息。