-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSampleAPICall.java
More file actions
105 lines (85 loc) · 3.36 KB
/
SampleAPICall.java
File metadata and controls
105 lines (85 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import org.json.JSONObject;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import org.json.*;
import javax.swing.*;
public class SampleAPICall {
private static final String BASE_URL = "https://newsapi.org/v2/";
// Load .env variable corresponding to API token.
private static final String API_TOKEN = System.getenv("API_TOKEN");
public static void main(String[] args) {
}
public Article[] getSampleNews(String rawSearchTerm) {
Article[] first_article = new Article[1];
// Replace any spaces with '-' for a valid link.
String searchTerm = rawSearchTerm.replace(' ', '-');
try {
URI uri = new URI(BASE_URL + "everything/?q=" + searchTerm + "&apiKey=" + API_TOKEN);
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().
uri(uri).
GET().
build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
String body = response.body();
JSONObject data = new JSONObject(body);
// System.out.println(data);
// Retrieve first article.
JSONArray articles = data.getJSONArray("articles");
JSONObject firstArticle = articles.getJSONObject(0);
// System.out.println(firstArticle);
// Retrieve details of first article.
String author = firstArticle.getString("author");
String description = firstArticle.getString("description");
String title = firstArticle.getString("title");
String url = firstArticle.getString("url");
// Not sure how to get the sources from the nested dict
String source = firstArticle.getJSONObject("source").getString("name");
// System.out.println(source);
Article article = new Article(description, author, title, url, source);
first_article[0] = article;
} catch (Exception e) {
System.out.println("Request for search \""
+ rawSearchTerm + "\" unsuccessful - caught " + e);
System.out.println(Arrays.toString(e.getStackTrace()));
}
System.out.println("Request for search \"" + rawSearchTerm + "\" successful");
// Return all articles here.
return first_article;
}
protected class Article {
private String content;
private String author;
private String title;
private String url;
private String source;
public Article(String content, String author, String title, String url, String source) {
this.author = author;
this.content = content;
this.title = title;
this.url = url;
this.source = source;
}
public String getContent() {
return content;
}
public String getAuthor() {
return author;
}
public String getTitle() {
return title;
}
public String getUrl() {
return url;
}
public String getSource() {
return source;
}
}
}