Skip to content

Commit fea35df

Browse files
committed
Add a constructor to Client class.
1 parent 981171e commit fea35df

File tree

5 files changed

+86
-21
lines changed

5 files changed

+86
-21
lines changed

README.md

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ If you have any questions, please contact [[email protected]](hello@listenno
2222
Add this dependency to your project's build file:
2323

2424
```groovy
25-
implementation "com.listennotes:podcast-api:1.0.4"
25+
implementation "com.listennotes:podcast-api:1.0.5"
2626
```
2727

2828
### Maven users
@@ -33,19 +33,79 @@ Add this dependency to your project's POM:
3333
<dependency>
3434
<groupId>com.listennotes</groupId>
3535
<artifactId>podcast-api</artifactId>
36-
<version>1.0.4</version>
36+
<version>1.0.5</version>
3737
</dependency>
3838
```
3939

40-
## Development
40+
## Usage
41+
42+
The library needs to be configured with your account's API key which is
43+
available in your [Listen API Dashboard](https://www.listennotes.com/api/dashboard/#apps). Set `apiKey` to its
44+
value:
45+
46+
```java
47+
import java.util.HashMap;
48+
import org.json.JSONObject;
49+
import com.listennotes.podcast_api.Client;
50+
import com.listennotes.podcast_api.ApiResponse;
51+
import com.listennotes.podcast_api.exception.ListenApiException;
52+
53+
public class PodcastApiExample {
54+
public static void main(String[] args) {
55+
try {
56+
// If apiKey is null or "", then we'll use mock api for test data
57+
String apiKey = System.getenv("LISTEN_API_KEY");
58+
Client objClient = new Client(apiKey);
59+
60+
// All parameters can be found at
61+
// https://www.listennotes.com/api/docs/
62+
HashMap<String, String> parameters = new HashMap<>();
63+
parameters.put("q", "startup");
64+
parameters.put("type", "podcast");
65+
66+
ApiResponse response = objClient.search(parameters);
67+
68+
// For successful response, you get a org.json.JSONObject
69+
System.out.println(response.toJSON().toString(2));
70+
71+
// Some handy methods to get your account stats
72+
System.out.println("\n=== Some Account Information ===\n");
73+
System.out.println("Free Quota this month: " + response.getFreeQuota() + " requests");
74+
System.out.println("Usage this month: " + response.getUsage() + " requests");
75+
System.out.println("Next billing date: " + response.getNextBillingDate());
76+
} catch (ListenApiException e) {
77+
e.printStackTrace();
78+
}
79+
}
80+
}
81+
```
4182

42-
```sh
43-
# Run sample app
44-
./gradlew run -q
83+
If `apiKey` is None, then we'll connect to a [mock server](https://www.listennotes.com/api/tutorials/#faq0) that returns fake data for testing purposes.
4584

46-
# Run all unit tests
47-
./gradlew test
85+
You can quickly run sample code using gradle:
86+
```shell
87+
# Use api mock server for test data
88+
./gradlew run
4889

49-
# Run a specific test case
50-
./gradlew test --tests ClientTest.testSearch
90+
# Use production server for real data
91+
LISTEN_API_KEY=your-api-key-here ./gradlew run
5192
```
93+
94+
95+
### Handling exceptions
96+
97+
Unsuccessful requests raise exceptions. The class of the exception will reflect
98+
the sort of error that occurred.
99+
100+
| Exception Class | Description |
101+
| ------------- | ------------- |
102+
| AuthenticationException | wrong api key or your account is suspended |
103+
| ApiConnectionException | fail to connect to API servers |
104+
| InvalidRequestException | something wrong on your end (client side errors), e.g., missing required parameters |
105+
| RateLimitException | you are using FREE plan and you exceed the quota limit |
106+
| NotFoundException | endpoint not exist, or podcast / episode not exist |
107+
| ListenApiException | something wrong on our end (unexpected server errors) |
108+
109+
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).
110+
111+
And you can see some sample code [here](https://github.com/ListenNotes/podcast-api-java/blob/main/src/main/java/com/listennotes/podcast_api/Sample.java).

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
GROUP_ID=com.listennotes
2-
VERSION_NAME=1.0.4
2+
VERSION_NAME=1.0.5
33

44
POM_URL=https://github.com/ListenNotes/podcast-api-java
55
POM_SCM_URL[email protected]:ListenNotes/podcast-api-java.git

src/main/java/com/listennotes/podcast_api/Client.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.net.MalformedURLException;
2020
import java.net.ProtocolException;
2121

22+
2223
public final class Client {
2324
public static final String BASE_URL_TEST = "https://listen-api-test.listennotes.com/api/v2";
2425
public static final String BASE_URL_PROD = "https://listen-api.listennotes.com/api/v2";
@@ -29,6 +30,14 @@ public final class Client {
2930
protected HttpURLConnection con;
3031
protected Map<String, String> requestParams;
3132

33+
public Client(String apiKey) {
34+
this.apiKey = apiKey;
35+
}
36+
37+
public Client() {
38+
// No api key. Use api mock server.
39+
}
40+
3241
public ApiResponse submitPodcast(Map<String, String> mapParams) throws ListenApiException {
3342
return this.post("podcasts/submit", mapParams);
3443
}
@@ -124,18 +133,14 @@ public ApiResponse search(Map<String, String> mapParams) throws ListenApiExcepti
124133

125134
protected String getUrl(String strPath) {
126135
String strUrl = BASE_URL_TEST;
127-
if (this.apiKey.length() > 0) {
136+
if (this.apiKey != null &&this.apiKey.length() > 0) {
128137
strUrl = BASE_URL_PROD;
129138
}
130139

131140
strUrl = strUrl + "/" + strPath;
132141
return strUrl;
133142
}
134143

135-
public void setApiKey(String strKey) {
136-
this.apiKey = strKey;
137-
}
138-
139144
public HttpURLConnection getConnection(String strUrl) throws ListenApiException {
140145
URL url;
141146
try {
@@ -151,7 +156,7 @@ public HttpURLConnection getConnection(String strUrl) throws ListenApiException
151156

152157
con.setDoOutput(true);
153158
con.setRequestProperty("User-Agent", USER_AGENT);
154-
if (this.apiKey.length() > 0) {
159+
if (this.apiKey != null && this.apiKey.length() > 0) {
155160
con.setRequestProperty("X-ListenAPI-Key", this.apiKey);
156161
}
157162
con.setConnectTimeout(5000);

src/main/java/com/listennotes/podcast_api/Sample.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.listennotes.podcast_api;
22

33
import com.listennotes.podcast_api.exception.*;
4-
import java.util.Map;
54
import java.util.HashMap;
65

76
public final class Sample {
87
public static void main(String[] args) {
98
try {
10-
Client objClient = new Client();
9+
String apiKey = System.getenv("LISTEN_API_KEY");
10+
Client objClient = new Client(apiKey);
1111

12-
Map<String, String> parameters;
12+
HashMap<String, String> parameters;
1313
ApiResponse response;
1414

1515
parameters = new HashMap<>();

src/test/java/com/listennotes/podcast_api/ClientTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void testSetApiKey() throws Exception {
1919
assertEquals(null, con.getRequestProperty("X-ListenAPI-Key"));
2020

2121
String strApiKey = "helloWorld";
22-
client.setApiKey(strApiKey);
22+
client = new Client(strApiKey);
2323
con = client.getConnection(client.getUrl("test"));
2424
assertEquals(con.getRequestProperty("X-ListenAPI-Key"), strApiKey);
2525

0 commit comments

Comments
 (0)