Last active
September 21, 2022 09:20
-
-
Save jarrodnorwell/03abdf8ce7028a77f759f495ee889331 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
import Foundation | |
/* | |
WHY YOURS WASN'T WORKING | |
Line 320: Your URL is incorrect, it's missing the search in /1.0[/search/]setlists | |
Line 332: Setlist is not what is returned by the response (more below) | |
You can see that the response is the root dict and *setlist* is actually a key/value within. | |
- response | |
- setlist | |
... | |
... | |
*/ | |
enum SetListFMError : Error { | |
case invalidURL | |
} | |
// SetList and its internal data is or could be incomplete. You'll need to add the tour, set, etc. data. | |
// This is a very basic implementation based on the endpoint provided from Reddit (countryCode=US&p=1) | |
struct SetList : Codable { | |
struct Artist : Codable { | |
let mbid: String | |
let name: String | |
let sortName: String | |
let disambiguation: String | |
let url: String | |
} | |
struct Venue : Codable { | |
struct City : Codable { | |
struct Coodrinates : Codable { | |
let lat: Double | |
let long: Double | |
} | |
struct Country : Codable { | |
let code: String | |
let name: String | |
} | |
let id: String | |
let name: String | |
let state: String | |
let stateCode: String | |
let coords: Coordinates | |
let country: Country | |
} | |
let id: String | |
let name: String | |
let city: City | |
let url: String | |
} | |
struct Set : Codable { | |
// Incomplete | |
} | |
struct Tour : Codable { | |
// Incomplete | |
} | |
let id: String | |
let versionId: String | |
let eventDate: String | |
let lastUpdated: String | |
let artist: Artist | |
let venue: Venue | |
let sets: [Set] | |
let url: String | |
// Incomplete | |
} | |
struct Response : Codable { | |
let setlist: [SetList] | |
} | |
// CLASS | |
class SetListFM { | |
static let shared = SetListFM() | |
let endpoint = "https://api.setlist.fm/rest/1.0" | |
func search(_ countryCode: String, _ page: Int) async throws -> [SetList] { | |
guard let url = URL(string: "\(endpoint)/search/setlists?countryCode?\(countryCode)&p=\(page)") else { | |
throw SetListFMError.invalidURL | |
} | |
var request = URLRequest(url: url) | |
request.set("APIKEY", forHTTPHeaderField: "x-api-key") // Add your API key | |
request.set("application/json", forHTTPHeaderField: "Accept") | |
request.set("application/json", forHTTPHeaderField: "Content-Type") | |
let (data, _) = try await URLSession.shared.data(for: request) // (data, response) | |
let decodedData = try JSONDecoder().decode(Response.self, from: data) | |
return decodedData.setlist | |
} | |
} | |
// USAGE | |
Task { | |
let setlists = try await SetListFM.shared.search(countryCode: "US", page: 1) | |
// OR | |
let setlists = try await SetListFM.shared.search("US", 1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Revision 3: Added a small comment explaining some issues.