TwitchAPIWrapper 1.0.0

TwitchAPIWrapper 1.0.0

测试测试
语言语言 SwiftSwift
许可证 MIT
发布日期最后发布日期2021 年 8 月
SPM支持 SPM

Eric Vennaro 维护。



  • 作者:
  • epv44

Twitch API Wrapper

关于

Twitch API Wrapper 是一个 Twitch API 的 HTTP 客户端,它包含了对不同 Twitch 端点的对象映射,可以通过提供的数据源轻松显示。该库还包含一个 OAuth 客户端,允许您访问 Twitch 的受保护端点以及您的应用程序的 OAuth。

当前进度

  • 为用户 Twitch 端点创建了表示对象。
  • 为表情 Twitch 端点创建了表示对象。
  • 实现了 OAuth 客户端。
  • 创建了示例应用程序。

待办事项

  • 为其他端点构建模型/表示对象。
  • 改进示例应用程序,演示如何连接到所有端点。
  • 测试覆盖率。
  • Swift 包管理器连接。
  • 在 travis-ci 中构建 Carthage。
  • 提高代码覆盖率。
  • 构建 tvOS 目标和示例项目。
  • 构建 macOS 目标和示例项目。
  • 添加模型透镜

需求

  • Swift 3.0+
  • iOS 10.0+

文档

阅读 文档。由 jazzy 生成

入门

import TwitchAPIWrapper

设计

此库有两个主要部分

  1. 授权 - 在您的应用程序中使用 Twitch OAuth 协议。
  2. 表示对象 - 展示与 Twitch API 端点对应的提供模型对象。

示例

以下示例说明了如何设置 OAuth 组件并利用表示对象(数据源)来检索模型。

Twitch OAuth

重要:在授权之前确保

  1. 添加一个 URL 类型并指定与 Twitch 匹配的 URL Scheme。
  2. 设置正确的 clientID、redirectURI、scopes 和 clientSecret,因为这些是成功认证所必需的。
  3. 建议不要将上述任何内容放入版本控制中,如下所示示例应用程序。
// 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 认证返回的属性:

  1. 刷新令牌:TwitchAuthorizationManager.sharedInstance.refreshToken
  2. 认证令牌:TwitchAuthorizationManager.sharedInstance.authToken
  3. 作用域: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

端点

  1. GET /users/:user
  2. GET /user (需要授权)

表情

EmotePresenter

端点

  1. GET /users/:user/emotes (需要授权)

单元测试

该项目包含单元测试,有助于更好地理解库。

贡献

请随时贡献,只要您遵循项目其余部分的编码格式。

作者

Eric Vennaro, [email protected]

许可

EVSlidingTableViewCell 在 MIT 许可证 下可用。有关更多信息,请参阅 LICENSE 文件。

版权所有 © 2016-至今 Eric Vennaro。