Skip to content

Commit

Permalink
use uritemplates
Browse files Browse the repository at this point in the history
Use uritemplates instead of cleanPathString to correctly encode the
URL path.

We follow the Google Go API here. See
https://code.google.com/p/google-api-go-client/ and especially
the subdirectory
https://code.google.com/p/google-api-go-client/source/browse/#hg%2Fgoogleapi%2Finternal%2Furitemplates
  • Loading branch information
olivere committed Oct 31, 2014
1 parent cffd2ab commit ceb70f8
Show file tree
Hide file tree
Showing 22 changed files with 713 additions and 65 deletions.
12 changes: 11 additions & 1 deletion aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"net/http/httputil"
"net/url"
"strings"

"github.com/olivere/elastic/uritemplates"
)

type AliasesService struct {
Expand Down Expand Up @@ -50,13 +52,21 @@ func (s *AliasesService) Indices(indexNames ...string) *AliasesService {
}

func (s *AliasesService) Do() (*AliasesResult, error) {
var err error

// Build url
urls := "/"

// Indices part
indexPart := make([]string, 0)
for _, index := range s.indices {
indexPart = append(indexPart, cleanPathString(index))
index, err = uritemplates.Expand("{index}", map[string]string{
"index": index,
})
if err != nil {
return nil, err
}
indexPart = append(indexPart, index)
}
urls += strings.Join(indexPart, ",")

Expand Down
18 changes: 16 additions & 2 deletions bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"net/http"
"net/http/httputil"
"net/url"

"github.com/olivere/elastic/uritemplates"
)

type BulkService struct {
Expand Down Expand Up @@ -122,10 +124,22 @@ func (s *BulkService) Do() (*BulkResponse, error) {
// Build url
urls := "/"
if s.index != "" {
urls += cleanPathString(s.index) + "/"
index, err := uritemplates.Expand("{index}", map[string]string{
"index": s.index,
})
if err != nil {
return nil, err
}
urls += index + "/"
}
if s._type != "" {
urls += cleanPathString(s._type) + "/"
typ, err := uritemplates.Expand("{type}", map[string]string{
"type": s._type,
})
if err != nil {
return nil, err
}
urls += typ + "/"
}
urls += "_bulk"

Expand Down
20 changes: 18 additions & 2 deletions count.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"net/http/httputil"
"net/url"
"strings"

"github.com/olivere/elastic/uritemplates"
)

// CountService is a convenient service for determining the
Expand Down Expand Up @@ -89,13 +91,21 @@ func (s *CountService) Debug(debug bool) *CountService {
}

func (s *CountService) Do() (int64, error) {
var err error

// Build url
urls := "/"

// Indices part
indexPart := make([]string, 0)
for _, index := range s.indices {
indexPart = append(indexPart, cleanPathString(index))
index, err = uritemplates.Expand("{index}", map[string]string{
"index": index,
})
if err != nil {
return 0, err
}
indexPart = append(indexPart, index)
}
if len(indexPart) > 0 {
urls += strings.Join(indexPart, ",")
Expand All @@ -104,7 +114,13 @@ func (s *CountService) Do() (int64, error) {
// Types part
typesPart := make([]string, 0)
for _, typ := range s.types {
typesPart = append(typesPart, cleanPathString(typ))
typ, err = uritemplates.Expand("{type}", map[string]string{
"type": typ,
})
if err != nil {
return 0, err
}
typesPart = append(typesPart, typ)
}
if len(typesPart) > 0 {
urls += "/" + strings.Join(typesPart, ",")
Expand Down
36 changes: 33 additions & 3 deletions create_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ package elastic

import (
"encoding/json"
"log"
"net/http"
"strings"
"net/http/httputil"

"github.com/olivere/elastic/uritemplates"
)

type CreateIndexService struct {
client *Client
index string
body string
pretty bool
debug bool
}

func NewCreateIndexService(client *Client) *CreateIndexService {
Expand All @@ -33,10 +38,24 @@ func (b *CreateIndexService) Body(body string) *CreateIndexService {
return b
}

func (b *CreateIndexService) Pretty(pretty bool) *CreateIndexService {
b.pretty = pretty
return b
}

func (b *CreateIndexService) Debug(debug bool) *CreateIndexService {
b.debug = debug
return b
}

func (b *CreateIndexService) Do() (*CreateIndexResult, error) {
// Build url
urls := "/{index}/"
urls = strings.Replace(urls, "{index}", cleanPathString(b.index), 1)
urls, err := uritemplates.Expand("/{index}/", map[string]string{
"index": b.index,
})
if err != nil {
return nil, err
}

// Set up a new request
req, err := b.client.NewRequest("PUT", urls)
Expand All @@ -47,6 +66,11 @@ func (b *CreateIndexService) Do() (*CreateIndexResult, error) {
// Set body
req.SetBodyString(b.body)

if b.debug {
out, _ := httputil.DumpRequestOut((*http.Request)(req), true)
log.Printf("%s\n", string(out))
}

// Get response
res, err := b.client.c.Do((*http.Request)(req))
if err != nil {
Expand All @@ -56,6 +80,12 @@ func (b *CreateIndexService) Do() (*CreateIndexResult, error) {
return nil, err
}
defer res.Body.Close()

if b.debug {
out, _ := httputil.DumpResponse(res, true)
log.Printf("%s\n", string(out))
}

ret := new(CreateIndexResult)
if err := json.NewDecoder(res.Body).Decode(ret); err != nil {
return nil, err
Expand Down
15 changes: 10 additions & 5 deletions delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"strings"

"github.com/olivere/elastic/uritemplates"
)

type DeleteService struct {
Expand Down Expand Up @@ -76,10 +77,14 @@ func (s *DeleteService) Debug(debug bool) *DeleteService {

func (s *DeleteService) Do() (*DeleteResult, error) {
// Build url
urls := "/{index}/{type}/{id}"
urls = strings.Replace(urls, "{index}", cleanPathString(s.index), 1)
urls = strings.Replace(urls, "{type}", cleanPathString(s._type), 1)
urls = strings.Replace(urls, "{id}", cleanPathString(s.id), 1)
urls, err := uritemplates.Expand("/{index}/{type}/{id}", map[string]string{
"index": s.index,
"type": s._type,
"id": s.id,
})
if err != nil {
return nil, err
}

// Parameters
params := make(url.Values)
Expand Down
11 changes: 8 additions & 3 deletions delete_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ package elastic
import (
"encoding/json"
"net/http"
"strings"

"github.com/olivere/elastic/uritemplates"
)

type DeleteIndexService struct {
Expand All @@ -29,8 +30,12 @@ func (b *DeleteIndexService) Index(index string) *DeleteIndexService {

func (b *DeleteIndexService) Do() (*DeleteIndexResult, error) {
// Build url
urls := "/{index}/"
urls = strings.Replace(urls, "{index}", cleanPathString(b.index), 1)
urls, err := uritemplates.Expand("/{index}/", map[string]string{
"index": b.index,
})
if err != nil {
return nil, err
}

// Set up a new request
req, err := b.client.NewRequest("DELETE", urls)
Expand Down
15 changes: 10 additions & 5 deletions exists.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ package elastic
import (
"fmt"
"net/http"
"strings"

"github.com/olivere/elastic/uritemplates"
)

type ExistsService struct {
Expand Down Expand Up @@ -48,10 +49,14 @@ func (s *ExistsService) Id(id string) *ExistsService {

func (s *ExistsService) Do() (bool, error) {
// Build url
urls := "/{index}/{type}/{id}"
urls = strings.Replace(urls, "{index}", cleanPathString(s.index), 1)
urls = strings.Replace(urls, "{type}", cleanPathString(s._type), 1)
urls = strings.Replace(urls, "{id}", cleanPathString(s.id), 1)
urls, err := uritemplates.Expand("/{index}/{type}/{id}", map[string]string{
"index": s.index,
"type": s._type,
"id": s.id,
})
if err != nil {
return false, err
}

// Set up a new request
req, err := s.client.NewRequest("HEAD", urls)
Expand Down
10 changes: 9 additions & 1 deletion flush.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"net/http"
"net/url"
"strings"

"github.com/olivere/elastic/uritemplates"
)

type FlushService struct {
Expand Down Expand Up @@ -61,7 +63,13 @@ func (s *FlushService) Do() (*FlushResult, error) {
if len(s.indices) > 0 {
indexPart := make([]string, 0)
for _, index := range s.indices {
indexPart = append(indexPart, cleanPathString(index))
index, err := uritemplates.Expand("{index}", map[string]string{
"index": index,
})
if err != nil {
return nil, err
}
indexPart = append(indexPart, index)
}
urls += strings.Join(indexPart, ",") + "/"
}
Expand Down
14 changes: 10 additions & 4 deletions get.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"net/http"
"net/url"
"strings"

"github.com/olivere/elastic/uritemplates"
)

type GetService struct {
Expand Down Expand Up @@ -92,10 +94,14 @@ func (b *GetService) Realtime(realtime bool) *GetService {

func (b *GetService) Do() (*GetResult, error) {
// Build url
urls := "/{index}/{type}/{id}"
urls = strings.Replace(urls, "{index}", cleanPathString(b.index), 1)
urls = strings.Replace(urls, "{type}", cleanPathString(b._type), 1)
urls = strings.Replace(urls, "{id}", cleanPathString(b.id), 1)
urls, err := uritemplates.Expand("/{index}/{type}/{id}", map[string]string{
"index": b.index,
"type": b._type,
"id": b.id,
})
if err != nil {
return nil, err
}

params := make(url.Values)
if b.realtime != nil {
Expand Down
16 changes: 10 additions & 6 deletions index.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"strings"

"github.com/olivere/elastic/uritemplates"
)

// IndexResult is the result of indexing a document in Elasticsearch.
Expand Down Expand Up @@ -143,16 +144,19 @@ func (b *IndexService) Do() (*IndexResult, error) {
// Create document with manual id
method = "PUT"
urls = "/{index}/{type}/{id}"
urls = strings.Replace(urls, "{index}", cleanPathString(b.index), 1)
urls = strings.Replace(urls, "{type}", cleanPathString(b._type), 1)
urls = strings.Replace(urls, "{id}", cleanPathString(b.id), 1)
} else {
// Automatic ID generation
// See: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-index_.html#index-creation
method = "POST"
urls = "/{index}/{type}/"
urls = strings.Replace(urls, "{index}", cleanPathString(b.index), 1)
urls = strings.Replace(urls, "{type}", cleanPathString(b._type), 1)
}
urls, err := uritemplates.Expand(urls, map[string]string{
"index": b.index,
"type": b._type,
"id": b.id,
})
if err != nil {
return nil, err
}

// Parameters
Expand Down
11 changes: 8 additions & 3 deletions index_exists.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ package elastic
import (
"fmt"
"net/http"
"strings"

"github.com/olivere/elastic/uritemplates"
)

type IndexExistsService struct {
Expand All @@ -29,8 +30,12 @@ func (b *IndexExistsService) Index(index string) *IndexExistsService {

func (b *IndexExistsService) Do() (bool, error) {
// Build url
urls := "/{index}"
urls = strings.Replace(urls, "{index}", cleanPathString(b.index), 1)
urls, err := uritemplates.Expand("/{index}", map[string]string{
"index": b.index,
})
if err != nil {
return false, err
}

// Set up a new request
req, err := b.client.NewRequest("HEAD", urls)
Expand Down
Loading

0 comments on commit ceb70f8

Please sign in to comment.