-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optional configs for setting custom headers / metadata for REST / gRP…
…C operations (#18) ## Problem Currently, the Go SDK does not support specifying custom headers or metadata for control & data plane operations (REST and gRPC). This is a useful feature to aid in debugging or tracking a specific request, and we'd like to enable this functionality in the Go SDK. ## Solution - Update `NewClientParams` and `Client` structs to support `headers`, also allow passing a custom `RestClient` to the new client. This is primarily for allowing mocking in unit tests, but we have a similar approach in other clients allowing users to customize the HTTP module. - Add new `buildClientOptions` function. - Update `NewClient` to support appending `Headers` and `RestClient` to `clientOptions`. - Add error message for empty `ApiKey` without any `Authorization` header provided. If the user passes both, avoid applying the `Api-Key` header in favor of the `Authorization` header. - Add new `Client.IndexWithAdditionalMetadata` constructor. - Update `IndexConnection` struct to include `additionalMetadata`, and update `newIndexConnection` to accept `additionalMetadata` as an argument. - Update `IndexConnection.akCtx` to handle appending the `api-key` and any `additionalMetadata` to requests. - Update unit tests, add new `mocks` package and `mock_transport` to facilitate validating requests made via `Client` including. I spent some time trying to figure out how to mock / test `IndexConnection` but I think it'll be a bit trickier than `Client`, so somewhat saving for a future PR atm. ## Type of Change - [ ] Bug fix (non-breaking change which fixes an issue) - [X] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update - [ ] Infrastructure change (CI configs, etc) - [ ] Non-code change (docs, etc) - [ ] None of the above: (explain here) ## Test Plan `just test` to run the test suite locally. Validate tests pass in CI.
- Loading branch information
1 parent
32391bc
commit 5a9897e
Showing
7 changed files
with
237 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package mocks | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
type MockTransport struct { | ||
Req *http.Request | ||
Resp *http.Response | ||
Err error | ||
} | ||
|
||
func (m *MockTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
m.Req = req | ||
return m.Resp, m.Err | ||
} | ||
|
||
func CreateMockClient(jsonBody string) *http.Client { | ||
return &http.Client { | ||
Transport: &MockTransport{ | ||
Resp: &http.Response{ | ||
StatusCode: 200, | ||
Body: io.NopCloser(bytes.NewReader([]byte(jsonBody))), | ||
Header: make(http.Header), | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.