Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introducing APIKit

Introducing APIKit

Yosuke Ishikawa

March 12, 2015
Tweet

More Decks by Yosuke Ishikawa

Other Decks in Technology

Transcript

  1. let path = "/search/repositories" let parameters = [ "q": "APIKit",

    "sort": "stars", "order": "asc", ] GitHub.call(.GET, path, parameters) { JSON, error in if error != nil { println("error: \(error!.localizedDescription)”) } else { self.repositories = /* model object from JSON */ } }
  2. let path = "/search/repositories" let parameters = [ "q": "APIKit",

    "sort": "stars", "order": "asc", ] GitHub.call(.GET, path, parameters) { JSON, error in if error != nil { println("error: \(error!.localizedDescription)”) } else { self.repositories = /* model object from JSON */ } } ✗ ΤϯυϙΠϯτ͕ଘࡏ͢Δ͔ෆ໌
  3. let path = "/search/repositories" let parameters = [ "q": "APIKit",

    "sort": "stars", "order": "asc", ] GitHub.call(.GET, path, parameters) { JSON, error in if error != nil { println("error: \(error!.localizedDescription)”) } else { self.repositories = /* model object from JSON */ } } ✗ ύϥϝʔλʔʹ ԿͰ΋ಥͬࠐΊΔ
  4. let path = "/search/repositories" let parameters = [ "q": "APIKit",

    "sort": "stars", "order": "asc", ] GitHub.call(.GET, path, parameters) { JSON, error in if error != nil { println("error: \(error!.localizedDescription)”) } else { self.repositories = /* model object from JSON */ } } ✗ ྆ํoptionalͳͷͰ4ύλʔϯ
  5. let path = "/search/repositories" let parameters = [ "q": "APIKit",

    "sort": "stars", "order": "asc", ] GitHub.call(.GET, path, parameters) { JSON, error in if error != nil { println("error: \(error!.localizedDescription)”) } else { self.repositories = /* model object from JSON */ } } ✗ ྆ํoptionalͳͷͰ4ύλʔϯ JSON Error .None .None .None .Some .Some .None .Some .Some
  6. let request = GitHub.Endpoint.SearchRepositories(query: "APIKit", sort: .Stars, order: .Ascending) GitHub.sendRequest(request)

    { response in switch response { case .Success(let box): self.repositories = box.unbox case .Failure(let box): println("error: \(box.unbox.localizedDescription)") } }
  7. let request = GitHub.Endpoint.SearchRepositories(query: "APIKit", sort: .Stars, order: .Ascending) GitHub.sendRequest(request)

    { response in switch response { case .Success(let box): self.repositories = box.unbox case .Failure(let box): println("error: \(box.unbox.localizedDescription)") } } ✓ ΤϯυϙΠϯτ͕ิ׬͞ΕΔ
  8. let request = GitHub.Endpoint.SearchRepositories(query: "APIKit", sort: .Stars, order: .Ascending) GitHub.sendRequest(request)

    { response in switch response { case .Success(let box): self.repositories = box.unbox case .Failure(let box): println("error: \(box.unbox.localizedDescription)") } } ✓ ύϥϝʔλʔΛܕͰ੍ݶ
  9. let request = GitHub.Endpoint.SearchRepositories(query: "APIKit", sort: .Stars, order: .Ascending) GitHub.sendRequest(request)

    { response in switch response { case .Success(let box): self.repositories = box.unbox case .Failure(let box): println("error: \(box.unbox.localizedDescription)") } } ✓ ύϥϝʔλʔΛܕͰ੍ݶ ✓ enum΋࢖͑Δ
  10. let request = GitHub.Endpoint.SearchRepositories(query: "APIKit", sort: .Stars, order: .Ascending) GitHub.sendRequest(request)

    { response in switch response { case .Success(let box): self.repositories = box.unbox case .Failure(let box): println("error: \(box.unbox.localizedDescription)") } } ✓ ݁Ռ͸2ύλʔϯ
  11. let request = GitHub.Endpoint.SearchRepositories(query: "APIKit", sort: .Stars, order: .Ascending) GitHub.sendRequest(request)

    { response in switch response { case .Success(let box): self.repositories = box.unbox case .Failure(let box): println("error: \(box.unbox.localizedDescription)") } } ✓ ݁Ռ͸2ύλʔϯ ✓ ໢ཏੑ΋໌Β͔
  12. let request = GitHub.Endpoint.SearchRepositories(query: "APIKit", sort: .Stars, order: .Ascending) GitHub.sendRequest(request)

    { response in switch response { case .Success(let box): self.repositories = box.unbox case .Failure(let box): println("error: \(box.unbox.localizedDescription)") } } ✓ unwrapͤͣʹ ஋ΛऔΓग़ͤΔ
  13. let request = GitHub.Endpoint.SearchRepositories(query: "APIKit", sort: .Stars, order: .Ascending) GitHub.sendRequest(request)

    { response in switch response { case .Success(let box): self.repositories = box.unbox case .Failure(let box): println("error: \(box.unbox.localizedDescription)") } } ✓ unwrapͤͣʹ ஋ΛऔΓग़ͤΔ ✓ ஋ͷܕ͸ϦΫΤετ͔Βਪ࿦͞ΕΔ
  14. let request = GitHub.Endpoint.SearchRepositories(query: "APIKit", sort: .Stars, order: .Ascending) GitHub.sendRequest(request)

    { response in switch response { case .Success(let box): self.repositories = box.unbox case .Failure(let box): println("error: \(box.unbox.localizedDescription)") } } ✓ unwrapͤͣʹ ஋ΛऔΓग़ͤΔ ✓ ஋ͷܕ͸ϦΫΤετ͔Βਪ࿦͞ΕΔ [Repository]
  15. let request = GitHub.Endpoint.SearchRepositories(query: "APIKit", sort: .Stars, order: .Ascending) GitHub.sendRequest(request)

    { response in switch response { case .Success(let box): self.repositories = box.unbox case .Failure(let box): println("error: \(box.unbox.localizedDescription)") } } ✓ unwrapͤͣʹ ஋ΛऔΓग़ͤΔ ✓ ஋ͷܕ͸ϦΫΤετ͔Βਪ࿦͞ΕΔ [Repository] ✓ ࣦഊ࣌͸NSError͕ೖΔ NSError
  16. let request = GitHub.Endpoint.SearchRepositories(query: "APIKit", sort: .Stars, order: .Ascending) GitHub.sendRequest(request)

    { response in switch response { case .Success(let box): self.repositories = box.unbox case .Failure(let box): println("error: \(box.unbox.localizedDescription)") } } ✓ unwrapͤͣʹ ஋ΛऔΓग़ͤΔ ✓ ஋ͷܕ͸ϦΫΤετ͔Βਪ࿦͞ΕΔ [Repository] ✓ ࣦഊ࣌͸NSError͕ೖΔ NSError LlamaKit.Result<T.Response, NSError>
  17. public protocol Request { typealias Response: Any var URLRequest: NSURLRequest?

    { get } func responseFromObject(object: AnyObject) -> Response? } Requestϓϩτίϧʹ ద߹͢Ε͹׬ྃ
  18. public protocol Request { typealias Response: Any var URLRequest: NSURLRequest?

    { get } func responseFromObject(object: AnyObject) -> Response? } Requestϓϩτίϧʹ ద߹͢Ε͹׬ྃ ֤ΤϯυϙΠϯτͷఆٛ͸1ՕॴͰࡁΉ
  19. class SearchRepositories: APIKit.Request { enum Sort: String { case Stars

    = "stars" case Forks = "forks" case Updated = "updated" } enum Order: String { case Ascending = "asc" case Descending = "desc" } typealias Response = [Repository] let query: String let sort: Sort let order: Order var URLRequest: NSURLRequest? { return GitHub.URLRequest(.GET, "/search/repositories", [ "q": query, "sort": sort.rawValue, "order": order.rawValue ]) } init(query: String, sort: Sort = .Stars, order: Order = .Ascending) { self.query = query self.sort = sort self.order = order } }