Skip to content

Instantly share code, notes, and snippets.

@OskarGroth
Created June 27, 2018 13:53
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 OskarGroth/4ccc00c09e1c2d554b03fc7041100a7d to your computer and use it in GitHub Desktop.
Save OskarGroth/4ccc00c09e1c2d554b03fc7041100a7d to your computer and use it in GitHub Desktop.
extension URLRequest {
public enum HTTPMethod: String {
case get = "GET"
case put = "PUT"
case post = "POST"
case delete = "DELETE"
case head = "HEAD"
case options = "OPTIONS"
case trace = "TRACE"
case connect = "CONNECT"
}
}
typealias SpotifyQueryParameter = [String: String]
enum APIPath: String {
case search = "search"
case ...
...and so on
}
struct APIResponse {
let error: Error?
let result: [String: AnyObject]?
}
func searchArtist(queryParams: [SpotifyQueryParameter], completionHandler: ((APIResponse) -> Void)) {
let request = createSpotifyRequest(path: .search, parameters: queryParams, method: .get, completionHandler: completionHandler)
request.resume()
}
func createSpotifyRequest(path: String, method: URLRequest.HTTPMethod, completionHandler: ((APIResponse) -> Void)) {
...
}
// Somewhere in your app:
searchArtist(queryParams: ["artist": "Katy Perry"], completionHandler: { response in
if let error = response.error { print(error); return }
// deal with response.result
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment