Created
June 27, 2018 13:53
-
-
Save OskarGroth/4ccc00c09e1c2d554b03fc7041100a7d to your computer and use it in GitHub Desktop.
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
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