Skip to content

Commit c11ba63

Browse files
committed
Finish code refractoring for 3.0
- align to naming Go naming convention - update documentation
1 parent 5f25897 commit c11ba63

13 files changed

+300
-253
lines changed

.github/workflows/go.yml

-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ jobs:
3030
dep ensure
3131
fi
3232
33-
- name: Build
34-
run: go build -v .
35-
3633
- name: Test
3734
run: go test -v .
3835
env:

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
tmp/
22
*.iml
33
.idea/
4+
go.mod

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018 SerpApi
3+
Copyright (c) 2018-2020 SerpApi
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

Makefile

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,23 @@ version=3.0
22

33
all: test
44

5+
check:
6+
go vet .
7+
go fmt .
8+
59
test:
610
go test -v .
711

12+
# check that everything is pushed
13+
package:
14+
git status | grep "Nothing"
15+
816
oobt:
9-
cd demo ; go run demo.go
17+
mkdir -p /tmp/oobt
18+
cp demo/demo.go /tmp/oobt
19+
cd /tmp/oobt ; \
20+
go get -u github.com/serpapi/google_search_results_golang ; \
21+
go run demo.go
1022

1123
release: oobt
1224
git tag -a ${version}

README.md

+8-10
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ parameter := map[string]string{
4848
"location": "Portland"
4949
}
5050

51-
query := NewGoogleClient(parameter, api)
52-
// Many clients available: bing, yahoo, baidu, googemaps, googleproduct, googlescholar
51+
query := NewGoogleSearch(parameter, api)
52+
// Many search engine available: bing, yahoo, baidu, googemaps, googleproduct, googlescholar, ebay, walmart, youtube..
5353

5454
rsp, err := query.json()
5555
results := rsp["organic_results"].([]interface{})
@@ -108,7 +108,7 @@ api_key := "your personal API key"
108108
engine := "yahoo"
109109

110110
// define the search search
111-
search := NewSerpApiSearch(engine, parameter, api_key)
111+
search := NewSearch(engine, parameter, api_key)
112112

113113
// override an existing parameter
114114
search.parameter["location"] = "Portland,Oregon,United States"
@@ -129,16 +129,14 @@ see below for more hands on examples.
129129
### Example by specification
130130

131131
We love true open source, continuous integration and Test Drive Development (TDD).
132-
We are using "go test" [our infrastructure around the clock](https://travis-ci.org/serpapi/google-search-results-golang) to achieve the best QoS (Quality Of Service).
132+
We are using "go test" to test our infrastructure around the clock
133+
to achieve the best QoS (Quality Of Service).
133134

134135
The directory test/ includes specification/examples.
135136

136-
Set your api key.
137+
To run the test from bash using make
137138
```bash
138139
export API_KEY="your secret key"
139-
```
140-
141-
```bash
142140
make test
143141
```
144142

@@ -165,7 +163,7 @@ parameter := map[string]string{
165163
"location": "Portland"
166164
}
167165

168-
search := NewGoogleClient(parameter, "your user key")
166+
search := NewGoogleSearch(parameter, "your user key")
169167
rsp, err := search.GetJSON()
170168

171169
if err != nil {
@@ -190,7 +188,7 @@ it prints the search ID from the archive.
190188

191189
### Account API
192190
```go
193-
var data SerpResponse
191+
var data SearchResult
194192
var err error
195193
data, err = search.GetAccount()
196194

client.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package search
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"io"
7+
"io/ioutil"
8+
"net/http"
9+
"net/url"
10+
)
11+
12+
// decodeJson response
13+
func (search *Search) decodeJSON(body io.ReadCloser) (SearchResult, error) {
14+
// Decode JSON from response body
15+
decoder := json.NewDecoder(body)
16+
17+
// Response data
18+
var rsp SearchResult
19+
err := decoder.Decode(&rsp)
20+
if err != nil {
21+
return nil, errors.New("fail to decode")
22+
}
23+
24+
// check error message
25+
errorMessage, derror := rsp["error"].(string)
26+
if derror {
27+
return nil, errors.New(errorMessage)
28+
}
29+
return rsp, nil
30+
}
31+
32+
// decodeJSONArray decodes response body to SearchResultArray
33+
func (search *Search) decodeJSONArray(body io.ReadCloser) (SearchResultArray, error) {
34+
decoder := json.NewDecoder(body)
35+
var rsp SearchResultArray
36+
err := decoder.Decode(&rsp)
37+
if err != nil {
38+
return nil, errors.New("fail to decode array")
39+
}
40+
return rsp, nil
41+
}
42+
43+
// decodeHTML decodes response body to html string
44+
func (search *Search) decodeHTML(body io.ReadCloser) (*string, error) {
45+
buffer, err := ioutil.ReadAll(body)
46+
if err != nil {
47+
return nil, err
48+
}
49+
text := string(buffer)
50+
return &text, nil
51+
}
52+
53+
// execute HTTP get reuqest and returns http response
54+
func (search *Search) execute(path string, output string) (*http.Response, error) {
55+
query := url.Values{}
56+
if search.Parameter != nil {
57+
for k, v := range search.Parameter {
58+
query.Add(k, v)
59+
}
60+
}
61+
62+
// api_key
63+
if len(search.ApiKey) != 0 {
64+
query.Add("api_key", search.ApiKey)
65+
}
66+
67+
// engine
68+
if len(query.Get("engine")) == 0 {
69+
query.Set("engine", search.Engine)
70+
}
71+
72+
// source programming language
73+
query.Add("source", "go")
74+
75+
// set output
76+
query.Add("output", output)
77+
78+
endpoint := "https://serpapi.com" + path + "?" + query.Encode()
79+
rsp, err := search.HttpSearch.Get(endpoint)
80+
if err != nil {
81+
return nil, err
82+
}
83+
return rsp, nil
84+
}

demo/demo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
/***
11-
* demo how to create a search for SerpApi
11+
* Demonstrate how to search on Google
1212
*
1313
* go get -u github.com/serpapi/google_search_results_golang
1414
*/

go.mod

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
module github.com/serpapi/google_search_results_golang
1+
module github.com/serpapi/google-search-results-golang
22

33
go 1.13
4-
5-
require github.com/serpapi/google-search-results-golang v0.0.0-20200705022805-f5cfa820f3aa // indirect

go.sum

-2
This file was deleted.

0 commit comments

Comments
 (0)