Skip to content

Instantly share code, notes, and snippets.

@rizumita
Created October 21, 2015 07:24
Show Gist options
  • Save rizumita/ba45f9ee6f039ae2c09b to your computer and use it in GitHub Desktop.
Save rizumita/ba45f9ee6f039ae2c09b to your computer and use it in GitHub Desktop.
protocolで組み上げるRESTクライアントライブラリ書いてる。 #swift #CodePiece
protocol TestResourceType: JRResourceType {
typealias ServiceType = TestService
}
struct TestService: JRServiceType {
static let baseURL: NSURL? = NSURL(string: "https://example.com/api")
struct UsersResource: TestResourceType, JRPaginate {
static let path: JRPathType = "/users"
static var pageSerializer = JSONPageSerializer(contentsDecoder: JSONDecoder(keyPath: "users") { try decodeArray($0) as [User] }, paginationDecoder: JSONPaginationDecoder(keyPath: "pagination") { try decode($0) as Pagination })
}
struct UserResource: TestResourceType, JRGet, JRPost {
static let getSerializer = JSONSerializer(contentsDecoder: JSONDecoder { try decode($0) as User })
static let postSerializer = JSONSerializer(contentsDecoder: JSONDecoder { try decode($0) as User })
static let path: JRPathType = "/user/:id"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment