|
1 |
| -# podcast-api-swift |
| 1 | +# Podcast API Swift Library |
| 2 | + |
| 3 | +[](https://github.com/ListenNotes/podcast-api-swift/actions/workflows/swift.yml) |
| 4 | + |
| 5 | +The Podcast API Swift library provides convenient access to the [Listen Notes Podcast API](https://www.listennotes.com/api/) from |
| 6 | +applications written in the Swift language. |
| 7 | + |
| 8 | +Simple and no-nonsense podcast search & directory API. Search the meta data of all podcasts and episodes by people, places, or topics. It's the same API that powers [the best podcast search engine Listen Notes](https://www.listennotes.com/). |
| 9 | + |
| 10 | +If you have any questions, please contact [[email protected]]([email protected]?subject=Questions+about+the+Swift+SDK+of+Listen+API) |
| 11 | + |
| 12 | +<a href="https://www.listennotes.com/api/"><img src="https://raw.githubusercontent.com/ListenNotes/ListenApiDemo/master/web/src/powered_by_listennotes.png" width="300" /></a> |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +### CocoaPods |
| 17 | + |
| 18 | +[CocoaPods](https://cocoapods.org) is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate PodcastAPI into your Xcode project using CocoaPods, specify it in your `Podfile`: |
| 19 | + |
| 20 | +```ruby |
| 21 | +pod 'PodcastAPI', '~> 1.0.1' |
| 22 | +``` |
| 23 | + |
| 24 | +### Carthage |
| 25 | + |
| 26 | +[Carthage](https://github.com/Carthage/Carthage) is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate PodcastAPI into your Xcode project using Carthage, specify it in your `Cartfile`: |
| 27 | + |
| 28 | +```ogdl |
| 29 | +github "ListenNotes/PodcastAPI" ~> 1.0.1 |
| 30 | +``` |
| 31 | + |
| 32 | +### Swift Package Manager |
| 33 | + |
| 34 | +The [Swift Package Manager](https://swift.org/package-manager/) is a tool for automating the distribution of Swift code and is integrated into the `swift` compiler. |
| 35 | + |
| 36 | +Once you have your Swift package set up, adding PodcastAPI as a dependency is as easy as adding it to the `dependencies` value of your `Package.swift`. |
| 37 | + |
| 38 | +```swift |
| 39 | +dependencies: [ |
| 40 | + .package(url: "https://github.com/ListenNotes/podcast-api-swift.git", .upToNextMajor(from: "1.0.1")) |
| 41 | +] |
| 42 | +``` |
| 43 | + |
| 44 | +### Requirements |
| 45 | + |
| 46 | +- Swift 5+ |
| 47 | + |
| 48 | + |
| 49 | +## Usage |
| 50 | + |
| 51 | +The library needs to be configured with your account's API key which is |
| 52 | +available in your [Listen API Dashboard](https://www.listennotes.com/api/dashboard/#apps). Set `apiKey` to its |
| 53 | +value: |
| 54 | + |
| 55 | +```swift |
| 56 | +import Foundation |
| 57 | +import PodcastAPI |
| 58 | + |
| 59 | +let apiKey = ProcessInfo.processInfo.environment["LISTEN_API_KEY", default: ""] |
| 60 | + |
| 61 | +// For command line executables, we have to use synchronous requests, otherwise |
| 62 | +// the program would exit before requests return any response |
| 63 | +let client = PodcastAPI.Client(apiKey: apiKey, synchronousRequest: true) |
| 64 | + |
| 65 | +// By default, we do asynchronous requests. |
| 66 | +// let client = PodcastAPI.Client(apiKey: apiKey) |
| 67 | + |
| 68 | +// All parameters are passed via this Dictionary[String: String] |
| 69 | +// For all parameters, please refer to https://www.listennotes.com/api/docs/ |
| 70 | +var parameters: [String: String] = [:] |
| 71 | + |
| 72 | +parameters["q"] = "startup" |
| 73 | +parameters["sort_by_date"] = "1" |
| 74 | +client.search(parameters: parameters) { response in |
| 75 | + if let error = response.error { |
| 76 | + switch (error) { |
| 77 | + case PodcastApiError.apiConnectionError: |
| 78 | + print("Can't connect to Listen API server") |
| 79 | + case PodcastApiError.authenticationError: |
| 80 | + print("wrong api key") |
| 81 | + default: |
| 82 | + print("unknown error") |
| 83 | + } |
| 84 | + } else { |
| 85 | + // It's a SwiftyJSON object |
| 86 | + if let json = response.toJson() { |
| 87 | + print(json) |
| 88 | + } |
| 89 | + |
| 90 | + // Some account stats |
| 91 | + print(response.getFreeQuota()) |
| 92 | + print(response.getUsage()) |
| 93 | + print(response.getNextBillingDate()) |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +If `apiKey` is an empty string "", then we'll connect to a [mock server](https://www.listennotes.com/api/tutorials/#faq0) that returns fake data for testing purposes. |
| 99 | + |
| 100 | + |
| 101 | +### Handling errors |
| 102 | + |
| 103 | +Unsuccessful requests return errors. |
| 104 | + |
| 105 | +| Error | Description | |
| 106 | +| ------------- | ------------- | |
| 107 | +| authenticationError | wrong api key or your account is suspended | |
| 108 | +| invalidRequestError | something wrong on your end (client side errors), e.g., missing required parameters | |
| 109 | +| tooManyRequestsError | you are using FREE plan and you exceed the quota limit | |
| 110 | +| notFoundError | endpoint not exist, or podcast / episode not exist | |
| 111 | +| apiConnectionError | failed to connect to Listen API servers | |
| 112 | +| serverError | something wrong on our end (unexpected server errors) | |
| 113 | + |
| 114 | +All errors can be found in [this file](https://github.com/ListenNotes/podcast-api-swift/blob/main/Sources/PodcastAPI/PodcastApiError.swift). |
0 commit comments