OctoKit.swift 0.8.0

OctoKit.swift 0.8.0

测试已测试
语言编程语言 SwiftSwift
许可证 MIT
发布最后发布2018年3月
SPM支持 SPM

Piet Brauer 维护。



Octokit.swift

Build Status codecov.io

身份验证

Octokit 支持GitHub和GitHub企业版,身份验证通过配置进行处理。

有两种类型的配置,分别是 TokenConfigurationOAuthConfiguration

TokenConfiguration

TokenConfiguration 适用于使用基于访问令牌的身份验证(例如,用户向您提供了一个在网站上生成的访问令牌)或如果通过 OAuth 流程获得访问令牌的情况。

您可以使用以下方式初始化用于 github.com 的新配置

let config = TokenConfiguration(token: "12345")

或者,对于 GitHub 企业版

let config = TokenConfiguration("12345", url: "https://github.example.com/api/v3/")

获取令牌后,您可以使用它在 Octokit 中使用

Octokit(config).me() { response in
  switch response {
  case .success(let user):
    print(user.login)
  case .failure(let error):
    print(error)
  }
}

OAuthConfiguration

OAuthConfiguration 适用于您还没有访问令牌,并且用户需要登录到您的应用程序的情况。它也处理 OAuth 流程。

您可以按照以下步骤对 github.com 进行用户认证:

let config = OAuthConfiguration(token: "<Your Client ID>", secret: "<Your Client secret>", scopes: ["repo", "read:org"])
let url = config.authenticate()

或者,对于 GitHub 企业版

let config = OAuthConfiguration("https://github.example.com/api/v3/", webURL: "https://github.example.com/", token: "<Your Client ID>", secret: "<Your Client secret>", scopes: ["repo", "read:org"])

在获取配置之后,您可以对用户进行认证。

// AppDelegate.swift

config.authenticate()

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
  config.handleOpenURL(url) { config in
    self.loadCurrentUser(config) // purely optional of course
  }
  return false
}

func loadCurrentUser(config: TokenConfiguration) {
  Octokit(config).me() { response in
    switch response {
    case .success(let user):
      print(user.login)
    case .failure(let error):
      print(error)
    }
  }
}

请注意,您会从 OAuth 流中收到一个 TokenConfiguration。您需要自己存储 accessToken。如果您想进行更多的请求,不必再次执行 OAuth 流。您可以仅使用 TokenConfiguration

let token = // get your token from your keychain, user defaults (not recommended) etc.
let config = TokenConfiguration(token)
Octokit(config).user("octocat") { response in
  switch response {
  case .success(let user):
  	print("User login: \(user.login!)")
  case .failure(let error):
  	print("Error: \(error)")
  }
}

用户

获取单个用户

let username = ... // set the username
Octokit().user(username) { response in
  switch response {
    case .success(let user):
      // do something with the user
    case .failure(let error):
      // handle any errors
  }
}

获取认证用户

Octokit().me() { response in
  switch response {
    case .success(let user):
      // do something with the user
    case .failure(let error):
      // handle any errors
  }

仓库

获取单个仓库

let (owner, name) = ("owner", "name") // replace with actual owner and name
Octokit().repository(owner, name) { response in
  switch response {
    case .success(let repository):
      // do something with the repository
    case .failure(let error):
      // handle any errors
  }
}

获取认证用户的仓库

Octokit().repositories() { response in
  switch response {
    case .success(let repository):
      // do something
    case .failure(let error):
      // handle any errors
  }
}

标星仓库

获取某个用户的标星仓库

let username = "username"
Octokit().stars(username) { response in
  switch response {
    case .success(let repositories):
      // do something with the repositories
    case .failure(let error):
      // handle any errors
  }
}

获取授权用户的标星仓库

Octokit().myStars() { response in
  switch response {
    case .success(let repositories):
      // do something with the repositories
    case .failure(let error):
      // handle any errors
  }
}

粉丝和关注对象

获取某个用户的粉丝

let username = "username"
Octokit().followers(username) { response in
  switch response {
    case .success(let users):
      // do something with the users
    case .failure(let error):
      // handle any errors
  }
}

获取授权用户的粉丝

Octokit().myFollowers() { response in
  switch response {
    case .success(let users):
      // do something with the users
    case .failure(let error):
      // handle any errors
  }
}

获取某个用户的关注对象

let username = "username"
Octokit().following(username) { response in
  switch response {
    case .success(let users):
      // do something with the users
    case .failure(let error):
      // handle any errors
  }
}

获取认证用户的关注者

Octokit().myFollowing() { response in
  switch response {
    case .success(let users):
      // do something with the users
    case .failure(let error):
      // handle any errors
  }
}

问题

获取认证用户的问题

获取所有认证用户可见仓库的所有问题,包括属于用户自己的仓库、成员仓库和组织仓库中的问题。

Octokit(config).myIssues() { response in
    switch response {
        case .success(let issues):
        // do something with the issues
    case .failure:
        // handle any errors
    }   
}

获取单个问题

let (owner, repo, number) = ("owner", "repo", 1347) // replace with actual owner, repo name, and issue number
Octokit(config).issue(owner, repository: repo, number: number) { response in
    switch response {
    case .success(let issue):
        // do something with the issue
    case .failure:
        // handle any errors
    }
}

开启新问题

Octokit(config).postIssue("owner", repository: "repo", title: "Found a bug", body: "I'm having a problem with this.", assignee: "octocat") { response in
    switch response {
    case .success(let issue):
        // do something with the issue
    case .failure:
        // handle any errors
    }
}

编辑现有问题

Octokit(config).patchIssue("owner", repository: "repo", number: 1347, title: "Found a bug", body: "I'm having a problem with this.", assignee: "octocat", state: .Closed) { response in
    switch response {
    case .success(let issue):
        // do something with the issue
    case .failure:
        // handle any errors
    }
}

拉取请求

获取单个拉取请求

let task = Octokit().pullRequest(session, owner: "octocat", repository: "Hello-World", number: 1) { response in
    switch response {
        case .success(let pullRequests):
            // do something with a pull request
        case .failure:
            // handle any errors
     }
}

列出拉取请求

let task = Octokit().pullRequests(session, owner: "octocat", repository: "Hello-World", base: "develop", state: Openness.Open) { response in
    switch response {
        case .success(let pullRequests):
        // do something with a pull request list
        case .failure:
        // handle any errors
    }
}