Last active
April 28, 2020 21:11
-
-
Save dubeboy/3c164455d354266bab02ae472103a471 to your computer and use it in GitHub Desktop.
Playground Implementation of Retrofit for swift AKA retroSwift
This file contains hidden or 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
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 | |
} |
This file contains hidden or 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
struct ApplicationService { | |
@GET(path="/weather") | |
var getWeather: Weather | |
} |
This file contains hidden or 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
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) | |
} | |
} | |
} | |
} |
This file contains hidden or 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
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