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

Experiment: Add Builds request #9

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
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
90 changes: 90 additions & 0 deletions TanukiKit/Builds.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import Foundation
import RequestKit

@objc public class apiBuildClass: NSObject {
public let id: Int
public let user: apiUserClass
public var status: String?
public var stage: String?
public var ref: String?
public var tag: Bool?
public var coverage: Double?
public var createdAt: String?
public var startedAt: String?
public var finishedAt: String?

public init(_ json: [String: AnyObject]) {
user = apiUserClass(json["user"] as? [String: AnyObject] ?? [:])
if let id = json["id"] as? Int {
self.id = id
status = json["status"] as? String
stage = json["stage"] as? String
ref = json["ref"] as? String
tag = json["tag"] as? Bool
coverage = json["coverage"] as? Double
createdAt = json["created_at"] as? String
startedAt = json["started_at"] as? String
finishedAt = json["finished_at"] as? String
} else {
id = -1
}
}
}

public extension TanukiKit {

/**
Fetches the Builds for the specified Project
- parameter Project: The Project to get the builds from.
- parameter Build: The specific build to get, if nil, gives a list of all the builds available.
- parameter completion: Callback for the outcome of the fetch.
*/

public func builds(project: String, build: String, completion: (response: Response<[apiBuildClass]>) -> Void) {
let router = BuildRouter.ReadProjectBuilds(configuration, project, build)
router.loadJSON([[String: AnyObject]].self) { json, error in
if let error = error {
completion(response: Response.Failure(error))
}
if let json = json {
let repos = json.map { apiBuildClass($0) }
completion(response: Response.Success(repos))
}
}
}
}

// MARK: Router

enum BuildRouter: Router {
case ReadProjectBuilds(Configuration, String, String)

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

var method: HTTPMethod {
return .GET
}

var encoding: HTTPEncoding {
return .URL
}

var params: [String: String] {
switch self {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be:

return [:]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enum BuildRouter: Router {
    case ReadProjectBuilds(Configuration, String, String)

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

    var method: HTTPMethod {
        return .GET
    }

    var encoding: HTTPEncoding {
        return .URL
    }

    var params: [String: String] {
        return [:]
    }

    var path: String {
        switch self {
        case .ReadProjectBuilds(_, let project, let build):
            return "/projects/\(project)/builds/\(build)"
        }
    }
}

Yup! It works! Thank you so much!

case .ReadProjectBuilds(_, let project, let build):
return ["project": project, "build": build]
}
}

var path: String {
switch self {
case .ReadProjectBuilds:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be:

case .ReadProjectBuilds(_, let project, let build):
    return "/projects/\(project)/builds/\(build)"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the params? Really? Shouldn't it be on the path? And if not then what should the path be?

return "/projects/\(params["project"])/builds/\(params["build"])"
}
}
}