|
| 1 | +// |
| 2 | +// LetterboxdAPI+Query.swift |
| 3 | +// LetterboxdAPI |
| 4 | +// |
| 5 | +// Created by Gianpiero Spinelli. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +public extension LetterboxdAPI { |
| 11 | + /// Generic query for any endpoint on the letterboxd APIs |
| 12 | + /// - Parameters: |
| 13 | + /// - path: the endpoint, eg: `/film/id` |
| 14 | + /// - parameters: the parameters for the endpoint |
| 15 | + /// - body: if the endpoint supports a body, please insert use it here |
| 16 | + /// - completion: the completion of the request |
| 17 | + func query<D: Decodable>(path: String, parameters: [String: String], body: String? = nil, completion: @escaping (Result<D, Error>) -> Void) { |
| 18 | + query(path: path, parameters: parameters, body: body?.data(using: .utf8), completion: completion) |
| 19 | + } |
| 20 | + |
| 21 | + /// Generic query for any endpoint on the letterboxd APIs |
| 22 | + /// - Parameters: |
| 23 | + /// - path: the endpoint, eg: `/film/id` |
| 24 | + /// - parameters: the parameters for the endpoint |
| 25 | + /// - body: if the endpoint supports a body, please insert use it here |
| 26 | + /// - completion: the completion of the request |
| 27 | + func query<D: Decodable>(path: String, parameters: [String: String], body: Data? = nil, completion: @escaping (Result<D, Error>) -> Void) { |
| 28 | + let url = URLBuilder.url(path: path, body: body, params: parameters) |
| 29 | + |
| 30 | + guard let request = generateRequest(url: url, method: .get) else { |
| 31 | + completion(.failure(LetterboxdAPIError.generatingRequest)) |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + processRequest(request: request, completion: completion) |
| 36 | + } |
| 37 | +} |
0 commit comments