teepsllc / BuckoNetworking

iOS Protocol-Oriented Networking in Swift
MIT License
18 stars 3 forks source link

Promises #5

Closed chayelheinsen closed 6 years ago

chayelheinsen commented 6 years ago

This PR adds Promises to Bucko.

struct User: Decodable {
  var name: String
  var phoneNumber: String

  enum CodingKeys: String, CodingKey {
    case name
    case phoneNumber = "phone_number"
  }
}

struct UserService: DecodableEndpoint {
  typealias ResponseType = User
  var baseURL: String { return "https://example.com" }
  var path: String { return "/users" }
  var method: HTTPMethod { return .get }
  var body: Parameters { return Parameters() }
  var headers: HTTPHeaders { return HTTPHeaders() }
}

// You can now use:

Bucko.shared.request(UserService()).then { user in
  // Do something with user
}.catch { error in
  // Do something with error
}

// or

UserService().request().then { user in
  // Do something with user
}.catch { error in
  // Do something with error
}

Enums still work as well:

enum UserService: Endpoint {
  case index

  var baseURL: String { return "https://example.com" }
  var path: String { return "/users" }
  var method: HTTPMethod { return .get }
  var body: Parameters { return Parameters() }
  var headers: HTTPHeaders { return HTTPHeaders() }
}

// Use your Endpoint
UserService.index.request(responseType: [User].self).then { users in
  // Do something with users
}.catch { error in
 // Do something with error
}