Skip to content

Instantly share code, notes, and snippets.

@moallemi
Created June 1, 2023 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moallemi/fd819a2347e91522801fa2815236ff20 to your computer and use it in GitHub Desktop.
Save moallemi/fd819a2347e91522801fa2815236ff20 to your computer and use it in GitHub Desktop.
Grid Challenge API Helpers
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
}
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