Created
June 1, 2023 07:20
-
-
Save moallemi/fd819a2347e91522801fa2815236ff20 to your computer and use it in GitHub Desktop.
Grid Challenge API Helpers
This file contains 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 fetchPhotos() async throws -> [Photo] { | |
let url = URL(string: "https://api.unsplash.com/photos?order_by=popular&orientation=squarish&per_page=40")! | |
var request = URLRequest(url: url) | |
request.addValue("Client-ID REPLACE_WITH_ACCESS_KEY", forHTTPHeaderField: "Authorization") | |
let (data, _) = try await URLSession.shared.data(for: request) | |
let photos = try JSONDecoder().decode([Photo].self, from: data) | |
return photos | |
} |
This file contains 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 Photo: Codable { | |
let id: String | |
let urls: Urls | |
let width: Int | |
let height: Int | |
} | |
struct Urls: Codable { | |
let regular: String | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment