Skip to content

Commit

Permalink
Return HTTP response as second value from typed client functions
Browse files Browse the repository at this point in the history
Signed-off-by: Stefano Arlandini <sarlandini@alice.it>
  • Loading branch information
ste93cry committed Sep 26, 2024
1 parent 3f1ca5e commit 03927e6
Show file tree
Hide file tree
Showing 190 changed files with 1,911 additions and 2,889 deletions.
2 changes: 1 addition & 1 deletion internal/test/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func GetPassword() (string, error) {

// GetVersion gets cluster info and returns version as int's
func GetVersion(client *opensearchapi.Client) (int64, int64, int64, error) {
resp, err := client.Info(context.Background(), nil)
resp, _, err := client.Info(context.Background(), nil)
if err != nil {
return 0, 0, 0, err
}
Expand Down
21 changes: 7 additions & 14 deletions opensearchapi/api_aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ import (
)

// Aliases executes an /_aliases request with the required AliasesReq
func (c Client) Aliases(ctx context.Context, req AliasesReq) (*AliasesResp, error) {
var (
data AliasesResp
err error
)
if data.response, err = c.do(ctx, req, &data); err != nil {
return &data, err
func (c Client) Aliases(ctx context.Context, req AliasesReq) (*AliasesResp, *opensearch.Response, error) {
var data AliasesResp

resp, err := c.do(ctx, req, &data)
if err != nil {
return nil, resp, err
}

return &data, nil
return &data, resp, nil
}

// AliasesReq represents possible options for the / request
Expand All @@ -49,10 +48,4 @@ func (r AliasesReq) GetRequest() (*http.Request, error) {
// AliasesResp represents the returned struct of the / response
type AliasesResp struct {
Acknowledged bool `json:"acknowledged"`
response *opensearch.Response
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r AliasesResp) Inspect() Inspect {
return Inspect{Response: r.response}
}
19 changes: 10 additions & 9 deletions opensearchapi/api_aliases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,31 @@ func TestAliases(t *testing.T) {
client.Indices.Delete(nil, opensearchapi.IndicesDeleteReq{Indices: []string{index}})
})

_, err = client.Indices.Create(nil, opensearchapi.IndicesCreateReq{Index: index})
_, _, err = client.Indices.Create(nil, opensearchapi.IndicesCreateReq{Index: index})
require.Nil(t, err)

t.Run("with request", func(t *testing.T) {
resp, err := client.Aliases(
resp, httpResp, err := client.Aliases(
nil,
opensearchapi.AliasesReq{
Body: strings.NewReader(`{"actions":[{"add":{"index":"test-aliases","alias":"logs"}},{"remove":{"index":"test-aliases","alias":"logs"}}]}`),
},
)
require.Nil(t, err)
require.NotEmpty(t, resp)
require.NotEmpty(t, resp.Inspect().Response)
ostest.CompareRawJSONwithParsedJSON(t, resp, resp.Inspect().Response)
require.NotNil(t, resp)
require.NotNil(t, httpResp)
ostest.CompareRawJSONwithParsedJSON(t, resp, httpResp)
})

t.Run("inspect", func(t *testing.T) {
t.Run("with failing request", func(t *testing.T) {
failingClient, err := osapitest.CreateFailingClient()
require.Nil(t, err)

res, err := failingClient.Aliases(nil, opensearchapi.AliasesReq{})
res, httpResp, err := failingClient.Aliases(nil, opensearchapi.AliasesReq{})
require.NotNil(t, err)
require.NotNil(t, res)
osapitest.VerifyInspect(t, res.Inspect())
require.NotNil(t, httpResp)
require.Nil(t, res)
osapitest.VerifyResponse(t, httpResp)
})
})
}
27 changes: 10 additions & 17 deletions opensearchapi/api_bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ import (
)

// Bulk executes a /_bulk request with the needed BulkReq
func (c Client) Bulk(ctx context.Context, req BulkReq) (*BulkResp, error) {
var (
data BulkResp
err error
)
if data.response, err = c.do(ctx, req, &data); err != nil {
return &data, err
func (c Client) Bulk(ctx context.Context, req BulkReq) (*BulkResp, *opensearch.Response, error) {
var data BulkResp

resp, err := c.do(ctx, req, &data)
if err != nil {
return nil, resp, err
}

return &data, nil
return &data, resp, nil
}

// BulkReq represents possible options for the /_bulk request
Expand Down Expand Up @@ -60,10 +59,9 @@ func (r BulkReq) GetRequest() (*http.Request, error) {

// BulkResp represents the returned struct of the /_bulk response
type BulkResp struct {
Took int `json:"took"`
Errors bool `json:"errors"`
Items []map[string]BulkRespItem `json:"items"`
response *opensearch.Response
Took int `json:"took"`
Errors bool `json:"errors"`
Items []map[string]BulkRespItem `json:"items"`
}

// BulkRespItem represents an item of the BulkResp
Expand Down Expand Up @@ -102,8 +100,3 @@ type BulkRespItem struct {
} `json:"caused_by,omitempty"`
} `json:"error,omitempty"`
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r BulkResp) Inspect() Inspect {
return Inspect{Response: r.response}
}
18 changes: 10 additions & 8 deletions opensearchapi/api_bulk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,24 @@ func TestBulkClient(t *testing.T) {

for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
res, err := client.Bulk(
res, httpResp, err := client.Bulk(
nil,
test.Request,
)
require.Nil(t, err)
assert.NotEmpty(t, res)
ostest.CompareRawJSONwithParsedJSON(t, res, res.Inspect().Response)
assert.NotNil(t, res)
assert.NotNil(t, httpResp)
ostest.CompareRawJSONwithParsedJSON(t, res, httpResp)
})
}
t.Run("inspect", func(t *testing.T) {
t.Run("with failing request", func(t *testing.T) {
failingClient, err := osapitest.CreateFailingClient()
require.Nil(t, err)

res, err := failingClient.Bulk(nil, opensearchapi.BulkReq{Index: index})
assert.NotNil(t, err)
assert.NotNil(t, res)
osapitest.VerifyInspect(t, res.Inspect())
res, httpResp, err := failingClient.Bulk(nil, opensearchapi.BulkReq{Index: index})
require.NotNil(t, err)
require.NotNil(t, httpResp)
require.Nil(t, res)
osapitest.VerifyResponse(t, httpResp)
})
}
10 changes: 1 addition & 9 deletions opensearchapi/api_cat-aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func (r CatAliasesReq) GetRequest() (*http.Request, error) {

// CatAliasesResp represents the returned struct of the /_cat/aliases response
type CatAliasesResp struct {
Aliases []CatAliasResp
response *opensearch.Response
Aliases []CatAliasResp
}

// CatAliasResp represents one index of the CatAliasesResp
Expand All @@ -54,10 +53,3 @@ type CatAliasResp struct {
RoutingSearch string `json:"routing.search"`
IsWriteIndex string `json:"is_write_index"`
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r CatAliasesResp) Inspect() Inspect {
return Inspect{
Response: r.response,
}
}
8 changes: 0 additions & 8 deletions opensearchapi/api_cat-allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func (r CatAllocationReq) GetRequest() (*http.Request, error) {
// CatAllocationsResp represents the returned struct of the /_cat/allocation response
type CatAllocationsResp struct {
Allocations []CatAllocationResp
response *opensearch.Response
}

// CatAllocationResp represents one index of the CatAllocationResp
Expand All @@ -58,10 +57,3 @@ type CatAllocationResp struct {
IP *string `json:"ip"`
Node string `json:"node"`
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r CatAllocationsResp) Inspect() Inspect {
return Inspect{
Response: r.response,
}
}
8 changes: 0 additions & 8 deletions opensearchapi/api_cat-cluster_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func (r CatClusterManagerReq) GetRequest() (*http.Request, error) {
// CatClusterManagersResp represents the returned struct of the /_cat/cluster_manager response
type CatClusterManagersResp struct {
ClusterManagers []CatClusterManagerResp
response *opensearch.Response
}

// CatClusterManagerResp represents one index of the CatClusterManagerResp
Expand All @@ -42,10 +41,3 @@ type CatClusterManagerResp struct {
IP string `json:"ip"`
Node string `json:"node"`
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r CatClusterManagersResp) Inspect() Inspect {
return Inspect{
Response: r.response,
}
}
10 changes: 1 addition & 9 deletions opensearchapi/api_cat-count.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func (r CatCountReq) GetRequest() (*http.Request, error) {

// CatCountsResp represents the returned struct of the /_cat/count response
type CatCountsResp struct {
Counts []CatCountResp
response *opensearch.Response
Counts []CatCountResp
}

// CatCountResp represents one index of the CatCountResp
Expand All @@ -51,10 +50,3 @@ type CatCountResp struct {
Timestamp string `json:"timestamp"`
Count int `json:"count,string"`
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r CatCountsResp) Inspect() Inspect {
return Inspect{
Response: r.response,
}
}
8 changes: 0 additions & 8 deletions opensearchapi/api_cat-fielddata.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func (r CatFieldDataReq) GetRequest() (*http.Request, error) {
// CatFieldDataResp represents the returned struct of the /_cat/fielddata response
type CatFieldDataResp struct {
FieldData []CatFieldDataItemResp
response *opensearch.Response
}

// CatFieldDataItemResp represents one index of the CatFieldDataResp
Expand All @@ -54,10 +53,3 @@ type CatFieldDataItemResp struct {
Field string `json:"field"`
Size string `json:"size"`
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r CatFieldDataResp) Inspect() Inspect {
return Inspect{
Response: r.response,
}
}
10 changes: 1 addition & 9 deletions opensearchapi/api_cat-health.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ func (r CatHealthReq) GetRequest() (*http.Request, error) {

// CatHealthResp represents the returned struct of the /_cat/health response
type CatHealthResp struct {
Health []CatHealthItemResp
response *opensearch.Response
Health []CatHealthItemResp
}

// CatHealthItemResp represents one index of the CatHealthResp
Expand All @@ -54,10 +53,3 @@ type CatHealthItemResp struct {
MaxTaskWaitTime string `json:"max_task_wait_time"`
ActiveShardsPercent string `json:"active_shards_percent"`
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r CatHealthResp) Inspect() Inspect {
return Inspect{
Response: r.response,
}
}
10 changes: 1 addition & 9 deletions opensearchapi/api_cat-indices.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func (r CatIndicesReq) GetRequest() (*http.Request, error) {

// CatIndicesResp represents the returned struct of the /_cat/indices response
type CatIndicesResp struct {
Indices []CatIndexResp
response *opensearch.Response
Indices []CatIndexResp
}

// CatIndexResp represents one index of the CatIndicesResp
Expand Down Expand Up @@ -194,10 +193,3 @@ type CatIndexResp struct {
PrimaryMemoryTotal string `json:"pri.memory.total"`
SearchThrottled bool `json:"search.throttled,string"`
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r CatIndicesResp) Inspect() Inspect {
return Inspect{
Response: r.response,
}
}
10 changes: 1 addition & 9 deletions opensearchapi/api_cat-master.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ func (r CatMasterReq) GetRequest() (*http.Request, error) {

// CatMasterResp represents the returned struct of the /_cat/master response
type CatMasterResp struct {
Master []CatMasterItemResp
response *opensearch.Response
Master []CatMasterItemResp
}

// CatMasterItemResp represents one index of the CatMasterResp
Expand All @@ -42,10 +41,3 @@ type CatMasterItemResp struct {
IP string `json:"ip"`
Node string `json:"node"`
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r CatMasterResp) Inspect() Inspect {
return Inspect{
Response: r.response,
}
}
8 changes: 0 additions & 8 deletions opensearchapi/api_cat-nodeattrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func (r CatNodeAttrsReq) GetRequest() (*http.Request, error) {
// CatNodeAttrsResp represents the returned struct of the /_cat/nodeattrs response
type CatNodeAttrsResp struct {
NodeAttrs []CatNodeAttrsItemResp
response *opensearch.Response
}

// CatNodeAttrsItemResp represents one index of the CatNodeAttrsResp
Expand All @@ -46,10 +45,3 @@ type CatNodeAttrsItemResp struct {
Attr string `json:"attr"`
Value string `json:"value"`
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r CatNodeAttrsResp) Inspect() Inspect {
return Inspect{
Response: r.response,
}
}
10 changes: 1 addition & 9 deletions opensearchapi/api_cat-nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ func (r CatNodesReq) GetRequest() (*http.Request, error) {

// CatNodesResp represents the returned struct of the /_cat/nodes response
type CatNodesResp struct {
Nodes []CatNodesItemResp
response *opensearch.Response
Nodes []CatNodesItemResp
}

// CatNodesItemResp represents one index of the CatNodesResp
Expand Down Expand Up @@ -137,10 +136,3 @@ type CatNodesItemResp struct {
SuggestTime *string `json:"suggest.time"`
SuggestTotal *int `json:"suggest.total,string"`
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r CatNodesResp) Inspect() Inspect {
return Inspect{
Response: r.response,
}
}
8 changes: 0 additions & 8 deletions opensearchapi/api_cat-pending_tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func (r CatPendingTasksReq) GetRequest() (*http.Request, error) {
// CatPendingTasksResp represents the returned struct of the /_cat/pending_tasks response
type CatPendingTasksResp struct {
PendingTasks []CatPendingTaskResp
response *opensearch.Response
}

// CatPendingTaskResp represents one index of the CatPendingTasksResp
Expand All @@ -42,10 +41,3 @@ type CatPendingTaskResp struct {
Priority string `json:"priority"`
Source string `json:"source"`
}

// Inspect returns the Inspect type containing the raw *opensearch.Reponse
func (r CatPendingTasksResp) Inspect() Inspect {
return Inspect{
Response: r.response,
}
}
Loading

0 comments on commit 03927e6

Please sign in to comment.