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

APIKit Tips

APIKit Tips

matuyuji

August 27, 2015
Tweet

More Decks by matuyuji

Other Decks in Programming

Transcript

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

    result { case .Success(let response): : case .Failure(let error): : } }
  2. 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Ͱม਺ͷ௥Ճ͕Մೳ
  3. 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******* )
  4. 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()] } }
  5. Slack API { "ok": true, "url": "https://foobar.slack.com/", "team": "foobar", "user":

    "john", "team_id": “T0*******”, "user_id": “U0*******” } { "ok": false, "error": "not_authed" }
  6. 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) }
  7. 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)) } } }
  8. 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) } }
  9. 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)) } }
  10. public protocol SimpleResultType { typealias Element var result: SimpleResult<Element> {

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