Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import SwiftFormat #117

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# file options

--exclude Tests/LinuxMain.swift,Package.swift

# format options
--swiftversion 4.0
--allman false
--commas inline
--ifdef no-indent
--importgrouping testable-bottom
--maxwidth 120
--wrapparameters before-first

# disabled format
--disable wrapEnumCases
--disable numberFormatting
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ post_coverage:
bundle exec slather coverage --input-format profdata -x --ignore "../**/*/Xcode*" --ignore "Carthage/**" --output-directory slather-report --scheme OctoKit OctoKit.xcodeproj
curl -X POST -d @slather-report/cobertura.xml "https://codecov.io/upload/v2?token="$(CODECOV_TOKEN)"&commit="$(SHA)"&branch="$(BRANCH)"&job="$(TRAVIS_BUILD_NUMBER)

format:
mint run SwiftFormat swiftformat .
1 change: 1 addition & 0 deletions Mintfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nicklockwood/SwiftFormat@0.47.8
64 changes: 39 additions & 25 deletions OctoKit/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public struct TokenConfiguration: Configuration {
///
/// Used for preview support of new APIs, for instance Reaction API.
/// see: https://developer.github.com/changes/2016-05-12-reactions-api-preview/
private var previewCustomHeaders: Array<HTTPHeader>?
private var previewCustomHeaders: [HTTPHeader]?

public var customHeaders: Array<HTTPHeader>? {
public var customHeaders: [HTTPHeader]? {
// More (non-preview) headers can be appended if needed in the future
return previewCustomHeaders
}
Expand All @@ -46,31 +46,41 @@ public struct OAuthConfiguration: Configuration {
///
/// Used for preview support of new APIs, for instance Reaction API.
/// see: https://developer.github.com/changes/2016-05-12-reactions-api-preview/
private var previewCustomHeaders: Array<HTTPHeader>?
private var previewCustomHeaders: [HTTPHeader]?

public var customHeaders: Array<HTTPHeader>? {
public var customHeaders: [HTTPHeader]? {
// More (non-preview) headers can be appended if needed in the future
return previewCustomHeaders
}

public init(_ url: String = githubBaseURL, webURL: String = githubWebURL,
token: String, secret: String, scopes: [String], previewHeaders: [PreviewHeader] = []) {
apiEndpoint = url
webEndpoint = webURL
self.token = token
self.secret = secret
self.scopes = scopes
previewCustomHeaders = previewHeaders.map { $0.header }
public init(
_ url: String = githubBaseURL,
webURL: String = githubWebURL,
token: String,
secret: String,
scopes: [String],
previewHeaders: [PreviewHeader] = []
) {
apiEndpoint = url
webEndpoint = webURL
self.token = token
self.secret = secret
self.scopes = scopes
previewCustomHeaders = previewHeaders.map { $0.header }
}

public func authenticate() -> URL? {
return OAuthRouter.authorize(self).URLRequest?.url
}

public func authorize(_ session: RequestKitURLSession = URLSession.shared, code: String, completion: @escaping (_ config: TokenConfiguration) -> Void) {
public func authorize(
_ session: RequestKitURLSession = URLSession.shared,
code: String,
completion: @escaping (_ config: TokenConfiguration) -> Void
) {
let request = OAuthRouter.accessToken(self, code).URLRequest
if let request = request {
let task = session.dataTask(with: request) { data, response, err in
let task = session.dataTask(with: request) { data, response, _ in
if let response = response as? HTTPURLResponse {
if response.statusCode != 200 {
return
Expand All @@ -89,9 +99,13 @@ public struct OAuthConfiguration: Configuration {
}
}

public func handleOpenURL(_ session: RequestKitURLSession = URLSession.shared, url: URL, completion: @escaping (_ config: TokenConfiguration) -> Void) {
public func handleOpenURL(
_ session: RequestKitURLSession = URLSession.shared,
url: URL,
completion: @escaping (_ config: TokenConfiguration) -> Void
) {
if let code = url.URLParameters["code"] {
authorize(session, code: code) { (config) in
authorize(session, code: code) { config in
completion(config)
}
}
Expand All @@ -112,8 +126,8 @@ enum OAuthRouter: Router {

var configuration: Configuration {
switch self {
case .authorize(let config): return config
case .accessToken(let config, _): return config
case let .authorize(config): return config
case let .accessToken(config, _): return config
}
}

Expand Down Expand Up @@ -146,27 +160,27 @@ enum OAuthRouter: Router {

var params: [String: Any] {
switch self {
case .authorize(let config):
case let .authorize(config):
let scope = (config.scopes as NSArray).componentsJoined(by: ",")
return ["scope": scope, "client_id": config.token, "allow_signup": "false"]
case .accessToken(let config, let code):
case let .accessToken(config, code):
return ["client_id": config.token, "client_secret": config.secret, "code": code]
}
}

#if canImport(FoundationNetworking)
#if canImport(FoundationNetworking)
typealias FoundationURLRequestType = FoundationNetworking.URLRequest
#else
#else
typealias FoundationURLRequestType = Foundation.URLRequest
#endif
#endif

var URLRequest: FoundationURLRequestType? {
switch self {
case .authorize(let config):
case let .authorize(config):
let url = URL(string: path, relativeTo: URL(string: config.webEndpoint)!)
let components = URLComponents(url: url!, resolvingAgainstBaseURL: true)
return request(components!, parameters: params)
case .accessToken(let config, _):
case let .accessToken(config, _):
let url = URL(string: path, relativeTo: URL(string: config.webEndpoint)!)
let components = URLComponents(url: url!, resolvingAgainstBaseURL: true)
return request(components!, parameters: params)
Expand Down
3 changes: 1 addition & 2 deletions OctoKit/File.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ open class File: Codable {
open var type: String?
open var language: String?
open var size: Int?

enum CodingKeys: String, CodingKey {
case rawURL = "raw_url"
case filename
case type
case language
case size
}

}
71 changes: 42 additions & 29 deletions OctoKit/Follow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import FoundationNetworking
#endif

public extension Octokit {

/**
Fetches the followers of the authenticated user
- parameter session: RequestKitURLSession, defaults to NSURLSession.sharedSession()
- parameter completion: Callback for the outcome of the fetch.
*/
Fetches the followers of the authenticated user
- parameter session: RequestKitURLSession, defaults to NSURLSession.sharedSession()
- parameter completion: Callback for the outcome of the fetch.
*/
@discardableResult
func myFollowers(_ session: RequestKitURLSession = URLSession.shared, completion: @escaping (_ response: Response<[User]>) -> Void) -> URLSessionDataTaskProtocol? {
func myFollowers(
_ session: RequestKitURLSession = URLSession.shared,
completion: @escaping (_ response: Response<[User]>) -> Void
) -> URLSessionDataTaskProtocol? {
let router = FollowRouter.readAuthenticatedFollowers(configuration)
return router.load(session, expectedResultType: [User].self) { users, error in
if let error = error {
Expand All @@ -26,13 +28,17 @@ public extension Octokit {
}

/**
Fetches the followers of a user
- parameter session: RequestKitURLSession, defaults to NSURLSession.sharedSession()
- parameter name: Name of the user
- parameter completion: Callback for the outcome of the fetch.
*/
Fetches the followers of a user
- parameter session: RequestKitURLSession, defaults to NSURLSession.sharedSession()
- parameter name: Name of the user
- parameter completion: Callback for the outcome of the fetch.
*/
@discardableResult
func followers(_ session: RequestKitURLSession = URLSession.shared, name: String, completion: @escaping (_ response: Response<[User]>) -> Void) -> URLSessionDataTaskProtocol? {
func followers(
_ session: RequestKitURLSession = URLSession.shared,
name: String,
completion: @escaping (_ response: Response<[User]>) -> Void
) -> URLSessionDataTaskProtocol? {
let router = FollowRouter.readFollowers(name, configuration)
return router.load(session, expectedResultType: [User].self) { users, error in
if let error = error {
Expand All @@ -46,12 +52,15 @@ public extension Octokit {
}

/**
Fetches the users following the authenticated user
- parameter session: RequestKitURLSession, defaults to NSURLSession.sharedSession()
- parameter completion: Callback for the outcome of the fetch.
*/
Fetches the users following the authenticated user
- parameter session: RequestKitURLSession, defaults to NSURLSession.sharedSession()
- parameter completion: Callback for the outcome of the fetch.
*/
@discardableResult
func myFollowing(_ session: RequestKitURLSession = URLSession.shared, completion: @escaping (_ response: Response<[User]>) -> Void) -> URLSessionDataTaskProtocol? {
func myFollowing(
_ session: RequestKitURLSession = URLSession.shared,
completion: @escaping (_ response: Response<[User]>) -> Void
) -> URLSessionDataTaskProtocol? {
let router = FollowRouter.readAuthenticatedFollowing(configuration)
return router.load(session, expectedResultType: [User].self) { users, error in
if let error = error {
Expand All @@ -65,13 +74,17 @@ public extension Octokit {
}

/**
Fetches the users following a user
- parameter session: RequestKitURLSession, defaults to NSURLSession.sharedSession()
- parameter name: The name of the user
- parameter completion: Callback for the outcome of the fetch.
*/
Fetches the users following a user
- parameter session: RequestKitURLSession, defaults to NSURLSession.sharedSession()
- parameter name: The name of the user
- parameter completion: Callback for the outcome of the fetch.
*/
@discardableResult
func following(_ session: RequestKitURLSession = URLSession.shared, name: String, completion: @escaping (_ response: Response<[User]>) -> Void) -> URLSessionDataTaskProtocol? {
func following(
_ session: RequestKitURLSession = URLSession.shared,
name: String,
completion: @escaping (_ response: Response<[User]>) -> Void
) -> URLSessionDataTaskProtocol? {
let router = FollowRouter.readFollowing(name, configuration)
return router.load(session, expectedResultType: [User].self) { users, error in
if let error = error {
Expand Down Expand Up @@ -101,22 +114,22 @@ enum FollowRouter: Router {

var configuration: Configuration {
switch self {
case .readAuthenticatedFollowers(let config): return config
case .readFollowers(_, let config): return config
case .readAuthenticatedFollowing(let config): return config
case .readFollowing(_, let config): return config
case let .readAuthenticatedFollowers(config): return config
case let .readFollowers(_, config): return config
case let .readAuthenticatedFollowing(config): return config
case let .readFollowing(_, config): return config
}
}

var path: String {
switch self {
case .readAuthenticatedFollowers:
return "user/followers"
case .readFollowers(let username, _):
case let .readFollowers(username, _):
return "users/\(username)/followers"
case .readAuthenticatedFollowing:
return "user/following"
case .readFollowing(let username, _):
case let .readFollowing(username, _):
return "users/\(username)/following"
}
}
Expand Down
Loading