|
1 | 1 | import Foundation |
2 | | -import Alamofire |
3 | 2 |
|
4 | 3 | let BASE_URL_PROD = "https://listen-api.listennotes.com/api/v2" |
5 | 4 | let BASE_URL_TEST = "https://listen-api-test.listennotes.com/api/v2" |
6 | 5 |
|
7 | | -//extension Request { |
8 | | -// public func debugLog() -> Self { |
9 | | -// #if DEBUG |
10 | | -// debugPrint(self) |
11 | | -// #endif |
12 | | -// return self |
13 | | -// } |
14 | | -//} |
15 | 6 |
|
16 | 7 | public class Client { |
17 | 8 | private var apiKey: String |
18 | 9 | private var baseUrl: String = BASE_URL_PROD |
19 | 10 | private var userAgent: String = "podcast-api-swift" |
20 | 11 | private var responseTimeout: Int = 30000 |
| 12 | + private var synchronousRequest: Bool = false |
21 | 13 |
|
22 | | - public init(apiKey: String) { |
| 14 | + public convenience init(apiKey: String) { |
| 15 | + self.init(apiKey: apiKey, synchronousRequest: false) |
| 16 | + } |
| 17 | + |
| 18 | + public init(apiKey: String, synchronousRequest: Bool) { |
23 | 19 | self.apiKey = apiKey |
24 | 20 |
|
25 | 21 | if apiKey.trimmingCharacters(in: .whitespacesAndNewlines).count == 0 { |
26 | 22 | self.baseUrl = BASE_URL_TEST |
27 | 23 | } |
| 24 | + |
| 25 | + self.synchronousRequest = synchronousRequest |
28 | 26 | } |
29 | 27 |
|
30 | 28 | public func setUserAgent(userAgent: String) { |
31 | 29 | self.userAgent = userAgent; |
32 | 30 | } |
33 | 31 |
|
34 | | - public func search(parameters: [String: String]) { |
35 | | - let url = "\(self.baseUrl)/search" |
36 | | - let result = self.query(address: url) |
37 | | - print(result) |
| 32 | + public func search(parameters: [String: String], completion: @escaping (ApiResponse) -> ()) { |
| 33 | + self.sendHttpRequest(path: "search", method: "GET", parameters: parameters, completion: completion) |
| 34 | + |
| 35 | + } |
| 36 | + |
| 37 | + public func batchFetchPodcasts(parameters: [String: String], completion: @escaping (ApiResponse) -> ()) { |
| 38 | + self.sendHttpRequest(path: "podcasts", method: "POST", parameters: parameters, completion: completion) |
38 | 39 | } |
39 | 40 |
|
40 | | - func query(address: String) -> String { |
41 | | - let url = URL(string: address) |
42 | | - let semaphore = DispatchSemaphore(value: 0) |
| 41 | + public func deletePodcast(parameters: [String: String], completion: @escaping (ApiResponse) -> ()) { |
| 42 | + self.sendHttpRequest(path: "deletePodcast", method: "DELETE", parameters: parameters, completion: completion) |
| 43 | + } |
| 44 | + |
| 45 | + func sendHttpRequest(path: String, method: String, parameters: [String: String], completion: ((ApiResponse) -> ())?) { |
| 46 | + let urlString = "\(self.baseUrl)/\(path)" |
| 47 | + |
| 48 | + var request: URLRequest |
43 | 49 |
|
44 | | - var result: String = "" |
| 50 | + if method == "POST" { |
| 51 | + request = URLRequest(url: URL(string: urlString)!) |
| 52 | + request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") |
| 53 | + var data = [String]() |
| 54 | + for(key, value) in parameters { |
| 55 | + data.append(key + "=\(value)") |
| 56 | + } |
| 57 | + let postData = data.map { String($0) }.joined(separator: "&") |
| 58 | + request.httpBody = postData.data(using: .utf8) |
| 59 | + } else { |
| 60 | + var components = URLComponents(string: urlString)! |
| 61 | + components.queryItems = parameters.map { (key, value) in |
| 62 | + URLQueryItem(name: key, value: value) |
| 63 | + } |
| 64 | + components.percentEncodedQuery = components.percentEncodedQuery?.replacingOccurrences(of: "+", with: "%2B") |
| 65 | + request = URLRequest(url: components.url!) |
| 66 | + } |
| 67 | + request.httpMethod = method |
| 68 | + request.setValue(self.apiKey, forHTTPHeaderField: "X-ListenAPI-Key") |
45 | 69 |
|
46 | | - let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in |
47 | | - result = String(data: data!, encoding: String.Encoding.utf8)! |
48 | | - semaphore.signal() |
| 70 | + let sema: DispatchSemaphore? = self.synchronousRequest ? DispatchSemaphore(value: 0) : nil; |
| 71 | + let task = URLSession.shared.dataTask(with: request) {(data, response, error) in |
| 72 | + if let error = error { |
| 73 | + completion?(ApiResponse(data: data, response: response, httpError: error, apiError: PodcastApiError.apiConnectionError)) |
| 74 | + if let sema = sema { |
| 75 | + sema.signal() |
| 76 | + } |
| 77 | + return |
| 78 | + } |
| 79 | + completion?(ApiResponse(data: data, response: response, httpError: error, apiError: nil)) |
| 80 | + if let sema = sema { |
| 81 | + sema.signal() |
| 82 | + } |
49 | 83 | } |
50 | 84 |
|
51 | 85 | task.resume() |
52 | | - semaphore.wait() |
53 | | - return result |
| 86 | + if let sema = sema { |
| 87 | + sema.wait() |
| 88 | + } |
54 | 89 | } |
55 | 90 | } |
0 commit comments