Skip to content

Commit

Permalink
Merge pull request #2 from nasrul21/feat/search
Browse files Browse the repository at this point in the history
Feat/search
  • Loading branch information
nasrul21 authored Oct 18, 2021
2 parents 667631d + a6150d2 commit fdebdfe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,15 @@ func main() {
| | | | |
#### Example
```go
// set your filter
filter := map[string]interface{
"ID": 134,
// set read option
readOption := stein.ReadOption{
// don't set a `Search` field, if you wanna get all data
Search: map[string]interface{
"ID": 134,
}
}
result := &YourStruct{}
status, err := client.Read("Sheet1", filter, &result)
var result []YourStruct
status, err := client.Read("Sheet1", readOption, &result)

// if you want to get the data without filter
// status, err := client.Read("Sheet1", nil, &result)
Expand Down
18 changes: 16 additions & 2 deletions stein.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package stein
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
Expand All @@ -23,6 +24,10 @@ type Option struct {
Timeout time.Duration
}

type ReadOption struct {
Search map[string]interface{}
}

// default http timeout
var defHTTPTimeout = 15 * time.Second

Expand All @@ -44,8 +49,16 @@ func NewClient(baseURL string, option *Option) *Stein {
}

// Read Stein API
func (s *Stein) Read(sheetName string, filter interface{}, v interface{}) (status int, err error) {
status, err = s.Call(http.MethodGet, sheetName, nil, v)
func (s *Stein) Read(sheetName string, option ReadOption, v interface{}) (status int, err error) {
var search string
searchByte, err := json.Marshal(option.Search)
if err != nil {
return
}
if len(option.Search) != 0 {
search = fmt.Sprintf("search=%s", string(searchByte))
}
status, err = s.Call(http.MethodGet, fmt.Sprintf("%s?%s", sheetName, search), nil, v)
if err != nil {
return
}
Expand Down Expand Up @@ -154,6 +167,7 @@ func (s *Stein) executeRequest(req *http.Request, v interface{}) (status int, er
// itself, otherwise nil.
func (s *Stein) Call(method, path string, body io.Reader, v interface{}) (status int, err error) {
req, err := s.newRequest(method, s.BaseURL+"/"+path, body)
fmt.Println(path)
if err != nil {
return http.StatusInternalServerError, err
}
Expand Down

0 comments on commit fdebdfe

Please sign in to comment.