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

APIKit Tips

53e469a19bcb4584c87789d237128ca0?s=47 matuyuji
August 27, 2015

APIKit Tips

53e469a19bcb4584c87789d237128ca0?s=128

matuyuji

August 27, 2015
Tweet

Transcript

  1. APIKit Tips @matuyuji 2015.8.26 ؔ੢ϞόΠϧΞϓϦݚڀձ #5

  2. @matuyuji safx-dev.blogspot.jp

  3. ishkawa/APIKit let request = SomeRequest(…) API.sendRequest(request) { result in switch

    result { case .Success(let response): : case .Failure(let error): : } }
  4. let request = FooListRequest(…) API.sendRequest(request) { result in switch result

    { case .Success(let response): : case .Failure(let error): : } } public protocol Request { typealias Response : } ϓϩτίϧϕʔεͷ࣮૷ (σϑΥϧτ࣮૷͋Γ) struct FooListRequest: Request { typealias Response = [Foo] var path: String { return “list" } : } ϨεϙϯεͷܕΛਪ࿦Մೳ init࣌ʹσϑΥϧτ஋ΛઃఆՄೳ staticΦϒδΣΫτͳͷͰextensionͰม਺ͷ௥Ճ͕Մೳ
  5. safx/SlackKit SlackAPI.send(auth.test(token: token)) { result in switch result { case

    .Success(let res) : print(res) case .Failure(let error): print(error) } } testResponse( url=https://foobar-inc.slack.com/, team=foobar-inc, user=john, teamId=T0*******, userId=U0******* )
  6. safx/swift-idl enum channels { struct list: ClassInit, APIKitHelper, SlackRequest {

    // router:",channels.list" typealias APIKitResponse = ChannelsResult let token: Token let excludeArchived: Int = 0 // json:"exclude_archived" } } public struct list: SlackRequest { public typealias APIKitResponse = ChannelsResult public let token: Token public let excludeArchived: Int public init(token: Token, excludeArchived: Int = 0) { self.token = token self.excludeArchived = excludeArchived } public var method: HTTPMethod { return .GET } public var path: String { return “channels.list" } public var parameters: [String: AnyObject] { return ["token": token.toJSON(), “exclude_archived": excludeArchived.toJSON()] } }
  7. Slack API { "ok": true, "url": "https://foobar.slack.com/", "team": "foobar", "user":

    "john", "team_id": “T0*******”, "user_id": “U0*******” } { "ok": false, "error": "not_authed" }
  8. APIKitRequest public protocol APIKitRequest: Request { typealias APIKitResponse typealias Response

    = SimpleResult<APIKitResponse> } public protocol SlackRequest: APIKitRequest {} public protocol SlackOAuthRequest: APIKitRequest {} public enum SimpleResult<Type> { case OK(Type) case Error(SlackError) }
  9. extension APIKitRequest where Self.APIKitResponse: JSONDecodable, Self.APIKitResponse == Self.APIKitResponse.DecodedType { public

    func responseFromObject(object: AnyObject, URLResponse: NSHTTPURLResponse) -> SimpleResult<APIKitResponse>? { guard let ok = object["ok"] as? Bool else { return nil } if !ok { guard let error = object["error"] as? String else { return nil } return .Error(SlackError(rawText: error)) } do { return .OK(try Self.APIKitResponse.parseJSON(object)) } catch { return .Error(.JSONError(error)) } } }
  10. Result<SimpleResult<APIKitResponse>, APIError> SlackAPI.sendRequest(auth.test(token: token)) { result in switch result {

    case .Success(let res): switch res { case .OK(let r): print(r) case .Error(let err): print(err) } case .Failure(let error): print(error) } }
  11. SlackAPI.send extension SlackAPI { public class func send<T: APIKitRequest where

    T.Response: SimpleResultType, T.Response.Element == T.APIKitResponse>(request: T, URLSession: NSURLSession = defaultURLSession, handler: (Result<T.APIKitResponse, SlackError>) -> Void = {r in}) -> NSURLSessionDataTask? { func trampoline(handler: (Result<T.APIKitResponse, SlackError>) -> Void) (result: Result<T.Response, APIError>) { switch result { case .Success(let some): switch some.result { case .OK(let res): handler(.Success(res)) case .Error(let err): handler(.Failure(err)) } case .Failure(let error): handler(.Failure(.OtherError(error))) } } return sendRequest(request, URLSession: URLSession, handler: trampoline(handler)) } }
  12. public protocol SimpleResultType { typealias Element var result: SimpleResult<Element> {

    get } } public protocol APIKitRequest: Request { typealias APIKitResponse typealias Response = SimpleResult<APIKitResponse> }