|
| 1 | +// |
| 2 | +// NodeZooService.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Pankaj Kulkarni on 09/05/23. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +/// Use NodeZooService to fetch the public search function of [nodezoo.com](nodezoo.com) |
| 11 | +/// |
| 12 | +/// While instantiating the ``NodeZooService`` instance pass the API Key as follows: |
| 13 | +/// ``` |
| 14 | +///import NodeZooService |
| 15 | +/// |
| 16 | +///let nodeZooService = NodeZooService(apiKey: "<API Key of >") |
| 17 | +/// ``` |
| 18 | +/// |
| 19 | +/// And then search the public package as follows: |
| 20 | +/// |
| 21 | +/// ``` |
| 22 | +/// let dataTask = nodeZooService.search("express") { result in |
| 23 | +/// switch result { |
| 24 | +/// case .success(let packages): |
| 25 | +/// // packages is the array of the found packages |
| 26 | +/// case .failure(let error): |
| 27 | +/// // handle the error here. |
| 28 | +/// } |
| 29 | +/// ``` |
| 30 | +/// The *dataTask* can be used to cancel the search. |
| 31 | +/// |
| 32 | +public class NodeZooService { |
| 33 | + |
| 34 | + private var networkManager: NetworkManager |
| 35 | + |
| 36 | + /// Initializer of NodeZooService |
| 37 | + /// - Parameter apiKey: API Key to search packages from [nodezoo.com](nodezoo.com) |
| 38 | + public init(apiKey: String) { |
| 39 | + networkManager = NetworkManager(apiKey: apiKey) |
| 40 | + } |
| 41 | + |
| 42 | + /// Search the packages |
| 43 | + /// - Parameters: |
| 44 | + /// - query: The query string to search for. |
| 45 | + /// - completion: The completion will be invoked with either success or failure. |
| 46 | + /// - Returns: Discardable data task. This can be used to cancel the search. |
| 47 | + /// |
| 48 | + @discardableResult |
| 49 | + public func search(query: String, completion: @escaping (Result<[NZPackage], Error>)->Void) -> URLSessionDataTask? { |
| 50 | + return networkManager.search(package: query, completion: completion) |
| 51 | + } |
| 52 | + |
| 53 | +} |
0 commit comments