测试测试 | ✓ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布日期最后发布日期 | 2021 年 8 月 |
SPM支持 SPM | ✗ |
由 Eric Vennaro 维护。
Twitch API Wrapper 是一个 Twitch API 的 HTTP 客户端,它包含了对不同 Twitch 端点的对象映射,可以通过提供的数据源轻松显示。该库还包含一个 OAuth 客户端,允许您访问 Twitch 的受保护端点以及您的应用程序的 OAuth。
import TwitchAPIWrapper
此库有两个主要部分
以下示例说明了如何设置 OAuth 组件并利用表示对象(数据源)来检索模型。
重要:在授权之前确保
// OAuth setup can be placed wherever you wish; however, as in the example project it is placed inside of the AppDelegate so
// that authorization is done when the application is first loaded.
TwitchAuthorizationManager.sharedInstance.clientID = configuration["clientID"] as? String
TwitchAuthorizationManager.sharedInstance.redirectURI = configuration["redirectURI"] as? String
TwitchAuthorizationManager.sharedInstance.scopes = configuration["scopes"] as? String
TwitchAuthorizationManager.sharedInstance.clientSecret = configuration["clientSecret"] as? String
if !TwitchAuthorizationManager.sharedInstance.hasOAuthToken() {
do {
try TwitchAuthorizationManager.sharedInstance.login()
} catch AuthorizationError.invalidURLResponse(let url) {
NSLog("error thrown: \(url)")
} catch AuthorizationError.unableToParseJSON(let json) {
NSLog(json)
} catch AuthorizationError.invalidQueryParameters(let d) {
NSLog(d)
} catch AuthorizationError.invalidAuthURL(let d, let url) {
NSLog("\(d), url: \(url)")
} catch AuthorizationError.unknownError(let e) {
NSLog(e.localizedDescription)
} catch {
NSLog("unknown exception occured")
}
} else {
NSLog("OAuth Token exists, no need to authorize")
}
//Add the following function to the AppDelegate:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
if url.host == yourURLScheme {
TwitchAuthorizationManager.sharedInstance.processOauthResponse(with: url,
completion: { (result) in
switch result {
case let .failure(error):
switch error {
case let AuthorizationError.invalidAuthURL(desc, url):
NSLog(desc + url)
case let AuthorizationError.invalidQueryParameters(desc):
NSLog(desc)
case let AuthorizationError.invalidURLResponse(url):
NSLog("\(url)")
case let AuthorizationError.noCode(desc):
NSLog(desc)
case let AuthorizationError.unableToParseJSON(json):
NSLog(json)
case let AuthorizationError.unknownError(error):
NSLog(error.localizedDescription)
case ParsingError.cannotParseJSONArray:
NSLog("Error parsing JSON Array")
case ParsingError.invalidJSONData:
NSLog("Invalid JSON Data")
case ParsingError.cannotParseJSONDictionary:
NSLog("Error parsing JSON Dictionary")
case ParsingError.unsupportedType:
NSLog("Type you are trying to parse is currently unsupported")
case let NetworkJSONServiceError.networkError(error):
NSLog(error.localizedDescription)
case NetworkJSONServiceError.noData:
NSLog("No data returned")
default:
NSLog("Unknown error occurred")
}
//case let .success(credentials)
case .success(_):
//returns credential object; however, values are stored securely in the Keychain and can be accessed as:
print(TwitchAuthorizationManager.sharedInstance.authToken)
}
}
)
}
return true
}
TwitchAuthorizationManager
是一个单例,可以使用共享实例访问以下 Twitch 认证返回的属性:
TwitchAuthorizationManager.sharedInstance.refreshToken
TwitchAuthorizationManager.sharedInstance.authToken
TwitchAuthorizationManager.sharedInstance.scopes
//Set the presenter object for the Model you want to display
fileprivate lazy var userPresenter: UserPresenter = {
return UserPresenter(dataSource: self)
}()
//Call the presenter's get method, in this case the user parameter is the name of the user you wish to retrieve
userPresenter.get(user: "test_user1")
//Extend the TwitchAPIDataSource
extension UserViewController: TwitchAPIDataSource {
public func set<T>(resource: T) {
if let user = resource as? User {
print(user.displayName)
} else {
NSLog("Invalid Generic Resource")
}
}
func startLoading(for resource: TwitchResource) {
NSLog("Started Loading: \(resource)")
}
func finishLoading(for resource: TwitchResource) {
NSLog("Finished Loading: \(resource)")
}
func handle(error: Error) {
NSLog("Error")
}
}
包含的示例项目包含了所有可使用的模型更详细的实现以及完整的 OAuth 设置。
UserPresenter
端点
EmotePresenter
端点
该项目包含单元测试,有助于更好地理解库。
请随时贡献,只要您遵循项目其余部分的编码格式。
Eric Vennaro, [email protected]
EVSlidingTableViewCell 在 MIT 许可证 下可用。有关更多信息,请参阅 LICENSE 文件。
版权所有 © 2016-至今 Eric Vennaro。