Skip to content

Commit

Permalink
Merge branch 'feature/upgrade-search-client' of https://github.com/ON…
Browse files Browse the repository at this point in the history
…Sdigital/dp-search-api into feature/upgrade-search-client
  • Loading branch information
NoelleDL committed Jun 18, 2021
2 parents 8a8806c + ffe5d61 commit 3758b40
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 35 deletions.
4 changes: 2 additions & 2 deletions cmd/dp-search-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os/signal"
"syscall"

net "github.com/ONSdigital/dp-net"
dphttp "github.com/ONSdigital/dp-net/http"
"github.com/ONSdigital/dp-search-api/api"
"github.com/ONSdigital/dp-search-api/config"
"github.com/ONSdigital/dp-search-api/elasticsearch"
Expand Down Expand Up @@ -50,7 +50,7 @@ func main() {
os.Exit(1)
}

elasticSearchClient := elasticsearch.New(cfg.ElasticSearchAPIURL, cfg.SignElasticsearchRequests, net.NewClient())
elasticSearchClient := elasticsearch.New(cfg.ElasticSearchAPIURL, dphttp.NewClient(), cfg.SignElasticsearchRequests)
transformer := transformer.New()

if err := api.CreateAndInitialise(cfg.BindAddr, queryBuilder, elasticSearchClient, transformer, apiErrors); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions elasticsearch/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import (
"net/http"
"strings"

net "github.com/ONSdigital/dp-net"
dphttp "github.com/ONSdigital/dp-net/http"
"github.com/pkg/errors"
awsauth "github.com/smartystreets/go-aws-auth"
)

// Client represents an instance of the elasticsearch client
type Client struct {
url string
client net.Clienter
client dphttp.Clienter
signRequests bool
}

// New creates a new elasticsearch client. Any trailing slashes from the URL are removed.
func New(url string, client net.Clienter, signRequests bool) *Client {
func New(url string, client dphttp.Clienter, signRequests bool) *Client {
return &Client{
url: strings.TrimRight(url, "/"),
client: client,
Expand Down
50 changes: 25 additions & 25 deletions elasticsearch/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"net/http/httptest"
"testing"

net "github.com/ONSdigital/dp-net"
dphttp "github.com/ONSdigital/dp-net/http"

. "github.com/smartystreets/goconvey/convey"
)
Expand All @@ -18,19 +18,19 @@ func TestSearch(t *testing.T) {
Convey("When Search is called", t, func() {

Convey("Then a request with the search action should be posted", func() {
rchttpMock := &rchttp.ClienterMock{
dphttpMock := &dphttp.ClienterMock{
DoFunc: func(ctx context.Context, req *http.Request) (*http.Response, error) {
return newResponse("moo"), nil
},
}

client := New("http://localhost:999", rchttpMock)
client := New("http://localhost:999", dphttpMock, false)

res, err := client.Search(context.Background(), "index", "doctype", []byte("search request"))
So(err, ShouldBeNil)
So(res, ShouldNotBeEmpty)
So(rchttpMock.DoCalls(), ShouldHaveLength, 1)
actualRequest := rchttpMock.DoCalls()[0].Req
So(dphttpMock.DoCalls(), ShouldHaveLength, 1)
actualRequest := dphttpMock.DoCalls()[0].Req
So(actualRequest.URL.String(), ShouldResemble, "http://localhost:999/index/doctype/_search")
So(actualRequest.Method, ShouldResemble, "POST")
body, err := ioutil.ReadAll(actualRequest.Body)
Expand All @@ -40,19 +40,19 @@ func TestSearch(t *testing.T) {
})

Convey("Then a returned error should be passed back", func() {
rchttpMock := &rchttp.ClienterMock{
dphttpMock := &dphttp.ClienterMock{
DoFunc: func(ctx context.Context, req *http.Request) (*http.Response, error) {
return nil, errors.New("http error")
},
}

client := New("http://localhost:999", rchttpMock)
client := New("http://localhost:999", dphttpMock, false)

_, err := client.Search(context.Background(), "index", "doctype", []byte("search request"))
So(err, ShouldNotBeNil)
So(err.Error(), ShouldResemble, "http error")
So(rchttpMock.DoCalls(), ShouldHaveLength, 1)
actualRequest := rchttpMock.DoCalls()[0].Req
So(dphttpMock.DoCalls(), ShouldHaveLength, 1)
actualRequest := dphttpMock.DoCalls()[0].Req
So(actualRequest.URL.String(), ShouldResemble, "http://localhost:999/index/doctype/_search")
So(actualRequest.Method, ShouldResemble, "POST")
body, err := ioutil.ReadAll(actualRequest.Body)
Expand All @@ -70,19 +70,19 @@ func TestMultiSearch(t *testing.T) {

Convey("Then a request with the multi search action should be posted", func() {

rchttpMock := &rchttp.ClienterMock{
dphttpMock := &dphttp.ClienterMock{
DoFunc: func(ctx context.Context, req *http.Request) (*http.Response, error) {
return newResponse("moo"), nil
},
}

client := New("http://localhost:999", rchttpMock)
client := New("http://localhost:999", dphttpMock, false)

res, err := client.MultiSearch(context.Background(), "index", "doctype", []byte("multiSearch request"))
So(err, ShouldBeNil)
So(res, ShouldNotBeEmpty)
So(rchttpMock.DoCalls(), ShouldHaveLength, 1)
actualRequest := rchttpMock.DoCalls()[0].Req
So(dphttpMock.DoCalls(), ShouldHaveLength, 1)
actualRequest := dphttpMock.DoCalls()[0].Req
So(actualRequest.URL.String(), ShouldResemble, "http://localhost:999/index/doctype/_msearch")
So(actualRequest.Method, ShouldResemble, "POST")
body, err := ioutil.ReadAll(actualRequest.Body)
Expand All @@ -91,19 +91,19 @@ func TestMultiSearch(t *testing.T) {
})

Convey("Then a returned error should be passed back", func() {
rchttpMock := &rchttp.ClienterMock{
dphttpMock := &dphttp.ClienterMock{
DoFunc: func(ctx context.Context, req *http.Request) (*http.Response, error) {
return nil, errors.New("http error")
},
}

client := New("http://localhost:999", rchttpMock)
client := New("http://localhost:999", dphttpMock, false)

_, err := client.MultiSearch(context.Background(), "index", "doctype", []byte("search request"))
So(err, ShouldNotBeNil)
So(err.Error(), ShouldResemble, "http error")
So(rchttpMock.DoCalls(), ShouldHaveLength, 1)
actualRequest := rchttpMock.DoCalls()[0].Req
So(dphttpMock.DoCalls(), ShouldHaveLength, 1)
actualRequest := dphttpMock.DoCalls()[0].Req
So(actualRequest.URL.String(), ShouldResemble, "http://localhost:999/index/doctype/_msearch")
So(actualRequest.Method, ShouldResemble, "POST")
body, err := ioutil.ReadAll(actualRequest.Body)
Expand All @@ -120,37 +120,37 @@ func TestGetStatus(t *testing.T) {

Convey("Then a GET request with the status action should be called", func() {

rchttpMock := &rchttp.ClienterMock{
dphttpMock := &dphttp.ClienterMock{
DoFunc: func(ctx context.Context, req *http.Request) (*http.Response, error) {
return newResponse("moo"), nil
},
}

client := New("http://localhost:999", rchttpMock)
client := New("http://localhost:999", dphttpMock, false)

res, err := client.GetStatus(context.Background())
So(err, ShouldBeNil)
So(res, ShouldNotBeEmpty)
So(rchttpMock.DoCalls(), ShouldHaveLength, 1)
actualRequest := rchttpMock.DoCalls()[0].Req
So(dphttpMock.DoCalls(), ShouldHaveLength, 1)
actualRequest := dphttpMock.DoCalls()[0].Req
So(actualRequest.URL.String(), ShouldResemble, "http://localhost:999/_cat/health")
So(actualRequest.Method, ShouldResemble, "GET")
})

Convey("Then a returned error should be passed back", func() {
rchttpMock := &rchttp.ClienterMock{
dphttpMock := &dphttp.ClienterMock{
DoFunc: func(ctx context.Context, req *http.Request) (*http.Response, error) {
return nil, errors.New("http error")
},
}

client := New("http://localhost:999", rchttpMock)
client := New("http://localhost:999", dphttpMock, false)

_, err := client.GetStatus(context.Background())
So(err, ShouldNotBeNil)
So(err.Error(), ShouldResemble, "http error")
So(rchttpMock.DoCalls(), ShouldHaveLength, 1)
actualRequest := rchttpMock.DoCalls()[0].Req
So(dphttpMock.DoCalls(), ShouldHaveLength, 1)
actualRequest := dphttpMock.DoCalls()[0].Req
So(actualRequest.URL.String(), ShouldResemble, "http://localhost:999/_cat/health")
So(actualRequest.Method, ShouldResemble, "GET")

Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ go 1.16
require (
github.com/ONSdigital/dp-api-clients-go v1.34.4 // indirect
github.com/ONSdigital/dp-net v1.0.12
github.com/ONSdigital/dp-elasticsearch v1.3.0
github.com/ONSdigital/go-ns v0.0.0-20210410105122-6d6a140e952e
github.com/ONSdigital/log.go v1.0.1
github.com/gorilla/mux v1.8.0
github.com/hokaccha/go-prettyjson v0.0.0-20210113012101-fb4e108d2519 // indirect
github.com/kelseyhightower/envconfig v1.4.0
github.com/pkg/errors v0.9.1
github.com/smartystreets/go-aws-auth v0.0.0-20180515143844-0c1422d1fdb9
github.com/smartystreets/goconvey v1.6.4
github.com/smartystreets/gunit v1.4.2 // indirect
github.com/tdewolff/minify v2.3.6+incompatible
github.com/tdewolff/parse v2.3.4+incompatible // indirect
github.com/tdewolff/test v1.0.6 // indirect
Expand Down
7 changes: 3 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ github.com/ONSdigital/dp-api-clients-go v1.1.0/go.mod h1:9lqor0I7caCnRWr04gU/r7x
github.com/ONSdigital/dp-api-clients-go v1.28.0/go.mod h1:iyJy6uRL4B6OYOJA0XMr5UHt6+Q8XmN9uwmURO+9Oj4=
github.com/ONSdigital/dp-api-clients-go v1.34.4 h1:SHdhxHxflgoAesWpwrVR0OQz+pLKI4jMl58mZrzKyA4=
github.com/ONSdigital/dp-api-clients-go v1.34.4/go.mod h1:kX+YKuoLYLfkeLHMvQKRRydZVxO7ZEYyYiwG2xhV51E=
github.com/ONSdigital/dp-elasticsearch v1.3.0 h1:JFAPShr9JSpshAhP71dfm4jHV64cH+Z8rfNa6BA+c60=
github.com/ONSdigital/dp-elasticsearch v1.3.0/go.mod h1:ah5gobZW0FctrYeQtD16lfEI7aDeVlprj1nZD4AA2EY=
github.com/ONSdigital/dp-frontend-models v1.1.0/go.mod h1:TT96P7Mi69N3Tc/jFNdbjiwG4GAaMjP26HLotFQ6BPw=
github.com/ONSdigital/dp-healthcheck v0.0.0-20200131122546-9db6d3f0494e/go.mod h1:zighxZ/0m5u7zo0eAr8XFlA+Dz2ic7A1vna6YXvhCjQ=
github.com/ONSdigital/dp-healthcheck v1.0.5/go.mod h1:2wbVAUHMl9+4tWhUlxYUuA1dnf2+NrwzC+So5f5BMLk=
Expand All @@ -16,8 +14,6 @@ github.com/ONSdigital/dp-net v1.0.12 h1:Vd06ia1FXKR9uyhzWykQ52b1LTp4N0VOLnrF7KOe
github.com/ONSdigital/dp-net v1.0.12/go.mod h1:2lvIKOlD4T3BjWQwjHhBUO2UNWDk82u/+mHRn0R3C9A=
github.com/ONSdigital/dp-rchttp v0.0.0-20190919143000-bb5699e6fd59/go.mod h1:KkW68U3FPuivW4ogi9L8CPKNj9ZxGko4qcUY7KoAAkQ=
github.com/ONSdigital/dp-rchttp v0.0.0-20200114090501-463a529590e8/go.mod h1:821jZtK0oBsV8hjIkNr8vhAWuv0FxJBPJuAHa2B70Gk=
github.com/ONSdigital/dp-rchttp v1.0.0 h1:K/1/gDtfMZCX1Mbmq80nZxzDirzneqA1c89ea26FqP4=
github.com/ONSdigital/dp-rchttp v1.0.0/go.mod h1:821jZtK0oBsV8hjIkNr8vhAWuv0FxJBPJuAHa2B70Gk=
github.com/ONSdigital/go-ns v0.0.0-20191104121206-f144c4ec2e58/go.mod h1:iWos35il+NjbvDEqwtB736pyHru0MPFE/LqcwkV1wDc=
github.com/ONSdigital/go-ns v0.0.0-20210410105122-6d6a140e952e h1:64qsVTEixfFUuPcl4SOOGU3WRv136aEKRqIwXNIrttE=
github.com/ONSdigital/go-ns v0.0.0-20210410105122-6d6a140e952e/go.mod h1:kb2qyEcVuTkuz9yxOKTXroMrEniTsR2u9rwAqxgQ8MQ=
Expand Down Expand Up @@ -62,9 +58,12 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/go-aws-auth v0.0.0-20180515143844-0c1422d1fdb9 h1:hp2CYQUINdZMHdvTdXtPOY2ainKl4IoMcpAXEf2xj3Q=
github.com/smartystreets/go-aws-auth v0.0.0-20180515143844-0c1422d1fdb9/go.mod h1:SnhjPscd9TpLiy1LpzGSKh3bXCfxxXuqd9xmQJy3slM=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/smartystreets/gunit v1.4.2 h1:tyWYZffdPhQPfK5VsMQXfauwnJkqg7Tv5DLuQVYxq3Q=
github.com/smartystreets/gunit v1.4.2/go.mod h1:ZjM1ozSIMJlAz/ay4SG8PeKF00ckUp+zMHZXV9/bvak=
github.com/tdewolff/minify v2.3.6+incompatible h1:2hw5/9ZvxhWLvBUnHE06gElGYz+Jv9R4Eys0XUzItYo=
github.com/tdewolff/minify v2.3.6+incompatible/go.mod h1:9Ov578KJUmAWpS6NeZwRZyT56Uf6o3Mcz9CEsg8USYs=
github.com/tdewolff/parse v2.3.4+incompatible h1:x05/cnGwIMf4ceLuDMBOdQ1qGniMoxpP46ghf0Qzh38=
Expand Down

0 comments on commit 3758b40

Please sign in to comment.