Good day, All
So I am learning/practicing Network calls. I came across a video by Paul Hudson where he makes a call to the Itunes API using the same code I am trying to use here. However, I am trying to make a call to the Google Books API. My call (code below) is not working, it (returns a blank screen) I am not sure why. I am of course using variables from the URL I am trying to call/make a request from.
import SwiftUI
struct Response: Codable {
var results: [Result]
}
struct Result: Codable {
var id: Int
var etag: String
}
struct ContentView: View {
@State private var results = [Result]()
var body: some View {
List(results, id: \.id) { item in
VStack(alignment: .leading) {
Text(item.etag)
}
}
.task {
await loadData()
// ????
}
}
func loadData() async {
guard let url = URL(string: "https://www.googleapis.com/books/v1/volumes?q=flowers+inauthor:keyes") else {
print("Invalid URL")
return
}
do {
let (data, _) = try await URLSession.shared.data(from: url)
if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) {
results = decodedResponse.results
}
} catch {
print("Invalid data")
}
}
}
As previously mentioned, this code was tested using the Itunes API and it worked flawlessly. I am not sure what is causing the issue or what can fix it. I will keep searching and practicing on my end.
Thank you!