|
1 | | -# podcast-api-scala |
2 | | -The Official Scala library for the Listen Notes Podcast API |
| 1 | +# Podcast API Scala Library |
| 2 | + |
| 3 | +The Podcast API Scala library provides convenient access to the [Listen Notes Podcast API](https://www.listennotes.com/api/) from |
| 4 | +applications written in the Scala language. |
| 5 | + |
| 6 | +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/). |
| 7 | + |
| 8 | +This repo is actually a demo app using [the podcast-api Java library](https://github.com/ListenNotes/podcast-api-java). |
| 9 | +You can find example Scala code in the README.md of this repo. |
| 10 | + |
| 11 | +If you have any questions, please contact [hello@listennotes.com](hello@listennotes.com?subject=Questions+about+the+Scala+SDK+of+Listen+API) |
| 12 | + |
| 13 | +<a href="https://www.listennotes.com/api/"><img src="https://raw.githubusercontent.com/ListenNotes/ListenApiDemo/master/web/src/powered_by_listennotes.png" width="300" /> |
| 14 | + |
| 15 | + |
| 16 | +**Table of Contents** |
| 17 | + |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +You can install this library for JVM-based languages, including Java, Kotlin, Clojure, Scala, Groovy... |
| 22 | + |
| 23 | +### Gradle users |
| 24 | + |
| 25 | +Add this dependency to your project's build file: |
| 26 | + |
| 27 | +```groovy |
| 28 | +implementation "com.listennotes:podcast-api:1.0.6" |
| 29 | +``` |
| 30 | + |
| 31 | +### Maven users |
| 32 | + |
| 33 | +Add this dependency to your project's POM: |
| 34 | + |
| 35 | +```xml |
| 36 | +<dependency> |
| 37 | + <groupId>com.listennotes</groupId> |
| 38 | + <artifactId>podcast-api</artifactId> |
| 39 | + <version>1.0.6</version> |
| 40 | +</dependency> |
| 41 | +``` |
| 42 | + |
| 43 | +## Usage |
| 44 | + |
| 45 | +The library needs to be configured with your account's API key which is |
| 46 | +available in your [Listen API Dashboard](https://www.listennotes.com/api/dashboard/#apps). Set `apiKey` to its |
| 47 | +value: |
| 48 | + |
| 49 | +```scala |
| 50 | +import com.listennotes.podcast_api.Client |
| 51 | +import com.listennotes.podcast_api.exception._ |
| 52 | + |
| 53 | +object App { |
| 54 | + def main(args: Array[String]): Unit = { |
| 55 | + // If apiKey is not set or an empty string, then we'll connect |
| 56 | + // to the api mock server, which returns fake data for testing |
| 57 | + val apiKey = scala.util.Properties.envOrElse("LISTEN_API_KEY", "") |
| 58 | + val client = new Client(apiKey) |
| 59 | + |
| 60 | + // Parameters are passed via this HashMap |
| 61 | + // All parameters can be found at: |
| 62 | + // https://www.listennotes.com/api/docs/ |
| 63 | + val parameters = new java.util.HashMap[String, String] |
| 64 | + parameters.put("q", "startup") |
| 65 | + parameters.put("type", "episode") |
| 66 | + parameters.put("sort_by_date", "1") |
| 67 | + |
| 68 | + try { |
| 69 | + val response = client.search(parameters) |
| 70 | + |
| 71 | + // response.toJSON() returns an org.json.JSONObject |
| 72 | + println(response.toJSON().toString(2)) |
| 73 | + |
| 74 | + println("\n=== Some stats of your account ===\n") |
| 75 | + println("Free Quota this month: " + response.getFreeQuota() + " requests") |
| 76 | + println("Usage this month: " + response.getUsage() + " requests") |
| 77 | + } catch { |
| 78 | + case e: AuthenticationException => println("Wrong API key") |
| 79 | + case e: RateLimitException => println("you are using FREE plan and you exceed the quota limit") |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +If `apiKey` is null or "", then we'll connect to a [mock server](https://www.listennotes.com/api/tutorials/#faq0) that returns fake data for testing purposes. |
| 86 | + |
| 87 | +You can quickly run sample code using gradle: |
| 88 | +```shell |
| 89 | +# Use api mock server for test data |
| 90 | +./gradlew run |
| 91 | + |
| 92 | +# Use production server for real data |
| 93 | +LISTEN_API_KEY=your-api-key-here ./gradlew run |
| 94 | +``` |
| 95 | + |
| 96 | + |
| 97 | +### Handling exceptions |
| 98 | + |
| 99 | +Unsuccessful requests raise exceptions. The class of the exception will reflect |
| 100 | +the sort of error that occurred. |
| 101 | + |
| 102 | +| Exception Class | Description | |
| 103 | +| ------------- | ------------- | |
| 104 | +| AuthenticationException | wrong api key or your account is suspended | |
| 105 | +| ApiConnectionException | fail to connect to API servers | |
| 106 | +| InvalidRequestException | something wrong on your end (client side errors), e.g., missing required parameters | |
| 107 | +| RateLimitException | you are using FREE plan and you exceed the quota limit | |
| 108 | +| NotFoundException | endpoint not exist, or podcast / episode not exist | |
| 109 | +| ListenApiException | something wrong on our end (unexpected server errors) | |
| 110 | + |
| 111 | +All exception classes can be found in [this folder](https://github.com/ListenNotes/podcast-api-java/tree/main/src/main/java/com/listennotes/podcast_api/exception). |
| 112 | + |
| 113 | +And you can see some sample code [here](https://github.com/ListenNotes/podcast-api-scala/blob/main/app/src/main/scala/podcast/api/scala/demo/App.scala). |
0 commit comments