Skip to content

Instantly share code, notes, and snippets.

@paulofaria
Last active January 15, 2016 01:44
Show Gist options
  • Save paulofaria/b245ac2e44715d23c297 to your computer and use it in GitHub Desktop.
Save paulofaria/b245ac2e44715d23c297 to your computer and use it in GitHub Desktop.
struct GetPokemonEndpoint: EndpointType {
let middleware: [MiddlewareType] = [
debugLogger,
clientContentNegotiator(JSONMediaType)
]
func serialize(id: Int) throws -> Request {
return Request(method: .GET, uri: URI(path: "/api/v1/pokemon/\(id)/"))
}
func parse(response: Response) throws -> Pokemon {
let content = response.content!
return Pokemon(
id: content["national_id"]!.int!,
name: content["name"]!.string!
)
}
func mock(id: Int) throws -> Pokemon {
return Pokemon(id: id, name: "Pokemon \(id)")
}
}
let getPokemonEndpoint = GetPokemonEndpoint()
let getPokemonMock = getPokemonEndpoint.createWithMock()
let pokemonMock = try getPokemonMock(129)
let client = try Client(host: "www.pokeapi.co", port: 80)
let getPokemon = getPokemonEndpoint.createWithClient(client)
let pokemon = try getPokemon(129)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment