Created
May 10, 2022 19:30
-
-
Save aliakhtar49/812f13d47966e485bd0d46f017a59af3 to your computer and use it in GitHub Desktop.
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 downloadMetadata(for id: Int) async throws -> ImageMetadata { | |
let metadataUrl = URL(string: "https://www.andyibanez.com/fairesepages.github.io/tutorials/async-await/part1/\(id).json")! | |
let metadataRequest = URLRequest(url: metadataUrl) | |
let (data, metadataResponse) = try await URLSession.shared.data(for: metadataRequest) | |
guard (metadataResponse as? HTTPURLResponse)?.statusCode == 200 else { | |
throw ImageDownloadError.invalidMetadata | |
} | |
return try JSONDecoder().decode(ImageMetadata.self, from: data) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment