Skip to content

Commit ab95f10

Browse files
committed
fix #3
upgrade gradle version
1 parent 5e16c3f commit ab95f10

12 files changed

Lines changed: 339 additions & 84 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ build/
66
.project
77
bin/
88
data.json
9-
demo/gradle/
109
demo/libs/*.jar
1110
derive.rb
1211
gradlew.bat

README.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,39 +43,39 @@ make all SERPAPI_KEY='<your private key>'
4343
Use quotes if your key contains shell-special characters. You need a SerpApi account to obtain a key: https://serpapi.com/dashboard
4444

4545
`demo/src/main/java/demo/App.java`:
46-
```java
47-
public class App {
48-
public static void main(String[] args) throws SerpApiException {
49-
if(args.length != 1) {
50-
System.out.println("Usage: app <secret api key>");
51-
System.exit(1);
52-
}
46+
```javapublic
47+
class App {
48+
public static void main(String[] args) {
49+
String apiKey = System.getenv("SERPAPI_KEY");
5350
51+
// set search location
5452
String location = "Austin,Texas";
55-
System.out.println("find the first Coffee in " + location);
53+
String engine = "google";
54+
System.out.println("find the first coffee shop in " + location + " using " + engine);
5655
57-
// set api_key provided by the command line
5856
Map<String, String> auth = new HashMap<>();
59-
auth.put("api_key", args[0]);
57+
auth.put("engine", engine);
58+
auth.put("api_key", apiKey);
6059
61-
// Create search
60+
// create client
6261
SerpApi serpapi= new SerpApi(auth);
6362
63+
// create search parameters
6464
Map<String, String> parameter = new HashMap<>();
6565
parameter.put("q", "Coffee");
6666
parameter.put("location", location);
6767
68+
// perform search
6869
try {
69-
// Perform search
70-
JsonObject data = serpapi.search(parameter);
71-
72-
// Decode response
73-
JsonArray results = data.get("local_results").getAsJsonObject().get("places").getAsJsonArray();
74-
JsonObject first_result = results.get(0).getAsJsonObject();
75-
System.out.println("first coffee shop: " + first_result.get("title").getAsString() + " found on Google in " + location);
70+
// get search results
71+
JsonObject data = serpapi.search(parameter);
72+
JsonArray organic = data.getAsJsonArray("organic_results");
73+
JsonObject first = organic.get(0).getAsJsonObject();
74+
System.out.println("First result: " + first.get("title").getAsString() + " (search near " + location + ")");
7675
} catch (SerpApiException e) {
77-
System.out.println("oops exception detected!");
76+
System.out.println("SerpApi request failed.");
7877
e.printStackTrace();
78+
System.exit(1);
7979
}
8080
}
8181
}

README.md.erb

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,39 +61,39 @@ make all SERPAPI_KEY='<your private key>'
6161
Use quotes if your key contains shell-special characters. You need a SerpApi account to obtain a key: https://serpapi.com/dashboard
6262

6363
`demo/src/main/java/demo/App.java`:
64-
```java
65-
public class App {
66-
public static void main(String[] args) throws SerpApiException {
67-
if(args.length != 1) {
68-
System.out.println("Usage: app <secret api key>");
69-
System.exit(1);
70-
}
64+
```javapublic
65+
class App {
66+
public static void main(String[] args) {
67+
String apiKey = System.getenv("SERPAPI_KEY");
7168

69+
// set search location
7270
String location = "Austin,Texas";
73-
System.out.println("find the first Coffee in " + location);
71+
String engine = "google";
72+
System.out.println("find the first coffee shop in " + location + " using " + engine);
7473

75-
// set api_key provided by the command line
7674
Map<String, String> auth = new HashMap<>();
77-
auth.put("api_key", args[0]);
75+
auth.put("engine", engine);
76+
auth.put("api_key", apiKey);
7877

79-
// Create search
78+
// create client
8079
SerpApi serpapi= new SerpApi(auth);
8180

81+
// create search parameters
8282
Map<String, String> parameter = new HashMap<>();
8383
parameter.put("q", "Coffee");
8484
parameter.put("location", location);
8585

86+
// perform search
8687
try {
87-
// Perform search
88-
JsonObject data = serpapi.search(parameter);
89-
90-
// Decode response
91-
JsonArray results = data.get("local_results").getAsJsonObject().get("places").getAsJsonArray();
92-
JsonObject first_result = results.get(0).getAsJsonObject();
93-
System.out.println("first coffee shop: " + first_result.get("title").getAsString() + " found on Google in " + location);
88+
// get search results
89+
JsonObject data = serpapi.search(parameter);
90+
JsonArray organic = data.getAsJsonArray("organic_results");
91+
JsonObject first = organic.get(0).getAsJsonObject();
92+
System.out.println("First result: " + first.get("title").getAsString() + " (search near " + location + ")");
9493
} catch (SerpApiException e) {
95-
System.out.println("oops exception detected!");
94+
System.out.println("SerpApi request failed.");
9695
e.printStackTrace();
96+
System.exit(1);
9797
}
9898
}
9999
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,6 @@ publishing {
8383
}
8484

8585
wrapper {
86-
gradleVersion = "8.5"
86+
gradleVersion = "9.1.0"
8787
distributionType = Wrapper.DistributionType.ALL
8888
}

demo/.gitignore

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Ignore Gradle project-specific cache directory
22
.gradle
3-
gradle/
4-
gradlew
53

64
# Ignore Gradle build output directory
7-
build
5+
build/
6+
7+
# Local jars from `make dep`
8+
libs/

demo/Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ else
66
$(info "found SERPAPI_KEY variable")
77
endif
88

9+
ifeq ($(strip $(SERPAPI_KEY)),)
10+
$(error "SERPAPI_KEY must be non-empty")
11+
endif
12+
913
all: init clean build run
1014

15+
# Ensure wrapper script is executable (wrapper files should be committed under demo/)
1116
init:
12-
../gradlew wrapper
1317
chmod +x ./gradlew
1418

1519
clean:
@@ -26,4 +30,4 @@ build: clean
2630
@./gradlew build
2731

2832
run:
29-
@./gradlew run --args="$(SERPAPI_KEY)"
33+
@env SERPAPI_KEY="$(SERPAPI_KEY)" ./gradlew run

demo/build.gradle

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
*/
88

99
plugins {
10-
// Apply the java plugin to add support for Java
1110
id 'java'
12-
13-
// Apply the application plugin to add support for building an application
1411
id 'application'
1512
}
1613

17-
sourceCompatibility = 21
18-
targetCompatibility = 21
14+
java {
15+
sourceCompatibility = JavaVersion.VERSION_21
16+
targetCompatibility = JavaVersion.VERSION_21
17+
}
18+
19+
application {
20+
mainClass = 'demo.App'
21+
}
1922

2023
repositories {
2124
mavenCentral()
@@ -24,13 +27,12 @@ repositories {
2427
}
2528

2629
dependencies {
27-
// local jar file
28-
// implementation fileTree(dir: "./libs", includes: ['*.jar'])
29-
// download jar file from github over jitpack
30-
implementation 'com.github.serpapi:serpapi-java:1.0.0'
31-
// JSON dependencies
30+
def localJars = fileTree(dir: 'libs', include: '*.jar')
31+
if (!localJars.isEmpty()) {
32+
implementation localJars
33+
} else {
34+
implementation 'com.github.serpapi:serpapi-java:1.0.0'
35+
}
3236
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
3337
}
3438

35-
// Define the main class for the application
36-
mainClassName = 'demo.App'

0 commit comments

Comments
 (0)