Skip to content

Instantly share code, notes, and snippets.

@dubeboy
Last active April 28, 2020 21:11
Show Gist options
  • Save dubeboy/3c164455d354266bab02ae472103a471 to your computer and use it in GitHub Desktop.
Save dubeboy/3c164455d354266bab02ae472103a471 to your computer and use it in GitHub Desktop.
Playground Implementation of Retrofit for swift AKA retroSwift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let builder = RetroSwift.Builder()
.baseUrl("http://api.openweathermap.org/data/2.5")
.build()
_ = RetroSwift(builder: builder)
return true
}
struct ApplicationService {
@GET(path="/weather")
var getWeather: Weather
}
class ViewController: UIViewController {
let client: ApplicationService = ApplicationService()
...
func getWeather(city: String) {
client.$getWeather(query: ["q": city, "units": "metric"]) { result in
switch result {
case .success(let weather):
print("weather summary in \(city): \(weather.weather.first!.desc), temprature: \(weather.main.temp)°C")
case .failure(let e):
print(e.localizedDescription)
}
}
}
}
struct Main: Codable { ... } // left out for brevity
struct WeatherData: Codable { ... } // left out for brevity
//1. Create one to one mapping to your response JSON
struct Weather: Codable {
let weather: [WeatherData]
let main: Main
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment