Created
May 12, 2018 01:36
-
-
Save yimajo/24ec21e626d57691a3e5bd36a5463386 to your computer and use it in GitHub Desktop.
ATND APIののAPIKit.Request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import APIKit | |
struct AtndAPI {} | |
extension AtndAPI { | |
struct SearchResult: Decodable { | |
let events: [EventContainer] | |
} | |
struct EventContainer: Decodable { | |
let event: Event | |
} | |
struct Event: Decodable { | |
let title: String | |
} | |
} | |
extension AtndAPI { | |
final class DecodableDataParser: DataParser { | |
var contentType: String? { | |
return "application/json" | |
} | |
func parse(data: Data) throws -> Any { | |
return data | |
} | |
} | |
} | |
protocol AtndRequest: Request {} | |
extension AtndRequest { | |
var baseURL: URL { | |
return URL(string: "https://api.atnd.org/")! | |
} | |
} | |
extension AtndAPI { | |
struct SearchRequest: AtndRequest { | |
typealias Response = SearchResult | |
let keyword: String | |
let method: HTTPMethod = .get | |
let path: String = "events" | |
var dataParser: DataParser { | |
return DecodableDataParser() | |
} | |
var parameters: Any? { | |
return ["keyword": keyword, "format": "json"] | |
} | |
func response(from object: Any, urlResponse: HTTPURLResponse) throws -> Response { | |
return try JSONDecoder().decode(Response.self, from: object as! Data) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment