Skip to content

Instantly share code, notes, and snippets.

@yimajo
Last active May 12, 2018 01:35
Show Gist options
  • Save yimajo/5637e7ba98e29e06af33cbc51ec86b3f to your computer and use it in GitHub Desktop.
Save yimajo/5637e7ba98e29e06af33cbc51ec86b3f to your computer and use it in GitHub Desktop.
Connpass APIのAPIKit.Request
import Foundation
import APIKit
struct ConnpassAPI {}
extension ConnpassAPI {
struct SearchResult: Decodable {
let events: [Event]
}
struct Event: Decodable {
let title: String
}
}
extension ConnpassAPI {
final class DecodableDataParser: DataParser {
var contentType: String? {
return "application/json"
}
func parse(data: Data) throws -> Any {
return data
}
}
}
extension ConnpassAPI {
struct SearchRequest: Request {
typealias Response = SearchResult
let baseURL = URL(string: "https://connpass.com/api/v1/")!
let keyword: String
let method: HTTPMethod = .get
let path = "event"
var dataParser: DataParser {
return DecodableDataParser()
}
var parameters: Any? {
return ["keyword": keyword]
}
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